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

Moving Stop above/below prior high/low

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

    Moving Stop above/below prior high/low

    I have a strategy that, once a position is in the money, the stop eventually begins to be reset with if statements bools that go from true to false Example:
    (Sell1, Sell2, & Sell3 are declared as private bools earlier in the code)

    if(Position.MarketPosition == MarketPosition.Short
    && CONDITIONS
    && Sell1)

    {
    SetProfitTarget(".......);
    SetStopLoss("..", CalculationMode.Price,Position.AveragePrice - 2*TickSize,true);
    Sell1=false;
    Sell2=true;
    }


    if(Position.MarketPosition == MarketPosition.Short
    && CONDITIONS
    && Sell2)

    {
    SetProfitTarget(".......);
    SetStopLoss("...", CalculationMode.Price,High[1]+1*TickSize,true);
    Sell2=false;
    Sell3=true;
    }


    The problem I am having is the final stop, which is High[1]+1*TickSize, doesn't continue to reset itself, although the conditions are true for it to continue to move down as the market moves down (vice-versa for longs).

    For example, in the attached pic, the conditions for the stop to trail to the prior high +1 tick occurred where the dotted line is. So the stop was set on that bar. However, the condition continued to be true for the next bar...SO...the stop should have moved to the high +1 tick of that next bar and gotten stopped out where the checkmark is.Why did it not work that way?
    Attached Files

    #2
    Without setting up a test case, a Print statement to the output window might help.


    Print ( "High[1]+1*TickSize=" + High[1]+1*TickSize );

    Of course, CONDITIONS && Sell2 might not have been TRUE, so you might need some prints before that to debug further.

    Comment


      #3
      Thanks Sledge, I'll take a further look at that.

      It is pretty basic, so I am sure there is something simple I am just missing.I am sure someone has run into this issue before.

      Comment


        #4
        Hello CMillz,

        Thanks for your post.

        If I understand correctly you are wanting the second set to keep adjusting the profit target. The use of the bool Sell2 is limiting this to a one time change as the code sets Sell2 to false after setting the profit target which would prevent further changes until Sell2 is reset to true. Sell2 is true in the first condition but once Sell1 is set false, Sell2 cannot be set to true until Sell1 has been set to True.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hey Paul,

          Thanks. Not quite, actually the profit target stays the same, it is the STOP LOSS that is adjusting.

          But, I can see how the issue may be related to the Sell2 bool, as that has become false, even though the condition continues to be true. I would see then how the stop doesn't continue to trail.

          My question then, is there a way to make the bools no longer true or say get out of the bool structure in some way and just trail the stop above each prior high +1 tick until I am either stopped out or the profit target is hit?

          Does what I am asking make sense?

          Comment


            #6
            Hello CMillz,

            Thanks for your reply.

            Yes, I miswrote and meant the stoploss.

            Perhaps you might use the Sell3 as the bool for the stop loss control in a related but separate condition set, so move the stop loss out of where it is and use the second set to only adjust your profit target once and then enable the Sell3 bool (as it does now) to then allow dynamic changes to your stoploss.

            if (Sell3 && Position.MarketPosition == MarketPosition.Short)
            {
            SetStopLoss("...", CalculationMode.Price,High[1]+1*TickSize,true);
            }

            So once your code has gone through fixed the previous two sets, the third set will dynamically adjust the stop until you are no longer short. I don't see where you are setting Sell3 as false but just make sure it gets set false when you are flat.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Ok, that makes sense. I'll play with that and see what I get.

              Actually good point on being flat.

              At the beginning of my code I have a set of private bools as such:

              private bool Flat=true;
              private bool Buy1=false;
              private bool Buy2=false;
              private bool Buy3=false;
              private bool Sell1=false;
              private bool Sell2=false;
              private bool Sell3=false;

              After I get into a long or short position, Flat=false. However, does Sell3 go false after the position is either stopped out or hits the profit target? I thought the way I set up the code (with flat=true), once a position is closed (hence I am flat) that flat bool is always reset to true. Is this logic correct, or do I need another code to reset the flat bool back to true?

              Comment


                #8
                Hello CMillz,

                Thanks for your reply.

                The bool declarations (assuming they are outside on OnBarUpdate()) would only be set when the strategy is first loaded. In the onBarUpdate, you can add a check of MarketPosition.Flat to reset them to the initial conditions.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Ok got it. Yes the bool declarations are outside on OnBarUpdate().

                  Currently what I have in the onBarUpdate is:

                  if(Position.MarketPosition == MarketPosition.Flat)
                  {
                  Flat=true
                  }


                  should I instead have this:


                  if(Position.MarketPosition == MarketPosition.Flat)
                  {
                  Flat=true
                  Buy1=false
                  Buy2=false
                  Buy3=false
                  Sell1=false
                  Sell2=false
                  Sell3=false
                  }

                  Comment


                    #10
                    Hello CMillz,

                    Thanks for your post.

                    Yes, that would ensure that the bools are set as you expect them prior to being in a position.
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by RookieTrader, Today, 09:37 AM
                    3 responses
                    15 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by kulwinder73, Today, 10:31 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post kulwinder73  
                    Started by terofs, Yesterday, 04:18 PM
                    1 response
                    23 views
                    0 likes
                    Last Post terofs
                    by terofs
                     
                    Started by CommonWhale, Today, 09:55 AM
                    1 response
                    4 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Started by Gerik, Today, 09:40 AM
                    2 responses
                    7 views
                    0 likes
                    Last Post Gerik
                    by Gerik
                     
                    Working...
                    X