Example #1
0
 public Task <List <HistoricalDataEventArgs> > GetHistoricalDataAsync(
     Contract contract,
     DateTime endDateTime,
     TwsDuration duration,
     TwsBarSizeSetting barSizeSetting,
     TwsHistoricalDataRequestType whatToShow,
     bool useRegularTradingHours = true,
     bool formatDate             = true)
 {
     return(_twsObjectFactory.TwsController.GetHistoricalDataAsync(contract, endDateTime, duration, barSizeSetting, whatToShow, useRegularTradingHours, formatDate));
 }
Example #2
0
 public async Task <List <HistoricalDataEventArgs> > GetHistoricalDataAsync(Contract contract, DateTime endDateTime, TwsDuration duration, TwsBarSizeSetting barSizeSetting, TwsHistoricalDataRequestType whatToShow, bool useRth = true, bool formatDate = true)
 {
     return(await this.twsControllerBase.GetHistoricalDataAsync(contract, endDateTime, duration, barSizeSetting, whatToShow, useRth, formatDate));
 }
        /// <summary>
        /// Gets historical data from TWS.
        /// </summary>
        /// <param name="contract">The contract type</param>
        /// <param name="endDateTime">The end date of the request</param>
        /// <param name="duration">The duration of the request</param>
        /// <param name="barSizeSetting">The bar size to request</param>
        /// <param name="whatToShow">The historical data request type</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public Task <List <HistoricalDataEventArgs> > GetHistoricalDataAsync(Contract contract, DateTime endDateTime, TwsDuration duration, TwsBarSizeSetting barSizeSetting, TwsHistoricalDataRequestType whatToShow)
        {
            int             requestId    = this.twsRequestIdGenerator.GetNextRequestId();
            int             useRth       = 1;
            int             formatDate   = 1;
            List <TagValue> chartOptions = null;

            string value = string.Empty;

            var taskSource = new TaskCompletionSource <List <HistoricalDataEventArgs> >();

            EventHandler <HistoricalDataEventArgs>    historicalDataEventHandler    = null;
            EventHandler <HistoricalDataEndEventArgs> historicalDataEndEventHandler = null;
            EventHandler <ErrorEventArgs>             errorEventHandler             = null;

            List <HistoricalDataEventArgs> historicalDataList = new List <HistoricalDataEventArgs>();

            historicalDataEventHandler = (sender, args) =>
            {
                historicalDataList.Add(args);
            };

            historicalDataEndEventHandler = (sender, args) =>
            {
                this.twsCallbackHandler.HistoricalDataEvent    -= historicalDataEventHandler;
                this.twsCallbackHandler.HistoricalDataEndEvent -= historicalDataEndEventHandler;
                this.twsCallbackHandler.ErrorEvent             -= errorEventHandler;
                taskSource.TrySetResult(historicalDataList);
            };

            errorEventHandler = (sender, args) =>
            {
                if (args.Id == requestId)
                {
                    // The error is associated with this request
                    this.twsCallbackHandler.HistoricalDataEvent    -= historicalDataEventHandler;
                    this.twsCallbackHandler.HistoricalDataEndEvent -= historicalDataEndEventHandler;
                    this.twsCallbackHandler.ErrorEvent             -= errorEventHandler;
                    taskSource.TrySetException(new TwsException(args.ErrorMessage));
                }
            };

            // Set the operation to cancel after 1 minute
            CancellationTokenSource tokenSource = new CancellationTokenSource(60 * 1000);

            tokenSource.Token.Register(() =>
            {
                this.twsCallbackHandler.HistoricalDataEvent    -= historicalDataEventHandler;
                this.twsCallbackHandler.HistoricalDataEndEvent -= historicalDataEndEventHandler;
                this.twsCallbackHandler.ErrorEvent             -= errorEventHandler;

                taskSource.TrySetCanceled();
            });

            this.twsCallbackHandler.HistoricalDataEvent    += historicalDataEventHandler;
            this.twsCallbackHandler.HistoricalDataEndEvent += historicalDataEndEventHandler;
            this.twsCallbackHandler.ErrorEvent             += errorEventHandler;

            this.clientSocket.ReqHistoricalData(requestId, contract, endDateTime.ToString("yyyyMMdd HH:mm:ss"), duration.ToTwsParameter(), barSizeSetting.ToTwsParameter(), whatToShow.ToTwsParameter(), useRth, formatDate, chartOptions);
            return(taskSource.Task);
        }
Example #4
0
        /// <summary>
        /// Gets historical data from TWS.
        /// </summary>
        /// <param name="contract">The contract type</param>
        /// <param name="endDateTime">The end date of the request</param>
        /// <param name="duration">The duration of the request</param>
        /// <param name="barSizeSetting">The bar size to request</param>
        /// <param name="whatToShow">The historical data request type</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public Task <List <HistoricalDataEventArgs> > GetHistoricalDataAsync(Contract contract, DateTime endDateTime, TwsDuration duration, TwsBarSizeSetting barSizeSetting, TwsHistoricalDataRequestType whatToShow)
        {
            // Set the operation to cancel after 1 minute
            CancellationTokenSource tokenSource = new CancellationTokenSource(60 * 1000);

            return(this.GetHistoricalDataAsync(contract, endDateTime, duration, barSizeSetting, whatToShow, tokenSource.Token));
        }