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

Dynamic Trailing Stop

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

    Dynamic Trailing Stop

    Hi....
    I am coding a dynamic trailing stop as per the following sample:

    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetTrailStop(CalculationMode.Ticks, TrailingStop10);
    Trail =
    166;
    highestHigh = High[
    0];
    }

    // If a long position is open, allow for stop loss modification.
    elseif (Position.MarketPosition == MarketPosition.Long)
    {
    // if high of current bar greater than prior high, modify stop loss.
    if (High[0] > highestHigh)
    {
    Trail = Trail - (Factor/
    10 * (High[0] - highestHigh));

    SetTrailStop(CalculationMode.Ticks, Trail);
    highestHigh = High[
    0];

    }

    The issue is that if the trailing stop value (Trail) < 0 AND the the high of the next bar is lower than that of the previous bar, then the trade exits at the current price of Trail - which is higher than any tick of the current bar. i.e. trade exits in mid-air !

    Is this clear ??? ---it is difficult to explain without pictures. I am struggling to attached a screenshot.

    thx
    David




    #2
    Yes, this is expected since your trail stop price in a real time market would have executed and the bar would look different.
    RayNinjaTrader Customer Service

    Comment


      #3
      thx for the response. To confirm then.....

      In real time the trailing stop would have executed on the previous bar at the specific trailing stop point? (which is identified by the mid-air exit on the current bar) or at the openining price of the current bar. the reason this is important is because clearly it will effect the profit / loss of that particular trade.

      thx
      David

      Comment


        #4
        In real-time the trailing stop would have executed whenever the conditions were met. This generally means it will execute on the current bar, but please be aware that the current bar in real-time is loosely equivalent to the previous bar in a backtest. This is the case because in a backtest you are running everything as if you had CalculateOnBarClose = True. This means you only know if your conditions are met at the end of the bar and can only act on the start of the next bar with orders. The caveat you should be aware of is that the trailing stop stuff will execute even on the current bar regardless of when the order was filled because thats how it would normally behave. What I mean by that is it will exit if the conditions were met instead of waiting till the next bar to act.
        Last edited by NinjaTrader_JoshP; 09-26-2007, 10:43 AM.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh -
          I understand everything you have said (but I am still not clear), & I presume you mean CalculateOnBarClose = True rather than False.

          By the way my strategy is coded with CalculateOnBarClose = True which means my backtest data should be very similar to my realtime data - correct?
          Therefore, I still do not understand the answers to my questions below:

          1/ My SetTrailStop is in the OnBarUpdate section which I assume means that it will only be updated at the close of the bar (when set to true) --- & this includes the SetTrailStop as well as the indicators.

          2/ In real time, would the trailing stop have executed on the previous bar at the specific trailing stop point? ---- which I believe it should do (which is identified by the mid-air exit on the current bar). I am struggling to understand why I can have a mid-air exit on the current bar (higher than current bar price) --- which I assume is the trailstop exit point --- should that have been executed at a real-price on the current bar (i.e. lower that the mid-air position) or at a price on the previous bar.

          The most fundamental question probably is the following....with CalculateOnBarClose=True, is the SetTrailStop function still triggered throughout the duration of that bar or only at the end of the bar IF SetTrailStop function is coded in the OnBarUpdate section of the code.

          The reason this is important is because clearly it will effect the profit / loss of that particular trade.

          thx in advance
          David

          Comment


            #6
            1) If these methods are called before a position is opened then the orders generated by these methods are submitted in real-time as the entry order is filled or even part filled. If these methods are called with a different price, existing orders will be modified. This is true for when the method is called. So if called on the close of the bar, order prices are modified at the close of a bar.

            2) In real-time, the orders are live at the exchange or broker so they are triggered when the market reaches the price which is independant of the bar. The executions of these orders will display at the bar time where the execution occured.
            RayNinjaTrader Customer Service

            Comment


              #7
              thx - one more question. is it possible to dynamically change the stop loss via the 'initialise' sequence - e.g. using similar code as below:

              // Resets the stop loss to the original value when all positions are closed
              if
              (Position.MarketPosition == MarketPosition.Flat)
              {
              SetTrailStop(CalculationMode.Ticks, TrailingStop10);
              Trail =
              166
              ;
              highestHigh = High[
              0
              ];
              }

              // If a long position is open, allow for stop loss modification.
              elseif
              (Position.MarketPosition == MarketPosition.Long)
              {
              // if high of current bar greater than prior high, modify stop loss.
              if (High[0
              ] > highestHigh)
              {
              Trail = Trail - (Factor/
              10 * (High[0
              ] - highestHigh));

              SetTrailStop(CalculationMode.Ticks, Trail);
              highestHigh = High[
              0
              ];

              Comment


                #8
                Please check out our educational resource here: http://www.ninjatrader-support.com/v...ead.php?t=3222

                Comment


                  #9
                  The Initialize sequence will only be executed once at the very being when you load the indicator. You can "start" the SetStopLoss() in the Initialize() section and then you can make dynamic updates to it through the OnBarUpdate() section.

                  The reference sample Dierk linked you to is an example of just that.

                  P.S. Thanks for the catch on the typo.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Have checked this out tonight in real time vs. backtest mode:

                    1/ In Realtime mode with CalculateOnBarClose=True. If SetTrailStop value of <= 0 achieved at close of prior bar (i.e. had created a higher high), trade exited at current bar open price minus one. This is as I would expect.

                    2/ In Backtest mode with CalculateOnBarClose=True. If SetTrailStop value of <=0 achieved at close of prior bar (i.e. had created a higher high), trade exited in mid-air if high[0] is lower than high[1].

                    This clearly creates a discrepency in profit/loss with realtime vs. backtest mode.

                    Am I being stupid ?

                    Comment


                      #11
                      In real-time, do you mean that the stop loss order triggered on bars built in real-time (incoming market data) or do you mean on historical data?
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        Ray - incoming market data

                        Comment


                          #13
                          Thanks.

                          You can not compare a live order (real-time) with results from a backtest. A live order is triggered by the market/exchange or market/simulation engine. Backtest is its own separate enginer that aproximates fills based on bar data.
                          RayNinjaTrader Customer Service

                          Comment


                            #14
                            understood, but the fill for the backtest is irrational !?!?! - i.e. why does it not exit within the limits of the high-low of the current bar when there has been a 'highest high' on the prior par. thx. DG

                            Comment


                              #15
                              Its not irrational. If you have a sell stop at price X and the next bar gaps down, we fill you at price X and not the high of the gap down bar. In rea-time, this is what would happen. Visually, you will see a fill in mid air on the bar where the fill occured.
                              RayNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by frankthearm, Today, 09:08 AM
                              7 responses
                              29 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by NRITV, Today, 01:15 PM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by maybeimnotrader, Yesterday, 05:46 PM
                              5 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by quantismo, Yesterday, 05:13 PM
                              2 responses
                              17 views
                              0 likes
                              Last Post quantismo  
                              Started by adeelshahzad, Today, 03:54 AM
                              5 responses
                              33 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X