Exemplo n.º 1
0
        /// <summary>
        /// 得到股票或期货的K线数据
        /// </summary>
        /// <param name="code"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="klinePeriod"></param>
        /// <returns></returns>
        public virtual IKLineData GetKLineData(String code, int startDate, int endDate, KLinePeriod klinePeriod)
        {
            IList <int> tradingDays = GetTickDataDays(code);// GetTradingDays();

            if (tradingDays == null)
            {
                return(null);
            }
            CacheUtils_TradingDay cache     = new CacheUtils_TradingDay(tradingDays);
            IList <int>           openDates = cache.GetTradingDays(startDate, endDate);

            if (openDates == null || openDates.Count == 0)
            {
                return(null);
            }
            float lastEndPrice   = -1;
            int   lastEndHold    = -1;
            int   prevTradingDay = cache.GetPrevTradingDay(openDates[0]);

            //找到之前交易日的收盘价和收盘持仓
            if (prevTradingDay > 0)
            {
                ITickData tickData = GetTickData(code, prevTradingDay);
                lastEndPrice = tickData.Arr_Price[tickData.Length - 1];
                lastEndHold  = tickData.Arr_Hold[tickData.Length - 1];
            }

            //如果存在该周期的源数据直接生成,否则用1分钟K线生成
            if (Exist(code, openDates[0], klinePeriod))
            {
                return(GetKLineData(code, klinePeriod, openDates, lastEndPrice, lastEndHold));
            }

            IKLineData oneMinuteKLine = GetKLineData(code, KLinePeriod.KLinePeriod_1Minute, openDates, lastEndPrice, lastEndHold);

            if (oneMinuteKLine.Length == 0)
            {
                return(null);
            }
            IList <ITradingTime> sessions = GetTradingTime(code);

            if (sessions == null)
            {
                return(null);
            }
            return(DataTransfer_KLine2KLine.Transfer(oneMinuteKLine, klinePeriod, new CacheUtils_TradingTime(code, GetTradingTime(code))));
        }
Exemplo n.º 2
0
        private void AddSteps_TickData(List <IStep> steps)
        {
            ITickDataStore tickDataStore = dataStore.CreateTickDataStore();

            List <CodeInfo> allInstruments = historyData.GetInstruments();
            List <int>      allTradingDays = historyData.GetTradingDays();

            CacheUtils_TradingDay tradingDayCache = new CacheUtils_TradingDay(allTradingDays);

            for (int i = 0; i < allInstruments.Count; i++)
            //for (int i = 0; i < 10; i++)
            {
                CodeInfo instrument = allInstruments[i];

                //if (!(instrument.Exchange == "SQ" && instrument.Code.EndsWith("0000")))
                //    continue;

                int lastTradingDay = instrument.End;
                if (lastTradingDay <= 0)
                {
                    lastTradingDay = tradingDayCache.LastTradingDay;
                }
                int lastUpdatedDate = updatedDataInfo.GetLastUpdatedTickData(instrument.Code);
                if (lastUpdatedDate < instrument.Start)
                {
                    lastUpdatedDate = tradingDayCache.GetPrevTradingDay(instrument.Start);
                }

                List <int> allDays;
                if (!isFillUp)
                {
                    if (lastUpdatedDate >= lastTradingDay)
                    {
                        continue;
                    }
                    int startDate = tradingDayCache.GetNextTradingDay(lastUpdatedDate);
                    //如果不填充数据,直接根据最新交易日期和最后更新日期
                    int endDate = instrument.End;
                    if (endDate <= 0)
                    {
                        endDate = allTradingDays[allTradingDays.Count - 1];
                    }

                    //20171015 ww 在不全面更新数据情况下,如果最新的交易日比合约截止时间更新,则不再更新该合约数据
                    int firstNewTradingDay = newTradingDays.Count == 0 ? allTradingDays[allTradingDays.Count - 1] : newTradingDays[0];
                    if (firstNewTradingDay > endDate)
                    {
                        continue;
                    }

                    IList <int> tradingDays = tradingDayCache.GetTradingDays(lastUpdatedDate, endDate);
                    if (tradingDays == null || tradingDays.Count == 0)
                    {
                        continue;
                    }
                    allDays = new List <int>();
                    allDays.AddRange(tradingDays);
                    //allTradingDays
                }
                else
                {
                    //如果填充所有数据
                    List <int> storedAllDays = tickDataStore.GetAllDays(instrument.Code);
                    allDays = GetAllNotUpdateTickData(instrument, tradingDayCache, storedAllDays, isFillUp);
                    if (allDays == null)
                    {
                        continue;
                    }
                }

                AddSteps_TickData_Instrument(steps, instrument.Code, allDays);
            }
        }