Anyone have any hints for optimizing C# code? - NinjaTrader Programming | futures io social day trading
futures io futures trading


Anyone have any hints for optimizing C# code?
Updated: Views / Replies:24,880 / 111
Created: by ZTR Attachments:14

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
 14  
 
Thread Tools Search this Thread
 

Anyone have any hints for optimizing C# code?

  #31 (permalink)
Trading for Fun
Dallas TX/USA
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: AMP/CQG
Favorite Futures: ES
 
Posts: 154 since Jun 2009
Thanks: 33 given, 281 received


Zondor View Post
Anyone writing Ninjascript code should look at that post at MoveTheMarkets, and implement that idea in their code.

Yeah, it's faster, and it keeps you from repeating the arguments every time. Just all-around better. It's too bad ninja encourages the style that they do.

Another big source of inefficiency are indicators using MAX and MIN either directly or indirectly. I also have an article with an online algorithm for maintaining a sorted sliding window, which is much faster than doing an Array.Sort() every time OnBarUpdate is called (think about it... last bar you had a sorted window for everything but the newest value... make use of it rather than throw the almost correct answer away!). Much better for median filters and interquartile ranges, etc. There are other easy things to do, but I haven't had time to write them up. I'm kinda lazy about writing.

Reply With Quote
The following user says Thank You to Richard for this post:
 
  #32 (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

Article on Move The Markets re External Indicators

Richard, this is great! I optimized several custom indicators using this approach and the performance improvement is tremendous. Posted in the Elite Section is the optimized version of the Big Mike CMA Envelope Bands.. it loads in a split second. Thanks so much!

https://futures.io/free_downloads/vip_elite_circle/456-download.html?view


Sometimes the definitions of the external indicators include parameters that are updated dynamically. Whenever that happens, the definition of the external indicator must also be updated. Here is an example:
https://futures.io/elite-circle/3788-tsf-volume.html#post42162

For this reason, and also to avoid problems with NT6.5, I prefer to define the external indicators in a procedure module such as OnBarUpdate() rather than in OnStartUp().

Is there anything we can do about MAX and MIN? One particular custom indicator that uses them a lot is painfully slow to load. (BetterBuySellVolume, part of double Z trading package)

Do Math.Max and Math.Min also have problems?


Last edited by Zondor; May 25th, 2010 at 01:32 PM.
Reply With Quote
The following user says Thank You to Zondor for this post:
 
  #33 (permalink)
 Vendor: www.integrity-traders.com 
East Rochester, NY
 
Futures Experience: Intermediate
Platform: NT
 
eDanny's Avatar
 
Posts: 327 since Jul 2009
Thanks: 17 given, 419 received


But what if the called indicator does not have just one output? Which is more efficient?

This?

in Variables:
private DataSeries myMacdAvg;

in Initialize():
myMacdAvg = new DataSeries(this);

in OnBarUpdate();
myMacdAvg.Set(MACD(12,26,9).Avg[0]);

if(myMacdAvg[0] > 0)
DoSomething

Or this?

in Variables:
private MACD myMacd:

in OnBarUpdate();
if(!init)
{
myMacd = MACD(12,26,9); //myMacd = MACD(12,26,9).Avg will not work here
init = true
}
if(myMacd.Avg[0] > 0)
DoSomething


Last edited by eDanny; May 25th, 2010 at 05:44 PM.
Reply With Quote
 
  #34 (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

Sample Code: SMA of an MACD

If you want a dataseries to capture the SMA of the MACD: (Note... MACD12 should have been MyNewMACD)

in Variables:

private DataSeries myMacdAvg;
private bool init = false;
private MACD MyNewMACD
private SMA SMAofMACD


in Initialize():

myMacdAvg = new DataSeries(this);


in OnBarUpdate();

if(!init)
{
MyNewMACD= MACD(12,26,9);
SMAofMACD=SMA(MyNewMACD, 7); // 7 period SMA of MACD(12,26,9)
init = true;
}

MyMacdAvg.Set(SMAofMACD[0]);
if(MyMacdAvg[0] > 0) // if current value of 7 period SMA of MACD(12,26,9)>0
(
Print("Hello Vorld");
FabianCancellera.WinWorldTimeTrialChampionship;
)


Last edited by Zondor; May 25th, 2010 at 10:09 PM.
Reply With Quote
 
  #35 (permalink)
 Vendor: www.integrity-traders.com 
East Rochester, NY
 
Futures Experience: Intermediate
Platform: NT
 
eDanny's Avatar
 
Posts: 327 since Jul 2009
Thanks: 17 given, 419 received

