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

Trailing stop

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

    Trailing stop

    So I don't think my stops are trailing correctly, it doesn't seem to be responding once the required ticks are hit.

    //Trail behind Adaptive Moving Average by 1 ticks, after 10 ticks

    if (Position.MarketPosition == MarketPosition.Long
    && Close[0] > Position.AvgPrice + 10 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, KAMA(2, 10, 30)[0] - 1 * TickSize);
    }

    if (Position.MarketPosition == MarketPosition.Short
    && Close[0] < Position.AvgPrice - 10 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, KAMA(2, 10, 30)[0] + 1 * TickSize);
    }

    #2
    Hello brucelevy,

    On first glance your code appears correct.

    Are you running with CalculateOnBarClose = true or false?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      calculateonbarclose = false;

      So for long PnL goes past ten ticks and the stop essentially does nothing. The stop loss just sits at the swing low where it is initially set.

      Comment


        #4
        Hello brucelevy,

        Thanks for your reply.

        You code is setting a stoploss at 1 tick below the level of a moving average. Each time (Long) that price is > 10 ticks of your entry price the stop is adjusted to 1 tick below the current value of the moving average.

        What are you expecting to happen?
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by brucelevy View Post
          calculateonbarclose = false;

          So for long PnL goes past ten ticks and the stop essentially does nothing. The stop loss just sits at the swing low where it is initially set.
          Check your log. If COBC is false, it seems to me that you may be sending Stop orders in rapid succession and they are being ignored. Just a guess, but the log should probably tell us.

          Comment


            #6
            Yes the log seems to be updating on every tick


            Here is what I am seeing, it is over 10 ticks and still has stops at the swing high


            Should I be using changeorder after the initial stop needs to be moved to the next area?
            Last edited by brucelevy; 11-12-2015, 02:06 PM.

            Comment


              #7
              Hello brucelevy,

              Thanks for your reply.

              The log segment shows that your setstoploss is being adjusted and in that short view there were no errors shown.

              In the video, are you displaying the KAMA line? As you are setting your stoploss at 1 tick away from the KAMA line you should have that on your display to understand why it is or is not moving.
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Thanks guys I appreciate your help, here is exactly what Im working with....



                //Trail on 8 EMA by 1 tick after 10 ticks.

                if (Position.MarketPosition == MarketPosition.Long
                && Close[0] > Position.AvgPrice + 10 * TickSize)
                {
                SetStopLoss(CalculationMode.Price, EMA(8)[0] - 1 * TickSize);
                }

                if (Position.MarketPosition == MarketPosition.Short
                && Close[0] < Position.AvgPrice - 10 * TickSize)
                {
                SetStopLoss(CalculationMode.Price, EMA(8)[0] + 1 * TickSize);
                }




                // Initial Stop Loss - Swing Stops


                if(Position.MarketPosition == MarketPosition.Long);


                {
                SetStopLoss(CalculationMode.Price, Swing(3).SwingLow[0] - 3 * TickSize);
                }

                if(Position.MarketPosition == MarketPosition.Short);

                {
                SetStopLoss(CalculationMode.Price, Swing(3).SwingHigh[0] + 3 * TickSize);
                }

                Comment


                  #9
                  Originally posted by brucelevy View Post
                  Thanks guys I appreciate your help, here is exactly what Im working with....
                  Which completely explains your results.

                  If
                  Code:
                  if(Position.MarketPosition == MarketPosition.Long);
                  is true, then as it is a less restrictive condition than,
                  Code:
                  if (Position.MarketPosition == MarketPosition.Long
                  && Close[0] > Position.AvgPrice + 10 * TickSize)
                  that is the block that would run, because your less restrictive condition comes after the more restrictive one.

                  But it goes a little deeper than that in this particular case. Do you see the semi-colon here?
                  Code:
                  if(Position.MarketPosition == MarketPosition.Long);
                  That turns your conditional filter into a null statement; so the next code block will run no matter what.
                  Last edited by koganam; 11-12-2015, 02:59 PM.

                  Comment


                    #10
                    So I got the trailing on 8EMA working once price gets to 10 ticks, but once price goes under 10 ticks it seems to be resetting back to the swing low (for long).

                    What am I missing to stop the SL from resetting.

                    Thanks.

                    Comment


                      #11
                      if (Position.MarketPosition == MarketPosition.Long
                      && Close[0] > Position.AvgPrice + 10 * TickSize
                      && EMA(8)[0] > Position.AvgPrice)
                      It looks like this should do the trick, unless there is a more robust approach to not allowing it to go back.

                      Comment


                        #12
                        Originally posted by brucelevy View Post
                        It looks like this should do the trick, unless there is a more robust approach to not allowing it to go back.
                        Code:
                        if (Position.MarketPosition == MarketPosition.Long
                        && Close[0] > Position.AvgPrice + 10 * TickSize 
                        && EMA(8)[0] > Position.AvgPrice [COLOR="Blue"][B]&& EMA(8)[0] > EMA(8)[1])
                        {/* move the stop up */}[/B][/COLOR]
                        The reason should be obvious, but ask if it is still not clear.

                        Comment


                          #13
                          hello

                          would it be possible to have the stop to break even code take place first and then the trailing stop code to take over?
                          If you have a solution please let me know. I'm open to new ideas. thanks.

                          i have this stop to break even code:

                          else if (Position.MarketPosition == MarketPosition.Long)
                          {
                          if (Close[0] > Position.AvgPrice + 3* TickSize)

                          {
                          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                          }
                          }

                          else if (Position.MarketPosition == MarketPosition.Short)
                          {
                          if (Close[0] < Position.AvgPrice - 3* TickSize)

                          {
                          SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                          }

                          Thank you

                          Comment


                            #14
                            Hello bizzz10

                            You posted almost the same thing here: http://ninjatrader.com/support/forum...ad.php?t=80349. To avoid confusion, please do not create duplicate posts.
                            Paul H.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by gentlebenthebear, Today, 01:30 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by Aviram Y, Today, 05:29 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by cls71, Today, 04:45 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by TradeForge, Today, 02:09 AM
                            1 response
                            22 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by elirion, Today, 01:36 AM
                            2 responses
                            14 views
                            0 likes
                            Last Post elirion
                            by elirion
                             
                            Working...
                            X