(Note: All questions are unrelated to coding for graphics or UI objects. I'm considering only calculations and ISeries objects.)
An obvious scenario for taking care would be file I/O, but what about having indicator1's accessing indicator2? Certainly that's too broad a question, so here is an example.
Let's say I start with a basic indicator such as BuySellVolume. Now, I've seen at least one similar example where a lock was placed on the MarketDepthEventArgs object (something like "lock(e.Instrument.MarketDepth.Asks)", etc.). Why place a lock within OnMarketDepth and not OnMarketData? The Multi-Threading documentation mentions that market data is not thread safe such that use of the Dispatcher is recommended, but then none of the built-in indicators accessing market data use the Dispatcher. What am I missing, here?
Anyway, if I create another indicator that creates a BuySellVolume object or some other indicator object and pulls values from it, would the following ever be of any use as a field declaration?
[ThreadStatic] private static BuySellVolume _bsv;
Perhaps relatedly, why do some NT 8 indicators call Update() in the getters of Values series and others do not? Is there a reason to not always add an Update call to every getter?
Can it be explained to what extent multi-threading should be considered, or rather, what are the weaknesses that I need to consider? I mention Values series (e.g. those created from AddPlot( ...args... )) because those series would more obviously create conflicts considering the potentially synchronous reads and writes. What about a custom Series object? I see those are created using the ISeries interface, so are locks for them similarly unnecessary?
Is multi-threading an important consideration for only non-State methods? Do you use locks? Monitors? Tasks?
Are there any examples where one's simply calling another indicator's Values series will cause a multi-threading conflict even if the other indicator runs calculations in only OnBarUpdate? With OnEachTick? OnBarClose? IsFirstTickOfBar?
I apologize for cramming so many questions in there. I understand this is a very broad topic, but I feel the NT8 documentation is either too light on the subject or doesn't clearly explain the scope of the problem (e.g. it's for mainly rendering considerations and most everybody else can stop reading). If anybody has examples or even some good reading material, I'd greatly appreciate it. I'm fairly certain I've had threading bugs on even basic indicators (no fancy drawing, no rendering, just Values[0] = 1 + 1 kind of stuff) being called from other indicators before I learned about multi-threading, but then there doesn't seem to be any relevant NT8 documentation for basic NinjaScript coding. The more I learn about this, though, the more I think lock is not the right tool for the jobs I'm doing.
Many thanks in advance.
Best,
Caleb

Comment