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 NullPointStrategies, Today, 05:17 AM
|
0 responses
44 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
126 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
65 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
42 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
46 views
0 likes
|
Last Post
|

Comment