Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Loss and Profit Target Based on Range of One Bar

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

    Stop Loss and Profit Target Based on Range of One Bar

    I have been struggling to figure out how to program a particular strategy in the strategy builder. I have tried everything I can possibly think of and have sifted through countless forum entries looking for a solution to no avail. So here I am! I am not very proficient in coding and still learning strategy builder so forgive me if the answer is obvious.

    In short, I am wanting to enter a position using a stop market order at the high of the last bar + 1. I don't have an issue setting that up.

    The problem comes in trying to set my stop loss and take profit orders. What I am trying to do is use the same bar that triggered my order (high + 1) to create the value of my SL and Prof orders.

    So, say I enter a long stop market order at 10:30 at the high + 1 of the last hour bar (9:30-10:30). How can I set a stop order that will be, say, 50% of the range of that 9:30-10:30 bar?

    Example:
    9:30-10:30 bar = high of 1,000 and range of 10
    Thus, long stop market order at 1,001 with stop loss set to 995 and profit target set to 1,005.

    Is this possible? I tried to set a variables for the prof/SL so that could plug in a logical sequence in either an "order management" set or the stop/profit section. Unfortunately I cannot get it to execute properly no matter how I've set it up. Any help would be greatly appreciated!

    #2
    Hello Avs29,

    Thank you for your post.

    It wouldn't be possible to use the Stops and Targets screen for what you're wanting to do. You would need to use Exit orders in the Conditions and Actions screen in order to have dynamic prices for a stop and target that are based on the bar price. The issue with this, then is that if you're using OnBarClose for the Calculate setting, is that you would have to wait a full bar after the fill for the stop and target to be placed, as it wouldn't recognize it was in a long position yet on the same pass through OnBarUpdate that the entry filled on.

    A better option would be to unlock the code, as you can use stops and targets dynamically within OnBarUpdate. I'm including an example, complete with comments, of this approach. This example will enter long if there are 2 green bars in a row. It will submit a stop based on the bar range of the bar that triggered it and a target at +1 tick from the high of the triggering bar.

    Please let us know if we may be of further assistance to you.
    Attached Files

    Comment


      #3
      Thank you so much for this, that was very helpful. I have successfully got my orders entering at the right place and I have my stop loss functioning just as I want it to. However, I am running into some difficulty in coding my profit target and I cannot figure out why my SetStopLoss code is working but my SetProfitTarget code is not working.

      In my code, I have defined
      {
      MAX1 = MAX(High, 60);
      MIN1 = MIN(Low, 60);
      }

      This has allowed me to code my stop loss in my Set as:

      SetStopLoss(CalculationMode.Price, MAX1[1]-((MAX1[1]-(MIN1[1]))*0.5));

      In other words, I am able to take 50% of the range of the high and lows of a set of bars and subtract that number from the high in order to set my stop loss. Functions just fine.

      But, then I try to follow it up with:

      SetProfitTarget(CalculatonMode.Price, MAX1[1]+((MAX1[1]-(MIN1[1]))*0.5));

      And I get an error that says: "Cannot apply indexing with [] to an expression of type 'method group'" -- CS0021.

      I looked at the error page entry on NT and I did not find anything that I could use to help resolve this error. Are stop losses able to be set in this way and take profit orders unable to be programmed this way? Any help would be appreciated, and again thank you for your previous suggestions!
      Last edited by Avs29; 08-18-2022, 07:36 AM.

      Comment


        #4
        For some reason, I was able to get the code to compile with no issues by switching the order of the profit target and the stop loss. If I put the setprofit order in above the setstop order, no errors returned. So, while I don't understand why that happened...problem solved! Thanks again.

        Comment


          #5
          Hi Avs29,

          This sounds similar to this. You may find this useful: https://ninjatraderecosystem.com/use...entry-buttons/

          It uses the R:R (Range of the last closed candle) to enter trades, set stop, set profit, calculate share/contract size.
          -Has several options for auto Breakeven, Stop Loss move, or trail stop. ( Or combo of all)

          For what you are trying to do, select:
          1. Entry Parameters - Entry Order Type - HighLow (This enters Long at High[1], enters Short at Low[1] ---can include offset as well)

          4.3 Move Stop - Trigger Target(Wherever you want your Stop to move up. Could be 0 for it to automatically move up as soon as you enter)
          - Sets Stop Price(-0.5) This will set your stop to halfway of your entry candle.

          Recommend watching both videos at the bottom of the link to understand all the parameters. Hope this helps!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          55 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          132 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          73 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X