NexusFi: Find Your Edge


Home Menu

 





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


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Big Mike with 4 posts (3 thanks)
    2. looks_two Zondor with 3 posts (7 thanks)
    3. looks_3 BankRobberNT with 2 posts (1 thanks)
    4. looks_4 trendtrader with 2 posts (1 thanks)
      Best Posters
    1. looks_one Zondor with 2.3 thanks per post
    2. looks_two shodson with 1 thanks per post
    3. looks_3 Big Mike with 0.8 thanks per post
    4. looks_4 BankRobberNT with 0.5 thanks per post
    1. trending_up 17,019 views
    2. thumb_up 14 thanks given
    3. group 11 followers
    1. forum 18 posts
    2. attach_file 0 attachments




 
Search this Thread

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

  #1 (permalink)
 
xigreek's Avatar
 xigreek 
Bradford, UK
 
Experience: Beginner
Platform: NinjaTrader
Trading: Gold
Posts: 9 since Oct 2011
Thanks Given: 10
Thanks Received: 2

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

Started this thread Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
REcommedations for programming help
Sierra Chart
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
ZombieSqueeze
Platforms and Indicators
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
23 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #2 (permalink)
 futuretrader 
Como Italy
 
Experience: Intermediate
Platform: Ninjatrader, customized
Trading: ES
Posts: 525 since Feb 2010
Thanks Given: 471
Thanks Received: 643

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

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

(untested)

Reply With Quote
  #3 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603


 
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


We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603

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

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #5 (permalink)
 trendtrader 
AB, CA
 
Experience: None
Platform: MC
Trading: es
Posts: 3 since Nov 2009
Thanks Given: 24
Thanks Received: 1


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)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603


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

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #7 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731

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

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #8 (permalink)
 
Silver Dragon's Avatar
 Silver Dragon 
Cincinnati Ohio
Legendary Master Data Manipulator
 
Experience: Intermediate
Platform: TastyWorks / NT
Broker: TastyWorks /NT
Trading: FX, Stocks, Options
Posts: 2,107 since Feb 2011
Thanks Given: 6,422
Thanks Received: 5,238


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 [COLOR=#0066cc][I]CalculateOnBarClose[/I][/COLOR] 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.
Visit my NexusFi Trade Journal Reply With Quote
  #9 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,442 since Jun 2009
Thanks Given: 33,215
Thanks Received: 101,603

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

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #10 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,731



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.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote




Last Updated on July 31, 2015


© 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