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 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); }