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,811 / 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?

  #51 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,651 since Mar 2010
Thanks: 4,226 given, 25,599 received
Forum Reputation: Legendary

Most of my CPU problems were caused by Plot Override

I am impressed with all the contributions in this thread. I will read this again and try to make use of it...

However, the real problems I had with NT 7 so far, came from another source. My NT sometimes used more than 50% of the CPU, when I had added some custom indicators to various charts of my workspace, although all the indicators were in CalculateOnBarClose = "True" mode.

The reason for the high CPU usage was the customized plot. The custom plots are calculated for every incoming tick, and this can have a huge impact on performance.

The solution here is to define an external method, which writes the new input values to a DataSeries or arrays object, and then let the Plot Override check for changed values, before performing the calculations.

Reply With Quote
The following 2 users say Thank You to Fat Tails for this post:
 
  #52 (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

Thanks everyone

Great participation and help from everyone who has been visiting here lately!

I attempted to incorporate Richard's latest suggestions into the DoubleStochasticOptimized. I posted it briefly until I realized it had problems. Will re-post after it is fixed.

Regarding previous post by FatTails, if the PlotGraphics method is used, the update frequency can be throttled back by having a minimum millisecond value between screen refreshes, as is done in the RealStats indicator.


if (lastRefresh.AddMilliseconds(LevelIIRefreshDelay) < DateTime.Now){

lastRefresh = DateTime.Now;
}

It looks like the two biggest drains on performance are excessive graphics demand, and excessive and redundant use of calls to external indicators. Addressing both as applicable is very productive, especially combined with the efficiency improvements of NT7.


Last edited by Zondor; May 27th, 2010 at 11:51 AM.
Reply With Quote
The following user says Thank You to Zondor for this post:
 
  #53 (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

Everything including the kitchen sink


This version of the Double Stochastics has every coding trick I can think of.

All calls to external indicators are pre-instantiated.

It uses Richard's FastMAX and FastMin indicators instead of MIN and MAX.

Redundant p2 DataSeries from earlier version has been removed.

Calls to the FastMIN and FastMAX indicators can only happen during the first tick of a bar. And they are only called if the internal logic can no longer be sure of the actual minimum and maximum values. Usually it WILL be sure, and will not need to call these external indicators at all.

I consider this to be a coding exercise. Is it worth it? Not sure....



.... I am Ming the Merciless. I will RULE THE UNIVERSE!

Attached Files
Register to download File Type: cs DoubleStochasticsOptimized.cs (10.5 KB, 77 views)

Last edited by Zondor; May 27th, 2010 at 06:11 PM.
Reply With Quote
The following 2 users say Thank You to Zondor for this post:
 
  #54 (permalink)
Elite Member
Paris
 
Futures Experience: None
Platform: NinjaTrader
 
Posts: 1,173 since Oct 2009
Thanks: 227 given, 4,089 received

Another hint : if you use collections in your code, you have to beware of the complexity of access/insert the data.

Here are the objects I use the most


Most simple object is List
  • It's basically a checked, autogrowing ,and typed, array.
  • Access/Read is very quick (if you access by index of course)
  • You can sort the List, which is o(n log n) complexity

Then you have the Dictionary
  • It's a generic and typed Hashtable.
  • Access/Read is o(1) complexity, meaning access time doesn't depend on the size of the Dictionary, and is very quick (obviously less than an array as there is the hashing stuff to compute)

Then you have the sorted objects : SortedList and SortedDictionary
  • SortedList and SortedDictionary have o(log n) access
  • SortedList has o(n) insert, SortedDictionary has o(log n) insert
  • You can access SortedList by index (not possible with Dictionary)


So you have to remember if you're accessing tick data that you will be manipulating large structures, so choose the object with the less complexity that fits your need.

In special cases, if you do lots of random inserts and modifcations, and very rarely you need to access the sorted values, it can be quicker to use a List and sort it when needed than using a SortedList

Reply With Quote
The following 2 users say Thank You to gomi for this post:
 
  #55 (permalink)
 R.I.P. 1960-2010 
 
Futures Experience: Advanced
Platform: NinjaTrader7
Broker/Data: Mirus RCG/Zen-Fire
Favorite Futures: CL & 6e, looking at ES, ZB and AU again.
 
ZTR's Avatar
 
Posts: 2,099 since Nov 2009
Thanks: 1,104 given, 1,390 received

