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 trendtrader with 2 posts (1 thanks)
    4. looks_4 BankRobberNT 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 16,906 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.

  #11 (permalink)
 trendtrader 
AB, CA
 
Experience: None
Platform: MC
Trading: es
Posts: 3 since Nov 2009
Thanks Given: 24
Thanks Received: 1


Zondor View Post
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];


Thanks Zondor,

So, my dilemma is this... I've been trying to create clean, efficient code and I've seen others do two things:

1. Use Methods they have created and then call from their main indicator (UserDefinedMethods).
2. Create an indicator and then use its results similar to how you are suggesting above.

I'd like to create the fastest code possible and wonder if doing either of the above will slow my code down as opposed to keeping everything in the one indicator.
I did find a good pdf by Richard Todd, https://richardtodd.name/docs/pdf/efficientNinjaScript.pdf, that also talks about your method above.

So, for number 1, if I pull out code that is used in many different indicators and place this common code into a userdefinedmethod file, does my indicator run slower? My guess on this one is probably not.

Although number 2 appeals to me since I can create simple building blocks and use them in the main indicator, does Ninja end up using more resources? i.e. say for instance the building block utilizes a DataSeries in its code and then a variable is created in the main indicator of the building block type, as you show above, is Ninja then using twice the resources (2 DataSeries now instead of just one, compared to if I made one large indicator)?

I hope this makes sense... if not, please let me know & I'll try to explain it better. (I'm not a programmer by profession and C# is new to me.)

Thanks,

TT

Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - April 2024
Feedback and Announcements
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Better Renko Gaps
The Elite Circle
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Deepmoney LLM
Elite Quantitative GenAI/LLM
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #12 (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 
So, my dilemma is this... I've been trying to create clean, efficient code and I've seen others do two things:

1. Use Methods they have created and then call from their main indicator (UserDefinedMethods).
2. Create an indicator and then use its results similar to how you are suggesting above.

I'd like to create the fastest code possible and wonder if doing either of the above will slow my code down as opposed to keeping everything in the one indicator.
I did find a good pdf by Richard Todd, https://richardtodd.name/docs/pdf/efficientNinjaScript.pdf, that also talks about your method above.

So, for number 1, if I pull out code that is used in many different indicators and place this common code into a userdefinedmethod file, does my indicator run slower? My guess on this one is probably not.

Richard W. Todd is an expert virtuoso programmer who knows what he is talking about. The posts he made (as user "Richard") in the code optimization thread that ZTR started on futures.io (formerly BMT) were exceptionally valuable.

The only thing that would be faster than using the predefined instances of the MAX indicator would be putting the code of the MAX indicator INSIDE your indicator as one of its custom methods. I don't think the small performance improvement would be worth the trouble. You DO want to call the MAX function as infrequently as possible since it will sometimes iterate through a loop.

Ninjatrader's new improved version of MAX, which was suggested to them by RWT, does not do that nearly as often as the old one did. It uses logic tests to avoid unnecessary looping. But it's better to not do even those when a call to MAX is not needed at all.

If you send me your code in a PM, I will review it.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #13 (permalink)
 BankRobberNT 
NYC
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Interactive Brokers
Trading: forex
Posts: 38 since Aug 2011



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 the biggest fan of using functions instead of indicators. You can get great performance improvement.

Reply With Quote
  #14 (permalink)
 BankRobberNT 
NYC
 
Experience: Intermediate
Platform: Ninjatrader
Broker: Interactive Brokers
Trading: forex
Posts: 38 since Aug 2011


Zondor View Post
Richard W. Todd is an expert virtuoso programmer who knows what he is talking about. The posts he made (as user "Richard") in the code optimization thread that ZTR started on futures.io (formerly BMT) were exceptionally valuable.

The only thing that would be faster than using the predefined instances of the MAX indicator would be putting the code of the MAX indicator INSIDE your indicator as one of its custom methods. I don't think the small performance improvement would be worth the trouble. You DO want to call the MAX function as infrequently as possible since it will sometimes iterate through a loop.

