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

Problems with ATR based targets & stops - targets are set to entry price?!

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

    Problems with ATR based targets & stops - targets are set to entry price?!

    Hi

    I know there are a few posts around using ATR based targets & stops, none of them seem to cover the issue I'm having though sorry.

    When I use the below code, the targets look to be getting set to the entry price & the trade is opening & closing immediately, clearly I'm doing something wrong but not sure what this is

    Code:
    // Long Entries
                if (rules are met)
                {
                    // Setting stops & targets
    
    
                    EnterLong(Convert.ToInt32(DefaultQuantity), @"Buy_1");
    
                    SetProfitTarget(@"Buy_1", CalculationMode.Ticks, ATR1[0]);
    
                    SetStopLoss(@"Buy_1", CalculationMode.Ticks, ATR1[0], false);
    Could you please let me know why this is happening?

    I've tried all calculation methods, pips, ticks, & price & worked out the entry price - ATR value in ticks. Always the same, I'm not sure why this is happening, look forward to your help

    #2
    Hello Gav_G,

    Thanks for your ;post.

    I would suggest placing the set methods before your entry order to ensure that you don't have the situation of an entry fill before setting the correct values, the set method do erretain the last setting used which may be inappropriate in a new entry. So all you need to do is to put your EnterLong() after the set methods.

    When using calculation mode of ticks, the input is expecting an integer (whole number) and if the ATR1[0] value is less than 1.0 it will place a 0 tick level order. I would suggest using a Print statement so that you can properly assess the value of ATR1[0] being used. For example, on the CL 03-20, 5 minute bars an ATR(14) is currently showing a price range of 0.06 so the ticks in that case would be zero, so you would want to convert the price level to ticks for example like this: ATR1[0] / TickSize which would result in an acceptable value of 6 (if using a 14 period ATR).




    Paul H.NinjaTrader Customer Service

    Comment


      #3
      super thanks Paul I think the problem is the ATR setting, not the order of the orders if that make sense as I've tried having the stop & the targets before the order

      Another question though, can I use the signal names if I'm placing the stops & targets first?

      Comment


        #4
        Hello Gav_G,

        Thanks for your reply.

        Yes, you can place the set methods first (and should for good practice) with the signal names as all they do is sit there and look for an entry with that signal name (and they do have to match exactly).
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          I've got this working now, it was because the order was going a zero ticks.

          Once the stop loss has been entered, is there a way to set a variable to the number of ticks from entry price to stop loss?

          If so I can code up a simple way to move my stop to break even when price reaches a certain point

          Comment


            #6
            Hello Gav_G,

            Thanks for your reply.

            Glad to hear of your progress.

            You can use Position.AveragePrice as the entry price (unless you are trading multiple contracts that enter at different prices). https://ninjatrader.com/support/help...erageprice.htm
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul thanks for the reply, yes understand I can use Position.AveragePrice to know the price of the entry, but I want to be able to do something like the below plain english

              if market position is long & high of current bar > (Position.AveragePrice + "stop_size_in_ticks")

              set stop loss to Position.AveragePrice

              How can I know what the stop size was in ticks? I'm hoping that I can set it in a variable when I place the stop order? is that possible?

              I will want to keep moving by this amount of ticks you see as a form of trailing stop, so having this value stored in a variable is really going to help

              Comment


                #8
                Hello Gav_G,

                Thanks for your reply.

                Assuming you are using something like ATR1[0] /TickSize to enter the number of ticks for the stop, you can store the same value in a variable. Once you are in a position, long for example, then your stop would located at Position.AveragePrice - (variable * TickSize).
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Yes fantastic thanks Paul, I've thought through this properly now & come to the same obvious conclusion hehe, thanks again

                  Comment


                    #10
                    Hi

                    I'm having some problem with moving the stop to break even using the approach above. Not sure what is happening but the below is not setting the stops as I would expect at all.

                    The first if statement details the entry rules & then places a couple of long orders along with stops & targets, this would perfectly well if I run this code on it's own.

                    I've then added in the second if statement, stating that if the current position is long & the high of the current bar has reached the Buy_1 TP price, then set the stop loss for the Buy_2 position to Position average price (breakeven)

                    The stop losses are not setting correctly, the couple of trades in the test data are winners, the follow couple of trades do not close at all are stay open until the end of the data, it doesn't look like any stops are set.

                    I'm not sure why this is, please can you help? Can I not set another stop loss for the same buy signal? do I need to update it in some way? or should I move out of the OnBarUpdate method & into orderupdate or something?

                    I would be grateful if you could provide an example or a link to another thread, I've searched through the forum, but the other threads on breakeven are not what I'm trying to do.

                    Thanks in advance

                    Code:
                    protected override void OnBarUpdate()
                            {
                                if (BarsInProgress != 0) 
                                    return;
                    
                                if (CurrentBars[0] < 1)
                                    return;
                    
                    
                    
                                 // Long Entry rules
                                if ([I]long entry logic[/I])
                                {
                                    // Setting stops targets & placing orders for 2 positions
                                    // Set profits & set stops are done before the order is placed as reccomended                
                                    SetProfitTarget(@"Buy_1", CalculationMode.Ticks, (ATR1[0]*1.5)/TickSize);                
                                    SetStopLoss(@"Buy_1", CalculationMode.Ticks, (ATR1[0]*1.5)/TickSize, false);                
                                    EnterLong(Convert.ToInt32(DefaultQuantity), @"Buy_1");                
                                    SetProfitTarget(@"Buy_2", CalculationMode.Ticks, (ATR1[0]*3)/TickSize); //---No Profit Target is needed for Buy_2 as this will be managed with Trailing Stop
                                    SetStopLoss(@"Buy_2", CalculationMode.Ticks, (ATR1[0]*1.5)/TickSize, false);                
                                    EnterLong(Convert.ToInt32(DefaultQuantity), @"Buy_2");
                    
                                    //Set the size of the stop in ticks to the variable stopSize
                                    stopSize = ATR1[0];
                                }
                    
                                //Manage long position 2 Trailing Stop (@"Buy_2")
                                if ((Position.MarketPosition == MarketPosition.Long)
                                    && (High[0] > (Position.AveragePrice + stopSize)));
                    
                                {
                                    SetStopLoss(@"Buy_2", CalculationMode.Price, Position.AveragePrice, false);
                    
                                }
                    
                    
                    
                    
                            }

                    Comment


                      #11
                      Hello Gav_G,

                      Thanks for your reply.

                      Please check the "log" tab of the control center for any error/warning messages that appear after you start the strategy. If no clues there then i would suggest debugging.

                      I don't see anything wrong with your code except that it would continue to adjust the stop (returning the same code) so you might consider a bool so that it only adjust the stop to BE once rather than on every OnBarUpDate(). I would also suggest adding print statements in both your entry code block and the BE code block to print when those actions have occurred, as this will provide you with further info. Something like Print (Times[0][0]+" Entry order block hit"); and Print (Times[0][0]+" Break even set");

                      The print statements will go to the new>Ninjascript output window so make sure that is open before applying the strategy. If you are not checking for a flat position in your entry block, it is possible that you may get multiple print statements which would confirm it is being hit multiple times and if so that may be resetting your stops.

                      Print statements are always a good way to check out what is happening. You can find other tips here: https://ninjatrader.com/support/help...script_cod.htm

                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Ok thanks Paul. I am checking for flat market position. I will try adding the prints in. This isn’t something I’ve done before but hopefully I can sort it.

                        thanks again for your help. If I can’t get this to work. Can you recommend another example of how to change the stop price of a particular buy signal after I’ve set it using the set stop loss in on bar update? This must be something is commonly done, if so can you point me in the direction of the guidance on this?

                        thanks in advance

                        Comment


                          #13
                          Hello Gav_G,

                          Thanks for your reply.

                          I encourage you to work with debugging techniques as these will serve you well going forward. You need to be able to prove out your code to yourself when it is not working as expected and debugging removes the mystery of what is actually happening.

                          You can find a simple example of a breakeven stop here: https://ninjatrader.com/support/help...of_stop_lo.htm
                          Paul H.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by algospoke, Yesterday, 06:40 PM
                          2 responses
                          19 views
                          0 likes
                          Last Post algospoke  
                          Started by ghoul, Today, 06:02 PM
                          3 responses
                          14 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by jeronymite, 04-12-2024, 04:26 PM
                          3 responses
                          45 views
                          0 likes
                          Last Post jeronymite  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          7 responses
                          21 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by AttiM, 02-14-2024, 05:20 PM
                          10 responses
                          181 views
                          0 likes
                          Last Post jeronymite  
                          Working...
                          X