Efficient Code for finding highest high and lowest lows of a range of bars. - NinjaTrader Programming | futures io social day trading

Efficient Code for finding highest high and lowest lows of a range of bars.
 Updated: July 31st, 2015 (02:54 PM) Views / Replies: 10,996 / 18 Created: December 15th, 2011 (04:51 PM) by xigreek Attachments: 0

 Welcome to futures io.

# Efficient Code for finding highest high and lowest lows of a range of bars.

 December 15th, 2011, 04:51 PM #1 (permalink) Elite Member Bradford, UK   Trading Experience: Beginner Platform: NinjaTrader Favorite Futures: Gold     Posts: 9 since Oct 2011 Thanks: 10 given, 2 received Efficient Code for finding highest high and lowest lows of a range of bars. Hi I'm quite new to C# and ninja script and am convinced there must be a better way to do this... I'm trying to find the lowest low and highest high for a range of bars - (let's say 9 for the sake of argument) - e.g. so I can code a Tenkan Sen Ichimoku line. At the moment I'm doing it by looping through the indexes of the last nine Bars in like this: int Period = 9; //define the number of bars to check back double HighestH = 0; double LowestL = 0; for (int i=0; i < Period; i++) { if(High[i] > HighestH) HighestH = High[i]; if (Low[i] == 0) { LowestL = Low[i]; }else if(Low[i] < LowestL) LowestL = Low[i]; } Whilst this works, I can't imagine it's the neatest/most compact code. Maybe it's because I've spent most of my life using Excel and using functions like = Max(A1:A9) to effectively do the same thing. Anyone got any suggestions of code to neaten this up. Cheers XiGreek
 The following user says Thank You to xigreek for this post:

Como Italy

Favorite Futures: ES

Posts: 526 since Feb 2010

 This post has been selected as an answer to the original posters question

Using the built-in HighestBar() would be simpler.

E.g. High[HighestBar(High,9)];

(untested)

Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500

Posts: 47,177 since Jun 2009

When creating a new thread, note which subforum you are in. Here is a short list of suggestions:

- Topic: Anything to do with an Elite indicator -> Subforum: The Elite Circle
- Topic: Looking for an existing indicator, or how-to use an indicator -> Subforum: (the platform)
- Topic: Programmer needing help with non-Elite indicator -> Subforum: (the platform) - Programming
- Topic: Want an indicator created/modified -> Reply to "Want indicator created free" in Elite Circle
- Topic: Vendors (trading rooms, commercial indicators) -> Subforum: Vendors/Product Reviews
- Topic: Discussion of Forex or Currency trading -> Subforum: Forex and Currency Trading

Last, any Elite Member may create more or less any of these topics in The Elite Circle at your own discretion (your support is appreciated).

This is just a short general list and doesn't cover everything. If you are unsure where to create your new thread, just create it in Traders Hideout and a moderator will move it if necessary.

 Due to time constraints, please do not PM me if your question can be resolved or answered on the forum. Need help? 1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first. 2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses. 3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make. 4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance. 5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers. 6) Help using the forum? Watch this video to learn general tips on using the site. If you want to support our community, become an Elite Member.
 The following user says Thank You to Big Mike for this post:

Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500

Posts: 47,177 since Jun 2009

 This post has been selected as an answer to the original posters question

Multiple ways.

Another:

HighestH = MAX(High, lookback)[0];

You can easily put max and min into your local indicator as a function instead of calling it externally, speeding it up quite a bit.

Mike

 Due to time constraints, please do not PM me if your question can be resolved or answered on the forum. Need help? 1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first. 2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses. 3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make. 4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance. 5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers. 6) Help using the forum? Watch this video to learn general tips on using the site. If you want to support our community, become an Elite Member.
 The following user says Thank You to Big Mike for this post:

AB, CA

Platform: MC
Favorite Futures: es

Posts: 3 since Nov 2009

Big Mike
 Multiple ways. Another: HighestH = MAX(High, lookback)[0]; You can easily put max and min into your local indicator as a function instead of calling it externally, speeding it up quite a bit. Mike

I'm not sure of the implication of this. Does this mean if I take say 2 of my custom indicators and they have common code, then I put this common code in a UserDefinedMethod and call it from my indicators, they will run slower?

Thanks,

TT

Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500

