- The thread that is calling your indicator can change, even for the same call. OnBarUpdate can be called from different threads. I have observed OnBarUpdate first called by different threads when going from Historical to Realtime.
- Dispatcher.CheckAccess() fails for OnBarUpdate most of the time. What exactly is this Dispatcher for?
- OnRender and OnBarUpdate are called from different threads. How does NinjaTrader prevent thread synchronization issues between OnBarUpdate and OnRender?
- OnBarUpdate and TestTriggerCustomEvent execute in the same thread, separate from OnBarUpdate.
If the indicator is connecting to other external events, for example Account.ExecutionUpdate, how can I ensure that the code runs in the correct thread? Initially I thought I could use Dispatcher.InvokeAsync from the indicator but the indicator.OnBarUpdate could be called from a thread other then the one held by that dispatcher. This is assuming I need to access my own member variables and the bar series.
For example
class MyIndicator: Indicator
{
IDispose someDisposableObject = new SomeDisposableObject();
const double someValue = ...;
protected override void void OnBarUpdate()
{
if(Close[0] > someValue)
{
someDisposableObject.Dispose();
someDisposableObject = null;
}
}
protected override void OnRender(ChartControl control, ChartScale scale)
{
if(someDisposableObject != null)
{
someDisposableObject.CallSomeFunction();
}
}
}
Do I need to do thread synchronization to make sure someDisposableObject is not modified by two threads at once?
Here is the code used to determine what threads are executing which functions.
#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;
using System.Runtime.CompilerServices;
using System.Threading;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class ThreadTests : Indicator
{
protected override void OnStateChange()
{
TestCheckAccess();
if (State == State.SetDefaults)
{
Description = @"Tests thread behavior for indicators.";
Name = "ThreadTests";
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;
Calculate = Calculate.OnEachTick;
}
else if (State == State.Configure)
{
}
}
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
TestCheckAccess();
}
protected override void OnBarUpdate()
{
TestCheckAccess();
}
void TestCheckAccess([CallerMemberName] string callingMethod = null)
{
if(Dispatcher != null)
{
var checkAccess = Dispatcher.CheckAccess();
Print(string.Format("Function {0} State = {1} Dispatcher.CheckAccess = {2} ThreadID = {3}", callingMethod, State, checkAccess, Thread.CurrentThread.ManagedThreadId));
}
}
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private ThreadTests[] cacheThreadTests;
public ThreadTests ThreadTests()
{
return ThreadTests(Input);
}
public ThreadTests ThreadTests(ISeries<double> input)
{
if (cacheThreadTests != null)
for (int idx = 0; idx < cacheThreadTests.Length; idx++)
if (cacheThreadTests[idx] != null && cacheThreadTests[idx].EqualsInput(input))
return cacheThreadTests[idx];
return CacheIndicator<ThreadTests>(new ThreadTests(), input, ref cacheThreadTests);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.ThreadTests ThreadTests()
{
return indicator.ThreadTests(Input);
}
public Indicators.ThreadTests ThreadTests(ISeries<double> input )
{
return indicator.ThreadTests(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.ThreadTests ThreadTests()
{
return indicator.ThreadTests(Input);
}
public Indicators.ThreadTests ThreadTests(ISeries<double> input )
{
return indicator.ThreadTests(input);
}
}
}
#endregion

Comment