예제 #1
0
        public UpdatedDataLoader(string pluginPath, string csvDataPath)
        {
            PluginHelper helper = new PluginHelper(pluginPath);

            this.plugin_HistoryData = new Plugin_HistoryData_CnFutures(helper, csvDataPath);
            this.tradingDayCache    = new CacheUtils_TradingDay(plugin_HistoryData.GetTradingDays());
            this.codeInfoCache      = new CacheUtils_CodeInfo(plugin_HistoryData.GetInstruments());
            this.mainContracts      = plugin_HistoryData.GetMainContractInfos();
        }
예제 #2
0
 public DataForForward_Code(IDataPackage_Code dataPackage, ForwardReferedPeriods referedPeriods)
 {
     this.dataPackage          = dataPackage;
     this.referedPeriods       = referedPeriods;
     this.dic_Period_KLineData = dataPackage.CreateKLineData_RealTimes(referedPeriods.UsedKLinePeriods);
     this.mainKLinePeriod      = referedPeriods.GetMinPeriod();
     this.mainKLineData        = this.dic_Period_KLineData[mainKLinePeriod];
     this.cache_TradingDay     = new CacheUtils_TradingDay(dataPackage.GetTradingDays());
 }
예제 #3
0
        public void Load(XmlElement xmlElem)
        {
            this.dataPackage    = this.dataCenter.DataPackageFactory.CreateDataPackage_Code(xmlElem);
            this.referedPeriods = new ForwardReferedPeriods();
            this.referedPeriods.Load(xmlElem);

            this.dic_Period_KLineData = dataPackage.CreateKLineData_RealTimes(referedPeriods.UsedKLinePeriods);
            this.cache_TradingDay     = new CacheUtils_TradingDay(dataPackage.GetTradingDays());
            this.TradingDay           = int.Parse(xmlElem.GetAttribute("tradingDay"));
        }
예제 #4
0
        private static int GetEndIndex(CodeInfo codeInfo, CacheUtils_TradingDay allTradingDayCache)
        {
            int endIndex;

            if (codeInfo.End <= 0)
            {
                endIndex = allTradingDayCache.GetAllTradingDays().Count - 1;
            }
            else
            {
                endIndex = allTradingDayCache.GetTradingDayIndex(codeInfo.End, true);
            }
            return(endIndex);
        }
예제 #5
0
        private void AddSteps_KLineData(List <IStep> steps)
        {
            List <CodeInfo> instruments = historyData.GetInstruments();
            List <int>      tradingDays = historyData.GetTradingDays();

            CacheUtils_TradingDay cache = new CacheUtils_TradingDay(tradingDays);

            for (int i = 0; i < instruments.Count; i++)
            {
                CodeInfo codeInfo = instruments[i];
                if (codeInfo.End == 0 || codeInfo.End >= 20170718)
                {
                    AddSteps_KLineData_Instrument(steps, codeInfo, cache);
                }
            }
        }
예제 #6
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))));
        }
예제 #7
0
        private IList <double[]>[] GetPeriodArr(string code, int startDate, int endDate, KLinePeriod klinePeriod)
        {
            ITradingTimeReader_Code tradingTimeReader_Code = this.dataReader.CreateTradingTimeReader(code);
            List <int>            allTradingDays           = klineDataStore.GetAllTradingDay(code, klinePeriod);
            CacheUtils_TradingDay cache       = new CacheUtils_TradingDay(allTradingDays);
            IList <int>           tradingDays = cache.GetTradingDays(startDate, endDate);

            IList <double[]>[] periodArr = new List <double[]> [tradingDays.Count];
            for (int i = 0; i < tradingDays.Count; i++)
            {
                int              tradingDay = tradingDays[i];
                ITradingTime     time       = tradingTimeReader_Code.GetTradingTime(tradingDay);
                IList <double[]> periods    = time.TradingPeriods;
                periodArr[i] = periods;
            }

            return(periodArr);
        }
예제 #8
0
        public Step_UpdateTradingDays(IPlugin_HistoryData historyData, IDataStore dataStore)
        {
            this.tradingDayStore = dataStore.CreateTradingDayStore();
            this.tradingDays     = historyData.GetTradingDays();
            List <int> historyTradingDays = tradingDayStore.Load();

            this.newTradingDays = new List <int>();
            CacheUtils_TradingDay cache = new CacheUtils_TradingDay(historyTradingDays);

            for (int i = 0; i < tradingDays.Count; i++)
            {
                int tradingDay = tradingDays[i];
                if (!cache.IsTrade(tradingDay))
                {
                    newTradingDays.Add(tradingDay);
                }
            }
            newTradingDays.Sort();
        }
예제 #9
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);
            }
        }
예제 #10
0
 private IList <int> GetCodeTradingDays(CodeInfo codeInfo, CacheUtils_TradingDay allTradingCache)
 {
     return(allTradingCache.GetTradingDays(codeInfo.Start, codeInfo.End));
 }
예제 #11
0
        private List <int> GetAllNotUpdateTickData(CodeInfo codeInfo, CacheUtils_TradingDay allTradingDayCache, List <int> storedDays, bool isFillUp)
        {
            if (isFillUp)
            {
                HashSet <int> set = new HashSet <int>(storedDays);
                IList <int>   codeAllTradingDays   = GetCodeTradingDays(codeInfo, allTradingDayCache);
                List <int>    allNotUpdateTickData = new List <int>(set.Count);
                for (int i = 0; i < codeAllTradingDays.Count; i++)
                {
                    int day = codeAllTradingDays[i];
                    if (!set.Contains(day))
                    {
                        allNotUpdateTickData.Add(day);
                    }
                }
                return(allNotUpdateTickData);
            }
            else
            {
                /*
                 * 如果已更新数据记录中保存了该合约的最后更新日期,则直接按该信息更新后面的数据
                 * 否则扫描整个目录来获取待更新信息
                 */
                int lastUpdatedDate = updatedDataInfo.GetLastUpdatedTickData(codeInfo.Code);
                int startIndex;
                if (lastUpdatedDate >= 0)
                {
                    int lastSavedUpdateTickIndex = allTradingDayCache.GetTradingDayIndex(lastUpdatedDate);
                    startIndex = lastSavedUpdateTickIndex + 1;
                }
                else
                {
                    if (storedDays.Count == 0)
                    {
                        startIndex = allTradingDayCache.GetTradingDayIndex(codeInfo.Start, false);
                    }
                    else
                    {
                        storedDays.Sort();
                        int lastUpdateIndex = allTradingDayCache.GetTradingDayIndex(storedDays[storedDays.Count - 1]);
                        if (lastUpdateIndex < 0)
                        {
                            //TODO 不应该出现这种情况,TODO 写日志
                        }
                        startIndex = lastUpdateIndex + 1;
                    }

                    //保存更新信息
                    int lastUpdateTickIndex = startIndex - 1;
                    if (lastUpdateTickIndex >= 0 && lastUpdateTickIndex < allTradingDayCache.GetAllTradingDays().Count)
                    {
                        updatedDataInfo.WriteUpdateInfo_Tick(codeInfo.Code, allTradingDayCache.GetAllTradingDays()[lastUpdateTickIndex]);
                    }
                }
                if (startIndex < 0 || startIndex >= allTradingDayCache.GetAllTradingDays().Count)
                {
                    return(null);
                }

                int endIndex = GetEndIndex(codeInfo, allTradingDayCache);
                if (endIndex < startIndex)
                {
                    return(null);
                }

                int len = endIndex - startIndex + 1;
                if (len <= 0)
                {
                    return(null);
                }
                return(allTradingDayCache.GetAllTradingDays().GetRange(startIndex, len));
            }
        }
예제 #12
0
        private void AddSteps_KLineData_Instrument(List <IStep> steps, CodeInfo codeInfo, CacheUtils_TradingDay tradingDaysCache)
        {
            //if (!(codeInfo.Exchange == "SQ" && codeInfo.Code.EndsWith("0000")))
            //    return;
            string code = codeInfo.Code;

            for (int i = 0; i < updatePeriods.Count; i++)
            {
                //TODO 暂时没处理FillUp的情况,考虑使用全覆盖的方式实现
                KLinePeriod period          = updatePeriods[i];
                int         lastUpdatedDate = updatedDataInfo.GetLastUpdatedKLineData(code, period);
                int         lastCodeDate    = codeInfo.End;
                if (lastCodeDate <= 0)
                {
                    lastCodeDate = tradingDaysCache.LastTradingDay;
                }

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

                if (!isFillUp)
                {
                    if (lastUpdatedDate >= lastCodeDate || lastUpdatedDate >= tradingDaysCache.LastTradingDay)
                    {
                        continue;
                    }
                }

                Step_UpdateKLineData step = new Step_UpdateKLineData(codeInfo, period, tradingDaysCache, historyData, klineDataStore, updatedDataInfo, updateInfoStore, isFillUp);
                steps.Add(step);
            }
        }