NexusFi: Find Your Edge


Home Menu

 





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 7,579 views
    2. thumb_up 2 thanks given
    3. group 5 followers
    1. forum 10 posts
    2. attach_file 1 attachments




 
Search this Thread

Help with code error

  #1 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
Posts: 82 since May 2010
Thanks Given: 90
Thanks Received: 44

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
Elite Membership required to download: AAPriorDayOHLC.cs
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Request for MACD with option to use different MAs for fa …
NinjaTrader
NexusFi Journal Challenge - April 2024
Feedback and Announcements
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
66 thanks
NexusFi site changelog and issues/problem reporting
48 thanks
Battlestations: Show us your trading desks!
37 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
31 thanks

  #3 (permalink)
 gever17 
Cheyenne,Wyoming...USA
 
Experience: Beginner
Platform: NinjaTrader
Broker: Mirus Futures/Zen-Fire
Trading: NQ, YM
Posts: 69 since Nov 2010
Thanks Given: 329
Thanks Received: 54


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
  #4 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
Posts: 82 since May 2010
Thanks Given: 90
Thanks Received: 44

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
  #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
  #6 (permalink)
 MooreTech 
Orlando, Florida
 
Experience: Advanced
Platform: NinjaTrader, TradeStation, MultiCharts, eSignal, MetaTrader
Trading: ES
Posts: 57 since Aug 2010
Thanks Given: 6
Thanks Received: 73

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
  #7 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
Posts: 82 since May 2010
Thanks Given: 90
Thanks Received: 44

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
  #8 (permalink)
 MooreTech 
Orlando, Florida
 
Experience: Advanced
Platform: NinjaTrader, TradeStation, MultiCharts, eSignal, MetaTrader
Trading: ES
Posts: 57 since Aug 2010
Thanks Given: 6
Thanks Received: 73

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:
  #9 (permalink)
 kashter 
Moving
 
Experience: Intermediate
Platform: NT
Broker: Mirus/Zen Fire
Trading: ES
Posts: 82 since May 2010
Thanks Given: 90
Thanks Received: 44

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

Cheers.

Started this thread Reply With Quote
  #10 (permalink)
 MooreTech 
Orlando, Florida
 
Experience: Advanced
Platform: NinjaTrader, TradeStation, MultiCharts, eSignal, MetaTrader
Trading: ES
Posts: 57 since Aug 2010
Thanks Given: 6
Thanks Received: 73


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:





Last Updated on March 8, 2011


© 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