Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting dynamic stoploss and profit target

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

    Setting dynamic stoploss and profit target

    I have a strategy where I want to set stoplosses a certain number of points under the current candle low and I want to set the take profit to be a multiplier of the entry price minus the stoploss.

    I currently have a public int Stop and public int Target in the region Properties section as well as Target = 2 and Stop = 20 in OnStateChange()

    When my entry conditions are met for longs I have:

    SetStopLoss(@"", CalculationMode.Price, Lows[2][0] - Stop, false); // Set stop loss 20 points below candle low
    SetProfitTarget("", CalculationMode.Price, Closes[2][0] + (Target * (Closes[2][0] - (Lows[2][0] - Stop))), false); // Set profit target 2 times the (entry price - stoploss)

    The code works when I replace Stop and Target with an actual number but I want to test different stop and target variables in the strategy analyzer. Any suggestions?​

    #2
    Hello dj19820,

    You would need to call the Set methods before placing the entry in OnBarUpdate() (to reset) and then after the entry fills you can modify the price in OnBarUpdate() or wherever you would like.

    Below is a link to an example named ProfitChaseStopTrailSetMethodsExample_NT8.


    The bar information is not available in OnStateChange(), so the code needs to be in OnBarUpdate() (or similar method that runs after the data is processing).
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I'm not sure if this matters but I want my stoploss and target to be static once triggered, not changing after each bar update. But I want my stoploss and target to be dependent on the low/high of the entry candle. Thats where the dynamic part comes in. I'm not sure how to code that with the example because that's a trail stop.

      Comment


        #4
        Hello dj19820,

        The title of your thread was suggesting you are trying to code a dynamic stop loss. To confirm, this is not the case?

        The high low of the candle can be found with Low[0] and High[0].

        You can ensure these are valid, (meaning the chosen price is less than the bid for a sell or above the ask for a buy) and then supply the price as a stop price for the order.

        From the example, don't include the code that updates the price after the order is working.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Chelsea,

          Along these same lines...

          Using the examples given here in this thread, I came up with these in order to set a stop loss that was 10 ticks above the previous bar's close:

          SetStopLoss(@"name", CalculationMode.Ticks, Close[1] + 10, false);
          SetStopLoss(@"name", CalculationMode.Price, Close[1]+ (10*TickSize), false);


          Clearly, something is amiss, because in both cases the strategy just quits instantly, suggesting a syntax error or something. What am I missing?
          Last edited by LunaKai; 03-16-2023, 02:28 PM.

          Comment


            #6
            Hello LunaKai,

            If you are wanting assistance with a compile error shown in the NinjaScript Editor or a real-time logical error from the Log tab of the Control Center, please provide the full error message.

            Ctrl + c to copy the error, Ctrl + v to paste in your message
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              My apologies, I worded that wrong. I enable the strategy and it just fails when I click "Apply". In my experiences, this has meant some kind of coding error. There is no actual error message. It compiles fine but just doesn't run when enabled. I know it's one of these lines of code because the strategy runs fine when they are commented out.

              Something is wrong with both of these:
              SetStopLoss(@"name", CalculationMode.Ticks, Close[1] + 10, false);
              SetStopLoss(@"name", CalculationMode.Price, Close[1]+ (10*TickSize), false);​
              Last edited by LunaKai; 03-16-2023, 02:41 PM.

              Comment


                #8
                Hello LunaKai,

                If you would like assistance with a runtime logical error, (such as indexing errors when not ensuring the barsAgo index is less than CurrentBar), please provide the full error message as it appears on the Log tab of the Control Center.

                May I confirm those lines of code are in OnBarUpdate()?
                Chelsea B.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, 03-13-2026, 05:17 AM
                0 responses
                93 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                152 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                80 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                53 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                65 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X