I want to set a stoploss step by step.
There is an initial stoploss and a profit target.
What I want is a step-by-step stop loss.
For long, when the price reaches MoveTSToLong, I want to set the stoploss to MoveTSAfterLong, when the price reaches MoveTSToLong2, I want to set the stoploss to MoveTSAfterLong2, and when the price reaches MoveTSToLong3, I want to set the stoploss to MoveTSAfterLong3.
is this possible?
I wrote the code like this;
#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class XXX: Strategy { private double entryPrice; private double currentPrice; public enum MovingAverageType { SMA, EMA, DEMA, HMA, TEMA, TMA, VWMA, WMA } private SMA fastsma; private SMA slowsma; private EMA ema; private DEMA dema; private HMA hma; private TEMA tema; private TMA tma; private VWMA vwma; private WMA wma; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = ""; Name = ""; FastMA = 3; SlowMA = 135; SlowMAIndicatorType = MovingAverageType.HMA; TakeProfitLong = 0; StopLossLong = 0; TakeProfitShort = 0; StopLossShort = 0; MoveTSAfterLong = 0; MoveTSToLong = 0; MoveTSAfterLong2nd = 0; MoveTSToLong2nd = 0; MoveTSAfterLong3rd = 0; MoveTSToLong3rd = 0; MoveTSAfterShort = 75; MoveTSToShort = 10; MoveTSAfterShort2nd = 0; MoveTSToShort2nd = 0; MoveTSAfterShort3rd = 0; MoveTSToShort3rd = 0; AddPlot(Brushes.Red, "FastMAPlot"); AddPlot(Brushes.Blue, "SlowMAPlot"); } else if (State == State.Configure) { // if (TakeProfitLong > 0) // SetProfitTarget("buy", CalculationMode.Ticks, TakeProfitLong); // if (StopLossLong > 0) // SetStopLoss("buy", CalculationMode.Ticks, StopLossLong, false); // if (TakeProfitShort > 0) // SetProfitTarget("sell", CalculationMode.Ticks, TakeProfitShort); // if (StopLossShort > 0) // SetStopLoss("sell", CalculationMode.Ticks, StopLossShort, false); } else if (State == State.DataLoaded){ fastsma = SMA(FastMA); slowsma = SMA(SlowMA); ema = EMA(SlowMA); dema = DEMA(SlowMA); hma = HMA(SlowMA); tema = TEMA(SlowMA); tma = TMA(SlowMA); vwma = VWMA(SlowMA); wma = WMA(SlowMA); } } protected override void OnBarUpdate() { if (BarsInProgress != 0 || CurrentBars[0] < SlowMA) return; // determind FastMA and SlowMA FastMAPlot[0] = fastsma[0]; switch (SlowMAIndicatorType) { case MovingAverageType.SMA: SlowMAPlot[0] = slowsma[0]; break; case MovingAverageType.EMA: SlowMAPlot[0] = ema[0]; break; case MovingAverageType.DEMA: SlowMAPlot[0] = dema[0]; break; case MovingAverageType.HMA: SlowMAPlot[0] = hma[0]; break; case MovingAverageType.TEMA: SlowMAPlot[0] = tema[0]; break; case MovingAverageType.TMA: SlowMAPlot[0] = tma[0]; break; case MovingAverageType.VWMA: SlowMAPlot[0] = vwma[0]; break; case MovingAverageType.WMA: SlowMAPlot[0] = wma[0]; break; default: throw new ArgumentException("Unsupported Moving Average Type: " + SlowMAIndicatorType.ToString()); } // Resets the stop loss to the original value when all positions are closed if (Position.MarketPosition == MarketPosition.Flat) { // if (TakeProfitLong > 0) // SetProfitTarget("buy", CalculationMode.Ticks, TakeProfitLong); // if (StopLossLong > 0) // SetStopLoss("buy", CalculationMode.Ticks, StopLossLong, false); // if (TakeProfitShort > 0) // SetProfitTarget("sell", CalculationMode.Ticks, TakeProfitShort); // if (StopLossShort > 0) // SetStopLoss("sell", CalculationMode.Ticks, StopLossShort, false); } // If a long position is open, allow for stop loss modification to breakeven else if (Position.MarketPosition == MarketPosition.Long) { Print(" "); Print("@@@@@@@@@@@@@@@@@@@"); Print("EntryPriceLong : {" + Position.AveragePrice + "}"); Print("MoveTSAfterLongPrice : {" + (Position.AveragePrice + MoveTSAfterLong * TickSize).ToString() + "}"); Print("MoveTSToLongPrice : {" + (Position.AveragePrice + MoveTSToLong * TickSize).ToString() + "}"); Print("MoveTSAfterLong2Price : {" + (Position.AveragePrice + MoveTSAfterLong2nd * TickSize).ToString() + "}"); Print("MoveTSToLong2Price : {" + (Position.AveragePrice + MoveTSToLong2nd * TickSize).ToString() + "}"); Print("MoveTSAfterLong3Price : {" + (Position.AveragePrice + MoveTSAfterLong3rd * TickSize).ToString() + "}"); Print("MoveTSToLong3Price : {" + (Position.AveragePrice + MoveTSToLong3rd * TickSize).ToString() + "}"); Print("@@@@@@@@@@@@@@@@@@@"); // Once the price is greater than entry price + MoveTSAfterLong, set stop loss to breakeven if ((Close[0] > Position.AveragePrice + MoveTSAfterLong * TickSize) && (MoveTSAfterLong != 0)) { Print("MoveTSAfterLong"); // SetStopLoss("buy", CalculationMode.Price, Position.AveragePrice + MoveTSToLong * TickSize, false); ExitLongMIT(Convert.ToInt32(DefaultQuantity), Position.AveragePrice + MoveTSToLong * TickSize, "buy-stoploss1", "buy"); } // Once the price is greater than entry price + MoveTSAfterLong2nd , set stop loss to breakeven if ((Close[0] > Position.AveragePrice + MoveTSAfterLong2nd * TickSize) && (MoveTSAfterLong2nd !=0)) { Print("MoveTSAfterLong2"); // SetStopLoss("buy", CalculationMode.Price, Position.AveragePrice + MoveTSToLong2nd * TickSize, false); ExitLongMIT(Convert.ToInt32(DefaultQuantity), Position.AveragePrice + MoveTSToLong2nd * TickSize, "buy-stoploss2", "buy"); } // Once the price is greater than entry price + MoveTSAfterLong3rd , set stop loss to breakeven if ((Close[0] > Position.AveragePrice + MoveTSAfterLong3rd * TickSize) && (MoveTSAfterLong3rd != 0)) { Print("MoveTSAfterLong3"); // SetStopLoss("buy", CalculationMode.Price, Position.AveragePrice + MoveTSToLong3rd * TickSize, false); ExitLongMIT(Convert.ToInt32(DefaultQuantity), Position.AveragePrice + MoveTSToLong3rd * TickSize, "buy-stoploss3", "buy"); } if ((Close[0] > Position.AveragePrice + TakeProfitLong * TickSize) && (TakeProfitLong > 0)) { Print("TakeProfitLong"); ExitLong("buy-takeprofit", "buy"); } if ((Close[0] > Position.AveragePrice + StopLossLong * TickSize) && (StopLossLong > 0)) { Print("StopLossLong"); ExitLong("buy-stoploss", "buy"); } } else if (Position.MarketPosition == MarketPosition.Short) { Print(" "); Print("@@@@@@@@@@@@@@@@@@@"); Print("EntryPriceShort : {" + Position.AveragePrice + "}"); Print("MoveTSAfterShortPrice : {" + (Position.AveragePrice - MoveTSAfterShort * TickSize).ToString() + "}"); Print("MoveTSToShortPrice : {" + (Position.AveragePrice - MoveTSToShort * TickSize).ToString() + "}"); Print("MoveTSAfterShort2Price : {" + (Position.AveragePrice - MoveTSAfterShort2nd * TickSize).ToString() + "}"); Print("MoveTSToShort2Price : {" + (Position.AveragePrice - MoveTSToShort2nd * TickSize).ToString() + "}"); Print("MoveTSAfterShort3Price : {" + (Position.AveragePrice - MoveTSAfterShort3rd * TickSize).ToString() + "}"); Print("MoveTSToShort3Price : {" + (Position.AveragePrice - MoveTSToShort3rd * TickSize).ToString() + "}"); Print("@@@@@@@@@@@@@@@@@@@"); if ((Close[0] < Position.AveragePrice - MoveTSAfterShort * TickSize) && (MoveTSAfterShort != 0)) { Print("MoveTSAfterShort"); // SetStopLoss("sell", CalculationMode.Price, Position.AveragePrice - MoveTSToShort * TickSize, false); ExitShortMIT(Convert.ToInt32(DefaultQuantity), Position.AveragePrice - MoveTSToShort * TickSize, "sell-shortexit1", "sell"); } if ((Close[0] < Position.AveragePrice - MoveTSAfterShort2nd * TickSize) && (MoveTSAfterShort2nd != 0)) { Print("MoveTSToShort2"); // SetStopLoss("sell", CalculationMode.Price, Position.AveragePrice - MoveTSToShort2nd * TickSize, false); ExitShortMIT(Convert.ToInt32(DefaultQuantity), Position.AveragePrice - MoveTSAfterShort2nd * TickSize, "sell-shortexit2", "sell"); } if ((Close[0] < Position.AveragePrice - MoveTSAfterShort3rd * TickSize) && (MoveTSAfterShort3rd != 0)) { Print("MoveTSToShort3"); // SetStopLoss("sell", CalculationMode.Price, Position.AveragePrice - MoveTSToShort3rd * TickSize, false); ExitShortMIT(Convert.ToInt32(DefaultQuantity), Position.AveragePrice - MoveTSAfterShort3rd * TickSize, "sell-shortexit3", "sell"); } if ((Close[0] < Position.AveragePrice - TakeProfitShort * TickSize) && (TakeProfitShort > 0)) { Print("TakeProfitShort"); ExitShort("sell-takeprofit", "sell"); } if ((Close[0] < Position.AveragePrice - StopLossShort * TickSize) && (StopLossShort > 0)) { Print("StopLossShort"); ExitShort("sell-stoploss", "sell"); } } // create position if (CrossAbove(FastMAPlot, SlowMAPlot, 1)) { EnterLong(Convert.ToInt32(DefaultQuantity), "buy"); } if (CrossBelow(FastMAPlot, SlowMAPlot, 1)) { EnterShort(Convert.ToInt32(DefaultQuantity), "sell"); } } } }
Comment