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

Try to get the ATR for a daily chart

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

    Try to get the ATR for a daily chart

    I am trying to use the average true range to set my strategy stop loss and target exits. My strategy is running on a daily chart with the Calculate on price change set. When I try to get the ATR for a 5 day, 14 day, or 30 day period the values coming back are near zero. Is this because it is basing it off the ticks and not the days.

    Here is the code for the stop loss calculation.

    Code:
    private double LongStopPrice(double entryPrice)
    {
         // ATR_LB_Stop set to 5
         Series<double> averageTrueRange = new Series<double>(this);
         Indicators.ATR atr = ATR(averageTrueRange, ATR_LB_Stop);
         double stop = (StopLossMulti * atr[0]);
    
         return entryPrice - stop;
    }

    #2
    Cleaned up the code a bit but still have very small numbers coming back. This is returning ~0.005, for the ATR, is this correct?

    Code:
    private double LongStopPrice(double entryPrice)
    {
         double atr = ATR(ATR_LB_Stop)[0];
         double stop = (StopLossMulti * atr);
    
          return entryPrice - stop;
    }

    Comment


      #3
      Hello NeverDownMoney,

      Thank you for your posts.

      We would not expect to see the ATR indicator return low a value of ~0.005. You could check this by adding the ATR indicator to a chart to see the expected value.

      In your code, I see that you are instantiating an indicator in your private method. It is best practice to instantiate the indicator in OnStateChange(). You could then use AddChartIndicator() to view the indicator and its value on a chart.

      Additionally, you could add prints to print out the value of the ATR indicator and use that value along with SopLossMulti to create your stop loss

      Please review the help guide links below for more information.

      AddChartIndicator - https://ninjatrader.com/de/support/h...tindicator.htm

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

      Let us know if we may further assist.
      Attached Files
      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        I think that the reason that I am seeing such small numbers is that the chart instrument is 6E 12-20 (Euro December 2020), it has a very small ATR when I put it on the chart, therefore I believe it is working as expected. I put it on another cart (ES 12-20) and it seems to have much larger numbers.

        I am creating the ATR indicator in OnStateChange and saving the created value in a class private variable. Is this good coding practice?

        Code:
        else if (State == State.DataLoaded)
        {
             ATR_Target = ATR(ATR_LB_Target);
             ATR_Stop = ATR(ATR_LB_Stop);
             AddChartIndicator(ATR_Target);
        }

        Comment


          #5
          Hello NeverDownMoney,

          Thank you for that information.

          It is good coding practice to instantiate the private indicator variable before OnStateChange() followed by creating the indicator in OnStateChange() when the State reaches State.DataLoaded.

          Please see the 'Setting up resources that require market data' section of the NinjaScript Best Practices help guide page for more information - https://ninjatrader.com/support/help...urceManagement

          Let us know if we may further assist.

          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CortexZenUSA, Today, 12:53 AM
          0 responses
          1 view
          0 likes
          Last Post CortexZenUSA  
          Started by CortexZenUSA, Today, 12:46 AM
          0 responses
          1 view
          0 likes
          Last Post CortexZenUSA  
          Started by usazencortex, Today, 12:43 AM
          0 responses
          5 views
          0 likes
          Last Post usazencortex  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          168 responses
          2,266 views
          0 likes
          Last Post sidlercom80  
          Started by Barry Milan, Yesterday, 10:35 PM
          3 responses
          13 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X