コード例 #1
0
        public void setStrategyBase(NinjaTrader.NinjaScript.StrategyBase s)
        {
            strategybase = s;
            SetBaseCurrency(s.Account.Denomination);

            if (!string.IsNullOrEmpty(iehdata))
            {
                try
                {
                    BinaryFormatter bf     = new BinaryFormatter();
                    MemoryStream    memstr = new MemoryStream(Convert.FromBase64String(iehdata), true);
                    iexithandling = ibKastl.Helper.BinaryFormatterHelper.Read <IExitHandling>(memstr, Assembly.GetExecutingAssembly());
                    Zweistein.NinjaTraderLog.Process("SingleSpread.setStrategyBase:", iexithandling.ToString(), NinjaTrader.Cbi.LogLevel.Warning, NinjaTrader.Cbi.LogCategories.Strategy);
                }
                catch (Exception e) {
                    Zweistein.NinjaTraderLog.Process("SingleSpread.setStrategyBase:", e.Message, NinjaTrader.Cbi.LogLevel.Warning, NinjaTrader.Cbi.LogCategories.Strategy);
                    if (strategybase.TraceOrders)
                    {
                        strategybase.Print(e.Message + " SOURCE:" + e.Source);
                    }
                }
            }

            string tmp = "iexithandling=";

            if (iexithandling == null)
            {
                Zweistein.NinjaTraderLog.Process("SingleSpread.setStrategyBase:", "iexithandling==null", NinjaTrader.Cbi.LogLevel.Warning, NinjaTrader.Cbi.LogCategories.Strategy);

                tmp += "null";
            }
            else
            {
                tmp += iexithandling.ToString();
            }

            if (strategybase.TraceOrders)
            {
                strategybase.Print(tmp);
            }
            entry[0].setStrategyBase(s);
            entry[1].setStrategyBase(s);
            exit[0].setStrategyBase(s);
            exit[1].setStrategyBase(s);
        }
コード例 #2
0
 public void Zero()
 {
     if (strategybase.TraceOrders)
     {
         string tmp = "";
         if (iorder != null)
         {
             tmp += iorder.Name + ":";
         }
         tmp += "iorder==null";
         strategybase.Print(tmp);
     }
     iorder        = null;
     checkinflight = false;
 }
コード例 #3
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);
            }
        }