Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to reset a stoploss within OnBarUpdate()

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

    How to reset a stoploss within OnBarUpdate()

    Hi NT Support,

    I am using a SetStopLoss within the strategy OnBarUpdate() method. I am told you should always reset the stop loss price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position. Please could you explain how to reset the stop loss?

    Thanks

    #2
    sburtt, would suggest you review this reference here - http://www.ninjatrader.com/support/f...ead.php?t=3222

    It would include the reset part needed as well.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      sburtt, would suggest you review this reference here - http://www.ninjatrader.com/support/f...ead.php?t=3222

      It would include the reset part needed as well.
      hmm.. i am a little confused. let me explain what i am trying to do:

      I EnterLong() on a CrossOver() condition and once Postion.MarketPosition == MarketPosition.Long I SetStopLoss() to a price indicator similar to Low[0] - 2* ATR()[0]. This indicator that I use as stoploss is not static and changes at each OnBarUpdate(), how can I unsure that the stoploss also dinamically adjusts on OnBarUpdate() and, further on would a stop reset like the one in the examle you sent me work to reset this kind of stoploss?

      If I am not clear enough please advise and I will add some code to this thread.

      Thanks,

      Comment


        #4
        Yes, the reset would work just the same way - it's just meant to reset to same default offset value in ticks, instead of your last hardcoded in price value for the stoploss, so it's 'ready' for a new position.

        You call the SetStopLoss on each bar update when you're long - that's then your dynamic adjustment.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          Yes, the reset would work just the same way - it's just meant to reset to same default offset value in ticks, instead of your last hardcoded in price value for the stoploss, so it's 'ready' for a new position.

          You call the SetStopLoss on each bar update when you're long - that's then your dynamic adjustment.
          ok, i don't understand why the reset is needed if the stoploss adj dynamically, but thats a different story, thx

          Comment


            #6
            Originally posted by NinjaTrader_Bertrand View Post
            Yes, the reset would work just the same way - it's just meant to reset to same default offset value in ticks, instead of your last hardcoded in price value for the stoploss, so it's 'ready' for a new position.

            You call the SetStopLoss on each bar update when you're long - that's then your dynamic adjustment.
            I run a strategy that flips from long to short on a CrossOver, hence only if I am stopped my position would be flat. Using this code:
            if (Position.MarketPosition == MarketPosition.Flat)
            SetStopLoss(CalculationMode.Ticks,100);
            doesn't always guarantee that my StopLoss gets reset. Would something like this make the trick?
            Code:
            if (CrossAbove(...))
            	                     {
                                             EnterLong();
                                            SetStopLoss(CalculationMode.Ticks,100);
                                          }
            and then on the next OnBarUpdate()
            Code:
            		if (Position.MarketPosition != MarketPosition.Flat)
            				SetStopLoss(CalculationMode.Price,(...));

            Comment


              #7
              Hi sburtt,

              Thanks for your post.

              I am getting up to speed on this and will be giving some input in a little while.

              Thanks for your patience.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi sburtt,

                Thanks for your patience.

                You will only need to "reset" the stop loss if you are dynamically changing the stop loss as your strategy progresses.

                The idea is, if after conditions you set the stop loss closer to the market price, the amount that you set this to will be the same when you make a new order. If you have set it to 1 tick so that your order's stop is tightened, then the next order made will have that very tight stop as well.

                The best practice if you are changing the stop loss as your strategy progresses is to reset the stop just before placing your order, so that the new order starts with the correct stop and doesn't place one and then modify it or to reset it while flat.

                For example, I have just reversed the order of the setstoploss and enterlong.
                Code:
                if (CrossAbove(...))
                {
                SetStopLoss(CalculationMode.Ticks,100);
                EnterLong();
                }
                A further reset when flat would not really be necessary here.

                Please let me know if this does not resolve your inquiry.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hi sburtt,

                  Thanks for your patience.

                  You will only need to "reset" the stop loss if you are dynamically changing the stop loss as your strategy progresses.

                  The idea is, if after conditions you set the stop loss closer to the market price, the amount that you set this to will be the same when you make a new order. If you have set it to 1 tick so that your order's stop is tightened, then the next order made will have that very tight stop as well.

                  The best practice if you are changing the stop loss as your strategy progresses is to reset the stop just before placing your order, so that the new order starts with the correct stop and doesn't place one and then modify it or to reset it while flat.

                  For example, I have just reversed the order of the setstoploss and enterlong.
                  Code:
                  if (CrossAbove(...))
                  {
                  SetStopLoss(CalculationMode.Ticks,100);
                  EnterLong();
                  }
                  A further reset when flat would not really be necessary here.

                  Please let me know if this does not resolve your inquiry.
                  ChelseaB

                  I am running a strategy with a dynamic stoploss that trails price. hence I would need to reset the stoploss. i don't need the stoploss to be in place on the entrybar, but I need it to be reset, therefore something like this should work, please advise if otherwise:

                  Code:
                  if (Position.MarketPosition == MarketPosition.Flat && CrossAbove(...))
                  {
                  SetStopLoss(CalculationMode.Ticks,10000);
                  EnterLong();
                  }
                  
                  if(Position.MarketPosition != MarketPosition.Flat)
                  SetStopLoss(CalculationMode.Price, MIN(Low,10)[0] - TickSize);

                  Comment


                    #10
                    Hi sburtt,

                    Yes, that should work fine.

                    Let me know if you have any issues with that.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hi sburtt,

                      Yes, that should work fine.

                      Let me know if you have any issues with that.
                      Hi I have 1 more question regarding the stop-loss reset. Assume I am running this scrip:

                      Code:
                      //Break-even stop-loss
                      if (Position.MarketPosition == MarketPosition.Long && High[0]>=Instrument.MasterInstrument.Round2TickSize(Position.AvgPrice+100*TickSize))
                             SetStopLoss(CalculationMode.Ticks, 0);
                      
                      //Entry Long
                      if (CrossAbove(..))
                      {
                      	SetStopLoss(CalculationMode.Ticks, 100);
                      	EnterLong();
                      }
                      what I want the strategy to do is EnterLong with a 100 tick stoploss, and when I am 100 tick in profit, move the stoploss to break-even. Is this code correct?

                      How could I do to move the stoploss in profit? let's say to Position.AvgPrice+10*TickSize?

                      Thanks

                      Comment


                        #12
                        Hello sburtt,

                        Thanks for your note.

                        To move the stop loss to break even once 100 ticks of profit is reached, you can either set the stop loss to 100 ticks (which would be the entry point), or you can use a specific price of the entry price.

                        Setting the stop loss to 0 ticks would move the stop loss to the current price (and would most likely cause your order to stop out).

                        To set to a specific price:
                        SetStopLoss(CalculationMode.Price, Position.AvgPrice+10*TickSize);
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hi sburtt,

                          Thanks for your patience.

                          You will only need to "reset" the stop loss if you are dynamically changing the stop loss as your strategy progresses.

                          The idea is, if after conditions you set the stop loss closer to the market price, the amount that you set this to will be the same when you make a new order. If you have set it to 1 tick so that your order's stop is tightened, then the next order made will have that very tight stop as well.

                          The best practice if you are changing the stop loss as your strategy progresses is to reset the stop just before placing your order, so that the new order starts with the correct stop and doesn't place one and then modify it or to reset it while flat.

                          For example, I have just reversed the order of the setstoploss and enterlong.
                          Code:
                          if (CrossAbove(...))
                          {
                          SetStopLoss(CalculationMode.Ticks,100);
                          EnterLong();
                          }
                          A further reset when flat would not really be necessary here.

                          Please let me know if this does not resolve your inquiry.
                          Hi Chelsea. So when you use the reset code under OnBarUpdate when you are flat, is that order actually sent into a live market by Ninja? How is that reset order handled?

                          Thanks.

                          Comment


                            #14
                            Hello Trader17,

                            Thanks for your post.

                            No order is actually sent with the statement "SetStopLoss(CalculationMode.Ticks,100);" as the Set methods (SetStopLoss, SetProfitTarget(), etc, etc,) will only send their orders AFTER an entry order has been filled. It takes a filled entry order for the Set methods to react. All that is being done here with the statement is to prepare the StopLoss value for the next entry.

                            All of these Set methods retain the last value they were set to, so a good practice is to set the values to appropriate levels for the next order before the next entry order is placed.


                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks for the clarification. And while doing a reset can we also use any other value besides ticks, if say I am using an indicator or a price pattern for the actual Stop Loss Value just before Enter Long under OnBarUpdate? Like if I am using a MIN or MAX value for the actual Stop does it matter if I use that itself or just plain old ticks in the reset value?

                              So resetting the Stop value will not hurt anything even if added under OnBarUpdate even though Chelsea mentioned it does reset if Stop is added above the Enter Long.

                              Thank you very much.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by BarzTrading, Today, 07:25 AM
                              2 responses
                              26 views
                              1 like
                              Last Post BarzTrading  
                              Started by devatechnologies, 04-14-2024, 02:58 PM
                              3 responses
                              20 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tkaboris, Today, 08:01 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post tkaboris  
                              Started by EB Worx, 04-04-2023, 02:34 AM
                              7 responses
                              163 views
                              0 likes
                              Last Post VFI26
                              by VFI26
                               
                              Started by Mizzouman1, Today, 07:35 AM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X