Since this is an answer box & can just click thanks.

Thank you Gomi.

R.I.P. Andy Zektzer (ZTR), 1960-2010.
Please visit this thread for more information.
Reply With Quote
 
  #56 (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
This version of the Double Stochastics has every coding trick I can think of.

Since MAX and MIN maintain the max and min, I'm not sure why the indicator code keeps checking to see if the low is lower than the minLow[0]... if it's working correctly it never should be... so without running the code, I imagine OnBarUpdate() should look more like this:

 
Code
double r = maxHigh[0] - minLow[0];
p1.Set(((Close[0] - minLow[0]) / (r == 0 ? 1 : r)) * 100);
			
double s = maxpEMA[0] - minpEMA[0];
p3.Set(((pEMA[0] - minpEMA[0]) / (s == 0 ? 1 : s)) * 100);
K.Set(kEMA[0]);
			
if(Rising(K))PlotColors[0][0]=upColor;
if(Falling(K))PlotColors[0][0]=downColor;
much simpler. Maybe I misunderstood something, but glancing at the code that's what it looks like to me.

Reply With Quote
The following 2 users say Thank You to Richard for this post:
 
  #57 (permalink)
 R.I.P. 1960-2010 
 
Futures Experience: Advanced
Platform: NinjaTrader7
Broker/Data: Mirus RCG/Zen-Fire
Favorite Futures: CL & 6e, looking at ES, ZB and AU again.
 
ZTR's Avatar
 
Posts: 2,099 since Nov 2009
Thanks: 1,104 given, 1,390 received

Richard are you willing to do a Webinar?

R.I.P. Andy Zektzer (ZTR), 1960-2010.
Please visit this thread for more information.
Reply With Quote
 
  #58 (permalink)
Elite Member
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received

Hi guys,

I'm still trying to get my head around this, so can you guys help me with something simple? An example of something that I find to be painfully slow is a Market Analyzer grid I use every week or so that looks at the 14, 21, 50, and 200 day ROC of a basket of roughly 70 ETFs. The default ROC code is this:

 
Code
                            
protected override void OnBarUpdate()
{
int barsAgo Math.Min(CurrentBarPeriod);
Value.Set(((Input[0] - Input[barsAgo]) / Input[barsAgo]) * 100);

Is there an example of how this code could be optimized? I'm still not getting what types of calculations should be taken out of OnBarUpdate... or am I totally missing something? Thanks for the enlightenment.

Attached Thumbnails
Anyone have any hints for optimizing C# code?-ninjatrader-grid-2010-05-28-10_38-am.jpg  
Reply With Quote
 
  #59 (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

protected override void OnBarUpdate()
{
int barsAgo = Math.Min(CurrentBar, Period);
Value.Set(((Input[0] - Input[barsAgo]) / Input[barsAgo]) * 100);
}


The above will execute every time a BarUpate is triggered. However, during the lifetime of a bar, the value of CurrentBar never changes. So after executing the first time, on successive updates to a bar, this statement is calculating the same thing over and over and over and over.... and over again.

It should only execute ONCE per bar, as soon as a new bar starts.... ie on FirstTickOfBar.

So try this, (and before doing the division on the Value.Set line, be sure that the value of the denominator cannot be zero)


protected override void OnBarUpdate()
{
if(FirstTickOfBar)
{
int barsAgo = Math.Min(CurrentBar, Period);
Value.Set(((Input[0] - Input[barsAgo]) / Input[barsAgo]) * 100);
}
}





I am MING THE MERCILESS. I will RULE THE UNIVERSE!


Last edited by Zondor; May 28th, 2010 at 12:51 AM.
Reply With Quote
The following user says Thank You to Zondor for this post:
 
  #60 (permalink)
Elite Member
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received


Thanks Zondor,

I get the FirstTickOfBar thing if CalculateOnBarClose == false, but in this particular case its historical EOD data.

In retrospect, my performance issue is probably because I'm calling up four indicators per instrument (one each for the 14, 21, 50, and 200 ROC), on roughly 90 instruments.

So perhaps that was a poor choice on my part to use an example.

Let me look at some of the code posted here and I'll post back if I'm still confused.

Reply With Quote

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)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

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 07:05 PM.

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-11 in 0.24 seconds with 40 queries on phoenix via your IP 54.92.194.75