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

Trying to Plot ATR * factor above and below bar

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

    Trying to Plot ATR * factor above and below bar

    I'm trying to create a new indicator that is ATR * Factor above and below the current bars.

    I think I have the values calculating, but I can't verify and nothing shows on the charts.

    What am I missing?

    Here is my code:

    ================================================== =====

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class MyATR : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MyATR";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    ATRFactor = 1;
    ATRBars = 14;
    // AddPlot(Brushes.Red, NinjaTrader.NinjaScript.Indicators.MyATR.HighestBa r);
    // AddLine(Brushes.Green, 1, NinjaTrader.NinjaScript.Indicators.MyATR.UpperBar) ;


    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    }
    }

    protected override void OnBarUpdate()
    {
    {
    double high0 = High[0];
    double low0 = Low[0];

    if (CurrentBar == 0)
    Value[0] = high0 - low0;
    else
    {
    double close1 = Close[1];
    double trueRange = Math.Max(Math.Abs(low0 - close1), Math.Max(high0 - low0, Math.Abs(high0 - close1)));
    Value[0] = ((Math.Min(CurrentBar + 1, ATRBars) - 1 ) * Value[1] + trueRange) / Math.Min(CurrentBar + 1, ATRBars);
    }

    LowerBar[0] = Close[0] - (Value[0] * ATRFactor);
    UpperBar[0] = Close[0] + (Value[0] * ATRFactor);
    }
    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, double.MaxValue)]
    [Display(Name="ATRFactor", Order=1, GroupName="Parameters")]
    public double ATRFactor
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="ATRBars", Order=2, GroupName="Parameters")]
    public int ATRBars
    { get; set; }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> LowerBar
    {
    get { return Values[0]; }
    }
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> UpperBar
    {
    get { return Values[1]; }
    }

    #endregion

    }
    }

    #region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private MyATR[] cacheMyATR;
    public MyATR MyATR(double aTRFactor, int aTRBars)
    {
    return MyATR(Input, aTRFactor, aTRBars);
    }

    public MyATR MyATR(ISeries<double> input, double aTRFactor, int aTRBars)
    {
    if (cacheMyATR != null)
    for (int idx = 0; idx < cacheMyATR.Length; idx++)
    if (cacheMyATR[idx] != null && cacheMyATR[idx].ATRFactor == aTRFactor && cacheMyATR[idx].ATRBars == aTRBars && cacheMyATR[idx].EqualsInput(input))
    return cacheMyATR[idx];
    return CacheIndicator<MyATR>(new MyATR(){ ATRFactor = aTRFactor, ATRBars = aTRBars }, input, ref cacheMyATR);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.MyATR MyATR(double aTRFactor, int aTRBars)
    {
    return indicator.MyATR(Input, aTRFactor, aTRBars);
    }

    public Indicators.MyATR MyATR(ISeries<double> input , double aTRFactor, int aTRBars)
    {
    return indicator.MyATR(input, aTRFactor, aTRBars);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.MyATR MyATR(double aTRFactor, int aTRBars)
    {
    return indicator.MyATR(Input, aTRFactor, aTRBars);
    }

    public Indicators.MyATR MyATR(ISeries<double> input , double aTRFactor, int aTRBars)
    {
    return indicator.MyATR(input, aTRFactor, aTRBars);
    }
    }
    }

    #endregion

    #2
    Hello pauleman,

    Welcome to the NinjaTrader forums!

    When uncertain about behavior, use Print() to print the values to understand the behavior.

    Below is a link to a forum post that demonstrates using Print() to understand behavior.


    Further, be sure to check the Log tab of the Control Center for any errors that may cause the script to disable.

    Start by printing the time of the bar. Then print the value of trueRange after this is assigned.
    What is appearing the output window?


    As a tip, when using AddDataSeries() multiple series will update OnBarUpdate(). The logic should specify which BarsInProgress the conditions and actions should be evaluated on, and each series should be checked that CurrentBars[BarsInProgress index] is greater than any index used for that series.


    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the hints. In the Output window I see the following:
      Indicator 'MyATR': Error on calling 'OnBarUpdate' method on bar -1: Index was outside the bounds of the array.

      Any advice?

      Comment


        #4
        Hello pauleman,

        This message indicates an invalid index was used.

        Below is a link to a forum post on index errors.
        Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:


        What indexes are used in the script? Are all indexes less than the size of the collection?
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by gbourque, Today, 06:39 AM
        2 responses
        4 views
        0 likes
        Last Post gbourque  
        Started by cre8able, Yesterday, 07:24 PM
        1 response
        13 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by cocoescala, 10-12-2018, 11:02 PM
        6 responses
        939 views
        0 likes
        Last Post Jquiroz1975  
        Started by cmtjoancolmenero, Yesterday, 03:58 PM
        1 response
        17 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by benmarkal, Yesterday, 12:52 PM
        3 responses
        23 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X