NexusFi: Find Your Edge


Home Menu

 





Help!


Discussion in NinjaTrader

Updated
    1. trending_up 2,876 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 2 posts
    2. attach_file 2 attachments




 
Search this Thread
  #1 (permalink)
 
cclsys's Avatar
 cclsys 
Sydney, NS
 
Experience: Intermediate
Platform: Ninja
Broker: Zen-Fire
Trading: TF,S,GC
Posts: 605 since Nov 2009
Thanks Given: 248
Thanks Received: 394

I tried to code an extremely simple thing this afternoon. In TS it would take me about 2 minutes to write. After 30 minutes, I am nowhere.

The idea is simple and something I might put into the FibRatioBand indicator and also was going to offer to Cory to experiment with in Volume Stop, which is actually why I thought of it.

The idea: if the Donchian Range x period is less than the ATR (period) * Multiplier, then the current swing range is too narrow for a volume stop (or in FibBand case, for a band target to be triggered).

Now how simple is that?

I cannot get the code to work the way I want it. I had no problem at all to code in:

ATR value changing color if narrower or wider than Donchian range.

But when I tried to have 2 plots for the Donchian range line, one 1 color if the swing is up and a different color when down, using a simple condition (if the EMA(period)[0] > EMA(period)[1]), although it compiles fine, it gets an error and won't plot.

Err msg: Error on calling the onbar update method ....index was out of range, must be non-negative and less than the size of the parameter name: index. Have no idea what that means.

And no idea what is wrong with my code.

Would be grateful if anyone could explain what it is I am doing wrong here. After one year with Ninja I still find problems like this doing the most simple of things and still cannot figure out why.

+++++++++++++++++++++++

PS. I have worked around the problem by using Slope(...) instead, but I would still appreciate learning why the original won't work, simple as it is. Attached is picture. I think Cory might find this useful for filtering out some of the volume stops in the additional patterns that are being worked on somewhere on the forum. It's not meant so much as an 'indicator' as a condition. I made it first into an indicator to see it, that's all.



+++++++++++++++++++++++

