NexusFi: Find Your Edge


Home Menu

 





Anyone have any hints for optimizing C# code?


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Zondor with 20 posts (21 thanks)
    2. looks_two ZTR with 13 posts (0 thanks)
    3. looks_3 Richard with 11 posts (20 thanks)
    4. looks_4 Fat Tails with 7 posts (18 thanks)
      Best Posters
    1. looks_one Fat Tails with 2.6 thanks per post
    2. looks_two gomi with 2.1 thanks per post
    3. looks_3 Richard with 1.8 thanks per post
    4. looks_4 Zondor with 1.1 thanks per post
    1. trending_up 48,519 views
    2. thumb_up 108 thanks given
    3. group 39 followers
    1. forum 111 posts
    2. attach_file 13 attachments




 
Search this Thread

Anyone have any hints for optimizing C# code?

  #41 (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

"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."

Richard,

Is there a way to destroy the original one when I create the new one, so that only one instance will exist? Can I use Dispose() to do that?

Thanks to you we are making some great progress in this thread now. It's making a real difference.

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

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Better Renko Gaps
The Elite Circle
Exit Strategy
NinjaTrader
Pivot Indicator like the old SwingTemp by Big Mike
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
18 thanks
  #42 (permalink)
Richard
Dallas TX/USA
 
Posts: 153 since Jun 2009
Thanks Given: 33
Thanks Received: 284


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

I just want to make sure we're on the same page. Storing a reference to an existing DataSeries doesn't cost anything. Creating a new dataseries and filling it with values from another one is wasteful.

So, putting this in the init code:
 
Code
DataSeries macdavg; 
macdavg = MACD(12,26,9).Avg;
... costs nothing, and leaves you with access to the Dataseries that the MACD is filling. On the other hand:

 
Code
// Init Code
macdavg = new DataSeries(this);
...
// OnBarUpdate():
macdavg.Set(MACD(12,26,9).Avg[0]);
... stores copies of data that you already had access to, which is wasteful. In both cases, you end up with a DataSeries called macdavg that has the correct values in it, but the first version is practically free and the second one does a bunch of unnecessary work.

Reply With Quote
Thanked by:
  #43 (permalink)
Richard
Dallas TX/USA
 
Posts: 153 since Jun 2009
Thanks Given: 33
Thanks Received: 284



Zondor View Post
Is there a way to destroy the original one when I create the new one, so that only one instance will exist? Can I use Dispose() to do that?

I don't think so, because ninja is caching them and you don't know who else is looking at that same indicator reference. In the same way, you can't just change an indicator's inputs on the fly, because someone else may be depending on the object continuing to compute the 5 SMA rather than the 10, or whatever.

The three easiest workarounds I know of are:
  • Quantize so that you only ever create a few indicators to cover the input space and you don't have to worry about it.
  • Make an indicator that knows how to vary itself in the right way, rather than having the main indicator in control of adjusting the inputs (for bonus points, market it to people as an "adaptive indicator" for tons of cash to fund further development)
  • Suck the indicator code into a private function of the main indicator, so that you can call it with different inputs at will

Reply With Quote
Thanked by:
  #44 (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

Based on the suggestion by Richard in Post 38, I changed the DoubleStochasticsOptimized to check within the indicator, when a new bar starts, whether the [Period +1] value is equal to the minimum or maximum value for each of four different dataseries.

It is necessary to call the external MIN or MAX function ONLY when that is true.

A 30 tick, 30 days back chart of the YM with this indicator refreshes in less than one second.

(Printing the value of CurrentBar to the Output Window shows that this chart has 99,798 bars. ) That seems pretty good.....

Attached Files
Elite Membership required to download: DoubleStochasticsOptimized.cs
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #45 (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,443 since Jun 2009
Thanks Given: 33,217
Thanks Received: 101,607

In linux cli world, we can easily time stuff to find which method is most CPU efficient, just:

time <what you want to time>

It would be nice if a small block of code could be written to time these new optimized functions, so we can compare old vs. new and see what we are really accomplishing.

I'm all for writing efficient code and am just curious how much CPU time we're saving here. I think with .NET framework our only hope is to measure CPU time, measuring resource consumption like memory is not going to happen I don't think.

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
  #46 (permalink)
 
ZTR's Avatar
 ZTR 
 
Experience: Advanced
Platform: NinjaTrader7
Broker: Mirus RCG/Zen-Fire
Trading: CL & 6e, looking at ES, ZB and AU again.
Posts: 2,096 since Nov 2009
Thanks Given: 1,099
Thanks Received: 1,393

Anecdotally, there is a definite difference in load time and workspace switching times since Zondor started his optimization project.

I am running 3 workspaces of 3 GOM Volume Ladders & 5 copies of BSV39 (both notoriously slow loaders). Load times went from 30 + seconds for the attached chart when hitting F5 to less than 8 seconds with 4 worksapces running. The fourth workspace has 12 GOM recorders -2 each of YM, CL, 6e flat and binary and 2 contract months each. Load time is less then 3 seconds if this is the only chart loaded in an empty workspace & that includes data.

R.I.P. Andy Zektzer (ZTR), 1960-2010.
Please visit this thread for more information.
Attached Thumbnails
Click image for larger version

Name:	YM 06-10 (1024 Volume)  5_26_2010_2034.jpg
Views:	306
Size:	183.4 KB
ID:	14365   Click image for larger version

Name:	Indys.PNG
Views:	269
Size:	26.7 KB
ID:	14366  
Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #47 (permalink)
Richard
Dallas TX/USA
 
Posts: 153 since Jun 2009
Thanks Given: 33
Thanks Received: 284


Zondor View Post
A 30 tick, 30 days back chart of the YM with this indicator refreshes in less than one second.

I had in mind more of a FastMAX and FastMIN indicator that you can use as replacements for the original MAX and MIN... so other indicators wouldn't need to know the difference. Like the attached.

I can run the new 89-max and 89-min on a chart with 2.8 million bars in 6 seconds, versus more like 19 seconds with the built-in max and min. It definitely makes a difference to avoid redundant computations.

Attached Files
Elite Membership required to download: Z20100527FastMAX.cs
Elite Membership required to download: Z20100527FastMIN.cs
Reply With Quote
  #48 (permalink)
Richard
Dallas TX/USA
 
Posts: 153 since Jun 2009
Thanks Given: 33
Thanks Received: 284

While we're on the topic of optimization, DoubleStochasticsOptimized has a line like this in it:

 
Code
			p2.Set(pEMA[0]);
Whenever you see a line like this, it is a sign that you probably don't need p2 at all. Just use pEMA.Value instead. Also, by using fastMAX and fastMIN you can put those into OnStartUp() and avoid the cache lookup.

Reply With Quote
Thanked by:
  #49 (permalink)
 zikonc 
san ramon
 
Experience: Beginner
Platform: ninja
Broker: zen
Trading: es, 6e
Posts: 136 since Oct 2009
Thanks Given: 98
Thanks Received: 76

I propose a new platform by Gomi and Richard ........ "Be Rich GoRich" .....

Reply With Quote
Thanked by:
  #50 (permalink)
ScoobyStoo
London
 
Posts: 16 since Mar 2010
Thanks Given: 0
Thanks Received: 13



Big Mike View Post
It would be nice if a small block of code could be written to time these new optimized functions, so we can compare old vs. new and see what we are really accomplishing.

I'm all for writing efficient code and am just curious how much CPU time we're saving here. I think with .NET framework our only hope is to measure CPU time, measuring resource consumption like memory is not going to happen I don't think.

Try these puppies...

CPU profiling: EQATEC Profiler &mdash; EQATEC

Memory profiling: Download details: CLR Profiler for the .NET Framework 2.0

No need to part with any cash money.

Reply With Quote
Thanked by:




Last Updated on December 24, 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