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

How to use ATR to define stop placement?

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

    How to use ATR to define stop placement?

    Hi,

    I have searched the forums, and apparently this is a common question, however whenever I look at the responses/solutions, they seem to be unclear to me or not pertain to my situation. It's likely just me that doesn't understand, so I apologize in advance for asking this. But I am stuck and need help to move forward.

    How can I use ATR to define stop placement within this context:

    - Inside the OnBarUpdate() class
    - Using the SetStopLoss Function
    - Using NinjaScript Editor (not the Strategy Builder Wizard)

    For example, suppose I have entered the market at a particular entry price. I'd like to then Set a stop loss at 2 ATR behind that.

    How do I calculate and store some form of 2 ATR into a variable to be used with the SetStopLoss Function?

    And a related question, how do I call the current price as of that OnBarUpdate iteration? I've seen Close[0] as one way, but not certain if that works and even if it does, I can't use it with other functions due to some error with the Type.

    Thanks!

    #2
    Hello enzo_,

    Thank you for the post.

    The terms you outlined are pretty standard for using a stoploss in general, there is nothing special required for the points you outlined. You can use SetStopLoss from OnBarUpdate in manual coding, that is fine.

    You can use the standard ATR syntax from the help guide to get its value:

    https://ninjatrader.com/support/help..._range_atr.htm

    How you want the value to be used would determine the next step. You can see the following post for some existing information about using this for a stoploss and what is required of that: https://ninjatrader.com/support/foru...998#post178998

    The ATR is reported in a decimal so you will need to convert the decimal value into a meaningful value which is relevant for the stop such as a price or an amount of ticks. The linked post shows one way to supply Ticks to the SetStopLoss method. You can find all supported calculation modes in the SetStopLoss page here: https://ninjatrader.com/support/help...ub=SetStopLoss

    Close[0] would represent the current price so that can be used.

    I look forward to being of further assistance.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you. I'm working on this right now.

      In the meanwhile, I am using the daily timeframe for my Strategy, but how can I be sure that the values being used, for example as with Close[0] are not the close of the daily bar, and as of the current price intra-bar?

      I am using Calculate = Calculate.OnPriceChange; however something doesn't seem right so I'm not sure that is all you need. Do I need something else?

      Comment


        #4
        Hi,

        Even if Close[0] were to get the current price that I presumably entered at, there seems to be a problem with getting the stop to be 2 ATR behind it because there are sometimes differences between that value and the ACTUAL entry price after execution. Sometimes the difference is significant.

        Therefore, what is the best way to set my stop 2 ATR away from my ACTUAL entry? Is there a way to call my entry price after I have entered into a position?

        Thanks!

        Comment


          #5
          Hello enzo_,

          To better understand how OnBarUpdate is being called and what data is being referenced, I would suggest using a Print statement and using some of the scripts variables. Here is one example:

          Code:
          Print(BarsInProgress + " " + Time[0] + " " + Instrument.FullName);
          If you place this at the top of OnBarUpdate and print each bar value you can see how it is being called overall. That can help to decide logic and is specifically important if you have multiple series in the script. When using OnPriceChange you will see OnBarUpdate called more frequently as the price changes in realtime. Using Close[0] for the realtime building bar would give you the most recent price, however if you wanted the last close daily bar you would need to use Close[1] in that use case. You can use Time[0] or Time[1] in the print to observe the specific date of the data.

          If you are trying to work with order values such as the average fill price you will need to store a variable to your orders. We have an example of using the order event overrides here: https://ninjatrader.com/support/help...and_onexec.htm

          In the sample OnOrderUpdate is used to collect the order, after that point you could submit targets or set variables as needed with the order values. Because we are working in an even driven environment we need to wait for orders status to update and observe those changes to drive logic.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Hi,

            By printing after OnBarUpdate(), I can see that Close[0] is the close of each bar. I'd like for OnBarUpdate() to be updating every tick or price change for each bar. Not just once per bar after the bar closes. How do I accomplish this?

            Under (State == State.SetDefaults), I already have Calculate = Calculate.OnEachTick;. I've tried using Calculate.OnPriceChange as well to no avail.

            Do I need something more than this? Note that I am and want to continue using 1 Day bars to display my chart.

            Thanks!

            Comment


              #7

              Hello enzo_,

              Thank you for the reply.
              Under (State == State.SetDefaults), I already have Calculate = Calculate.OnEachTick;. I've tried using Calculate.OnPriceChange as well to no avail.
              Are you testing in historical data currently? This is only going to work tick by tick for realtime or if you have TickReplay enabled.



              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Oh, I'm testing this in the Strategy Analyzer. I didn't realize there were any other settings I needed to enable outside of it.

                I see under Tools > Options > Market Data, there is a checkbox for Show Tick Replay. Presumably I need to turn that on? Is there anything else I need to enable? Also, if I enable all these things, and run it in the Analyzer will it download the tick data if I don't have it automatically?

                I just tried enabling Show Tick Replay, and re-running the Strategy but that didn't change anything.

                Thank you.

                Comment


                  #9
                  Update: I just noticed the Tick Replay checkbox now appearing in the Strategy Analyzer. I have checked it and so far the backtest is taking way longer which means it's probably working.

                  I'll know soon if it worked and if I'll have any follow-up questions.Thanks for your time.

                  Comment


                    #10
                    Hello enzo_,

                    Thank you for the reply.

                    In a backtest you are using specifically historical data so you will generally see some differences in how the script is executed. You can find some common items here:
                    https://ninjatrader.com/support/help...lightsub=discr

                    It sounds like you figured out how to use TickReplay so you should see the other CalculationModes being used.



                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by sightcareclickhere, Today, 01:55 PM
                    0 responses
                    1 view
                    0 likes
                    Last Post sightcareclickhere  
                    Started by Mindset, 05-06-2023, 09:03 PM
                    9 responses
                    258 views
                    0 likes
                    Last Post ender_wiggum  
                    Started by Mizzouman1, Today, 07:35 AM
                    4 responses
                    18 views
                    0 likes
                    Last Post Mizzouman1  
                    Started by philmg, Today, 01:17 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_ChristopherJ  
                    Started by cre8able, Today, 01:01 PM
                    1 response
                    9 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Working...
                    X