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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        79 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        40 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        63 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        63 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        54 views
        0 likes
        Last Post CarlTrading  
        Working...
        X