Posts: 47,177 since Jun 2009

 I'm not sure of the implication of this. Does this mean if I take say 2 of my custom indicators and they have common code, then I put this common code in a UserDefinedMethod and call it from my indicators, they will run slower? Thanks, TT

The general implication is:

- Code called locally for things like min,max will be much faster if you don't use the "indicators" MIN, MAX as those have to maintain separate dataseries and indexes, while a local function does not.

@Zondor has lots to say on this subject and I don't use NT right now, so hopefully he can help you out.

Mike

 Due to time constraints, please do not PM me if your question can be resolved or answered on the forum. Need help? 1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first. 2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses. 3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make. 4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance. 5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers. 6) Help using the forum? Watch this video to learn general tips on using the site. If you want to support our community, become an Elite Member.
 The following user says Thank You to Big Mike for this post:

Portland Oregon, United States

Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex

Posts: 1,337 since Jul 2009

Here's how....

In the variables region, declare an instance, maxH of the MAX class:

Code
 `private MAX maxH;`
In OnStartUp(), define the instance of the MAX class:

Code
 `maxH = MAX(High, lookback);`

In OnBarUpdate(), call that instance, ONLY WHEN NECESSARY:

Code
 `if( FirstTickOfBar || Input[0] > HighestH ) HighestH = maxH[0];`

 The following 6 users say Thank You to Zondor for this post:

Legendary Market Wizard
Cincinnati Ohio

Broker/Data: Futures Broker
Favorite Futures: FX, Stocks, Options

Posts: 1,714 since Feb 2011

Zondor

In OnBarUpdate(), call that instance, ONLY WHEN NECESSARY:

Code
 `if( FirstTickOfBar || Input[0] > HighestH ) HighestH = maxH[0];`

Assuming your building a strategy: In order to use FirstTickOfBar, Calculate on Bar Close must be set to false.

From NT Help Section:

Indicates if the incoming tick is the first tick of a new bar. This property is only of value in strategies that run tick by tick which is when the CalculateOnBarClose property is set to false.

This can also be accessed from with NT Control Center under Help Menu.
Search on: FirstTickOfBar

SD

 nosce te ipsum You make your own opportunities in life.

Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500

Posts: 47,177 since Jun 2009

Also if you want to limit it to the 'x' number of last bars the code will need some tweaks, or limit it to daily high or low need to reset on new session.

Mike

 Due to time constraints, please do not PM me if your question can be resolved or answered on the forum. Need help? 1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first. 2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses. 3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make. 4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance. 5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers. 6) Help using the forum? Watch this video to learn general tips on using the site. If you want to support our community, become an Elite Member.

Portland Oregon, United States

Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex

Posts: 1,337 since Jul 2009

When COBC is true every tick is FirstTick... so what else is new?

Quoting
 Assuming your building a strategy: In order to use FirstTickOfBar, Calculate on Bar Close must be set to false. From NT Help Section: Indicates if the incoming tick is the first tick of a new bar. This property is only of value in strategies that run tick by tick which is when the CalculateOnBarClose property is set to false. Online Help Guide: NinjaTrader Version 7

If calculate on bar close is false, FirstTickOfBar is always true and the instance of the MAX function will be called once per bar, just like it is supposed to be.

 futures io > Efficient Code for finding highest high and lowest lows of a range of bars.

Upcoming Webinars and Events (4:30PM ET unless noted)

Right now

Right now

## Journal in July -- over \$2,500 in prizes w/NinjaTrader

July

 Similar Threads Thread Thread Starter Forum Replies Last Post jmejedi NinjaTrader Programming 37 October 6th, 2014 12:52 AM tigertrader Traders Hideout 38 May 22nd, 2014 10:39 AM wbtrader NinjaTrader Programming 4 May 14th, 2013 08:20 PM kaywai NinjaTrader Programming 3 May 8th, 2011 05:36 PM trendisyourfriend The Elite Circle 5 December 7th, 2010 01:53 PM

 Tags arrays, bar update, c#, cobc, code, coding, dataseries, dow, ema, es, futures, ichimoku, indexes, indicator, indicators, limit, lookback, new high, ninja, ninjascript, ninjatrader, nt help, nt7, ohlc, onstartup, optimization, pairs, people, pivots, review, sample, sma, strategies, strategy, support, tick, trend, type, update, userdefinedmethods