#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
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class ts1 : Strategy
{
private SMA SMA1;
private SMA SMA2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"sma";
Name = "ts1";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.High;
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;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
SMA1 = SMA(Close, 50);
SMA2 = SMA(Close, 200);
SMA2.Plots[0].Brush = Brushes.DarkRed;
SMA1.Plots[0].Brush = Brushes.Goldenrod;
AddChartIndicator(SMA1);
AddChartIndicator(SMA2);
SetProfitTarget(@"sma", CalculationMode.Currency, 2000);
SetStopLoss(@"sma", CalculationMode.Currency, 500, false);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((SMA1[0] > SMA2[0])
&& (Close[0] > SMA1[0]))
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"sma");
}
// If we have an open position, check if it's profitable by $1000 or more
if (Position.MarketPosition == MarketPosition.Long && Position.AveragePrice < Close[0])
{
double profit = (Close[0] - Position.AveragePrice) * TickSize;
if (profit >= 500)
{
// Set stop loss to break even
double newStop = Position.AveragePrice;
SetStopLoss("sma", CalculationMode.Currency, newStop, false);
}
}
}
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
break even not executing
Collapse
X
-
break even not executing
I'm looking to isolate why my break even stop condition is not showing up on my back test results for the below code
Code:Tags: None
-
Hello twomeangreens,
This would be a situation where a Print is needed to find out what is happening with the conditions you made.
Finding out if both conditions are true would be required to know why the stop is not being updated. you could print the variables being used in the two conditions:
Code:Print(Position.MarketPosition + " " + Position.AveragePrice + " < " + Close[0]); Print(iprofit + " >= " + 500);
-
Thank you Jesse,
I'm not sure if I'm looking in the right place or not but after added the print code to my strategy I'm seeing the below message in the logs.
Comment
-
-
Thanks for all your help thus far Jesse,
After adding the above code I'm now seeing small numbers being printed. I suspect this might be the issue but I'm not sure whats causing it
Comment
-
I added the code as suggested but I'm still not seeing break even executing in the backtest results. Curious if the strategy has an issue with backtests running off of tickets instead of minute bars? Otherwise I'm at a loss for what the issue might be
Comment
-
I've altered the code to reflect a 20 point target before the break even should be activated. I'm not seeing the stops being moved from $500 up to break even in the trades although I see the condition set evaluating as true in the output
#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
//This namespace holds Strategies in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Strategies
{
public class ts1 : Strategy
{
private SMA SMA1;
private SMA SMA2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"sma";
Name = "ts1";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.High;
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;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
SMA1 = SMA(Close, 50);
SMA2 = SMA(Close, 200);
SMA2.Plots[0].Brush = Brushes.DarkRed;
SMA1.Plots[0].Brush = Brushes.Goldenrod;
AddChartIndicator(SMA1);
AddChartIndicator(SMA2);
SetProfitTarget(@"sma", CalculationMode.Currency, 2000);
SetStopLoss(@"sma", CalculationMode.Currency, 500, false);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((SMA1[0] > SMA2[0])
&& (Close[0] > SMA1[0]))
{
EnterLong(Convert.ToInt32(DefaultQuantity), @"sma");
}
// If we have an open position, check if it's profitable by $1000 or more
if (Position.MarketPosition == MarketPosition.Long && Position.AveragePrice < Close[0])
{
Print(Position.MarketPosition + " " + Position.AveragePrice + " < " + Close[0]);
double profit = (Close[0] - Position.AveragePrice);
Print(profit + " >= " + 20);
if (profit >= 20)
{
Print(profit + " >= " + 20);
// Set stop loss to break even
double newStop = Position.AveragePrice;
SetStopLoss("@sma", CalculationMode.Currency, newStop, false);
}
}
}
}
}
Comment
-
Hello twomeangreens,
The SetStopLoss you have used is set to CalculationMode.Currency but the price you are using is not a currency, that is a price. Position.AveragePrice is the average price between the entries that make the position. If you had 1 quantity that would just be the entry price. To use that with SetStopLoss you would need to use CalculationMode.Price
Code:double newStop = Position.AveragePrice; SetStopLoss("@sma", CalculationMode.Price, newStop, false);
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
62 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
134 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
75 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
45 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
50 views
0 likes
|
Last Post
|

Comment