Very nice (except I don't see where MACD12 comes in or is actually used). In my previous example, which would be better in accomplishing the same result, using a DataSeries or not using a DataSeries? That is my question which might be better left to Richard.

Dan

Reply With Quote
 
  #36 (permalink)
Trading for Fun
Dallas TX/USA
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: AMP/CQG
Favorite Futures: ES
 
Posts: 154 since Jun 2009
Thanks: 33 given, 281 received


eDanny View Post
But what if the called indicator does not have just one output? Which is more efficient?

The main thing is to remember a reference to the indicator, so that you don't have to ask ninja to look it up again. The number of DataSeries you need to look at isn't a factor.

So, your second example--just referencing myMacd.Avg[0]--is the one I'd use. Putting the information in an additional DataSeries isn't a necessary step, and wastes time and memory.

If you really just want a reference to the Avg part of the MACD indicator, you should be able to do that like this, though:

 
Code
in Variables:
private DataSeries macdAvg;

in OnBarUpdate():
if(!init)
{
macdAvg= MACD(12,26,9).Avg; 
init = true;
}

if(macdAvg[0] > 0)
DoSomething
.. but you lose easy access to the rest of the MACD that way, and the performance increase would be negligible.

Reply With Quote
The following 2 users say Thank You to Richard for this post:
 
  #37 (permalink)
 Vendor: www.integrity-traders.com 
East Rochester, NY
 
Futures Experience: Intermediate
Platform: NT
 
eDanny's Avatar
 
Posts: 327 since Jul 2009
Thanks: 17 given, 419 received

Thanks Richard. I was trying to avoid the memory consumption of an additional DataSeries and that is why I asked. Also I could not get this to work (I was using something other than the MACD but same idea).

Dan


macdAvg= MACD(12,26,9).Avg;

Reply With Quote
The following user says Thank You to eDanny for this post:
 
  #38 (permalink)
Trading for Fun
Dallas TX/USA
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: AMP/CQG
Favorite Futures: ES
 
Posts: 154 since Jun 2009
Thanks: 33 given, 281 received


Zondor View Post
Sometimes the definitions of the external indicators include parameters that are updated dynamically.

Yeah, I can't see the code you are posting, but just be aware that when you change the inputs you are actually creating an additional indicator that runs in parallel with the original one. So, that can easily get out of hand if you don't do some quantizing... like: instead of changing the SMA length each bar, only change it in length multiples of 5, or whatever.



Quoting 
Is there anything we can do about MAX and MIN?

There are fast algorithms for maintaining the max/min of the last n samples. As with the sliding sorted window I posted about, the key is to realize that on the previous bar you had almost the right answer. If you can just incorporate the current bar's information without just searching all over again for the maximum, you are much better off. I just haven't had time to write those up yet. Maybe soon, but I'm sure someone motivated to speed them up can work out the details. Hint: if the maximum was 3 bars ago on the last bar, then it was 4 bars ago on this bar. So if the current bar's not larger than the previous maximum, and your length is >= 4, then there's no need to do anything. The maximum hasn't changed.

Reply With Quote
The following user says Thank You to Richard for this post:
 
  #39 (permalink)
Trading for Fun
Dallas TX/USA
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: AMP/CQG
Favorite Futures: ES
 
Posts: 154 since Jun 2009
Thanks: 33 given, 281 received


eDanny View Post
Thanks Richard. I was trying to avoid the memory consumption of an additional DataSeries and that is why I asked. Also I could not get this to work (I was using something other than the MACD but same idea).

Dan


macdAvg= MACD(12,26,9).Avg;

Avg will be a Dataseries or IDataSeries... so macdAvg must be defined as one, as well (not a MACD). Does that help? So...

 
Code
DataSeries macdAvg;
macdAvg = MACD(12,26,9).Avg;
... compiles for me.

Reply With Quote
The following user says Thank You to Richard for this post:
 
  #40 (permalink)
 Vendor: www.integrity-traders.com 
East Rochester, NY
 
Futures Experience: Intermediate
Platform: NT
 
eDanny's Avatar
 
Posts: 327 since Jul 2009
Thanks: 17 given, 419 received


Thanks Richard. I would be right back to using a DataSeries in that example so that is NOT the way to go.

Dan

Reply With Quote
The following user says Thank You to eDanny for this post:

Reply



futures io > > > > > Anyone have any hints for optimizing C# code?

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
Hints, Tips & Gocthas NW27 MultiCharts 2 August 7th, 2011 04:11 PM
Can someone code this ? skyfly NinjaTrader Programming 8 April 25th, 2011 01:40 PM
Japan's Intervention Hints Fail to Stall Yen's Strength Quick Summary News and Current Events 0 September 8th, 2010 03:20 AM
Optimizing Range Bar Settings? daedalus Traders Hideout 3 February 1st, 2010 01:00 AM
Does anyone esle find Ninja problematic for optimizing strategies? rsi77 NinjaTrader 3 June 17th, 2009 09:32 PM


All times are GMT -4. The time now is 06:59 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-18 in 0.17 seconds with 40 queries on phoenix via your IP 54.163.209.109