Thanks,
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Take in two indicators as inputs to one indicator.
Collapse
X
-
Take in two indicators as inputs to one indicator.
I am still learning how to code in NT so pardon my elementary questions. How can I take in two averages (indicators each with their own configurable tree underneath) as input into one indicator I will build and work with those two averages(indicators) in OnBarUpdate() method?
Thanks,Tags: None
-
Hello rocketstock,
Thanks for your post.
There are two ways, one of which may be easier but at a resource cost and the other is slightly more work to code but is significantly a better practice to follow.
The easy way, for example using two EMAs. You can call the indicator method directly by providing the parameters, in OnBarUpdate().
In the OnBarUpdate()
{
if (EMA(8)[0] > EMA(20)[0] && EMA(8)[1] < EMA(20)[1])
{
// crossover, do something
}
A better way, create local indicators:
At the class level:
private EMA myEMA8, myEMA20;
In OnStateChange(), in the State.DataLoaded (or State.Historical)
myEMA8 = EMA(8); // initialize
myEMA20 = EMA(20); // initialize
In OnBarUpdate()
{
if (myEMA8[0] > myEMA20[0] && myEMA8[1] < myEMA20[1])
{
// crossover, do something
}
The above is a very very simple example and becomes easier to use when you have an indicator that has several parameters.
References: See "Referencing indicator methods" here: https://ninjatrader.com/support/help...tm#Performance
-
Thank you Paul. Love the second option. Will try it. I do want the option on GUI screen to select two completely different indicators so this would be great.
Comment
-
Hello rocketstock,
Thanks for your reply.
I may have misunderstood your intentions.
The advice I provided would be for "using" indicators in another script where they are hardcoded in.
This would not expose or provide these indicators in the User interface. You can certainly create parameters to populate those indicators in the user interface, such as the Period for each of the EMAs in the example.
Comment
-
Hi Paul,
Working on this and not able to consume completely different indicators. Not able to create an option on this screen. As an example on screenshot, instead of currentPeriod and past Period, I'd like to take in two indicators. Example like RSI and OBV and get their current value to use in my custom formula in OnBar method.
So far I have this. Tried other things in comments before.
public class TwoIndicator : Indicator
{
//private Series<double> firstIndicator;
//private Series<double> secondIndicator;
private double constant;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"TwoIndicator";
Name = "TwoIndicator";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
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;
//Period = 14;
currentPeriod = 13;
pastPeriod = 14;
UpColor = Brushes.LimeGreen;
DnColor = Brushes.Red;
ColorSlope = true;
AddPlot(Brushes.Orange, "Slope_Color");
//AddLine(Brushes.White, 0, "ZeroLine");
//AddPlot(new Stroke(Brushes.Red, DashStyleHelper.Dash, 0), PlotStyle.Line, "ZeroLine");
AddLine(new Stroke(Brushes. Gray, DashStyleHelper.Dash, 0), 0, " ZeroLine");
}
else if (State == State.Configure)
{
constant= 1;
}
else if (State == State.DataLoaded)
{
//firstIndicator = new Series<double>(this);
//secondIndicator = new Series<double>(this);
}
}
protected override void OnBarUpdate()
{
//Add your custom indicator logic here.
double input0 = Input[0];
if (CurrentBar == 0)
{
//firstIndicator[0] = input0;
//secondIndicator[0] = input0;
//Value[0] = 0;
//Avg[0] = 0;
//Diff[0] = 0;
}
else
{
//double firstIndicator0 = constant * firstIndicator0[1];
//double secondIndicator0 = constant * secondIndicator[1];
//double doSomething = doSomething
//firstIndicator[0] = firstIndicator0;
//secondIndicator[0] = secondIndicator0;
Value[0] = doSomething;
//Avg[0] = macdAvg;
//Diff[0] = macd - macdAvg;
}
}
#region Properties
//[NinjaScriptProperty]
//[Range(1, int.MaxValue)]
//[Display(Name="Period", Description="Number of bars to include in the calculation", Order=1, GroupName="Parameters")]
//public int Period
//{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> firstIndicator
{
get { return Values[1]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> secondIndicator
{
get { return Values[2]; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="currentPeriod", Description="Number of current bars to include in the calculation", Order=1, GroupName="Parameters")]
public int currentPeriod
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="pastPeriod", Description="Number of past bars to include in the calculation", Order=1, GroupName="Parameters")]
public int pastPeriod
{ get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name="Up Color", Description="Color for rising condition", Order=3, GroupName="Parameters")]
public Brush UpColor
{ get; set; }
[Browsable(false)]
public string UpColorSerializable
{
get { return Serialize.BrushToString(UpColor); }
set { UpColor = Serialize.StringToBrush(value); }
}
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name="Down Color", Description="Color for falling condition", Order=4, GroupName="Parameters")]
public Brush DnColor
{ get; set; }
[Browsable(false)]
public string DnColorSerializable
{
get { return Serialize.BrushToString(DnColor); }
set { DnColor = Serialize.StringToBrush(value); }
}
[NinjaScriptProperty]
[Display(Name="Enable Slope Color?", Description="Color the plot?", Order=2, GroupName="Parameters")]
public bool ColorSlope
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> EMA_Slope_Color
{
get { return Values[0]; }
}
#endregion
}
}
Comment
-
Hello rocketstock,
Thanks for your reply.
What you want to do would not be possible in a supported means in Ninjascript. It may be possible through Reflection but this would be unsupported and I could give no further advice on that subject.
I think the closest you are going to get would be to use enums to select the indicator and use a type converter to expose/hide the appropriate parameters. I'll link you to an example of that.
This indicator is publicly available on our NinjaTrader Ecosystem website:Here is a basic guideline of how to import NinjaScript add-ons in NinjaTrader 8:
Note — To import NinjaScripts you will need the original .zip file.
To Import:- Download the NinjaScripts to your desktop, keep them in the compressed .zip file.
- From the Control Center window select the menu Tools > Import > NinjaScript Add-on...
- Select the downloaded .zip file
- NinjaTrader will then confirm if the import has been successful.
Critical - Specifically for some NinjaScripts, it will prompt that you are running newer versions of @SMA, @EMA, etc. and ask if you want to replace, press 'No'
Once installed, you may add the indicator to a chart by:- Right-click your chart > Indicators... > Select the Indicator from the 'Available' list on the left > Add > OK
Comment
-
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
577 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
334 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
553 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
551 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment