/// <summary>
 /// Get historical time-series data from IB
 /// </summary>
 public async Task <List <HistoricalDataMessage> > GetHistoricalData(
     Contract Contract, DateTime EndTime, BarSize BarSize,
     int DurationDays = 1, HistoricalDataType DataType = HistoricalDataType.TRADES, bool UseRTH = false
     )
 {
     return(await GetHistoricalData(Contract, EndTime, BarSize, DurationDays + " D", DataType, UseRTH));
 }
        private async Task <List <HistoricalDataMessage> > GetHistoricalData(
            Contract Contract, DateTime EndTime, BarSize BarSize,
            string Duration = "1 D", HistoricalDataType DataType = HistoricalDataType.TRADES, bool UseRTH = false
            )
        {
            var dataBars = await this.Dispatch <HistoricalDataMessage>((requestId) =>
            {
                this.Log("Requesting Historical Data for {0} ending on {1}...", Contract.LocalSymbol, EndTime);
                _ibClient.ClientSocket.reqHistoricalData(
                    requestId,
                    Contract,
                    EndTime.ToString("yyyyMMdd HH:mm:ss"),
                    Duration,
                    BarSize.ToDescription(),
                    DataType.ToString(),
                    UseRTH ? 1 : 0,
                    1,
                    false,
                    new List <TagValue>()
                    );
                return(true);
            });

            dataBars.All((bar) =>
            {
                //convert the datetime from IB format into normalized ISO time format
                bar.Date = Framework.ParseDateTz(bar.Date, DateTime.Now).Value.ToISOString();
                return(true);
            });

            return(dataBars.OrderBy(bar => bar.Date).ToList());
        }
Пример #3
0
        public static HistoricalData ParseCsv(string filePath, string Ticker, string Name, HistoricalDataType DataType)
        {
            HistoricalData result = new HistoricalData();

            result.Ticker = Ticker;
            result.Name = Name;
            result.Stocks = new List<Stock>();

            using (StreamReader reader = new StreamReader(File.OpenRead(filePath)))
            {
                // skip first line which is date,open,high,low,close,volumn,adj close
                reader.ReadLine();
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    Stock price = Stock.ParsePrice(line, Ticker, Name);
                    result.Stocks.Add(price);
                }

                // filter according to daily, weekly or monthly
                result.ProcessPrices(result.Stocks, DataType);

                result.Stocks.Reverse();

            }

            return result;
        }
Пример #4
0
        /// <summary>
        /// 获取某个证券的某段时间的历史数据
        /// </summary>
        /// <param name="instrument"></param>
        /// <param name="dataType"></param>
        /// <param name="barSize"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List <ISeriesObject> GetHistoricalData(Instrument instrument, HistoricalDataType dataType, int barSize, string beginTime, string endTime)
        {
            switch (dataType)
            {
            case HistoricalDataType.Bar:
                List <GMSDK.Bar> gskBars = new List <GMSDK.Bar>();
                gskBars = _md.GetBars(instrument.Symbol, barSize, beginTime, endTime);
                return(GSKToGM.ConvertBars(gskBars));

            case HistoricalDataType.Daily:
                List <GMSDK.DailyBar> gskDailys = new List <GMSDK.DailyBar>();
                gskDailys = _md.GetDailyBars(instrument.Symbol, beginTime, endTime);
                return(GSKToGM.ConvertDailys(gskDailys));

            case HistoricalDataType.Trade:
                this.gskTicksCache = _md.GetTicks(instrument.Symbol, beginTime, endTime);
                return(GSKToGM.ConvertTrades(this.gskTicksCache));

            case HistoricalDataType.Quote:
                this.gskTicksCache = _md.GetTicks(instrument.Symbol, beginTime, endTime);
                return(GSKToGM.ConvertQuotes(this.gskTicksCache));

            default:
                throw new ArgumentException("Unknown data type: " + dataType.ToString());
            }
        }
Пример #5
0
        public void RequestHistoricalData2(int requestId, Contract contract, DateTime endDateTime, string duration,
                                           BarSize barSizeSetting, HistoricalDataType whatToShow, int useRth, int formatDate, bool keepUpToDate, List <TagValue> chartOptions)
        {
            string endDT   = endDateTime.ToUniversalTime().ToString("yyyyMMdd HH:mm:ss", CultureInfo.InvariantCulture) + " UTC";
            string barSize = barSizeSetting.Value;
            string wts     = whatToShow.Value;

            lock (this)
                ClientSocket.reqHistoricalData(requestId, contract, endDT, duration, barSize, wts, useRth, formatDate, keepUpToDate, chartOptions);
        }
Пример #6
0
        /// <summary>
        /// Call the reqHistoricalData() method to start receiving historical data results through the historicalData() EWrapper method.
        /// </summary>
        /// <param name="requestId">the Id for the request. Must be a unique value. When the data is received, it will be identified by this Id. This is also used when canceling the historical data request.</param>
        /// <param name="contract">this structure contains a description of the contract for which market data is being requested.</param>
        /// <param name="endDateTime">Date is sent after a .ToUniversalTime, so make sure the kind property is set correctly, and assumes GMT timezone. Use the format yyyymmdd hh:mm:ss tmz, where the time zone is allowed (optionally) after a space at the end.</param>
        /// <param name="duration">This is the time span the request will cover, and is specified using the format:
        /// <integer /> <unit />, i.e., 1 D, where valid units are:
        /// S (seconds)
        /// D (days)
        /// W (weeks)
        /// M (months)
        /// Y (years)
        /// If no unit is specified, seconds are used. "years" is currently limited to one.
        /// </param>
        /// <param name="barSizeSetting">
        /// specifies the size of the bars that will be returned (within IB/TWS limits). Valid values include:
        /// <param name="useRth">
        /// </param>
        public void RequestHistoricalData(int requestId, Contract contract, DateTime endDateTime, TimeSpan duration,
                                          BarSize barSizeSetting, HistoricalDataType whatToShow, bool useRth)
        {
            DateTime beginDateTime = endDateTime.Subtract(duration);
            TimeSpan period        = endDateTime.Subtract(beginDateTime);
            string   dur;

            double secs = period.TotalSeconds;
            long   unit;

            if (secs < 1)
            {
                throw new ArgumentOutOfRangeException("Period cannot be less than 1 second.");
            }
            if (secs < 86400)
            {
                unit = (long)Math.Ceiling(secs);
                dur  = string.Concat(unit, " S");
            }
            else
            {
                double days = secs / 86400;
                unit = (long)Math.Ceiling(days);
                if (unit <= 34)
                {
                    dur = string.Concat(unit, " D");
                }
                else
                {
                    double weeks = days / 7;
                    unit = (long)Math.Ceiling(weeks);
                    if (unit > 52)
                    {
                        throw new ArgumentOutOfRangeException("Period cannot be bigger than 52 weeks.");
                    }
                    dur = string.Concat(unit, " W");
                }
            }


            RequestHistoricalData2(requestId, contract, endDateTime, dur, barSizeSetting, whatToShow, useRth ? 1 : 0, BarTimeFormat.UnixSeconds.Value, false, null);
        }
