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

ATR Indicator

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

    ATR Indicator

    I want to develop a simple indicator. I tried using the ninjascripts but I just can't figure it out. I stumbled upon a code called a ATR trailing which i downloaded off this website. What i want is very similar, just one thing thats different. The indicator takes an ATR and multplies it times 3 and plots it on the chart as a dot as a trailing method. What I want is to simply take that multiple (just the number and use the draw text fixed functions to simply write that number on the corner of the main chart. So if the ATR is 2, i want it to write on the corner on the first display panel ATR STOP:6
    Here is the code i got for the original ATR trailing function.. Here is the link to the file http://www.ninjatrader-support2.com/...ks.php?catid=1

    [Gui.Design.DisplayName("ATRTrailing")]
    public class ATRTrailing : Indicator
    {
    #region Variables
    private double ATRTimes = 4;
    private int period = 10;
    private int counter = 0;
    private double ratched = 0.005;
    private double ratchedval = 0;

    private double SigClose = 0;
    private bool shortval;
    private bool longval;
    private DataSeries Plotset;
    private DataSeries Sideset; // Short == 0 Long ==1
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.Red, PlotStyle.Dot, "ATR Trailing Dn"));
    Add(new Plot(Color.Blue, PlotStyle.Dot, "ATR Trailing Up"));
    CalculateOnBarClose = true; // Call 'OnBarUpdate' only as a bar closes
    Overlay = true; // Plots the indicator on top of price
    Plotset = new DataSeries(this);
    Sideset = new DataSeries(this);
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar < period)
    return;

    if (Low[0] < Low[1] )
    {
    Sideset.Set(0);
    }
    if (High[0] > High[1] )
    {
    Sideset.Set(1);
    }
    ratchedval=(1 - ( counter * ratched));
    if (Sideset[1] == 0 )
    {
    SigClose = MIN(Low,(period))[0];
    Plotset.Set(SigClose + Bars.Instrument.MasterInstrument.Round2TickSize((r atchedval*(ATRTimes*ATR(period)[0]))));
    }
    if (Sideset[1] == 1)
    {
    SigClose = MAX(High,(period))[0];
    Plotset.Set(SigClose - Bars.Instrument.MasterInstrument.Round2TickSize((r atchedval*(ATRTimes*ATR(period)[0]))));
    }
    if(Sideset[1] == 1 && Low[1] <= Plotset[1])
    {
    Sideset.Set(0);
    SigClose = High[1];
    counter = 0;
    Plotset.Set( SigClose + Bars.Instrument.MasterInstrument.Round2TickSize((r atchedval*(ATRTimes*ATR(period)[0]))));
    Lower.Set(Plotset[0]);
    }
    if( Sideset[1] == 1 && Low[1] > Plotset[1])
    {
    Sideset.Set(1);
    SigClose = MAX(High,(period))[0];
    Plotset.Set(SigClose - Bars.Instrument.MasterInstrument.Round2TickSize((r atchedval*(ATRTimes*ATR(period)[0]))));
    if (Plotset[1] > Plotset[0])
    Plotset.Set(Plotset[1]);
    Upper.Set(Plotset[0]);
    }
    if(Sideset[1] == 0 && High[1] >= Plotset[1])
    {
    Sideset.Set(1);
    SigClose = High[1];
    counter = 0;
    Plotset.Set(SigClose - Bars.Instrument.MasterInstrument.Round2TickSize((r atchedval*(ATRTimes*ATR(period)[0]))));
    Upper.Set(Plotset[0]);
    }
    if(Sideset[1] == 0 && High[1] < Plotset[1])
    {
    Sideset.Set(0);
    SigClose = MIN(Low,(period))[0];
    Plotset.Set( SigClose + Bars.Instrument.MasterInstrument.Round2TickSize((r atchedval*(ATRTimes*ATR(period)[0]))));
    if (Plotset[1] < Plotset[0])
    Plotset.Set(Plotset[1]);
    Lower.Set(Plotset[0]);
    }
    counter++;
    }
    #region Properties
    /// <summary>
    /// The acceleration step factor
    /// </summary>
    [Description("Number of ATR Multipliers")]
    [Category("Parameters")]
    [Gui.Design.DisplayNameAttribute("Number of ATR (Ex. 3 Time ATR) ")]
    public double ATRTIMES
    {
    get { return ATRTimes; }
    set { ATRTimes = Math.Max(0, value); }
    }
    [Description("Period")]
    [Category("Parameters")]
    [Gui.Design.DisplayNameAttribute("Periods")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(0, value); }
    }
    /// <summary>
    /// Gets the lower value.
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Lower
    {
    get { return Values[0]; }
    }
    /// <summary>
    /// Get the Upper value.
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Upper
    {
    get { return Values[1]; }
    }
    [Description("Ratchet Percent")]
    [Category("Parameters")]
    [Gui.Design.DisplayNameAttribute("Ratchet Percent")]
    public double Ratched
    {
    get { return ratched; }
    set { ratched = Math.Max(0, value); }
    }
    #endregion
    }
    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private ATRTrailing[] cacheATRTrailing = null;

    private static ATRTrailing checkATRTrailing = new ATRTrailing();

    /// <returns></returns>
    public ATRTrailing ATRTrailing(double aTRTIMES, int period, double ratched)
    {
    return ATRTrailing(Input, aTRTIMES, period, ratched);
    }
    /// <returns></returns>
    public ATRTrailing ATRTrailing(Data.IDataSeries input, double aTRTIMES, int period, double ratched)
    {
    checkATRTrailing.ATRTIMES = aTRTIMES;
    aTRTIMES = checkATRTrailing.ATRTIMES;
    checkATRTrailing.Period = period;
    period = checkATRTrailing.Period;
    checkATRTrailing.Ratched = ratched;
    ratched = checkATRTrailing.Ratched;

    if (cacheATRTrailing != null)
    for (int idx = 0; idx < cacheATRTrailing.Length; idx++)
    if (Math.Abs(cacheATRTrailing[idx].ATRTIMES - aTRTIMES) <= double.Epsilon && cacheATRTrailing[idx].Period == period && Math.Abs(cacheATRTrailing[idx].Ratched - ratched) <= double.Epsilon && cacheATRTrailing[idx].EqualsInput(input))
    return cacheATRTrailing[idx];

    ATRTrailing indicator = new ATRTrailing();
    indicator.SetUp();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    indicator.Input = input;
    indicator.ATRTIMES = aTRTIMES;
    indicator.Period = period;
    indicator.Ratched = ratched;

    ATRTrailing[] tmp = new ATRTrailing[cacheATRTrailing == null ? 1 : cacheATRTrailing.Length + 1];
    if (cacheATRTrailing != null)
    cacheATRTrailing.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheATRTrailing = tmp;
    Indicators.Add(indicator);

    return indicator;
    }
    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.ATRTrailing ATRTrailing(double aTRTIMES, int period, double ratched)
    {
    return _indicator.ATRTrailing(Input, aTRTIMES, period, ratched);
    }
    /// <summary>
    /// Wilder’s Volatility System, developed by and named after Welles
    }
    }
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.ATRTrailing ATRTrailing(double aTRTIMES, int period, double ratched)
    {
    return _indicator.ATRTrailing(Input, aTRTIMES, period, ratched);
    }
    /// </summary>
    /// <returns></returns>
    public Indicator.ATRTrailing ATRTrailing(Data.IDataSeries input, double aTRTIMES, int period, double ratched)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.ATRTrailing(input, aTRTIMES, period, ratched);
    }
    #endregion

    #2
    Due to bandwidth constraints we can not assist in coding actual indicators/strategies. You would need to debug your indicator e.g. as per here: http://www.ninjatrader-support2.com/...ead.php?t=3418

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by rocketman7, Today, 09:41 AM
    1 response
    2 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by traderqz, Today, 09:44 AM
    0 responses
    1 view
    0 likes
    Last Post traderqz  
    Started by rocketman7, Today, 02:12 AM
    7 responses
    31 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by guillembm, Yesterday, 11:25 AM
    3 responses
    16 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by junkone, 04-21-2024, 07:17 AM
    10 responses
    149 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Working...
    X