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

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 traderqz, Today, 12:06 AM
        6 responses
        12 views
        0 likes
        Last Post traderqz  
        Started by Skifree, Today, 03:41 AM
        3 responses
        12 views
        0 likes
        Last Post Skifree
        by Skifree
         
        Started by traderqz, Yesterday, 09:06 AM
        5 responses
        33 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by guillembm, Today, 11:25 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by owensd, 04-21-2024, 11:34 PM
        9 responses
        34 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X