Series<double> upticks, downticks;
private int activeBar = -1;
private double lastPrice = 0;
private int lastDir = 0, up = 0, down = 0;
private EMA emaUp, emaDown;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Calculate = Calculate.OnEachTick;
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;
Length = 4;
AddPlot(new Pen(Brushes.Red, 2), PlotStyle.Bar, "Plot1");
AddLine(Brushes.DarkGray, 1, "Line1");
}
else if (State == State.Configure)
{
upticks = new Series<double>(this);
downticks = new Series<double>(this);
emaUp = EMA(upticks,Length);
emaDown = EMA(downticks,Length);
}
else if (State == State.Historical)
{
activeBar = 0;
}
}
protected override void OnMarketData(MarketDataEventArgs e)
{
try
{
if (CurrentBar < Math.Max(Len,activeBar)) return;
if(e.MarketDataType == MarketDataType.Last)
{
if (lastPrice>0)
{
if (e.Price>lastPrice)
{
up++;
lastDir=1;
}
else if (e.Price<lastPrice)
{
down++;
lastDir=-1;
}
else
{
if (lastDir==1)
up++;
else if (lastDir==-1)
down++;
}
}
upticks[0]=up;
downticks[0]=down;
Plot1[0] = emaUp[0]-emaDown[0];
lastPrice = e.Price;
}
}
catch(Exception ex)
{
Log(ex.ToString(),LogLevel.Error);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < activeBar)
return;
if(CurrentBar != activeBar)
{
up = 0;
down = 0;
activeBar = CurrentBar;
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Tick Replay and EMA
Collapse
X
-
Tick Replay and EMA
I am trying to create an indicator very similar to the BuySellPressure indicator to count UpTicks and DownTicks instead of BidVolume and AskVolume. The code works fine until I try to add an EMA of the UpTicks and DownTicks series I've created. Once I add the EMA, NinjaTrader crashes when I try to run the indicator.
Code:Tags: None
-
I would try moving:
emaUp = EMA(upticks,Length);
emaDown = EMA(downticks,Length);
Into a section State.DataLoaded...
I think upticks & downticks are not valid yet (no data) which means they may be causing the EMA to crash.
I do have to tell you I find debugging in NT8 more difficult as it may be the multitasking framework that seems to hide what is actually happening within the code.
-
I tried that, but I get the same "Unhandled exception" error. I also tried calling the EMA's directly from the OnMarketData() method and got the same error.
Comment
-
My Trace Log also has the following:
2015-06-09 12:18:08:955 *************** unhandled exception trapped ***************
2015-06-09 12:18:08:955 Exception has been thrown by the target of an invocation.
2015-06-09 12:18:08:958 System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at NinjaTrader.NinjaScript.Indicators.MyTickIndicator .get_Zero()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeIn ternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Obj ect obj, Object[] index)
at NinjaTrader.NinjaScript.IndicatorBase.get_DisplayN ame()
at NinjaTrader.NinjaScript.IndicatorBase.OnAfterSetSt ate()
at NinjaTrader.NinjaScript.NinjaScriptBase.SetState(S tate state)
at NinjaTrader.Gui.NinjaScript.IndicatorRenderBase.Se tState(State state)
at NinjaTrader.NinjaScript.NinjaScriptBase.SetState(S tate state)
at NinjaTrader.Gui.NinjaScript.IndicatorRenderBase.Se tState(State state)
at NinjaTrader.Gui.Chart.ChartControl.<>c__DisplayCla ss28e.<RefreshIndicators>b__27c(Object o)
at System.Threading.ExecutionContext.RunInternal(Exec utionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System. Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
Comment
-
Len should be Length. This is just an error in my post, in my code I am using:Originally posted by NJA_MC View PostI see this line:
if (CurrentBar < Math.Max(Len,activeBar)) return;
But I don't see where "Len" is defined. Is this possible less than Length?
if (CurrentBar < Math.Max(Length,activeBar)) return;
Comment
-
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
666 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
376 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
110 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
575 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
580 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment