Пример #1
0
        public void RebuildPositions(NinjaTrader.NinjaScript.StrategyBase strategy)
        {
            DateTime t = NinjaTrader.Core.Globals.Now;

            //SetBaseCurrency(strategy.Account.Denomination);

            PropertyInfo pi           = strategy.GetType().GetProperty("FirstLoadUTC");
            string       FirstLoadUTC = (string)pi.GetValue(strategy);

            DateTime tmp     = DateTime.Parse(FirstLoadUTC);
            DateTime mintime = new DateTime(tmp.Year, tmp.Month, tmp.Day, tmp.Hour, tmp.Minute, tmp.Second);

            mintime = mintime.AddHours(-1);

            for (int i = 0; i < 2; i++)
            {
                int    n   = 0;
                double avg = 0;

                //if(t.AddDays(-2) <mintime) mintime=t.AddDays(-2);
                //   strategy.Print("RebuildPositions: mintime:"+ mintime.Kind.ToString() + " t:"+t.Kind.ToString());
                //   strategy.Print("RebuildPositions:"+ mintime.ToString());
                System.Collections.ObjectModel.Collection <NinjaTrader.Cbi.Execution> ce =
                    NinjaTrader.Cbi.Execution.DbGet(strategy.Account, strategy.Instruments[i], mintime, t);
                foreach (string s in exectokens[i])
                {
                    NinjaTrader.Cbi.Execution exec = ce.FirstOrDefault(x => x.OrderId == s);
                    if (exec != null)
                    {
                        if (i == 0 && exec.MarketPosition == mleg1 || i == 1 && exec.MarketPosition != mleg1)
                        {
                            // we add averaging
                            n   += exec.Quantity;
                            avg += exec.Price * exec.Quantity;
                        }
                        else
                        {
                            n   -= exec.Quantity;
                            avg -= exec.Price * exec.Quantity;
                        }
                        execs[i].Add(exec);
                        strategy.Executions.Add(exec);
                        strategy.Positions[i].AddExecution(exec);
                    }
                }

                int u = 0;
                if (mleg1 == MarketPosition.Long)
                {
                    u = 1;
                }
                if (mleg1 == MarketPosition.Short)
                {
                    u = -1;
                }
                if (i == 0)
                {
                    positionX = 1 * u * n;
                }
                if (i == 1)
                {
                    positionY = -1 * u * n;
                }

                if (n != 0)
                {
                    avg_fill[i] = avg / (double)n;
                }
                else
                {
                    avg_fill[i] = 0;
                }
            }
        }