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());
        }
Exemplo n.º 2
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());
            }
        }
        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);
        }