Пример #7
0
        /// <summary>
        /// Creates an observable which emits historical data.
        /// This method accepts arguments which will not necessarily be accepted by Tws/Gateway.
        /// Note that IB imposes limitations on the timing of historical data requests.
        /// </summary>
        public IObservable <HistoricalBars> HistoricalDataObservable(
            Contract contract,
            HistoricalBarSize barSize   = null,
            HistoricalDuration duration = null,
            Instant end = default,
            HistoricalDataType dataType  = null,
            bool regularTradingHoursOnly = true,
            params Tag[] options)
        {
            if (contract == null)
            {
                throw new ArgumentNullException(nameof(contract));
            }

            return(Response
                   .ToObservable <HistoricalBars>(
                       Request.NextId,
                       requestId => Request.RequestHistoricalData(requestId, contract, end, duration, barSize, dataType, regularTradingHoursOnly, options),
                       Request.CancelHistoricalData)
                   .ToShareSource());
        }
        public List <ISeriesObject> GetHistoricalData(Instrument instrument, HistoricalDataType dataType, int barSize, DateTime startTime, DateTime endTime)
        {
            List <ISeriesObject> results = new List <ISeriesObject>();
            DZHSymbol            symbol  = new DZHSymbol(instrument.SecurityExchange, instrument.SecurityID);
            List <DZHBar>        bars;
            List <DZHTick>       ticks;

            switch (dataType)
            {
            case HistoricalDataType.Bar:
                if (barSize == 300)
                {
                    if (min5Reader == null)
                    {
                        min5Reader = new DZHMin5BarReader(dzhDataPath);
                    }
                    bars = min5Reader.RequestBars(symbol, startTime, endTime);
                }
                else if (barSize == 60)
                {
                    if (min1Reader == null)
                    {
                        min1Reader = new DZHMin1BarReader(dzhDataPath);
                    }
                    bars = min1Reader.RequestBars(symbol, startTime, endTime);
                }
                else
                {
                    throw new ArgumentException("Unknowm bar size:" + barSize.ToString());
                }
                if ((bars != null) && (bars.Count > 0))
                {
                    /*前复权调整*/
                    if (forwardAdjust)
                    {
                        if (financeReader == null)
                        {
                            financeReader = new DZHFinanceReader(dzhDataPath);
                        }
                        List <DZHExDividend> exDivs = financeReader.RequestExDividends(symbol);
                        financeReader.ForwardAdjustedPrice(bars, exDivs);
                    }
                    foreach (DZHBar aBar in bars)
                    {
                        results.Add(new Bar(aBar.Time, aBar.Open, aBar.High, aBar.Low, aBar.Close, (long)aBar.Volume, barSize));
                    }
                }
                break;

            case HistoricalDataType.Daily:
                if (dayReader == null)
                {
                    dayReader = new DZHDayBarReader(dzhDataPath);
                }
                bars = dayReader.RequestBars(symbol, startTime, endTime);
                if ((bars != null) && (bars.Count > 0))
                {
                    /*前复权调整*/
                    if (forwardAdjust)
                    {
                        if (financeReader == null)
                        {
                            financeReader = new DZHFinanceReader(dzhDataPath);
                        }
                        List <DZHExDividend> exDivs = financeReader.RequestExDividends(symbol);
                        financeReader.ForwardAdjustedPrice(bars, exDivs);
                    }
                    foreach (DZHBar aBar in bars)
                    {
                        results.Add(new Daily(aBar.Time, aBar.Open, aBar.High, aBar.Low, aBar.Close, (long)aBar.Volume));
                    }
                }
                break;

            case HistoricalDataType.Trade:
                if (tickReader == null)
                {
                    tickReader = new DZHTickReader(dzhDataPath);
                }
                ticks = tickReader.RequestTicks(symbol, startTime, endTime);
                if ((ticks != null) && (ticks.Count > 0))
                {
                    int prevVol = 0;
                    foreach (DZHTick aTick in ticks)
                    {
                        results.Add(new Trade(aTick.Time, aTick.Price, (int)aTick.Volume - prevVol));
                        prevVol = (int)aTick.Volume;
                    }
                }
                break;

            default:
                throw new ArgumentException("Unknown data type: " + dataType.ToString());
            }
            return(results);
        }
Пример #9
0
        // Get the prices corresponding to the historical data type
        private void ProcessPrices(List<Stock> list, HistoricalDataType DataType)
        {
            // for now implement the case when data is provided daily and startdate is provided
            if (StartDay != 0)
            {
                if (DataType != HistoricalDataType.Daily)
                {
                    throw new ArgumentException("Historical datatype needs to be daily to use startdate");
                }

                List<Stock> filteredStocks = new List<Stock>();
                int index = 0;

                // select the current eligible month
                Stock currentStock = Stocks[index];
                Stock previousStock = Stocks[index];
                int currentMonth = currentStock.Date.Month;

                // ideally this should be the date in the month
                DateTime targetDateTime = new DateTime(currentStock.Date.Year, currentMonth, StartDay);

                DateTime firstDateOfCurrentMonth = new DateTime(currentStock.Date.Year, currentMonth, 1);

                while (index < Stocks.Count)
                {
                    while (firstDateOfCurrentMonth.Month == currentMonth)
                    {
                        index += 1;
                        // match!
                        if (currentStock.Date == targetDateTime)
                        {
                            filteredStocks.Add(currentStock);
                            if (index == Stocks.Count)
                            {
                                break;
                            }

                            // move to next month
                            currentMonth = (currentMonth + 1) % 12;
                        }
                        else if (currentStock.Date < targetDateTime)
                        {
                            // if we are at the last stock. just add this
                            if (index == Stocks.Count)
                            {
                                filteredStocks.Add(currentStock);
                                break;
                            }
                        }
                        else
                        {
                            // we just moved past the target date.
                            // let's just use the previous date if it is in the same month
                            if (previousStock.Date.Month == currentMonth)
                            {
                                filteredStocks.Add(previousStock);
                            }
                            else
                            {
                                filteredStocks.Add(currentStock);
                            }
                            if (index == Stocks.Count)
                            {
                                break;
                            }

                            currentMonth = (currentMonth + 1) % 12;
                        }

                        // move to the next stock
                        previousStock = currentStock;
                        currentStock = Stocks[index];
                    }

                    if (index == Stocks.Count)
                    {
                        // we break out of the loop because no more stock. so just break again
                        break;
                    }

                    // otherwise let's move to the next month
                    firstDateOfCurrentMonth = firstDateOfCurrentMonth.AddMonths(1);
                    targetDateTime = targetDateTime.AddMonths(1);
                    // move to the first stock of the next month
                    while (index < Stocks.Count)
                    {
                        if (currentStock.Date.Month == firstDateOfCurrentMonth.Month)
                        {
                            break;
                        }

                        index += 1;

                        if (index == Stocks.Count)
                        {
                            break;
                        }

                        previousStock = currentStock;
                        currentStock = Stocks[index];
                    }
                }

                // reconstruct the list of stocks so we only include stock data in that date of the month
                // if no data for that date of the month then select the first date just before it.
                foreach (var stock in Stocks)
                {

                }
            }
        }
Пример #10
0
        // Get the prices corresponding to the historical data type
        private void ProcessPrices(List <Stock> list, HistoricalDataType DataType)
        {
            // for now implement the case when data is provided daily and startdate is provided
            if (StartDay != 0)
            {
                if (DataType != HistoricalDataType.Daily)
                {
                    throw new ArgumentException("Historical datatype needs to be daily to use startdate");
                }

                List <Stock> filteredStocks = new List <Stock>();
                int          index          = 0;

                // select the current eligible month
                Stock currentStock  = Stocks[index];
                Stock previousStock = Stocks[index];
                int   currentMonth  = currentStock.Date.Month;

                // ideally this should be the date in the month
                DateTime targetDateTime = new DateTime(currentStock.Date.Year, currentMonth, StartDay);

                DateTime firstDateOfCurrentMonth = new DateTime(currentStock.Date.Year, currentMonth, 1);

                while (index < Stocks.Count)
                {
                    while (firstDateOfCurrentMonth.Month == currentMonth)
                    {
                        index += 1;
                        // match!
                        if (currentStock.Date == targetDateTime)
                        {
                            filteredStocks.Add(currentStock);
                            if (index == Stocks.Count)
                            {
                                break;
                            }

                            // move to next month
                            currentMonth = (currentMonth + 1) % 12;
                        }
                        else if (currentStock.Date < targetDateTime)
                        {
                            // if we are at the last stock. just add this
                            if (index == Stocks.Count)
                            {
                                filteredStocks.Add(currentStock);
                                break;
                            }
                        }
                        else
                        {
                            // we just moved past the target date.
                            // let's just use the previous date if it is in the same month
                            if (previousStock.Date.Month == currentMonth)
                            {
                                filteredStocks.Add(previousStock);
                            }
                            else
                            {
                                filteredStocks.Add(currentStock);
                            }
                            if (index == Stocks.Count)
                            {
                                break;
                            }

                            currentMonth = (currentMonth + 1) % 12;
                        }

                        // move to the next stock
                        previousStock = currentStock;
                        currentStock  = Stocks[index];
                    }

                    if (index == Stocks.Count)
                    {
                        // we break out of the loop because no more stock. so just break again
                        break;
                    }

                    // otherwise let's move to the next month
                    firstDateOfCurrentMonth = firstDateOfCurrentMonth.AddMonths(1);
                    targetDateTime          = targetDateTime.AddMonths(1);
                    // move to the first stock of the next month
                    while (index < Stocks.Count)
                    {
                        if (currentStock.Date.Month == firstDateOfCurrentMonth.Month)
                        {
                            break;
                        }

                        index += 1;

                        if (index == Stocks.Count)
                        {
                            break;
                        }

                        previousStock = currentStock;
                        currentStock  = Stocks[index];
                    }
                }

                // reconstruct the list of stocks so we only include stock data in that date of the month
                // if no data for that date of the month then select the first date just before it.
                foreach (var stock in Stocks)
                {
                }
            }
        }
Пример #11
0
        public static HistoricalData ParseCsv(string filePath, string Ticker, string Name, HistoricalDataType DataType)
        {
            HistoricalData result = new HistoricalData();

            result.Ticker = Ticker;
            result.Name   = Name;
            result.Stocks = new List <Stock>();

            using (StreamReader reader = new StreamReader(File.OpenRead(filePath)))
            {
                // skip first line which is date,open,high,low,close,volumn,adj close
                reader.ReadLine();
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    Stock price = Stock.ParsePrice(line, Ticker, Name);
                    result.Stocks.Add(price);
                }

                // filter according to daily, weekly or monthly
                result.ProcessPrices(result.Stocks, DataType);

                result.Stocks.Reverse();
            }

            return(result);
        }