Пример #1
0
        public void CollectTokens(NinjaTrader.Cbi.Execution execution)
        {
            lock (exectokens[0])
            {
                if (strategybase.TraceOrders)
                {
                    strategybase.Print("CollectTokens");
                }
                int sign = (execution.MarketPosition == MarketPosition.Long) ? 1 : -1;

                if (entry[0].Order != null && execution.Order.OrderId == entry[0].Order.OrderId)
                {
                    exectokens[0].Add(execution.OrderId);
                    if (positionX == 0)
                    {
                        exectokens[0].Clear();
                    }
                }

                if (exit[0].Order != null && execution.Order.OrderId == exit[0].Order.OrderId)
                {
                    exectokens[0].Add(execution.OrderId);
                    if (positionX == 0)
                    {
                        exectokens[0].Clear();
                    }
                }
            }
            lock (exectokens[1])
            {
                if (entry[1].Order != null && execution.Order.OrderId == entry[1].Order.OrderId)
                {
                    exectokens[1].Add(execution.OrderId);
                    if (positionY == 0)
                    {
                        exectokens[1].Clear();
                    }
                }

                if (exit[1].Order != null && execution.Order.OrderId == exit[1].Order.OrderId)
                {
                    exectokens[1].Add(execution.OrderId);
                    if (positionY == 0)
                    {
                        exectokens[1].Clear();
                    }
                }
            }
        }
Пример #2
0
        protected override void OnExecutionUpdate(NinjaTrader.Cbi.Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
        {
            try {
                foreach (SingleSpread spread in spreads)
                {
                    spread.OnExecution(execution);
                }

                CleanupQueue();
            }
            catch (Exception e) {
                if (TraceOrders)
                {
                    Print("ERROR: " + e.Message + " STACKTRACE: " + e.StackTrace);
                }
            }
        }
Пример #3
0
 public bool IsOurExecution(NinjaTrader.Cbi.Execution execution)
 {
     if (entry[0].Order != null && execution.Order.OrderId == entry[0].Order.OrderId)
     {
         return(true);
     }
     if (entry[1].Order != null && execution.Order.OrderId == entry[1].Order.OrderId)
     {
         return(true);
     }
     if (exit[0].Order != null && execution.Order.OrderId == exit[0].Order.OrderId)
     {
         return(true);
     }
     if (exit[1].Order != null && execution.Order.OrderId == exit[1].Order.OrderId)
     {
         return(true);
     }
     return(false);
 }
Пример #4
0
 public bool Inflight(NinjaTrader.Cbi.Execution execution)
 {
     if (checkinflight)
     {
         if (strategybase.TraceOrders)
         {
             strategybase.Print("WARNING: inflight execution:" + execution.MarketPosition.ToString() + " " + execution.Quantity.ToString() + " " + execution.Instrument.FullName);
         }
         strategybase.Print("Inflight execution:" + execution.MarketPosition.ToString() + " " + execution.Quantity.ToString() + " " + execution.Instrument.FullName + ",LogLevel.Warning");
         if (execution.Order.Filled == execution.Order.Quantity)
         {
             if (strategybase.TraceOrders)
             {
                 strategybase.Print("end inflight");
             }
             checkinflight = false;
         }
         return(true);
     }
     return(false);
 }
Пример #5
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;
                }
            }
        }
Пример #6
0
 public static int CompareTime(NinjaTrader.Cbi.Execution a, NinjaTrader.Cbi.Execution b)
 {
     return(a.Time.CompareTo(b.Time));
 }
Пример #7
0
        public void OnExecution(NinjaTrader.Cbi.Execution execution)
        {
            if (execution == null)
            {
                return;
            }
            if (execution.Order == null)
            {
                return;
            }
            int sign = (execution.MarketPosition == MarketPosition.Long) ? 1 : -1;

            if (entry[0].Order != null && execution.Order.OrderId == entry[0].Order.OrderId)
            {
                if (entry[0].Inflight(execution))
                {
                    inflight.Add(execution);
                    // we assume that an inflight execution will never come as a collection of
                    // partial executions. Only the first partial will stay inflight, a second
                    // partial would be cancelled
                    return;
                }
                positionX   += sign * execution.Quantity;
                totalprice0 += (double)convfactor0 * execution.Price * sign * execution.Quantity * strategybase.Instruments[0].MasterInstrument.PointValue;
                var commission = (double)convfactor0 * execution.Commission;
                totalprice0 += commission;

                int lsign = (mleg1 == MarketPosition.Long)?+1:-1;

                if (strategybase.State == State.Realtime)
                {
                    CollectTokens(execution);
                }

                if (lots1 * lsign == positionX)
                {
                    entry[0].Zero();
                }
            }
            if (entry[1].Order != null && execution.Order.OrderId == entry[1].Order.OrderId)
            {
                if (entry[1].Inflight(execution))
                {
                    inflight.Add(execution);
                    return;
                }
                positionY   += sign * execution.Quantity;
                totalprice1 += (double)convfactor1 * execution.Price * sign * execution.Quantity * strategybase.Instruments[1].MasterInstrument.PointValue;
                var commission = (double)convfactor1 * execution.Commission;
                totalprice1 += commission;


                int lsign = (mleg1 == MarketPosition.Short)?+1:-1;

                if (strategybase.State == State.Realtime)
                {
                    CollectTokens(execution);
                }

                if (lots2 * lsign == positionY)
                {
                    entry[1].Zero();
                }
            }
            if (exit[0].Order != null && execution.Order.OrderId == exit[0].Order.OrderId)
            {
                if (exit[0].Inflight(execution))
                {
                    inflight.Add(execution);
                    return;
                }
                positionX   += sign * execution.Quantity;
                totalprice0 += (double)convfactor0 * execution.Price * sign * execution.Quantity * strategybase.Instruments[0].MasterInstrument.PointValue;
                var commission = (double)convfactor0 * execution.Commission;
                totalprice0 += commission;
                _exitedX    += sign * execution.Quantity;

                if (strategybase.State == State.Realtime)
                {
                    CollectTokens(execution);
                }
            }
            if (exit[1].Order != null && execution.Order.OrderId == exit[1].Order.OrderId)
            {
                exitinprogress = false;
                if (exit[1].Inflight(execution))
                {
                    inflight.Add(execution);
                    return;
                }
                positionY   += sign * execution.Quantity;
                totalprice1 += (double)convfactor1 * execution.Price * sign * execution.Quantity * strategybase.Instruments[1].MasterInstrument.PointValue;
                var commission = (double)convfactor1 * execution.Commission;
                totalprice1 += commission;
                _exitedY    += sign * execution.Quantity;

                if (strategybase.State == State.Realtime)
                {
                    CollectTokens(execution);
                }
            }

            if (exit[0].Order != null && execution.Order.OrderId == exit[0].Order.OrderId)
            {
                if (_exitedX == -1 * _toexitX)
                {
                    exit[0].Zero();
                }
            }

            if (exit[1].Order != null && execution.Order.OrderId == exit[1].Order.OrderId)
            {
                if (_exitedY == -1 * _toexitY)
                {
                    exit[1].Zero();
                }
            }
            if (exit[0].Order == null && exit[1].Order == null)
            {
                exitinprogress = false;
            }

            if (strategybase.TraceOrders)
            {
                //strategybase.Print(execution.ToString());
                string tmp = "Spread.OnExecution: Leg1(" + positionX.ToString() + "),Leg2(" + positionY.ToString() + ")";
                strategybase.Print(tmp);
                Zweistein.NinjaTraderLog.Process(tmp, "", LogLevel.Information, LogCategories.Strategy);
            }
        }