NexusFi: Find Your Edge


Home Menu

 





Help with a Multi-Instrument Indicator


Discussion in NinjaTrader

Updated
    1. trending_up 4,448 views
    2. thumb_up 7 thanks given
    3. group 2 followers
    1. forum 12 posts
    2. attach_file 4 attachments




 
Search this Thread

Help with a Multi-Instrument Indicator

  #1 (permalink)
ECI Ed
Seminole, FL
 
Posts: 17 since Oct 2011
Thanks Given: 9
Thanks Received: 3

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.

This is what the output window looks like:
plArray[0] = 1553 phArray[0] = 1553.25 tlArray[0] = 1553 thArray[0] = 1553.25
plArray[0] = 1553 phArray[0] = 1553.75 tlArray[0] = 180 thArray[0] = 584
plArray[0] = 1553 phArray[0] = 1553.75 tlArray[0] = 1553 thArray[0] = 1553.75
plArray[0] = 1553.25 phArray[0] = 1554 tlArray[0] = 395 thArray[0] = 638
plArray[0] = 1553.25 phArray[0] = 1554 tlArray[0] = 1553.25 thArray[0] = 1554
plArray[0] = 1553 phArray[0] = 1553.75 tlArray[0] = -34 thArray[0] = 395
plArray[0] = 1553 phArray[0] = 1553.75 tlArray[0] = 1553 thArray[0] = 1553.75
plArray[0] = 1553.5 phArray[0] = 1554 tlArray[0] = 190 thArray[0] = 504
plArray[0] = 1553.5 phArray[0] = 1554 tlArray[0] = 1553.5 thArray[0] = 1554
plArray[0] = 1553.5 phArray[0] = 1554.5 tlArray[0] = 437 thArray[0] = 814
plArray[0] = 1553.5 phArray[0] = 1554.5 tlArray[0] = 1553.5 thArray[0] = 1554.5
plArray[0] = 1554 phArray[0] = 1554.75 tlArray[0] = 645 thArray[0] = 911
plArray[0] = 1554 phArray[0] = 1554.75 tlArray[0] = 1554 thArray[0] = 1554.75

Can someone please take a look at this and help me to understand what is going on?

Thanks

Attached Files
Elite Membership required to download: TickDivSigsES.cs
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Futures True Range Report
The Elite Circle
Exit Strategy
NinjaTrader
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
NexusFi Journal Challenge - April 2024
Feedback and Announcements
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
Battlestations: Show us your trading desks!
26 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
The Program
18 thanks
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


@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[0], but Times[0][0] - which designates the time stamp of a NYSE TICK bar - and Times[1][0] - which calls the time stamp of the current ES 06-13 bar.

Also avoid something like thArray[0] = Low[1]. Please make a decision to either store Lows[0][1] or Lows[1][1].

What do you need those arrays for?

Reply With Quote
Thanked by:
  #4 (permalink)
ECI Ed
Seminole, FL
 
Posts: 17 since Oct 2011
Thanks Given: 9
Thanks Received: 3

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?

Thanks for your help.

Reply With Quote
  #5 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


ECI Ed View 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?

Thanks for your help.


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.

Reply With Quote
  #6 (permalink)
ECI Ed
Seminole, FL
 
Posts: 17 since Oct 2011
Thanks Given: 9
Thanks Received: 3

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?

Reply With Quote
  #7 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


ECI Ed View Post
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?

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.


ECI Ed View Post
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?

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.

Indicator: Using a DataSeries object to store calculations - [AUTOLINK]NinjaTrader[/AUTOLINK] Support Forum
Strategy: Synchronizing a DataSeries object to a secondary time frame - [AUTOLINK]NinjaTrader[/AUTOLINK] Support Forum

Reply With Quote
Thanked by:
  #8 (permalink)
ECI Ed
Seminole, FL
 
Posts: 17 since Oct 2011
Thanks Given: 9
Thanks Received: 3

Fat Tails,

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?

Thanks.

Attached Files
Elite Membership required to download: SMIwDiv.cs
Reply With Quote
  #9 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


ECI Ed View Post
Fat Tails,

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?

Thanks.

There is a bug in lines 89 to 95, as you have initialized the same DataSeries twice and not initialized another one.

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.

Reply With Quote
Thanked by:
  #10 (permalink)
ECI Ed
Seminole, FL
 
Posts: 17 since Oct 2011
Thanks Given: 9
Thanks Received: 3


Fat Tails:
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.

Fat Tails:
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?

TIA

Attached Files
Elite Membership required to download: SMIwDiv.cs
Reply With Quote
Thanked by:




Last Updated on July 31, 2013


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts