Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adjustable stop

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

    Adjustable stop

    I'm trying to automate a stop that moves with the market in discreet jumps... the stop placement would reference a table that lists the current profit in ticks. I don't want a strictly trailing stop as the increments are not all the same.

    So something like:

    Current profit of 5 ticks = set stop to 1 tick profit
    Current profit of 10 = set stop to 5
    20 = set stop to 10
    30 = 20
    40 = 30

    Can something like this be achieved?

    #2
    Hello FutTrader9000,

    Thank you for your post.

    Yes, this would be possible in a NinjaScript strategy. We have a sample strategy that demonstrates modifying prices of Stop Loss and Profit Target orders. It shows how to modify the stop loss to breakeven after a certain number of ticks in profit; this idea could be modified to set up additional conditions based on the number of ticks of profit and where you would like your stop to be set to. The reference sample I am referring to may be found here:


    Please let us know if we may be of further assistance.

    Comment


      #3
      Hi Emily - How do I have the strategy respect only the relevant stop level?

      I'm trying to build a tree of If Close > X, set stop to Y like I mention above... but I'm having problems holding the stop. For example if the market is grinding lower, each new Close [0], bar will be treated as a new calculation. I only want it to reference the highest point of the move... do you know how to make that happen? Essentially I don't want the adjustable top to ever be lowered.

      If 10 points lock in 1
      20, 10
      30, 20
      ...
      100, 90

      Something like that. But if the market reaches 100, I don't want the stop to start lowering back down to 80, 70, 60 etc.

      Comment


        #4
        That is possible to de done, it just requires some work from your side to write the strategy.

        You will need to set some variables: your entry price, a high-variable representing the highest price after your fill, your trail distances, etc.
        After you have filled, you will check the current price in real time and whenever you have a new high you will update the value of your high-variable.
        When the price moves lower you will check in real-time the distance between that high-variable and the current price, and you will act
        according to your trail parameters and the distance between the entry price and the high-variable.

        Comment


          #5
          Hello FutTrader9000,

          Thank you for your patience.

          What you have described where the stop is set after the market reaches a certain profit is demonstrated in the sample I linked previously:


          Here is a snippet from SamplePriceModification to demonstrate:

          Code:
          // If a long position is open, allow for stop loss modification to breakeven
          else if (Position.MarketPosition == MarketPosition.Long)
          {
          // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
          if (Close[0] > Position.AveragePrice + 50 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AveragePrice);
          }
          }


          What this does is once the Close price moves greater than 50 ticks in profit from the average entry price, the stop loss is moved to breakeven. Even if the market ends up moving back below 50 ticks in profit, the stop loss is still set to the breakeven/entry price and will not adjust lower with the moves of the market. Since you have certain profit triggers where you want the stop loss to be moved, you could add conditions and actions for each of those moves. As KonstantinosNT mentioned, you could even use variables for those values if you'd like.

          You previously mentioned the following:
          Current profit of 5 ticks = set stop to 1 tick profit
          Current profit of 10 = set stop to 5
          20 = set stop to 10
          30 = 20
          40 = 30
          Here is a snippet of what those first three moves could look like by copying the example snippet from above:
          Code:
          // If a long position is open, allow for stop loss modification to breakeven
          else if (Position.MarketPosition == MarketPosition.Long)
          {
          // Once the price is greater than entry price+5 ticks, set stop loss to 1 tick profit
          if (Close[0] > Position.AveragePrice + 5 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 1 * TickSize);
          }
          // Once the price is greater than entry price+10 ticks, set stop loss to 5 ticks profit
          if (Close[0] > Position.AveragePrice + 10 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 5 * TickSize);
          }
          // Once the price is greater than entry price+20 ticks, set stop loss to 10 ticks profit
          if (Close[0] > Position.AveragePrice + 20 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 10 * TickSize);
          }​​
          }
          ​​

          Rather than hard-coding the profit ticks and the number of ticks to move the stop loss, you could set the values as variables/user input so they may be adjusted in the strategy properties. The SamplePriceModification script has the StopLossTicks and ProfitTargetTicks in OnStateChange:
          StopLossTicks = 20;
          ProfitTargetTicks = 100;

          You could also add other inputs from the strategy wizard (this ensures that the proper code is added in the Properties region at the end of the script) for something like Trigger1, Stop1, Trigger2, Stop2, etc. Trigger1 could default to 5 and Stop1 could default to 1, so then the code might look like this:
          Code:
          // If a long position is open, allow for stop loss modification to breakeven
          else if (Position.MarketPosition == MarketPosition.Long)
          {
          // Once the price is greater than entry price+5 ticks, set stop loss to 1 tick profit
          if (Close[0] > Position.AveragePrice + Trigger1 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AveragePrice + Stop1 * TickSize);
          }
          }
          ​​

          I hope this information has been useful. Please feel free to reach out with any additional questions or concerns.​​

          Comment


            #6
            Hi Emily thanks for the long response. The variable method you describe is exactly what the end goal ultimately is, however first I'm hard-coding to make sure the framework is working the way I want it to, which it isn't quite yet.

            Here is what I've got for this stop ladder sequence:

            // STOP CONDITIONS

            if (Position.MarketPosition == MarketPosition.Flat)
            {
            SetStopLoss(CalculationMode.Ticks, StopLossTicks);
            }

            // If a long position is open, allow for stop loss modification

            else if ((Position.MarketPosition == MarketPosition.Long)
            && (High[0] > High[1]))
            {
            // Once the high price of the current bar is greater than the previous bar, AND is greater than entry price + X ticks, set stop loss to Y ticks

            if (High[0] > Position.AveragePrice + 320 * TickSize)
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 300 * TickSize);
            }

            if ((High[0] > Position.AveragePrice + 280 * TickSize)
            && (High[0] < Position.AveragePrice + 320 * TickSize));
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 240 * TickSize);
            }
            if ((High[0] > Position.AveragePrice + 240 * TickSize)
            && (High[0] < Position.AveragePrice + 280 * TickSize));
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 200 * TickSize);
            }
            if ((High[0] > Position.AveragePrice + 200 * TickSize)
            && (High[0] < Position.AveragePrice + 240 * TickSize));
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 160 * TickSize);
            }
            if ((High[0] > Position.AveragePrice + 160 * TickSize)
            && (High[0] < Position.AveragePrice + 200 * TickSize));
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 120 * TickSize);
            }
            if ((High[0] > Position.AveragePrice + 120 * TickSize)
            && (High[0] < Position.AveragePrice + 160 * TickSize));
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 80 * TickSize);
            }
            if ((High[0] > Position.AveragePrice + 80 * TickSize)
            && (High[0] < Position.AveragePrice + 120 * TickSize));
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 40 * TickSize);
            }
            if ((High[0] > Position.AveragePrice + 40 * TickSize)
            && (High[0] < Position.AveragePrice + 80 * TickSize));
            {
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 20 * TickSize);
            }


            }



            I changed all the bars to High from Close because I want to refer to the high point of the move.

            In order to try to combat the stop from lowering itself, I added the High[0] > High[1] line in bold such that the stop modification should only run if the current bar High[0] is greater than the previous bar High[1], i.e. the market is going up (in a hacky way). I also added the upper bounds on the incremental levels, but unfortunately the stop is only being set to 20 ticks at the moment and isn't moving when a new high is set, and that new high is > 80 ticks. Basically the buckets are a second way to solve the redundancy problem of a value of 165 ticks profit being greater than multiple values (160, 120, 80, and 40).

            Can you see help me with what might be wrong here?

            Thanks for all the help.​

            Comment


              #7
              Hello FutTrader9000,

              Thank you for your note.

              Whenever you aren't sure of your strategy's behavior, I suggest adding Print() statements to your script to better understand its behavior and how the conditions are being evaluated. If you were to add a Print() statement inside of each condition after the SetStopLoss change, you would likely see that the stop loss is being changed but since the script is run in chronological order (from top to bottom), it will always stop at the last change to 20 ticks. You need to swap the order of your logic so it starts out with the smallest change, then increments to the next high and the next high.

              For more information about using prints:


              Please let us know if we may be of further assistance.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              75 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              146 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              79 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              50 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              54 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X