Help with code error - futures io
futures io



Help with code error


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one kashter with 5 posts (0 thanks)
    2. looks_two MooreTech with 3 posts (2 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 raffu with 1 posts (0 thanks)
    1. trending_up 5,968 views
    2. thumb_up 2 thanks given
    3. group 5 followers
    1. forum 10 posts
    2. attach_file 1 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Help with code error

(login for full post details)
  #1 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
 
Posts: 82 since May 2010
Thanks: 90 given, 44 received

Hi,
I have modified the PriorDayOHLC code slightly to suit me needs. The problem is the script works sometimes and in most times it throws the following error:

Quoting 
Error on calling 'OnBarUpdate' method for indicator 'AAPriorDayOHLC' on bar 1084: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

I am at my limited wits end. Anyone know what I need to do to get this thing going?

Cheers,
Kash

Attached Files
Register to download File Type: cs AAPriorDayOHLC.cs (12.6 KB, 23 views)
Started this thread Reply With Quote

Journal Challenge April 2021 results (now extended!):
Competing for $1800 in prizes from Jigsaw
looks_oneMaking a Living with the Microsby sstheo
(74 thanks from 10 posts)
looks_twoSalao's Journalby Salao
(23 thanks from 5 posts)
looks_3Deetee’s DAX Trading Journal (time based)by Deetee
(15 thanks from 7 posts)
looks_4Learning to Profit - A journey in algorithms and optionsby Syntax
(12 thanks from 7 posts)
looks_5Becoming a day trader (PA, volume profile, footprint etc.)by JirkaF
(0 thanks from 2 posts)
 
Best Threads (Most Thanked)
in the last 7 days on futures io
The Crude Dude Oil Trading System
72 thanks
futures io site changelog and issues/problem reporting
68 thanks
Would You Sell Your System?
65 thanks
FIO Journal Challenge - April 2021 w/Jigsaw Trading
30 thanks
Big Mike in Ecuador
25 thanks
 
(login for full post details)
  #3 (permalink)
 gever17 
Cheyenne,Wyoming...USA
 
Experience: Beginner
Platform: NinjaTrader
Broker: Mirus Futures/Zen-Fire
Trading: NQ, YM
 
Posts: 69 since Nov 2010
Thanks: 329 given, 54 received


Hi Kash,
Recently had this happen to me also. I only had 2 days loaded on the chart. As soon as I switched it to 10 days all was well.
Hope it is this simple for your case also!

gever17

Reply With Quote
 
(login for full post details)
  #4 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
 
Posts: 82 since May 2010
Thanks: 90 given, 44 received

Hi gever,
Tried your suggestion and loaded more than 15, 25 and 50 days, all still give error. Unfortunately it is more than that. Thanks

Kash


gever17 View Post
Hi Kash,
Recently had this happen to me also. I only had 2 days loaded on the chart. As soon as I switched it to 10 days all was well.
Hope it is this simple for your case also!

gever17


Started this thread Reply With Quote
 
(login for full post details)
  #5 (permalink)
 raffu 
Espoo, Finland
 
 
Posts: 11 since Dec 2010


kashter View Post
Hi,
I have modified the PriorDayOHLC code slightly to suit me needs. The problem is the script works sometimes and in most times it throws the following error:


I am at my limited wits end. Anyone know what I need to do to get this thing going?

Cheers,
Kash

Or have you checked "Maximum bars look bac" or "Min. bars required"

Sometimes those might help?

Reply With Quote
 
(login for full post details)
  #6 (permalink)
 MooreTech 
Orlando, Florida
 
Experience: Advanced
Platform: NinjaTrader, TradeStation, MultiCharts, eSignal, MetaTrader
Trading: ES
 
Posts: 57 since Aug 2010
Thanks: 6 given, 73 received

My guess is that the error is in the following line of code.

 
Code
// Calculate the bars prior intraday close value for the 1515 hrs bar
int barsAgo = CurrentBar - Bars.GetBar(new DateTime(Time[0].Year, Time[0].Month, Time[0].Day-1, CloseHour, CloseMinute, 59));
If the date is March 1st, Time[0].Day-1 will return a value of 0. You need to use Time[0].Subtract(new TimeSpan(x,x,x,x)).Day. Also, you can use try/catch blocks to verify that this is indeed the issue.

Follow me on Twitter Reply With Quote
 
(login for full post details)
  #7 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
 
Posts: 82 since May 2010
Thanks: 90 given, 44 received

Hi raffu & Moore,
I was stepping through the whole code line by line to make sure all is logical when I fumbled at the same line of code as you Moore have highlighted! So on further 'research' and pushing the boundaries of my c# coding abilities, I realized that "Time[0].Day-1" was the culprit. It works ok when today is Wednesday to Friday but fails when it is Monday or Tuesday, as I suspect it cannot gather values for Sunday or Saturday as my chart session templates are Mon-Fri.

So to cut a long story short, I added the following snippet to the script:
 
Code
DateTime day = DateTime.Now;
					
					//Amend days back based on present day
					if (day.DayOfWeek.ToString() == "Monday") 
						n = 3; //go to Friday
					else
						n = 1; // go to yesterday

int barsAgo = CurrentBar - Bars.GetBar(new DateTime(Time[0].Year, Time[0].Month, Time[0].Day-n, CloseHour, CloseMinute, 59));
Now the code above will substitute the right number of day/s to subtract depending on the today. If today is Monday, then code will skip Sun & Sat and use Friday values.

Unfortunately it still gives the same error!!! Not sure what more there is to it. Hope someone can help out.

Cheers,
Kash


MooreTech View Post
My guess is that the error is in the following line of code.

 
Code
// Calculate the bars prior intraday close value for the 1515 hrs bar
int barsAgo = CurrentBar - Bars.GetBar(new DateTime(Time[0].Year, Time[0].Month, Time[0].Day-1, CloseHour, CloseMinute, 59));
If the date is March 1st, Time[0].Day-1 will return a value of 0. You need to use Time[0].Subtract(new TimeSpan(x,x,x,x)).Day. Also, you can use try/catch blocks to verify that this is indeed the issue.


Started this thread Reply With Quote
 
(login for full post details)
  #8 (permalink)
 MooreTech 
Orlando, Florida
 
Experience: Advanced
Platform: NinjaTrader, TradeStation, MultiCharts, eSignal, MetaTrader
Trading: ES
 
Posts: 57 since Aug 2010
Thanks: 6 given, 73 received

The problem is still with the same line of code. Time[0].Day returns a value between 1 and 31 indicating the day of the month. If the day of the month is the first, Time[0].Day-n will return a 0 or negative value, which is the cause of your error. To get the proper value, you need to use Time[0].Subtract(new TimeSpan(n,0,0,0)).Day. Additionally, you will likely need to make similar changes to the month and year values.

Follow me on Twitter Reply With Quote
The following user says Thank You to MooreTech for this post:
 
(login for full post details)
  #9 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
 
Posts: 82 since May 2010
Thanks: 90 given, 44 received

Moore,
Thanks for the reply, will try out the fix during the lull hours.

Cheers.

Started this thread Reply With Quote
 
(login for full post details)
  #10 (permalink)
 MooreTech 
Orlando, Florida
 
Experience: Advanced
Platform: NinjaTrader, TradeStation, MultiCharts, eSignal, MetaTrader
Trading: ES
 
Posts: 57 since Aug 2010
Thanks: 6 given, 73 received


A little snippet to help get you started. I didn't test this code, but it should give you the idea.

 
Code
TimeSpan ts = new TimeSpan(1,0,0,0);
DateTime prevTime = Time[0].Subtract(ts);
int barsAgo = CurrentBar - Bars.GetBar(prevTime.Year,prevTime.Month,prevTime.Day,CloseHour,CloseMinute,59);

Follow me on Twitter Reply With Quote
The following user says Thank You to MooreTech for this post:
 
(login for full post details)
  #11 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
 
Posts: 82 since May 2010
Thanks: 90 given, 44 received

Moore,
The snippet did the trick.

I tested my original code further and seems to work with min chart type in both 24/5 & instrument session templates.
When same script was tested on volume chart, only 24/5 session template gave the error. Not sure why one works and the other doesn't?

Anyway I got what I needed. Thank you.

Cheers.

Started this thread Reply With Quote


futures io Trading Community Platforms and Indicators NinjaTrader > Help with code error


Last Updated on March 8, 2011


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Journal Challenge w/$1,800 in prizes!

April
 

Seven Trading Mistakes Solved With Smart Trading Tools w/Brannigan Barrett

Elite only
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts