示例#1
0
        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();
        }
示例#2
0
        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);
            }
        }