• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Daily ATR used in a Strategy no matter the period

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

    Daily ATR used in a Strategy no matter the period

    Hopefully this hasn't been asked before, I did several searches and found answers to other questions while looking for this one but not this answer.

    I'm trying to create a strategy that will be used in various time period charts, right now mostly a 5-minute candlestick chart. I want to have a stop loss and profit target in the strategy that's based on a seven day ATR value as two different percentages, one for SL and the other for PT say SL = seven day ATR*10%.

    Is there a method that would allow me to look at a seven day ATR , or any amount of days, in a strategy used on a five minute chart for example?

    Thanks,
    George

    #2
    Hi GMiller64,

    Here is a sample strategy that I believe answers your question.

    You have to include a secondary data series of daily bars and you'll calculate the ATR on that.

    One tricky part then is that you need to make sure you have enough bars on both series before you begin trading. This is the first line of the OnBarUpdate method.

    I've done this for ES ##-## bars so load ES ##-## on your chart and then choose whatever period for the chart bars you want. The ATR will still be based on the daily bars of the secondary series.


    Code:
    #region Using declarations
    using NinjaTrader.Cbi;
    using NinjaTrader.NinjaScript.Indicators;
    using System.ComponentModel.DataAnnotations;
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies.Forum
    {
        public class ForumDailyATR : Strategy
        {
            [NinjaScriptProperty, Range(1, int.MaxValue)]
            [Display(Name = "Profit Target Long %")]
            public int ProfitTargetLongPercentage { get; set; }
    
            [NinjaScriptProperty, Range(1, int.MaxValue)]
            [Display(Name = "Stop Loss Long %")]
            public int StopLossLongPercentage { get; set; }
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"";
                    Name = "Forum_DailyATR";
    
                    BarsRequiredToTrade = 20;
                    ProfitTargetLongPercentage = 100;
                    StopLossLongPercentage = 100;
                }
                else if (State == State.Configure)
                {
                    // If there's just one data series on the chart, then this series will be index=1.
                    // Bars dayBars = BarsArray[1];
                    AddDataSeries("ES ##-##", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] < 7) { return; }
                if (BarsInProgress != 0) { return; }
    
                var atr = ATR(BarsArray[1], 7)[0];
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    EnterLong();
                }
                else
                {
    
                    if (Position.MarketPosition == MarketPosition.Long)
                    {
                        double profitTarget = Position.AveragePrice + atr * ( ProfitTargetLongPercentage / 100d );
                        double stopLoss = Position.AveragePrice - atr * ( StopLossLongPercentage / 100d );
                        if (Close[0] > profitTarget || Close[0] < stopLoss)
                        {
                            ExitLong();
                        }
                    }
                }
    
            }
        }
    }
    Last edited by Steve L; 11-02-2019, 07:54 AM.
    Steve L
    NinjaTrader Ecosystem Vendor - Ninja Mastery

    Comment


      #3
      Thanks Steve, I'll give that a try as soon as I can get what I have written so far to load without getting an Error when calling 'OnStateChange" method error which I'm going to start a separate thread for.

      Comment


        #4
        Hi George and Steve.

        Steve's last post is an exemplary model for how to work with multiple time frames in NinjaScript. There is a guide on multi timeframe/instrument scripts here

        Best regards.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by StevenNelson, Today, 03:05 PM
        0 responses
        2 views
        0 likes
        Last Post StevenNelson  
        Started by Benjaminf, Today, 02:21 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by grayfrog, 05-24-2020, 09:29 PM
        5 responses
        32 views
        0 likes
        Last Post NinjaTrader_RyanS  
        Started by chrismcdermut, Today, 01:46 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by dpolyakov, Today, 01:13 PM
        3 responses
        14 views
        0 likes
        Last Post dpolyakov  
        Working...
        X