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

Profit target using yesterdays high

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

    Profit target using yesterdays high

    I have a setup a profit target strategy using the high of 1 bar ago. It saves and compiles successfully but I cannot enable the strategy on the chart? Please help

    Same applies with a stop lost calculated using prior bar lows.

    Thanks

    #2
    Hello AB2018,

    Thanks for your post and welcome to the NinjaTrader forums!

    When you go to enable the strategy, do you see any errors listed in the "log" tab of the control center?

    When you go to enable the strategy, does it momentarily enable then disable?
    Last edited by NinjaTrader_PaulH; 10-30-2018, 05:52 AM. Reason: added welcoming text.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi

      Thank you - there are some errors in the log I will look into

      Comment


        #4
        Stop loss SetStopLoss(@"Long", CalculationMode.Price, (Low[1] * 0.95) , false);

        The error in the log is as follows:

        Strategy 'TestPriceStopLoss': Error on calling 'OnStateChange' method: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

        Comment


          #5
          Hello AB2018,

          Thanks for your reply.

          The statement SetStopLoss(@"Long", CalculationMode.Price, (Low[1] * 0.95) , false); would be valid in the OnBarUpdate() method if you have more than 1 bar loaded. If the statement is in the OnStateChange() then it will cause that error.

          If you are using the Strategy builder to create the strategy then you would not be able to use SetStopLoss() with a variable that updates in OnStateChange(). You would need to use a fixed parameter (one that does not change such as CalculateIonMode.Ticks, 10).

          Alternatively, if you want to use the SetStopLoss() dynamically, you can unlock your code and move the statement from OnStateChange() into the OnBarUpdate() section within the block of code where your entry is made. Note, you would always need to set your stop value first then place the entry order as the stop value is retained by the method the last time it was called and is applied immediately upon entry order fill, so by setting the stop first you avoid incorrect stop order placement.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Ok understood, thanks

            Comment


              #7
              In the OnBarUpdate() section how is the BarsAgo calculated - is it from the bar when the trade was entered?

              Thanks

              Comment


                #8
                Hello AB2018,

                Thanks for your reply.

                BarsAgo would be relative to the current bar being processed and the Calculate mode of the script.

                I'm not sure of the context but in general what happens is when you apply a strategy (or indicator), it will read data starting from the very first bar in the data series of the chart. If the data series has data from 5 days ago then that would be the starting bar 5 days ago. Once the bar is loaded, OnBarUpdate() is called and your code is executed. The first bar is counted as bar 0, so if your code has a look back (barsago) of greater than 0 then you would end up with an error because there is no previous bar to refer to. To prevent the error, as the first code line in OnBarUpdate() you would want to check if the current bar being processed is greater than any barsAgo in your code. For example, if you were referring to a bars ago of 5, such as Low[5], then you would want to prevent execution until the bar number was greater than 5. NinjaTrader provides the int CurrentBar to use as it holds the bar number being processed. In OnBarUpdate, as an example, you would add if (CurrentBar < 6) return;

                As each bar is loaded, CurrentBar is updated, your script is executed and the process continues until reaching the right edge live data where the script waits until the next call of OnBarUpdate()

                Reference: https://ninjatrader.com/support/help...currentbar.htm

                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks - I actually want to use the price on the days before entering the position to calculate the profit target. ie not a dynamic profit target but calculated on the price before entering the position

                  What is the code for the number of bars the position has been open for and can I use this inside the "low[#bars]" code?







                  Comment


                    #10
                    Hello AB2018,

                    Thanks for your reply.

                    Can you clarify if you are coding directly in Ninjascript or are using the strategy builder?
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      I have tried both but easiest to use the strategy builder

                      Comment


                        #12
                        Hello AB2018,

                        Thanks for your reply.

                        In the strategy builder, you cannot set a dynamic stop, trailstop or profit target. These methods are set when the strategy first loads and do not change. You wrote, "calculated on the price before entering the position". That is an example of a dynamic calculation. If you want to use a dynamically set stop/target then you would need to unlock the code and work in Ninjascript directly.

                        You could use the method BarsSinceEntryExecution() to compare to a number of bars, please see: https://ninjatrader.com/support/help...yexecution.htm
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks - strategy running well now using BarsSinceEntryExecution().

                          I would now like to use Low[BarsSinceEntryExecution() + 3]. What is the syntax or do I need to first create a separate variable/object? If so where and what would the code/syntax be

                          Thanks again

                          Comment


                            #14
                            Hello AB2018,

                            Thanks for your reply.

                            You can use Low[BarsSinceEntryExecution()+3] which would provide the low of the bar of 3 bars prior to the entry bar.
                            Note that before using this you would want to ensure that BarsSinceEntryExecution is not equal to -1 which would be the case before the very first entry.
                            Reference: https://ninjatrader.com/support/help...yexecution.htm
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Thanks - working now!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ZeroKuhl, Today, 04:31 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post ZeroKuhl  
                              Started by Vulgoth_t_Destroyer, 05-09-2022, 04:45 PM
                              54 responses
                              5,336 views
                              0 likes
                              Last Post Gaterz
                              by Gaterz
                               
                              Started by ETFVoyageur, 05-07-2024, 07:05 PM
                              11 responses
                              74 views
                              0 likes
                              Last Post -=Edge=-  
                              Started by _Zero_, 04-10-2020, 03:21 PM
                              145 responses
                              7,900 views
                              6 likes
                              Last Post johng2
                              by johng2
                               
                              Started by TheTradingMantis, 01-19-2023, 02:05 AM
                              43 responses
                              921 views
                              0 likes
                              Last Post jmagaia
                              by jmagaia
                               
                              Working...
                              X