#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.
/// </summary>
[Description("Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.")]
public class ATRDonchian : Indicator
{
#region Variables
// Wizard generated variables
private int drange = 5; // Default setting for Drange
private double aTRMult = 2; // Default setting for ATRMult
private int aTRPeriod = 37; // Default setting for ATRPeriod
private double atrv = 0; // ATR value
private double drangev = 0; // Donchian range value
// User defined variables (add any user defined variables below)
#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()
{
if (CurrentBar < ATRPeriod *2) return;

Add(new Plot(Color.FromKnownColor(KnownColor.Fuchsia), PlotStyle.Bar, "ATRnarrow"));
Add(new Plot(Color.FromKnownColor(KnownColor.LimeGreen), PlotStyle.Bar, "ATRwide"));
Add(new Plot(Color.FromKnownColor(KnownColor.DarkTurquoise), PlotStyle.Line, "Ddown"));
Add(new Plot(Color.FromKnownColor(KnownColor.Goldenrod), PlotStyle.Line, "Dup"));
CalculateOnBarClose = false;
Overlay = false;
PriceTypeSupported = false;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
atrv = ATR(ATRPeriod)[0]*ATRMult;
drangev = DonchianChannel(drange).Upper[0] - DonchianChannel(drange).Lower[0];

if ( drangev <= atrv)
{ATRnarrow.Set(atrv);}
else ATRwide.Set(atrv);

//if (EMA(drange)[0] < EMA(drange)[1])

Ddown.Set(drangev);
//else
//Dup.Set(drangev);
}

#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries ATRnarrow
{
get { return Values[0]; }
}

[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries ATRwide
{
get { return Values[1]; }
}

[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Ddown
{
get { return Values[2]; }
}

[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Dup
{
get { return Values[3]; }
}

[Description("Donchian Period")]
[Category("Parameters")]
public int Drange
{
get { return drange; }
set { drange = Math.Max(1, value); }
}

[Description("ATR Multiplier")]
[Category("Parameters")]
public double ATRMult
{
get { return aTRMult; }
set { aTRMult = Math.Max(.1, value); }
}

[Description("Default ATR Period")]
[Category("Parameters")]
public int ATRPeriod
{
get { return aTRPeriod; }
set { aTRPeriod = Math.Max(1, 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 ATRDonchian[] cacheATRDonchian = null;

private static ATRDonchian checkATRDonchian = new ATRDonchian();

/// <summary>
/// Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.
/// </summary>
/// <returns></returns>
public ATRDonchian ATRDonchian(double aTRMult, int aTRPeriod, int drange)
{
return ATRDonchian(Input, aTRMult, aTRPeriod, drange);
}

/// <summary>
/// Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.
/// </summary>
/// <returns></returns>
public ATRDonchian ATRDonchian(Data.IDataSeries input, double aTRMult, int aTRPeriod, int drange)
{
checkATRDonchian.ATRMult = aTRMult;
aTRMult = checkATRDonchian.ATRMult;
checkATRDonchian.ATRPeriod = aTRPeriod;
aTRPeriod = checkATRDonchian.ATRPeriod;
checkATRDonchian.Drange = drange;
drange = checkATRDonchian.Drange;

if (cacheATRDonchian != null)
for (int idx = 0; idx < cacheATRDonchian.Length; idx++)
if (Math.Abs(cacheATRDonchian[idx].ATRMult - aTRMult) <= double.Epsilon && cacheATRDonchian[idx].ATRPeriod == aTRPeriod && cacheATRDonchian[idx].Drange == drange && cacheATRDonchian[idx].EqualsInput(input))
return cacheATRDonchian[idx];

ATRDonchian indicator = new ATRDonchian();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
indicator.Input = input;
indicator.ATRMult = aTRMult;
indicator.ATRPeriod = aTRPeriod;
indicator.Drange = drange;
indicator.SetUp();

ATRDonchian[] tmp = new ATRDonchian[cacheATRDonchian == null ? 1 : cacheATRDonchian.Length + 1];
if (cacheATRDonchian != null)
cacheATRDonchian.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheATRDonchian = 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
{
/// <summary>
/// Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.ATRDonchian ATRDonchian(double aTRMult, int aTRPeriod, int drange)
{
return _indicator.ATRDonchian(Input, aTRMult, aTRPeriod, drange);
}

/// <summary>
/// Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.
/// </summary>
/// <returns></returns>
public Indicator.ATRDonchian ATRDonchian(Data.IDataSeries input, double aTRMult, int aTRPeriod, int drange)
{
return _indicator.ATRDonchian(input, aTRMult, aTRPeriod, drange);
}

}
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.ATRDonchian ATRDonchian(double aTRMult, int aTRPeriod, int drange)
{
return _indicator.ATRDonchian(Input, aTRMult, aTRPeriod, drange);
}

/// <summary>
/// Showing both ATR*multiplier and Donchian H-L range to compare. Can be used as condition for triggers in other methods.
/// </summary>
/// <returns></returns>
public Indicator.ATRDonchian ATRDonchian(Data.IDataSeries input, double aTRMult, int aTRPeriod, int drange)
{
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.ATRDonchian(input, aTRMult, aTRPeriod, drange);
}

}
}
#endregion


Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Saylors 41-Month HODL Breaks: Strategy Sells 32 BTC as $ …
Prediction Markets & Event Contracts
Irans Answer Due Today: Peace Surges to 33.5%, Invasion …
Prediction Markets & Event Contracts
April 2026 Jobs Report: +115k vs +65k Expected
Traders Hideout
El Clasico Draws $9.2M in Prediction Market Action -- Bi …
Prediction Markets & Event Contracts
$4.5M Floods Russia Nuclear Contract in 24 Hours -- Krem …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
8 thanks
Darmok and Jalad at Tanagra
1 thanks
  #2 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,758 since Jun 2009
Thanks Given: 3,828
Thanks Received: 4,644

Try this one (bad idea in initialize ).
Bad:
 
Code
                            
protected override void Initialize()
{
    if (
CurrentBar ATRPeriod *2) return; 
Better:
 
Code
                            
protected override void OnBarUpdate()
{
if (
CurrentBar ATRPeriod *2
    return; 


Success requires no deodorant! (Sun Tzu)
Attached Files
Elite Membership required to download: ATRDonchian.zip
Reply With Quote
  #3 (permalink)
 
cclsys's Avatar
 cclsys 
Sydney, NS
 
Experience: Intermediate
Platform: Ninja
Broker: Zen-Fire
Trading: TF,S,GC
Posts: 605 since Nov 2009
Thanks Given: 248
Thanks Received: 394


Sam thanks. I figured that out this morning. Am in a Ninja script slump right now. Everything I try to do is bass ackwards. I think it's because I was working with a little TS script over the weekend. Scrambled me.


Visit my NexusFi Trade Journal Started this thread Reply With Quote




Last Updated on November 24, 2009


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts