public TargetExitQuantity ExitNow(SingleSpread spread)
        {
            double pnl = spread.PnlPerUnit();

            if (stoplossperunit != 0 && pnl <= -1 * stoplossperunit)
            {
                return(TargetExitQuantity.All);
            }
            if (profit1perunitreached == false && profit1perunit != 0 && pnl >= profit1perunit)
            {
                profit1perunitreached = true;
                return(target1exitquantity);
            }
            if (profit2perunitreached == false && profit2perunit != 0 && pnl >= profit2perunit)
            {
                profit2perunitreached = true;
                return(target2exitquantity);
            }

            StrategyBase sb = spread.StrategyBase();

            NinjaTrader.NinjaScript.Strategies.MySpreadTrader S = (NinjaTrader.NinjaScript.Strategies.MySpreadTrader)sb;
            NinjaTrader.NinjaScript.Strategies.Strategy       s = S as NinjaTrader.NinjaScript.Strategies.Strategy;
            //  variables S and s are always the same object sb which is our strategy
            //  the nt8 code editor intellisense logic does not show inherited  class members, but only direct members

            // example of using spread prices
            var spreadpriceseries = s.Spread(S.Closes[0], S.PriceString, spread.Lots1, S.strLeg2Instrument, spread.Lots2, S.Leg1PriceDisplayMultiplier, S.Leg2PriceDisplayMultiplier);
            // and applying SMA to it
            double smoothed = s.SMA(spreadpriceseries.SpreadValue, 3)[0];

            //S.Print(" SMA="+smoothed.ToString());

            return(TargetExitQuantity.None);
        }
        public static double StrategyUpdateSpreads(Queue <SingleSpread> spreads)
        {
            double realizedpnl = 0;

            lock (spreads){
                for (int i = 0; i < spreads.Count; i++)
                {
                    SingleSpread s = spreads.Peek();
                    if (s.Finished())
                    {
                        realizedpnl += s.RealizedPnl();
                        spreads.Dequeue();
                        s.Dispose();
                        s = null;
                    }
                }
            }

            return(realizedpnl);
        }
        public TargetExitQuantity ExitNow(SingleSpread spread)
        {
            if (DateTime.Now < created.AddMinutes(2))
            {
                return(TargetExitQuantity.None);
            }
            // let the trade breath for the first 2 minutes and therefore  do never exit within that time

            double pnl = spread.PnlPerUnit();

            if (stoplossperunit != 0 && pnl <= -1 * stoplossperunit)
            {
                return(TargetExitQuantity.All);
                // this is the case of a  hard stop
            }

            if (pnl <= minPnl)
            {
                return(TargetExitQuantity.All);
                // trailing stopp hit
            }
            if (pnl - stoplossperunit > minPnl)
            {
                //adjust trailing
                minPnl = pnl - stoplossperunit;
                if (spread.StrategyBase().TraceOrders)
                {
                    spread.StrategyBase().Print("MinPnl:" + minPnl.ToString());
                }
            }
            NinjaTrader.NinjaScript.Strategies.Strategy           s = spread.StrategyBase() as NinjaTrader.NinjaScript.Strategies.Strategy;
            NinjaTrader.NinjaScript.Strategies.SpreadExitStrategy S = s as NinjaTrader.NinjaScript.Strategies.SpreadExitStrategy;
            var spreadpriceseries = s.Spread(S.Closes[0], S.PriceString, spread.Lots1, S.strLeg2Instrument, spread.Lots2, S.Leg1PriceDisplayMultiplier, S.Leg2PriceDisplayMultiplier);
            // use all Indicators on the spread price series!!!
            // and applying SMA to it
            double smoothed = s.SMA(spreadpriceseries.SpreadValue, 3)[0];

            return(TargetExitQuantity.None);
        }
        public static void StrategyHandleClickedButton(StrategyBase strategy, ClickhandlerArgs cha, Queue <SingleSpread> spreads, string strratio, LegEntryType leg1entrytype, LegEntryType leg2entrytype, object ieh)
        {
            int _units = cha.Units;

            //  bool b= mut.WaitOne(new TimeSpan(0,0,3));
            // if (b == false) throw new TimeoutException("clickcustomhandler mutex timeout");
            switch (cha.ClickedButton)
            {
            case ClickedButton.Close:
                lock (spreads){
                    foreach (SingleSpread spread in spreads)
                    {
                        spread.Exit(LegExitType.Market, LegExitType.Market);
                    }
                }
                break;

            case ClickedButton.Reverse:
                lock (spreads){
                    foreach (SingleSpread s in spreads)
                    {
                        s.Reverse();
                    }
                }
                break;

            case ClickedButton.GoLong:
                lock (spreads){
                    foreach (SingleSpread s in spreads)
                    {
                        if (s.MarketPosition != MarketPosition.Long)
                        {
                            Zweistein.NinjaTraderLog.Process("GoLong: already Short", "", LogLevel.Error, LogCategories.User);
                            if (strategy.TraceOrders)
                            {
                                strategy.Print("GoLong: already Short");
                            }
                            return;
                        }
                    }
                    //strategy.Account.Currency
                    SingleSpread spread = new SingleSpread(MarketPosition.Long, _units, strratio, strategy);

                    spread.Entry(leg1entrytype, leg2entrytype);
                    IExitHandling eh = ieh as IExitHandling;
                    spread.setIExitHandling(eh);
                    if (cha.Guid != null)
                    {
                        spread.Guid = cha.Guid;
                    }

                    spreads.Enqueue(spread);
                }
                break;

            case ClickedButton.GoShort:
                lock (spreads){
                    foreach (SingleSpread s in spreads)
                    {
                        if (s.MarketPosition != MarketPosition.Short)
                        {
                            Zweistein.NinjaTraderLog.Process("GoShort: already Long", "", LogLevel.Error, LogCategories.User);
                            if (strategy.TraceOrders)
                            {
                                strategy.Print("GoShort: already Long");
                            }
                            return;
                        }
                    }
                    SingleSpread spread = new SingleSpread(MarketPosition.Short, _units, strratio, strategy);
                    spread.Entry(leg1entrytype, leg2entrytype);
                    IExitHandling eh = ieh as IExitHandling;
                    spread.setIExitHandling(eh);
                    if (cha.Guid != null)
                    {
                        spread.Guid = cha.Guid;
                    }
                    spreads.Enqueue(spread);
                }
                break;
            }
        }