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


Efficient Code for finding highest high and lowest lows of a range of bars.
Updated: Views / Replies:8,529 / 18
Created: by xigreek Attachments:0

Welcome to futures io.

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

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors – all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you don’t need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

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

Reply
 
Thread Tools Search this Thread
 

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

  #1 (permalink)
Elite Member
Bradford, UK
 
Futures Experience: Beginner
Platform: NinjaTrader
Favorite Futures: Gold
 
xigreek's Avatar
 
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

Reply With Quote
The following user says Thank You to xigreek for this post:
 
  #2 (permalink)
Elite Member
Milan Italy
 
Futures Experience: Intermediate
Platform: Ninjatrader, Sierra Chart
Favorite Futures: ES
 
Posts: 523 since Feb 2010
Thanks: 470 given, 629 received

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

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

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

(untested)

Reply With Quote
 
  #3 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,240 since Jun 2009
Thanks: 29,354 given, 83,235 received


 
Thread Moved


Moved to NinjaTrader Programming



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
- Topic: Journals of your trading -> Subforum: Trading Journals or Elite Trading Journals
- Topic: General trading related discussions -> Subforum: Traders Hideout
- Topic: Discussion of a trading method -> Subforum: Traders Hideout
- Topic: Automated Trading -> Subforum: Elite Automated 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.

-- Big Mike Trading


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.

Reply With Quote
The following user says Thank You to Big Mike for this post:
 
  #4 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,240 since Jun 2009
Thanks: 29,354 given, 83,235 received

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

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.

Reply With Quote
The following user says Thank You to Big Mike for this post:
 
  #5 (permalink)
Elite Member
AB, CA
 
Futures Experience: None
Platform: MC
Favorite Futures: es
 
Posts: 3 since Nov 2009
Thanks: 24 given, 1 received


Big Mike View Post
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

Reply With Quote
 
  #6 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,240 since Jun 2009
Thanks: 29,354 given, 83,235 received


trendtrader View Post
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.

Reply With Quote
The following user says Thank You to Big Mike for this post:
 
  #7 (permalink)
Elite Member
Portland Oregon, United States
 
Futures Experience: Beginner
Platform: Ninjatrader®
Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex
 
Zondor's Avatar
 
Posts: 1,327 since Jul 2009
Thanks: 1,246 given, 2,635 received

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];

Reply With Quote
The following 6 users say Thank You to Zondor for this post:
 
  #8 (permalink)
Elite Member
Cincinnati Ohio
 
Futures Experience: Intermediate
Platform: NinjaTrader
Broker/Data: Futures Broker
Favorite Futures: AUD
 
Silver Dragon's Avatar
 
Posts: 1,597 since Feb 2011
Thanks: 3,860 given, 3,318 received


Zondor View Post


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.

Online Help Guide: NinjaTrader Version 7
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.
Reply With Quote
 
  #9 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,240 since Jun 2009
Thanks: 29,354 given, 83,235 received

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.

Reply With Quote
 
  #10 (permalink)
Elite Member
Portland Oregon, United States
 
Futures Experience: Beginner
Platform: Ninjatrader®
Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex
 
Zondor's Avatar
 
Posts: 1,327 since Jul 2009
Thanks: 1,246 given, 2,635 received

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.

Reply With Quote

Reply



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

Thread Tools Search this Thread
Search this Thread:

Advanced Search



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

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
highest bar close occuring within the last X number of bars . . . . jmejedi NinjaTrader Programming 37 October 6th, 2014 01:52 AM
The Highest, High-Probability-Trade tigertrader Traders Hideout 38 May 22nd, 2014 11:39 AM
Converting code from Tradestation/ Plots Swing Point Highs/Lows wbtrader NinjaTrader Programming 4 May 14th, 2013 09:20 PM
Lowest Bar with the largest range kaywai NinjaTrader Programming 3 May 8th, 2011 06:36 PM
On finding the optimal settings for a range interval trendisyourfriend The Elite Circle 5 December 7th, 2010 02:53 PM


All times are GMT -4. The time now is 11:06 AM.

Copyright © 2017 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
Page generated 2017-12-16 in 0.15 seconds with 39 queries on phoenix via your IP 54.226.34.209