Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 09-22-2017, 11:33 AM   #1
chartish
Member
 
Join Date: Jul 2012
Posts: 37
Thanks: 2
Thanked 4 times in 3 posts
Default MultiInstruments in Indicator

I am trying to create a calculation based on the current instrument and an ETF. I add the series for the etf like this:

else if (State == State.Configure)
{
AddDataSeries("QQQ", Data.BarsPeriodType.Day, 500);
}
Since I have been getting errors I decided to just print out both closing prices like this:

protected override void OnBarUpdate()
{
if (CurrentBar < 252)
return;

Print("QQQ: " + Closes[1][0] + " " + "AAPL: " + Closes[0][0]);
}

I get the following error:

Indicator 'x': Error on calling 'OnBarUpdate' method on bar 252: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

I have plenty of data for the etf AND the current instrument. Both dataseries are set to 500 bars.

Dana
chartish is offline  
Reply With Quote
Old 09-22-2017, 11:39 AM   #2
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

Hello,

Thank you for the question.

Depending on the granularity of the series you use, this can come up. It would be suggested to use a current bar check for both series like the following:

if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return;

Could you try adding this instead of the CurrentBar check you have now and see if this resolves the error?

I look forward to being of further assistance.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-22-2017, 11:58 AM   #3
chartish
Member
 
Join Date: Jul 2012
Posts: 37
Thanks: 2
Thanked 4 times in 3 posts
Default

Here is what I changed:

Print("QQQ: " +CurrentBars[1] + " " + "AAPL: " + CurrentBars[0]);

if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return;

Print("QQQ: " + Closes[1][0] + " " + "AAPL: " + Closes[0][0]);

This is the result:

QQQ: -1 AAPL: 0
QQQ: -1 AAPL: 1
QQQ: -1 AAPL: 2
QQQ: -1 AAPL: 3
QQQ: -1 AAPL: 4
QQQ: -1 AAPL: 5
QQQ: -1 AAPL: 6
QQQ: -1 AAPL: 7
QQQ: -1 AAPL: 8
etc

Dana
chartish is offline  
Reply With Quote
Old 09-22-2017, 12:01 PM   #4
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

Hello,

I wanted to check, have you added the syntax I provided in at the top of OnBarUpdate and confirmed that has solved the problem?

Code:
if (CurrentBars[0] < 1 || CurrentBars[1] < 1) return;
I see the prints are printing, is the logic following this check being stopped so the error is no longer present?

I look forward to being of further assistance.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-22-2017, 12:03 PM   #5
chartish
Member
 
Join Date: Jul 2012
Posts: 37
Thanks: 2
Thanked 4 times in 3 posts
Default

The error is gone. It seems that AddDataSeries did not work as expected.
chartish is offline  
Reply With Quote
Old 09-22-2017, 02:54 PM   #6
chartish
Member
 
Join Date: Jul 2012
Posts: 37
Thanks: 2
Thanked 4 times in 3 posts
Default

It turns out there are two issues - I understood that AddDataSeries("QQQ", Data.BarsPeriodType.Day, 500); meant to load 500 bars of QQQ. Instead, the 500 was the period. I changed it to 1 and now I get data. However, in OnBarUpdate, the CurrentBars get out of sync after about bar 250. If this is the correct behavior, then I will have to keep my own bar array for each instrument. Any suggestions?

QQQ 244 AAPL 244
QQQ 244 AAPL 244
QQQ 245 AAPL 245
QQQ 245 AAPL 245
QQQ 246 AAPL 246
QQQ 246 AAPL 246
QQQ 247 AAPL 247
QQQ 247 AAPL 247
QQQ 248 AAPL 248
QQQ 248 AAPL 248
QQQ 249 AAPL 249
QQQ 249 AAPL 249
QQQ 250 AAPL 250
QQQ 250 AAPL 250
QQQ 251 AAPL 251
QQQ 251 AAPL 251
QQQ 252 AAPL 251
QQQ 253 AAPL 251
QQQ 254 AAPL 251
QQQ 255 AAPL 252
QQQ 255 AAPL 252
QQQ 256 AAPL 253
QQQ 256 AAPL 253
chartish is offline  
Reply With Quote
Old 09-22-2017, 03:07 PM   #7
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

Hello,

Thank you for the reply.

Can you try to open a chart for both the QQQ and AAPL, Reload the historical data for each, and then re-run the script? If that still has the same problem, please try the following print:

Code:
Print("QQQ: " + CurrentBars[0] + " Time: " + Times[0][0] + "AAPL: " + CurrentBars[1] + " Time: " + Times[1][0]);
If you also print the Times for these bars and then view a chart, is there data for the time that is missing or out of sync displayed on the chart? I could expect differences between two different instruments in regard to the data available but because these are daily bars and both instruments have the same session I would expect them to match closely.

I look forward to being of further assistance.
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-22-2017, 03:39 PM   #8
chartish
Member
 
Join Date: Jul 2012
Posts: 37
Thanks: 2
Thanked 4 times in 3 posts
Default

You are right. AAPL is missing the days of 12/29/2015 - 12/31/2015, 3/3/2017 and 3/10/2017. The data feed is wrong. I have verified these days exist in another charting package. I used Kinetick EOD for the data.
Last edited by chartish; 09-22-2017 at 03:46 PM.
chartish is offline  
Reply With Quote
Old 09-25-2017, 07:50 AM   #9
NinjaTrader_Jesse
NinjaTrader Customer Service
 
NinjaTrader_Jesse's Avatar
 
Join Date: Mar 2014
Location: Denver, CO
Posts: 4,068
Thanks: 28
Thanked 1,053 times in 985 posts
Default

Hello,

Thank you for the reply.

I tried this morning for the dates you mentioned but I see them populating for me. I attached an image of the historical data manager data.

Could you try the following to see if this is relating to caching?
  • Exit NinjaTrader
  • Open the folder: Documents\NinjaTrader 8\db
  • Delete the folder "cache" and only the cache folder.
  • Start NinjaTrader
  • In a chart for QQQ, right click and choose Reload historical data. Ensure the timeframe is included in the Days to load.
  • In a second chart for AAPL, right click and choose Reload historical data. Ensure the timeframe is included in the Days to load.

After doing this, could you use the historical data manager to verify that the days are present like the image from above?

I look forward to being of further assistance.
Attached Images
File Type: png NinjaTrader_2017-09-25_07-44-40.png (37.5 KB, 3 views)
File Type: png NinjaTrader_2017-09-25_07-50-21.png (40.3 KB, 0 views)
NinjaTrader_Jesse is offline  
Reply With Quote
Old 09-25-2017, 08:54 AM   #10
chartish
Member
 
Join Date: Jul 2012
Posts: 37
Thanks: 2
Thanked 4 times in 3 posts
Default

I actually deleted the whole year of historical data and downloaded it again and it works now. Don't know how the data got corrupted.

Dana
chartish is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Indicator list & Indicator return Values as a input parameters to an indicator Shai Samuel Indicator Development 3 09-13-2015 01:22 PM
OnMarketData & Multiinstruments Ramon General Development 8 03-01-2012 01:27 PM


All times are GMT -6. The time now is 03:31 AM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.