Thank you for the reply and helpful details.
I've updated the code as below:
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class MultiStepBreakeven : Strategy
{
private int StopLossMode;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MultiStepBreakeven";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
StopLossModeLong = 0;
StopLossModeShort = 0;
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
//LONG ORDERS
// Set 1
//When no trade is live yet (Flat Position),
// and the current day is either Monday or Tuesday,
// and the current bar's High Price is greater tha the previous bar's High Price, and the previous bar's High Price is also greater than the High Price 2 bars ago,
// and the current time is between 9:30 AM and 11:50 AM, then
// Place a LONG LIMIT order of default lot size (DefaultQuantity)
if ((Position.MarketPosition == MarketPosition.Flat)
&& ((Time[0].DayOfWeek == DayOfWeek.Monday)
|| (Time[0].DayOfWeek == DayOfWeek.Tuesday))
&& (High[0] < High[1])
&& (High[1] < High[2])
&& (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
&& (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
{
EnterLongLimit(Convert.ToInt32(DefaultQuantity), 0, "");
StopLossModeLong = 0;
}
// Set 2
// If/When The Short position gets trigger (= Set 1 conditions get TRUE), then
// ADD the INITIAL STOP to Breakeven + 10 ticks (= Position.AveragePrice (+ 10 ticks))
if ((Position.MarketPosition == MarketPosition.Long)
&& (StopLossModeLong == 0))
{
ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), (Position.AveragePrice + (-10 * TickSize)) , @"INITIAL STOP", "");
}
// Set 3
// If/When The Short position gets profitable by (Set 5 #/10) of ticks, then
// MOVE the STOP to Breakeven (=Position.AveragePrice (*1/+ 0 ticks))
if ((Position.MarketPosition == MarketPosition.Long)
&& (StopLossModeLong == 1))
{
ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), Position.AveragePrice, @"STOP MOVED FROM -10 TICKS (INITIAL STOP) TO BREAKEVEN", "");
}
// Set 4
// If/When The Short position gets profitable by (Set 6 #/20) of ticks, then
// MOVE the STOP to Breakeven + 10 ticks (=Position.AveragePrice (+ 10 ticks))
if ((Position.MarketPosition == MarketPosition.Long)
&& (StopLossModeLong == 2))
{
ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), (Position.AveragePrice + (10 * TickSize)) , @"STOP MOVED FROM BREAKEVEN TO +10 TICKS PROFIT", "");
}
// Set 5
// Once a/any Short Order is live (Set 2)
//and the current/last price (Close[0]) gets 10 or more ticks in profit (Short entry - 10 ticks or more), then
// ENABLE the Stop to be moved to Breakeven (StopLossMode = 1;) / but don't move it yet, just unlock it
// Set 9 will move the Stop from -10 ticks to Breakeven
if ((Position.MarketPosition == MarketPosition.Long)
&& (StopLossModeLong == 0)
&& (Close[0] >= (Position.AveragePrice + (10 * TickSize)) ))
{
StopLossModeLong = 1;
}
// Set 6
// Once a/any Short Order has been MOVED to Breakeven (Set 3)
//and the current/last price (Close[0]) gets 20 or more ticks in profit (Short entry - 20 ticks or more), then
// ENABLE the Stop to be moved to Breakeven + 10 ticks (StopLossMode = 2;) / but don't move it yet, just unlock it
// Set 10 will move the Stop from Breakeven to Breakeven + 10 ticks
if ((Position.MarketPosition == MarketPosition.Long)
&& (StopLossModeLong == 1)
&& (Close[0] >= (Position.AveragePrice + (20 * TickSize)) ))
{
StopLossModeLong = 2;
}
//SHORT ORDERS
// Set 7
//When no trade is live yet (Flat Position),
// and the current day is either Monday or Tuesday,
// and the current bar's Low Price is lesser than the previous bar's Low Price, and the previous bar's Low Price is also lesser than the Low Price 2 bars ago,
// and the current time is between 9:30 AM and 11:50 AM, then
// Place a SHORT LIMIT order of default lot size (DefaultQuantity)
if ((Position.MarketPosition == MarketPosition.Flat)
&& ((Time[0].DayOfWeek == DayOfWeek.Monday)
|| (Time[0].DayOfWeek == DayOfWeek.Tuesday))
&& (Low[0] < Low[1])
&& (Low[1] < Low[2])
&& (Times[0][0].TimeOfDay >= new TimeSpan(9, 30, 0))
&& (Times[0][0].TimeOfDay <= new TimeSpan(11, 50, 0)))
{
EnterShortLimit(Convert.ToInt32(DefaultQuantity), 0, "");
StopLossModeShort = 0;
}
// Set 8
// If/When The Short position gets trigger (= Set 7 conditions get TRUE), then
// ADD the INITIAL STOP to Breakeven + 10 ticks (= Position.AveragePrice (+ 10 ticks))
if ((Position.MarketPosition == MarketPosition.Short)
&& (StopLossModeShort == 0))
{
ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), (Position.AveragePrice + (10 * TickSize)) , @"INITIAL STOP", "");
}
// Set 9
// If/When The Short position gets profitable by (Set 11 #/10) of ticks, then
// MOVE the STOP to Breakeven (=Position.AveragePrice (*1/+ 0 ticks))
if ((Position.MarketPosition == MarketPosition.Short)
&& (StopLossModeShort == 1))
{
ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), Position.AveragePrice, @"STOP MOVED FROM +10 TICKS (INITIAL STOP) TO BREAKEVEN", "");
}
// Set 10
// If/When The Short position gets profitable by (Set 12 #/20) of ticks, then
// MOVE the STOP to Breakeven + 10 ticks (=Position.AveragePrice (+ 10 ticks))
if ((Position.MarketPosition == MarketPosition.Short)
&& (StopLossModeShort == 2))
{
ExitShortStopMarket(Convert.ToInt32(DefaultQuantit y), (Position.AveragePrice + (-10 * TickSize)) , @"STOP MOVED FROM BREAKEVEN TO +10 TICKS PROFIT", "");
}
// Set 11
// Once a/any Short Order is live (Set 8)
//and the current/last price (Close[0]) gets 10 or more ticks in profit (Short entry - 10 ticks or more), then
// ENABLE the Stop to be moved to Breakeven (StopLossMode = 1;) / but don't move it yet, just unlock it
// Set 9 will move the Stop from -10 ticks to Breakeven
if ((Position.MarketPosition == MarketPosition.Short)
&& (StopLossModeShort == 0)
&& (Close[0] <= (Position.AveragePrice + (-10 * TickSize)) ))
{
StopLossModeShort = 1;
}
// Set 12
// Once a/any Short Order has been MOVED to Breakeven (Set 9)
//and the current/last price (Close[0]) gets 20 or more ticks in profit (Short entry - 20 ticks or more), then
// ENABLE the Stop to be moved to Breakeven + 10 ticks (StopLossMode = 2;) / but don't move it yet, just unlock it
// Set 10 will move the Stop from Breakeven to Breakeven + 10 ticks
if ((Position.MarketPosition == MarketPosition.Short)
&& (StopLossModeShort == 1)
&& (Close[0] <= (Position.AveragePrice + (-20 * TickSize)) ))
{
StopLossModeShort = 2;
}
}
}
}
I can't export the script because of the errors below:
In order to prevent executions conflict and distinguish the Long and Shot Stop Losses
I changed the default statement
StopLossMode = 0;
StopLossModeLong = 0;
StopLossModeShort = 0;
The name 'StopLossModeShort' does not exist in the current context
https://i.imgur.com/s6WFoY9.png
Why does it not compile?
Is it because the StopLossMode is a reserved variable (I searched for it in the documentation but did not find it)?
Do you see any problem in creating new custom variables to remedy the issue, as follows:
StopLossModeLong = StopLossMode = 0;
StopLossModeShort= StopLossMode = 0;
EDIT:
Here's the code with only the StopLossMode both for Long and Short orders snippets:
EDIT2:
Attached is the StopLossMode both for Long and Short orders snippets .zip archive successfully exported:

Comment