if ((Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) + SystemPerformance.AllTrades.TradesPerformance.NetProfit) < accountSize * -.1) {
if (Position.MarketPosition != MarketPosition.Long)
ExitLong("Exit Long - MaxLoss", "");
else if (Position.MarketPosition != MarketPosition.Long)
ExitShort("Exit Short - MaxLoss", "");
CloseStrategy("MaxLoss");
return;
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy level stoploss
Collapse
X
-
Strategy level stoploss
I'm trying to create a strategy level stoploss that checks the strategies net profit and closes the strategy if the net profit is greater than -5% of the account. The issue I'm running into is that it's only checking realized profit and never unrealized, so if a trade it's currently in goes over that limit it won't close until it's too late.
Code:Tags: None
-
Hello RaddiFX,
Are you certain the unrealized is not being checked?
Use prints to confirm. Print the time of the bar, the value of Position.MarketPosition, the value of Position.GetUnrealizedProfitLoss(), the value of SystemPerformance.AllTrades.TradesPerformance.NetP rofit, and the value of these added together.
Below is a link to a support article on using prints to understand behavior.
The condition you have to exit long appears incorrect as well.
if (Position.MarketPosition != MarketPosition.Long)
ExitLong("Exit Long - MaxLoss", "");
This states that if the position is not long, meaning the position is either short or flat, then call ExitLong(). If the position is not long, then calling ExitLong() will have no effect as there is no long position to exit.Chelsea B.NinjaTrader Customer Service
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Mindset, 04-21-2026, 06:46 AM
|
0 responses
90 views
0 likes
|
Last Post
by Mindset
04-21-2026, 06:46 AM
|
||
|
Started by M4ndoo, 04-20-2026, 05:21 PM
|
0 responses
137 views
0 likes
|
Last Post
by M4ndoo
04-20-2026, 05:21 PM
|
||
|
Started by M4ndoo, 04-19-2026, 05:54 PM
|
0 responses
68 views
0 likes
|
Last Post
by M4ndoo
04-19-2026, 05:54 PM
|
||
|
Started by cmoran13, 04-16-2026, 01:02 PM
|
0 responses
120 views
0 likes
|
Last Post
by cmoran13
04-16-2026, 01:02 PM
|
||
|
Started by PaulMohn, 04-10-2026, 11:11 AM
|
0 responses
69 views
0 likes
|
Last Post
by PaulMohn
04-10-2026, 11:11 AM
|

Comment