public virtual void AnalyseResult(IInstrument instrument, DateTime startTime, DateTime endTime, MarketDataGrade?grade) { ReferenceValueList.Clear(); IsAnalyse = true; var g = MarketDataGrade.FiveMinutes; if (grade != null) { g = grade.Value; } AnalyseDataSource.PrepareCache(instrument, startTime, endTime, g); for (var time = startTime; time <= endTime; time = MarketData.GetNextTime(time, grade.Value)) { ISignal d = null; try { d = Calculate(instrument, startTime, time, grade); } catch { } if (d != null) { AddReference(Name, new TimeValueObject() { TargetName = instrument.Ticker, Time = d.Time, Value = d.Value, Name = instrument.Name }); } } IsAnalyse = false; }
public override void Start() { if (!CanRun()) { throw new Exception("Project not ready to run!"); } AddInfo("start running!"); if (Status == ProjectStatus.Stopped) { Status = ProjectStatus.Running; AddInfo("prepare running!"); PrepareWork(); AddInfo("prepared!"); } Status = ProjectStatus.Running; ProjectStartTime = DateTime.Now; AddInfo("started running, started time is " + ProjectStartTime.ToString()); for (DateTime time = TestCurrentTime; time <= TestEndTime; time = MarketData.GetNextTime(time, Grade)) { AddInfo("test time is :" + time.ToString() + ", prepare into step"); TestStep(time, MarketData.GetNextTime(time, Grade)); TestCurrentTime = time; if (Status != ProjectStatus.Running) { return; } } AddInfo("stop running!"); Stop(); AddInfo("stoped!"); if (_TestInfo.Count > 0) { if (File.Exists(logFilePath)) { File.Delete(logFilePath); } File.WriteAllLines(logFilePath, _TestInfo.ToArray()); File.WriteAllLines(logFilePath, GetResult().Split('\n')); if (File.Exists(logFilePath)) { System.Diagnostics.Process.Start("notepad.exe", logFilePath); } } }
public override void PrepareWork() { TargetPortfolio.PrepareWork(); MarketDataList.Clear(); CurrentDataSource.DataList.Clear(); CurrentValueList.Clear(); StandardValueList.Clear(); OrderList.Clear(); DateTime st = TestStartTime; if (TestStartTime > AnalyseStartTime) { st = AnalyseStartTime; } CurrentDataSource.CacheStartTime = st; CurrentDataSource.CacheEndTime = TestEndTime; TestCurrentTime = TestStartTime; analyseTime = MarketData.GetNextTime(TestCurrentTime, AnalyseGrade); _MaxLost = new Money() { FxCode = Pnl.FxCode, Number = 0 }; if (UseFirstMarketDataInit) { var fdl = new List <IMarketData>(); InstrumentList.ForEach(v => { var d = CurrentDataSource.GetFirstData(v, TestStartTime, TestEndTime, Grade); if (d != null) { fdl.Add(d); } }); TargetPortfolio.ProcessMarketData(fdl); TargetPortfolio.PositionList.ForEach(v => { var d = fdl.FirstOrDefault(m => m.InstrumentTicker == v.InstrumentTicker); if (d != null) { v.Cost = d.Close; } }); } if (IsUnlimited) { TargetPortfolio.IsUnlimited = true; } standardPortfolio = TargetPortfolio.Clone() as IPortfolio; testStartValue = TargetPortfolio.CurrentValue; CurrentDataSource.PrepareWork(); CurrentTradeGate.PrepareWork(); foreach (var condition in ConditionList) { condition.PrepareWork(); condition.GetInstrumentList = () => { return(InstrumentList); }; condition.AnalyseDataSource = CurrentDataSource; } TestStrategy.CurrentPortfolio = TargetPortfolio; TestStrategy.PrepareWork(); RiskPolicy.PrepareWork(); }
void TestStep(DateTime start, DateTime end) { TestStrategy.CurrentTime = end; AddInfo("test step, start:" + start.ToString() + ",end:" + end.ToString()); var dl = CurrentDataSource.GetDataList(InstrumentList, start, end, Grade); AddInfo("got market data ,count is:" + dl.Count.ToString()); if (dl == null || dl.Count == 0) { return; } TestStrategy.CurrentTime = dl.Max(d => d.Time); dl.ForEach(v => { var inst = InstrumentList.FirstOrDefault(i => i.Ticker == v.InstrumentTicker); if (inst != null) { AddInfo("update " + inst.Name + " price, value is:" + v.Close.ToString() + "(" + v.Time.ToString() + ")"); inst.CurrentPrice = v.Close; } }); MarketDataList.AddRange(dl); AddInfo("portfolio process market data!"); TargetPortfolio.ProcessMarketData(dl); AddInfo("standard portfolio process market data!"); standardPortfolio.ProcessMarketData(dl); AddInfo("strategy process market data!"); TestStrategy.ProcessMarketData(dl); if (analyseTime <= end) { AddInfo("prepare analyse, analyse time is:" + analyseTime.ToString()); AnalyseStep(); analyseTime = MarketData.GetNextTime(end, AnalyseGrade); var cl = new List <ISignal>(); foreach (var condition in ConditionList) { var rl = condition.GetResult(); if (rl != null && rl.Count > 0) { cl.AddRange(rl); } } if (cl.Count > 0) { AddInfo("got signal, count is " + cl.Count.ToString()); TestStrategy.ProcessSignal(cl); } } AddInfo("strategy process portfolio"); TestStrategy.ProcessPortfolio(); var ol = TestStrategy.GetOrderList(); if (ol.Count > 0) { AddInfo("strategy generate order, count is :" + ol.Count.ToString()); List <IOrder> col = new List <IOrder>(); foreach (var o in ol) { if (o != null && RiskPolicy.PredictOrder(o, TargetPortfolio)) { col.Add(o); } } OrderList.AddRange(col); AddInfo("trade gate process order"); CurrentTradeGate.ProcessorOrder(col); AddInfo("portfolio info before process order is" + GetPortfolioMemo(TargetPortfolio)); TargetPortfolio.ProcessOrderList(col); AddInfo("portfolio info after process order is" + GetPortfolioMemo(TargetPortfolio)); } if (!IsUnlimited)//adjust risk { AddInfo("adjust risk"); ol = RiskPolicy.AdjustRisk(TargetPortfolio); if (ol.Count > 0) { AddInfo("risk order generate, count is:" + ol.Count.ToString()); OrderList.AddRange(ol); List <IOrder> col = ol.Where(v => v != null).ToList(); CurrentTradeGate.ProcessorOrder(col); TargetPortfolio.ProcessOrderList(col); } } CurrentValueList.Add(new TimeValueObject() { Time = dl.Max(v => v.Time), Value = CurrentValue, Memo = GetPortfolioMemo(TargetPortfolio) }); StandardValueList.Add(new TimeValueObject() { Time = dl.Max(v => v.Time), Value = StandardValue, Memo = GetPortfolioMemo(standardPortfolio) }); if (_MaxLost.Number > Pnl.Number) { _MaxLost.Number = Pnl.Number; } if (TestStepDelayMS > 0) { Thread.Sleep(TestStepDelayMS); } }