Ninjatrader's new improved version of MAX, which was suggested to them by RWT, does not do that nearly as often as the old one did. It uses logic tests to avoid unnecessary looping. But it's better to not do even those when a call to MAX is not needed at all.

If you send me your code in a PM, I will review it.

I don't use external indicators in my strategy at all, not even the simplest one like SMA, EMA or ZLEMA
They are all coded as functions.
Strategy is running on 28 currency pairs 6 timeframes
modules(functions): Current OHLC, PreviousDayOHL, RoundNumbers, SwingRays, FibLevels, Pivots, SMA 2 timeframes, EMA 6 timeframes, ZLEMA, CurrencyMeter, New High/Lows

Strategy is running darn fast, and absolutely NO Freezing, NADA

Reply With Quote
  #15 (permalink)
 
Adamus's Avatar
 Adamus 
London, UK
 
Experience: Beginner
Platform: NinjaTrader, home-grown Java
Broker: IB/IQFeed
Trading: EUR/USD
Posts: 1,085 since Dec 2010
Thanks Given: 471
Thanks Received: 789


BankRobberNT View Post
I don't use external indicators in my strategy at all, not even the simplest one like SMA, EMA or ZLEMA
They are all coded as functions.
Strategy is running on 28 currency pairs 6 timeframes
modules(functions): Current OHLC, PreviousDayOHL, RoundNumbers, SwingRays, FibLevels, Pivots, SMA 2 timeframes, EMA 6 timeframes, ZLEMA, CurrencyMeter, New High/Lows

Strategy is running darn fast, and absolutely NO Freezing, NADA

Let me get this totally straight: you write the code into your Strategy for those indicators? Or do you write it into User Defined Functions?

If you write it into your Strategy, don't you have a headache when you have 10 strategies in development and you want to change one indicator that's in all of them?

You can discover what your enemy fears most by observing the means he uses to frighten you.
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #16 (permalink)
 eman 
Galveston ,TX
 
Experience: Intermediate
Platform: NT7
Broker: Zaner
Trading: Futures
Posts: 386 since Mar 2010
Thanks Given: 364
Thanks Received: 435


trendtrader View Post
I did find a good pdf by Richard Todd, https://richardtodd.name/docs/pdf/efficientNinjaScript.pdf, that also talks about your method above.

anyone have a copy of the pdf and/or an updated link to the pdf?

cheers,
-e

Reply With Quote
  #17 (permalink)
 
echoeversky's Avatar
 echoeversky 
Puyallup, Washington State
 
Experience: Intermediate
Platform: NinjaTrader
Broker: NINJATRADER/CQG
Trading: NQ, CL, ES, YM
Posts: 115 since Sep 2009
Thanks Given: 113
Thanks Received: 48


eman View Post
anyone have a copy of the pdf and/or an updated link to the pdf?

cheers,
-e


No but he's baaaaaaaaack...
richardwesleytodd.blogspot.com/2013/05/starting-another-in-long-line-of-new.html

Follow me on Twitter Reply With Quote
  #18 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
Quantoholic
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,976 since Jun 2009
Thanks Given: 533
Thanks Received: 3,709

You should keep your recent highs/low in a queue implemented as arrays, on bar update take out the oldest number, put on the newest number and iterate through them to get the high/low of the lookback period. Anytime you use indexers on DataSeries you are actually calling get() methods/functions on enumerable objects, which involves popping and pushing variables onto the stack, setting up function pointers and contexts, more garbage collection cycles, etc.

double[] arrays sit on the heap because "double" is a value type, not a reference type, and no methods are called to read/write arrays.

Sorry I'm not including sample code, too busy coding other things for other people right now, but maybe someone else can supply something.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #19 (permalink)
 luislam 
San Isidro Lima / Peru
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Stocks
Posts: 2 since Nov 2012
Thanks Given: 1
Thanks Received: 0

I'm trying to find the bar number of the highest bar of my current session, but when I try something like this:

numBarHigh = HighestBar(High, Bars.BarsSinceSession);

it has trouble when the highest bar is the 1st bar of the session.
Any advice?

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