Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stop

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

    Trailing Stop

    I am really struggling with this trailing stop. A while back someone from NinjaTrader was nice to enough to give me this code for it as was not just a basic trailing stop but it is not working correctly.

    The strategy has 2 entries that happen at the same time of which the first entry exits with a small profit using ExitLongLimit. When that ExitLongLimit is triggered and executed then the initial stop loss from the second entry is adjusted to a fixed place. Finally, when the SMA moves above this adjusted stop order, then the trailing stop is supposed to start trailing and be just below the SMA.

    According to my Print statements, the code that I have is being called upon, but the stop does not appear to be trailing. I have put in to have it print the trailing stop price in order to see how it moves, but it just gives me one price and then it never gives me any other prices as the trailing stop is supposed to be getting higher.

    Here is the code:
    Code:
    			if (LongLimit == execution.Order
    				&& SMA(sMAPeriod)[0] > (TargetPriceLong - LongEntryA.AvgFillPrice))
    			{
    				if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))
    				{
    					adjustedStopLong = SMA(BarsArray[0], sMAPeriod)[0]-((signalHigh-signalLow)*StopPerc);
    					LongStopB = ExitLongStop(0,true,positionSize,SMA(BarsArray[0], sMAPeriod)[0] - ((signalHigh-signalLow)*StopPerc),"LongBStop","LongEntryB");
    					Print(Time[0] + " Trailing Stop = " + adjustedStopLong);
    				}
    				
    			}
    In all honesty, I do not really understand what is actually happening within this code, so it makes it a bit difficult for me to troubleshoot. Specifically the BarsArray. I have read the material in the Help Guide about it but have found myself still quite confused and I have studied about arrays in general in C#. I feel that I have a basic understanding of what an array is now, but not enough to troubleshoot what is happening here and understand this bit of the code.

    Could you please help me understand what is happening in this code, and also help me figure out how to get this to properly trail.

    Thank you very much for your help!

    #2
    Hi jg123,

    The code here shows that the exit long stop should be set if:
    LongLimit == execution.Order
    and SMA(sMAPeriod)[0] > (TargetPriceLong - LongEntryA.AvgFillPrice))
    and (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))

    If Print(Time[0] + " Trailing Stop = " + adjustedStopLong); isn't printing, then that code is not being hit.

    I recommend that you print the values of every thing in the conditions to see if these all evaluate as true.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea

      I have run a bunch of more tests on this and it is clear that this trailing stop is not actually trailing. It is being set and, as I mentioned in my first comment, the code is being called on because it is printing the Print statements that i am writing.

      Attached are two pictures. The first picture shows the chart with a trade on the USDJPY. It shows that my second entry exited at 102.73 based on an exit from another part of the code that is written (Close position if candle closes below SMA).

      The second picture is of the Output Window. You will see that on 29 July the code for the Trailing stop was called as evidenced by the statement "Hooray!!!!" The output also shows that on 29 July @ 2:00 PM the trailing stop is set to 101.89. After that, we see on 31 July @ 8:00 the trailing stop is still at 101.89 at the same time that the trade had closed at 102.73. The blue horizontal line on the chart is where the trailing stop is set. Therefore it looks as though the trailing stop is being called, but it is not updating after every bar or as the SMA is ascending.

      Here is the code associated with this trailing stop:

      Code:
      			if (LongLimit == execution.Order
      				&& SMA(sMAPeriod)[0] > (TargetPriceLong - LongEntryA.AvgFillPrice))
      			{
      				if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))
      				{
      					adjustedStopLong = SMA(BarsArray[0], sMAPeriod)[0]-((signalHigh-signalLow)*StopPerc);
      					LongStopB = ExitLongStop(0,true,positionSize, adjustedStopLong,"LongBStop","LongEntryB");
      					Print(Time[0] + " Trailing Stop = " + adjustedStopLong);
      					Print("Hooray!!!!");
      				}
      				
      			}
      			Print(Time[0] + " Trailing Stop = " + LongStopB);
      Could you please help me with how to get this to update after every bar?

      Thank you very much for your help
      Attached Files
      Last edited by jg123; 08-06-2014, 10:46 AM.

      Comment


        #4
        Hi jg123,

        You have added a second print to your code outside of the condition that is exactly the same as the print inside of the condition.

        How are you able to tell these apart?


        Where is this code in your script? Is this code in OnBarUpdate?

        May I have your script to test on my end? (I can't tell what is going on with just this snippet especially when you have two prints that are the same)
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          This is in OnExecution because it is to be called after LongLimit has been triggered and executed.

          You are right, I did add it outside. That was a mistake on my part. Thanks for catching that. That is what I get for trying to do this after a 12 hour day at work on top of a roof in the beating sun.

          I will email the script. Thank you for your help

          Comment


            #6
            Hi jg123,

            If this is in OnExecution, then it will only trigger when an order fills (an execution) and not on every bar.

            Move this to OnBarUpdate if you would like it to trigger once per bar.

            (Or add it to both OnExection and OnBarUpdate)

            (Really you want to be specific about what you are coding. If this is in OnExecution, you probably don't want a condition on your exit so that it for sure gets placed after the entry fills. Having this condition in OnBarUpdate makes sense though)
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              That is what I thought too, honestly. But then I got the idea to move it to OnExecution because it is based on when the LongLimit order is triggered and executed. Therefore, I asked in this forum before moving it and was told that I should be moving to OnExecution. (http://www.ninjatrader.com/support/f...898#post382898)

              so, what do I need to do in order to not have this active until LongLimit is triggered and executed but have it update on every bar?

              Comment


                #8
                Hi jg123,

                You will want to have code in OnExecution() to submit the order initially. (This sets the exit to the LongStopB IOrder handle. Before it is set is should be null. If the order fills, this should be set back to null.

                Then after the order is initially placed you can trail it in OnBarUpdate().
                Check that the LongStopB handle OrderState is OrderState.Working so that you know the stop is live.

                So you will need both.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  That is great. Thanks

                  Here is the error that I get when I put it in OnBarUpdate:

                  "Operator '==' cannot be applied to operands of type 'NinjaTrader.Cbi.IOrder' and 'Ninjatrader.Cbi.OrderState'

                  I have looked at the help file on that which didn't quite explain what was happening here. I understand the concept of what it was talking about, but didn't know how to change what I am doing here though.

                  Here is the code:

                  Code:
                  			if ([COLOR="Red"]LongStopB[/COLOR] == OrderState.Working)
                  			{
                  				if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))
                  				{
                  					adjustedStopLong = SMA(BarsArray[0], sMAPeriod)[0]-((signalHigh-signalLow)*StopPerc);
                  					LongStopB = ExitLongStop(0,true,positionSize,adjustedStopLong,"LongBStop","LongEntryB");
                  				}
                  			}
                  When I clicked on the error, it highlighted the red area above.

                  Comment


                    #10
                    Hi jg123,

                    LongStopB.OrderState == OrderState.Working
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I am getting a Log Level error saying that it is not set to and instance of an object. I have done a try/catch and came to the conclusion that it is the area of the code that I have posted in #9.

                      In my experience, I have found that this generally happens to me when I have not looked to see if an order is set to null. I tried checking for null, but that of course did not make a difference - and, of course, it wouldn't be null as it would be a working order (if I understand correctly).

                      I have done a couple of searches here and on bigmike to see if I can find what else may be causing this, but it seems that the other issues that people have had that caused this are not similar to what I have.

                      Do you have any ideas to point me in the right direction?

                      Comment


                        #12
                        Hi jg123,

                        Try:

                        if (LongStopB != null && LongStopB.OrderState == OrderState.Working)
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by jg123 View Post
                          I am getting a Log Level error saying that it is not set to and instance of an object. I have done a try/catch and came to the conclusion that it is the area of the code that I have posted in #9.

                          In my experience, I have found that this generally happens to me when I have not looked to see if an order is set to null. I tried checking for null, but that of course did not make a difference - and, of course, it wouldn't be null as it would be a working order (if I understand correctly).

                          I have done a couple of searches here and on bigmike to see if I can find what else may be causing this, but it seems that the other issues that people have had that caused this are not similar to what I have.

                          Do you have any ideas to point me in the right direction?
                          Sounds to me that you set the order to null after it got filled, in which case you cannot reference it any more. Further, even if you checked for null first, you cannot reference its properties because a null object has no properties. Did you write down a spec of what you wanted to code, before you started to code? That usually nips problems like this in the bud, before they rear their ugly heads.

                          Comment


                            #14
                            Thanks.

                            To be honest, koganam, I did not write down specifically how to it is to be coded. The strategy is extremely clear to me as I have been trading it for years - but I am obviously new to coding so I probably have not done the best practices in trying to get this coded. Tough to know what notes to write down without knowing what all goes into this grand experiement of putting an easy strategy into computer language.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            647 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            369 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            108 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            572 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            573 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X