I have written several NinjaScript indicators, but this is the first time I've tried to write one that requires multi-instruments and arrays. I have attached the code. I added a print statement to verify that the data was being accessed correctly and got results that perplex me. It appears that its loading the correct data for the TICK on every other bar and ES data (incorrect data) on the opposite bars.
@ECI Ed: I think that you need to examine the BarsInProgress property. It is important to know, whether the bars that are currently processed are NYSE TICK bars or ES 06-13 bars. NinjaTrader processes the primary bar series (NYSE TICK) first and then the secondary bar series, if the bars have identical time stamps.
The way you have written the indicator, the entire code will be processed for each incoming tick of each of the two bar series. For example "FirstTickOfBar" can mean first tick of an ES 06-13 minute bar, or first tick of a NYSE TICK bar.
Please divide the OnBarUpdate() code section into one piece which is executed when BarsInProgress == 0 (primary NYSE TICK bars are being called) and another code section which is executed when BarsInProgress == 1 (secondary ES 06-13 bars are being called).
Also for better understanding you should not use Time, but Times - which designates the time stamp of a NYSE TICK bar - and Times - which calls the time stamp of the current ES 06-13 bar.
Also avoid something like thArray = Low. Please make a decision to either store Lows or Lows.
What do you need those arrays for?
The following 2 users say Thank You to Fat Tails for this post:
Fat Tails wrote:
What do you need those arrays for?
That is a good question. This indicator is a conversion from esignal code, and in esignal I found that by storing data in an Array once then pulling the data out of the array instead of using High and Low, which required a response from the server, allowed the indicator to run faster, putting less stress on the processor.
In Ninjascript however, though I have found the documentation to be very limited and at times confusing, it looks like the DataSeries is essentially the same as an array. I assume that is what you were referring to. Is the DataSeries stored localy like an array? Is the same true of High and Low or do these variables require a response from the server, like in esignal? Any references you could provide me with to better understand DataSeries would be greatly appreciated.
As I understand the DataSeries method however, it only applies to data where there is a corresponding value for each bar, and the Pivot arrays do not, therefore I need to use an Array, don't I?
I'm currently trying to use the same divergence code on 24 hour instruments, and something is causing the code to stop processing after the array is full, Could this be the way I'm copying the array? or something else? And if it is, can you suggest a way to get around this?
NinjaTrader is not a front end connected to any backend that is running on a server. It is a stand-alone application. All data can be downloaded to the historical data base and will be stored as daily data, minute data or tick data in Ninja tickfile format.
To download historical data, you can either use the historical data manager or simply open a chart when connected. NinjaTrader will then complement existing historical data by downloading missing data.
The DataSeries object can be considered as an array that can hold one value of the type double for each bar on your chart. The index 0 is used to access the value for the last bar, the highest index in the DataSeries object lets you access the value of the first bar on your chart.
When an indicator is set to CalculateOnBarClose = true, the last bar indexed with 0 is the last complete bar. With CalculateOnBarClose = false, the index 0 will collect the unstable value from the current bar in progress, when connected.
You make some good points, and your basic description of the process would be useful in the DataSeries section of the Ninjascript manual. I lost sight of how the programs handle the data. But the problem with many applicatons that store data locally is that they have to read and write from the hard drive instead of directly from memory. I assume that the DataSeries would be stored in memory for faster access, right?
Though you expanded on the basics nicely, what has been my struggle is understanding the syntax. I couldn't find very many samples of DataSeries syntax, expecially with multi-instrument applications. I don't want to put you on the spot or be too demanding, but perhaps you could demonstrate how to change my posted code using arrays into code using DataSeries?
When a chart is opened the first time, the chart is built from historical data loaded from the historical data base. For example for a 15 min chart the single bars will be assembled from 1-minute data by using the selected session template. This takes some time. NinjaTrader stores the assembled bars in the directory Documents -> NinjaTrader 7 -> db -> cache. When you open that same chart a second time, NinjaTrader does not need to do the assembling but loads the ready bars from this directory.
When the chart is open, all data including the price data and the DataSeries values of the indicators is loaded into RAM, such that they can be quickly accessed. Indicator values are not stored on the hard disk, but are recalculated from price data each time that a chart is opened. The only thing that can be saved as a template are the indicator settings.
I think you simply need to read through the help files and the samples shown on the NinjaTrader forum. For a multi-timeframe series it is necessary to understand how the bars are being processed by OnBarUpdate(). Below are some references.
Glad you confirmed the data is held in RAM, but I'm still struggling to understand the proper syntax for DataSeries. Attached is my latest attempt to implement a Data Series and although it compiles without error, when I run it I get:
Error on calling 'OnBarUpdata' method for indicator 'SMIwDiv' on bar 6209. Object reference not set to an instance of object.
Can you help me with what I'm doing wrong here?
There is a bug in lines 89 to 95, as you have initialized the same DataSeries twice and not initialized another one.
Big DUH on my part, thank you for pointing that out.
Further, I do not understand, why you use plArray, phArray and indArray. You can access Low, High and smis in the same way, no need to create new objects with identical values.
Good point, but I'm going to keep the indArray for portability.
Now as I edge closer to the solution when I add the necessary commands to make the arrays work properly, the editor comes up with numerous errors on the Data Series syntax that weren't problems before. Can you please help me figure out why this is happening, and how to fix it?
The following user says Thank You to ECI Ed for this post: