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

What is the best way to set stops.

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

    What is the best way to set stops.

    I am having problems with stops since I set different stop levels on BarUpdate depending on some parameter.. so inside onBarupdate I write this:
    if( Medians[1][0]<17.00)
    {
    stopLevelMovel=0.0076;
    stopGainNumber = 0.006;
    }
    And when triggering an order i set stopike this:
    EnterShort(0,positionTaken, "InRangeSYV");
    SetStopLoss("InRangeSYV", CalculationMode.Percent, stopLevelMovel*2, false);

    Is this a correct way to set stops? I have had problems when trading for real (stops has been generated just after the order).

    #2
    Hello dafonseca,

    Thanks for your post.

    The issue may be the sequence that you have, entering the order then placing the stop loss. The helpguide advises: "Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position "

    So what may be happening with your current sequence is that the dynamic stoploss value set in the previous order is being used immediately when the entry order is placed and is then being modified by the new setstoploss value.

    Depending on what your stoploss percent ranges, you may want to start with a larger percentage when flat and then keep your sequence as is where the stoploss is adjusted after the order is placed.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I see your point and it makes sense to me.

      Should I use setstoploss inside onBarUpdate?

      The reason I ve change that (it used to be like that) was as I set trace order true, I saw set stop orders generated every bar (1 min) and I though it was a waste of resources, due to it, I changed from onBarUpdate to below enter orders.

      What is I write set stop loss one line above the enterShort (Long)?

      Comment


        #4
        Hello dafonseca,

        Thanks for your reply.

        Just to be clear, the OnBarUpdate method is from { to } so everything between the braces will be executed on each update of the data. If CalculateOnBarClose is set to true then OnBarUpdate occurs once per bar, if set to false OnBarUpdate is executed once every tick.

        You can use boolean logic so that your SetStopLoss is set only once if you wish. Here is an example of setting the dynamic stop after your order and then later resetting your stop to a default value that you choose and only execute the code once through the use of two bools, doitonce and setMyStopback

        if (your condition to enter order are true && doitonce)
        {
        EnterShort(0,positionTaken, "InRangeSYV");
        SetStopLoss("InRangeSYV", CalculationMode.Percent, stopLevelMovel*2, false);
        doitonce = false; // set so only once per entry
        setMyStopBack = true; // prepare to reset when flat
        }

        if (Position.MarketPosition == MarketPosition.Flat && setMystopback) // when flat
        {
        doitonce = true; // re enable bool for next entry
        SetStopLoss("InRangeSYV", CalculationMode.Percent, yourdefaultstoplevelhere, false);
        setMyStopback = false; //set so only once per flat period
        }
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Do you see any problem in setstoploss at every onbarupdate? it that a common practice by NT users?

          Comment


            #6
            Hello dafonseca,

            Thanks for your reply.

            If you need to adjust on every bar update then you can certainly do that. Sorry, I cannot advise if that is a common practice.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              last question:
              would it make any difference use like this? in case I choose to use the logic controllers.
              setstop before entershort.

              SetStopLoss("InRangeSYV", CalculationMode.Percent, stopLevelMovel*2, false);
              EnterShort(0,positionTaken, "InRangeSYV");

              Comment


                #8
                Hello dafonseca,

                If you didn't want to use the logic controls then that sequence would allow the setstoploss to be set to the new value before placing the order rather than being adjusted after the entry order.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  One question regarding this matter.

                  How can I managed this boolean logic controllers in case setStopLoss was triggered?
                  I mean, when dealing with exit orders I can do this:
                  if (Position.MarketPosition == MarketPosition.Long)
                  {
                  ExitLong("ClosingLongAaplVolT","InRangeLVolT");
                  booleancontrol = false;
                  }
                  But how can I set booleanControl = false in case of a order coming from setStop methods?

                  Comment


                    #10
                    Hello dafonseca,

                    Thanks for your reply.

                    I'm not certain I understand what you are trying to do but in relationship to the previous posts in this thread we were using bool variables (True or False only) to control setting the stoploss once (rather than on each onbarupdate) and using a bool to reset the stoploss once to a known value when the market was flat.

                    The code segment you show:
                    Code:
                    if (Position.MarketPosition == MarketPosition.Long)
                    {
                    ExitLong("ClosingLongAaplVolT","InRangeLVolT");
                    booleancontrol = false;
                    }
                    would suggest that you will exit a long position as soon as (or next bar) that you are in a long position and that the variable booleancontrol will be set false.

                    The question you asked, "But how can I set booleanControl = false in case of a order coming from setStop methods?"
                    Please see post#4 for an example of how to set use/set a bool variable in the setStopLoss statement.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      I will try to explain better: your example below, the booleancontrol will get the value false in case exitLong was triggered. ok? but what if the long position is closed by a stopLoss order and I need to set booleancontrol to false. How can I handle that?
                      if (Position.MarketPosition == MarketPosition.Long)
                      {
                      ExitLong("ClosingLongAaplVolT","InRangeLVolT");
                      booleancontrol = false;
                      }
                      Another question would be. In the same code I have to enterLong conditions, When in "InRange1" the other is "InRange2"

                      Can I set two different stops levels? would it be like these?:
                      SetStopLoss("InRange1", CalculationMode.Percent, 0.008, false);
                      SetStopLoss("InRange2", CalculationMode.Percent, 0,005, false);

                      Comment


                        #12
                        Hello dafonseca,

                        Thanks for your reply.

                        You are setting the bool booleancontrol false when you are placing the exitorder. You are asking how to detect when the setstop order is used. If the stoploss is triggered you will no longer be in MarketPosition.long. So perhaps you can use the state of the MarketPosition as a way to set the state of the bool booleancontrol.

                        Yes, you can tie two different stop levels to two different orders through the use of signal names as you have shown.
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          I see your point.
                          I will try it.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by yertle, Today, 08:38 AM
                          1 response
                          5 views
                          0 likes
                          Last Post NinjaTrader_BrandonH  
                          Started by love2code2trade, Yesterday, 01:45 PM
                          3 responses
                          22 views
                          0 likes
                          Last Post NinjaTrader_BrandonH  
                          Started by trilliantrader, Today, 08:16 AM
                          2 responses
                          6 views
                          0 likes
                          Last Post trilliantrader  
                          Started by samish18, Today, 08:31 AM
                          1 response
                          2 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Started by Creamers, 09-08-2023, 10:26 AM
                          6 responses
                          157 views
                          0 likes
                          Last Post JonyGurt  
                          Working...
                          X