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 gomi with 7 posts (15 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,657 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?

  #81 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,103


Zondor View Post
Post #70 in this thread is a perfect example of what not to do. The writer, xxxx72, talks about his programming background, his expert observations regarding NT7, and his scheme to reduce CPU load by skipping OnBarUpdate cycles.

However he failed to correct some extremely obvious and totally ridiculous features of the original code that were the real causes of the poor performance. Adding sampling of intrabar ticks while ignoring the deficiencies of the bar update code is a wonderful example of putting lipstick on a pig.

Here are the major problems:
  • Variables that only need to be calculated once (ONCE, not once per bar) are being calculated on every tick. These variables retain constant values over the entire life span of each instance of the indicator.
  • Variables that only need to be calculated once per bar are also being calculated on every tick. Avoiding that situation is why God gave us the if(FirstTickOfBar) condition.

These problems were a cinch to fix. I guess you have to be a non-programmer like me to notice things like that. C'mon guys, this is inexcusable.

The TSI is a NinjaTrader default indicator, itis not the worst indicator that I have seen. At least it calculates correctly.

I have tried to use the FirstTickOfBar() condition for many indicators, and it does not always improve performance. The problem is that I do not know what NinjaTrader does if I check for FirstTickOfBar ...

In this case the FirstTickOfBar condition would just save me a few multiplications, so I would not consider it worth using FirstTickOfBar. This does not invalidate the concept. For one of my MTF VWAPs, I was certainly able to improve performance over 1,000 times during news releases, by using both FirstTickOfBar and a recursive formula. It all depends what is in the bracket.

For the TSI I just would take out the divisions into OnStartUp(). This gives me a piglet without lipstick. The gain in performance was not measurable.



Attached Files
Elite Membership required to download: TSI_Piglet_No_Lipstick.zip
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Cheap historycal L1 data for stocks
Stocks and ETFs
Trade idea based off three indicators.
Traders Hideout
ZombieSqueeze
Platforms and Indicators
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
  #82 (permalink)
 
Sunil P's Avatar
 Sunil P 
los angeles
 
Experience: Intermediate
Platform: nt
Trading: cl
Posts: 290 since Jan 2012

Pig reminds me of my ex wife....biatch

A King Cobra is a more docile creature and trading CL is more predictable.

Visit my NexusFi Trade Journal Reply With Quote
  #83 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,103



Sunil P View Post
Pig reminds me of my ex wife....biatch

A King Cobra is a more docile creature and trading CL is more predictable.

C# does neither stand for Cheating on Your Spouse, Cobra or CL. It is just a coding language.

Reply With Quote
Thanked by:
  #84 (permalink)
 
Sunil P's Avatar
 Sunil P 
los angeles
 
Experience: Intermediate
Platform: nt
Trading: cl
Posts: 290 since Jan 2012

Sorry Fats ...I got confused with another forum and its more colorful vernicular that C and CL reminded me of...

As you noticed,I usually just crack jokes....nothing of hard substance in my comments...usually.

Visit my NexusFi Trade Journal Reply With Quote
  #85 (permalink)
 
Sunil P's Avatar
 Sunil P 
los angeles
 
Experience: Intermediate
Platform: nt
Trading: cl
Posts: 290 since Jan 2012

MIT OpenCourseWare | Electrical Engineering and Computer Science | 6.096 Introduction to C++, January IAP 2011 | Home


FYI

Visit my NexusFi Trade Journal Reply With Quote
  #86 (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

During the lifetime of any bar, the output values of any DataSeries that have an index value other than zero do not change. Therefore the calculation of any algebraic expressions involving terms such as Close[1] only needs to be done OnFirstTickOfBar, NOT on every tick. There are about half a dozen of these expressions that can be isolated in Blau TSI. Some have arithmetic divisions in them, so not doing them on every tick is a big help since division is costly per Gomi.

I was surprised to see so much interest in this post... good!

@gomi, doesn't the calculation of 1/x entail division? Is multiplying by 1/x less costly than dividing by x?

FatTails, the piglet is so cute, what is his name? Does he have a FacebookŽ page?

Three things about FirstTickOfBar that we need to be aware of:
  • For historical data it is actually LAST Tick of Bar.
  • In a multiseries indicator you will get crazy results unless you are specific to a barsarray series For example If(BarsInPRogress==0 && FirstTickOfBar)
  • By the time FirstTickOfBar is true, the values of all the variables have ALREADY been set to the values appropriate to the new bar.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #87 (permalink)
 gomi 
Paris
Market Wizard
 
Experience: None
Platform: NinjaTrader
Posts: 1,270 since Oct 2009
Thanks Given: 282
Thanks Received: 4,505


Zondor View Post
@ gomi, doesn't the calculation of 1/x entail division? Is multiplying by 1/x less costly than dividing by x.

Of course calculation of 1/x implies a division, that's why I mentioned "if you have lost of divisions by a rarely changing x", so you can only make the division once , and use the result afterwards.

Also remember that with NT , you're likely to see the improvement only on very heavy numerical computations...

And a last word, only try to outsmart the compiler when you know what you're doing. For instance if you're only dividing by constant numbers (available at compile time), the compiler will optimize the division.
example : optimization - What's the fastest way to divide an integer by 3? - Stack Overflow

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

There are now two complementary approaches that, together, will give a big performance boost to Ninjatrader. And there is only one place on the web where you will find this information... Big Mike's. The two approaches are:

  1. Reducing resource usage of indicators

  2. Moving the historical chart and replay data from the System Disk to a faster or less busy device, ideally a RAM disk but alternately a non system SSD or HDD, as discussed in


Regarding the first item:

What is being called "Optimizing" of code should not be confused with the process of optimizing a strategy by adjusting parameters. Maybe code improvement should be referred to as "Economizing".

I think that the CPU consumption of some indicators can easily be reduced by one or two orders of magnitude (90 to 99%) however, I have never tried to measure the change in performance. I think this could be done by inserting code into a control version and an economized version of an indicator that would measure, and print to the output window, things like the total time in ms to backfill historical data, and the total time per bar spent processing OnBarUpdate events.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #89 (permalink)
FalseProphets
Toledo, OH
 
Posts: 41 since Nov 2012
Thanks Given: 16
Thanks Received: 46

Hello,
I agree with the previous post. There is nothing better than outputting/printing time to the output window before and after critical chunks of code to figure out the bottleneck. Pay attention to variables, array size for memory, switch statements instead of if/else where applicable and use of Ninja's multi threading. ( using OnMarketUpdate with OnBarUpdate to divvy up the functions as they will be called at different events but may accomplish the task faster together)

Thx

Reply With Quote
  #90 (permalink)
 pawnbroker 
Cheltenham
 
Experience: Advanced
Platform: InvestorRT, NinjaTrader
Trading: ES
Posts: 54 since Jan 2012
Thanks Given: 8
Thanks Received: 107



eDanny View Post

private EMA emathingy;
emathingy = EMA(CCI(CCIPeriod),10);

In the example above, there is a EMA and a CCI indicator, but a reference is created only for the EMA.

Should there be a reference for the CCI too, such as in the example below?


private EMA emathingy;
private CCI cmithingy;

cmithingy = CCI(CCIPeriod);
emathingy = EMA(cmithingy,10);

Reply With Quote




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