Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy level stoploss

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:
    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;
                }​

    #2
    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

    Comment


      #3
      Good catch, I copied and pasted them from a different part of the code without editing. I'm running a backtest with the changes now.

      Comment


        #4
        That alone seems to have fixed the whole issue. thanks!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        44 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        126 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        65 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        42 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X