Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

For you Wizard Wizards... SAR traiing stop?

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

    For you Wizard Wizards... SAR traiing stop?

    Hi All...

    Hope you're all having a great day!

    Here's my latest brainstorm: Creating a parabolic SAR trailing stop using the wizard!

    So far, no luck.

    I searched the forum here hoping to find a morsel or two... but alas, I was unable to locate anything specifically dealing with this kind of stop in the wizard.

    Here's what I've tried so far: In the conditions and actions section of the wizard, in the actions pane, I've set the strategy to endow user variable0 with the SAR from one bar ago.

    1. Click "add" in the "Do the following:" section.
    2. Expand the "Misc" section and choose "Set user defined variable."
    3. In the parameters section, I click the little oval next to the "0"
    4. I select "indicators" and then "parabolic SAR" and make the necessary settings.
    NOTE: I'm not sure what to do with the "input series" of the SAR, but I suspect I'm supposed to do something with it... like select "price, close" or something else.

    5. I set the "bars ago" to 1.
    6. I click OK.

    Next, I go to the stop loss and profit target page of the wizard and click "add" and select "trailing stop."

    I select "ticks" for the mode.
    For the value, I click the little button that appears when I hover over it, and select Variable0.

    I then click OK... and head out to test it in replay mode... and... (drum roll please)...

    ZILCH!

    Any ideas? A nudge in the right direction would really help turn that purple face into something more like:

    Sincere thanks in advance.

    -V

    #2
    Hello Aurum,

    Thank you for your post.

    You would want to set the Mode for the Stop Loss to Price and not ticks. Please advise if you continue to not see any results.

    Comment


      #3
      Still no luck... :-(

      Hi Patrick... thank you for your reply!

      Okay... I've made the changes... but still no luck... so... I've posted the code below generated by the "vew code" button in the wizard. Does anything look out of place there? The orders are executing, but the trailing stop doesn't appear. (there's also an error message in the log which says: "Trailing stops for strategy 'MyNewStrategy' cannot be calculated with CalculationMode.Price. Please use one of the other methods.") Not quite sure what that's about.

      I'm thinking I need to encase the SAR in something... like "current.market.position(SAR( ))" or "close(average.position.price((SAR( )))... something like that... of course, I'm TOTALLY shooting in the dark here...I like being fed morsels of information because they make me figure things out for myself... which I love... but please feel free to give me the recipe on this one... since I'll never figure in out on my own.

      Also... do both the Variable0 AND the trailingstop have to be set to "price" as the mode, or just the trailingstop? The Variable0 is set to "ticks" by default.

      Looking forward to getting this working....

      I've also posted a few related questions below the code box for your review. Just a couple more things that are making me crazier than a box of FROGS!

      Code:
      protected override void Initialize()
              {
                  SetTrailStop("", CalculationMode.Price, Variable0, false);
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (CrossAbove(TEMA(250), TEMA(300), 1))
                  {
                      Variable0 = ParabolicSAR(0.001, 0.1, 0.5)[0];
                      EnterLong(DefaultQuantity, "");
                      DrawDot("My dot" + CurrentBar, false, 0, Low[0] + -20 * TickSize, Color.LimeGreen);
                  }
      
      
                  // Condition set 6
                  if (CrossBelow(TEMA(250), TEMA(300), 1))
                  {
                      EnterShort(DefaultQuantity, "");
                      DrawDot("My dot" + CurrentBar, false, 0, High[0] + 20 * TickSize, Color.Magenta);
                  }
              }
      In addition to the above... Since I am assigning the variable in condition set 1 (long trades), do I need to also assign it in the condition set for "short trades" as well?

      Does it matter where it appears in the list of "Do the following" actions? In other words, does the variable0 need to be created before the "EnterLong" command is given?

      Do I somehow have to "zero out" the variable again sometime after the trades?

      Thanks again to you Patrick, and to everyone who is helping out. I really do appreciate it.

      -V
      Last edited by Aurum; 07-07-2014, 01:04 PM. Reason: Added a few questions after the code box.

      Comment


        #4
        Originally posted by Aurum View Post
        Hi Patrick... thank you for your reply!

        Okay... I've made the changes... but still no luck... so... I've posted the code below generated by the "vew code" button in the wizard. Does anything look out of place there? The orders are executing, but the trailing stop doesn't appear. (there's also an error message in the log which says: "Trailing stops for strategy 'MyNewStrategy' cannot be calculated with CalculationMode.Price. Please use one of the other methods.") Not quite sure what that's about.

        I'm thinking I need to encase the SAR in something... like "current.market.position(SAR( ))" or "close(average.position.price((SAR( )))... something like that... of course, I'm TOTALLY shooting in the dark here...I like being fed morsels of information because they make me figure things out for myself... which I love... but please feel free to give me the recipe on this one... since I'll never figure in out on my own.

        Also... do both the Variable0 AND the trailingstop have to be set to "price" as the mode, or just the trailingstop? The Variable0 is set to "ticks" by default.

        Looking forward to getting this working....

        I've also posted a few related questions below the code box for your review. Just a couple more things that are making me crazier than a box of FROGS!

        Code:
        protected override void Initialize()
                {
                    SetTrailStop("", CalculationMode.Price, Variable0, false);
                    CalculateOnBarClose = true;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                    // Condition set 1
                    if (CrossAbove(TEMA(250), TEMA(300), 1))
                    {
                        Variable0 = ParabolicSAR(0.001, 0.1, 0.5)[0];
                        EnterLong(DefaultQuantity, "");
                        DrawDot("My dot" + CurrentBar, false, 0, Low[0] + -20 * TickSize, Color.LimeGreen);
                    }
        
        
                    // Condition set 6
                    if (CrossBelow(TEMA(250), TEMA(300), 1))
                    {
                        EnterShort(DefaultQuantity, "");
                        DrawDot("My dot" + CurrentBar, false, 0, High[0] + 20 * TickSize, Color.Magenta);
                    }
                }
        In addition to the above... Since I am assigning the variable in condition set 1 (long trades), do I need to also assign it in the condition set for "short trades" as well?

        Does it matter where it appears in the list of "Do the following" actions? In other words, does the variable0 need to be created before the "EnterLong" command is given?

        Do I somehow have to "zero out" the variable again sometime after the trades?

        Thanks again to you Patrick, and to everyone who is helping out. I really do appreciate it.

        -V
        You first have to clarify your thinking.

        By definition, what you are describing as a Trailing Stop (we all do it) is really a moving stop. You are manually moving the Stop, in one direction, based on something: in this case the value of the PSAR. So use SetStopLoss(). An automatic trailing stop, (as opposed to a manually implemented one) is by its very nature impossible to implement using a price. After all, the price is supposed to be automatically moving, so cannot be specified as any fixed value. That is why NT is correctly informing you that you cannot specify CalculationMode.Price for SetTrailStop().

        Comment


          #5
          Hello Aurum,

          Thank you for your response and thank you to Koganam for your assistance.

          Unfortunately, Price could not be used for the Trailing Stop and we would need to set this as a SetStopLoss() in the OnBarUpdate() with CalculationMode.Price, or calculate the tick difference and use CalculationMode.Ticks. So you are going to need to unlock the code for this one and begin hard coding these items.

          As far as the SAR used for the Stop Loss price level, it would be much simpler to unlock the code and set this directly in the OnBarUpdate() method:
          Code:
          SetStopLoss(CalculationMode.Price, SAR()[0]);
          Variable0 should be set as a double and thus just the price value. Any variable to be used for the Stop Loss in both long and short would need to be set appropriately when taking the long or short position. We would need to make sure that the variable is set before the entry is submitted so the Stop Loss is set to the proper value. It is not necessary to reset the variable in this case, however, going with setting the SetStopLoss() in the OnBarUpdate() you are going to need to reset the SetStopLoss() when flat to a starting point so the previous values are not used again for a trade int he opposite direction of the previous.

          Comment


            #6
            Thank you for the thoughtful reply, koganam.

            I'm a bit confused now because Patrick had said to change the mode to "price." That's why I went in that direction. I think I'm getting a fuzzy picture of what I need to do here... fuzzy... but a picture nonetheless!

            From reading through the NinjaTrader help manual, and by referencing the "break even" stop loss method in the "Position > AvgPrice" chapter, I'm getting an idea of how I should maybe proceed.

            So... what about this:

            Code:
            If (Close[0] == Close[1] + 1 *TickSize)
            ExitLongStop(ParabolicSAR(blah, blah)[0], "", "");
            Am I at least getting warmer?

            Thanks again for all your input!

            -V

            Comment


              #7
              Hello Aurum,

              That was my over-sight as I did not check what Set() method you were using. SetStopLoss() can use Price but not SetTrailStop() as it would nullify the trail aspect.

              In your example, you can use ExitLongStop() if you would like and it would update when the condition returns true in OnBarUpdate(). So you would be getting the idea correct there.

              Comment


                #8
                Okay... SOLVED! (I think)

                Hi all...

                Okay... your morsels of insight have been like a storm in my wee brain these past hours... and I've come up with what I think is a solution to the problem. It is working. Give it a try on your end and see if it works for you and let me know. Also... if there are any downsides you see from this, please let me know those also.

                It is as I suspected in my last post to koganam. The formula is very much like the break even formula expressed in the "position.AvgPrice" section of the help guide. (with a few modifications)

                The formula can be accomplished using the wizard (see screenshot below). Expressed like this:

                Code:
                // Condition set 1
                            if (Close[0] == Close[1] + 1 * TickSize)
                            {
                                ExitLongStop(ParabolicSAR(0.02, 0.2, 0.2) [0], "", "");
                            }
                Let me know if I've gone totally off the rails here, okay? This is working for me exactly as expected.

                Thank you all for your continued prodding! It's fantastic.

                -V
                Attached Files

                Comment


                  #9
                  Works on the long side.... not on the short side... any thoughts?

                  Hello all...

                  I got the trailing stoploss to work on the long side using the formula I mentioned above. But... it's not working on the short side.

                  It kills the stop on each uptick. It's fine on the down ticks... but as soon as price ticks up past its previous position, it kills the stop.

                  It leaves the order in place, but takes out the stop. And a few ticks later, it puts the stop back in place again... only to kill it on the next uptick. I'm not sure why. Any thoughts?

                  On the long side, I've got the code as follows:
                  Code:
                  // Condition set 1
                              if (Close[0] == Close[1] + 1 * TickSize)
                              {
                                  ExitLongStop(ParabolicSAR(0.02, 0.2, 0.2) [0], "", "");
                              }
                  On the short side, the "+ 1" is changed to,

                  Code:
                  (Close[0] == Close[1] + - 1 * TickSize)
                  Could that be the problem? Since I'm headed "down," I figure the "-1" tick offset would be the correct offset. Is this not correct?

                  I can see why you programmers do what you do. It's like everyday you get a new big puzzle to solve. I love puzzles!

                  -V

                  Comment


                    #10
                    Hello Aurum,

                    Thank you for your response.

                    The condition to check Close[0] == Close[1]+1*TickSize, is only going to set the ExitLongStop() if the current price is only one tick above the previous close, is this you intention?

                    The reason it is cancelling and re-entering again and again on the short side is you have an opposite condition that submits a conflicting order in place (the original ExitLongStop()). You would need to add a check for the current position such as Position.MarketPosition == MarketPosition.Long for the ExitLongStop() and Position.MarketPosition == MarketPosition.Short for the ExitShortStop().

                    You can find information on Position.MarketPosition at the following link: http://www.ninjatrader.com/support/h...etposition.htm

                    Comment


                      #11
                      Hello again Patrick!

                      Thank you for your very quick response.

                      You asked if my intention was to modify the order on each tick. Yes, this particular strategy I'm building uses 1 range bars, so I am having the order follow the PSAR on each uptick (for longs) or downtick (for shorts). In other words, I'm attempting to have the "moving stop" updated on every tick to follow the price of the range bars up or down at a very specific distance dictated by the PSAR's position.

                      Thank you also for the recommendation to use a "state" checker for the trailing orders. And while I've got you here... please allow me to ask you a question regarding the "Position.MarketPosition" method as it relates to other forms of orders.

                      In the past few days, as the strategy has been taking shape, I've noticed some order behavior that has perplexed me (to put it mildly).

                      Sometimes, when a position is open, (let's say a long position for this example), and then, another long signal occurs, the strategy will add to the current position. In other words, if I'm long 3 contracts, and there's another "go long" signal, instead of the strategy knowing I'm already long 3, it will add 3 to my current position, making me long 6.

                      What is the best way to handle this? In some of the NinjaTrader examples, the MA crossover is used as the strategy example, but your "enter long" or "enter short" commands don't contain a "state" checker like: "if (Position.MarketPosition == X) etc." so I take it it's not a critical component of all orders.

                      I have come to understand that using a check for position (such as "Position.MarketPosition == Long"), would keep the strategy from entering another 3 contracts if there are already 3 open (unless I assign special names to each long or short signal).

                      Is this "state" check necessary (or is it considered "best practice" to put these in every "order related" condition set)? I posted an earlier thread about this and the nice (and very much appreciated) reply I got was regarding "1 entry per direction." I've noticed that sometimes, even with this option engaged, I can get orders upon orders. I'm just not familiar enough with NinjaTrader to know where these are coming from.

                      You've given me an excellent use for these "state" checkers in your last reply. I'm just curious as to whether or not they should be used in all cases where an order of some kind will result (kind of like "standard best practice"). Any comments or thoughts on this, (or maybe an in depth video where you or your collegues discuss the different applications of this method)? I'm sure it has some very powerful applications.

                      Thank you again greatly for your input!

                      -V

                      Comment


                        #12
                        Hello V,

                        Thank you for your response.

                        The Position.MarketPosition check in this case is for the Exit() methods to avoid a conflict. The EntriesPerDirection will limit the entries as designed but not the exit orders.

                        Please let me know if I may be of further assistance.

                        Comment


                          #13
                          @Aurum and koganam

                          I'm pretty new to coding as well, but have been working on getting better. Try the below code for PSAR. It worked for me and thought it might help you guys as well. Cheers.

                          SetStopLoss(CalculationMode.Ticks, ParabolicSAR(0.02, 0.2, 0.02)[0]);

                          How can one succeed at anything without failing as something.

                          Comment

                          Latest Posts

                          Collapse

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