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 kinfxhk, 07-14-2026, 09:39 AM
|
0 responses
13 views
0 likes
|
Last Post
by kinfxhk
07-14-2026, 09:39 AM
|
||
|
Started by kinfxhk, 07-13-2026, 10:18 AM
|
0 responses
59 views
0 likes
|
Last Post
by kinfxhk
07-13-2026, 10:18 AM
|
||
|
Started by kinfxhk, 07-13-2026, 09:50 AM
|
0 responses
42 views
0 likes
|
Last Post
by kinfxhk
07-13-2026, 09:50 AM
|
||
|
Started by kinfxhk, 07-13-2026, 07:21 AM
|
0 responses
47 views
0 likes
|
Last Post
by kinfxhk
07-13-2026, 07:21 AM
|
||
|
Started by kinfxhk, 07-11-2026, 02:11 AM
|
0 responses
38 views
0 likes
|
Last Post
by kinfxhk
07-11-2026, 02:11 AM
|

Comment