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?

  #91 (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 
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);

Yes, as long as the value of CCIPeriod does not change within the indicator.

Once in a while, as in "Adaptive" indicators, the parameters of external classes vary under programmatic control.

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

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
MC PL editor upgrade
MultiCharts
How to apply profiles
Traders Hideout
REcommedations for programming help
Sierra Chart
NT7 Indicator Script Troubleshooting - Camarilla Pivots
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
17 thanks
  #92 (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

The LinReg indicator does not have much of a performance impact but this example shows how it can be made more efficient.
  • Calls to the external SUM class eliminated. SUM algorithm moved into this indicator. Beats using a reusable instance of the external SUM class.
  • Variables that do not change intrabar are calculated only on FirstTickOfBar.
  • OnBarUpdate does not process redundant intrabar ticks.
  • Single precision "Float" variables reduce amount of arithmetic and memory load since a Float has half the bytes of a Double.

If you want to use this, rename the file to @LinReg and REPLACE the existing file that has that name.

"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.”
Prof. Albert Bartlett
Attached Files
Elite Membership required to download: LinReg.cs
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #93 (permalink)
 artemiso 
New York, NY
 
Experience: Beginner
Platform: Vanguard 401k
Broker: Yahoo Finance
Trading: Mutual funds
Posts: 1,152 since Jul 2012
Thanks Given: 784
Thanks Received: 2,685


@Zondor

You actually lose performance by downcasting those doubles. C# double addition takes 0.11 ns more than float addition on a 2 GHz machine, all else is the same. So downcasting is more expensive for your algorithm. I would only do it if the memory bandwidth was comparable to the L2 cache.

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

Thanks @artemiso, I was wondering about that. The float downcasting is a sledgehammer approach.

I was brought up in the days of slide rules, so am very aware that doing arithmetic with excessive non signficant precision is very wasteful. But I am not sure that there is any good way to curtail it. Math.Round, Trunc, and string formatting don't sound like good options either.

Noticed that lots of people have looked at this post. You are probably admiring my necktie and wondering where you can get your own.

"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.”
Prof. Albert Bartlett
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #95 (permalink)
 artemiso 
New York, NY
 
Experience: Beginner
Platform: Vanguard 401k
Broker: Yahoo Finance
Trading: Mutual funds
Posts: 1,152 since Jul 2012
Thanks Given: 784
Thanks Received: 2,685


Zondor View Post
Thanks @artemiso, I was wondering about that.

Doing arithmetic with excessive non signficant precision is very wasteful, but I am not sure that there is any good way to curtail it. Math.Round, Trunc, and string formatting don't sound like good options either.

You're welcome. Unfortunately, modern processors are so fast that the primitives nearly all take 1 clock cycle. The extra precision is indeed wasteful, but it's here to stay.

Reply With Quote
Thanked by:
  #96 (permalink)
 
MWinfrey's Avatar
 MWinfrey 
Lubbock TX
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Stage 5 Trading
Trading: CL
Posts: 1,878 since Jul 2009
Thanks Given: 1,450
Thanks Received: 3,335


Zondor View Post
RWT-004 :: Move the Markets


Very interesting post at MoveTheMarkets. The way we usually do things is terribly inefficient and there is a better way:

"In all of the coding tutorials and examples I've seen, Ninjatrader indicators are used in-line. So, if you want to know when the SMA is going up, you'd check:
SMA(Close, 5)[0] > SMA(Close, 5)[1]
I find this practice repulsive for two reasons: redundancy and inefficiency. This paper chronicles the path I took to fifinding a better way. If you just want to know what the better way is, skip to the 'Solution' section at the end......"

@Zondor, @Richard

That movethemarkets link does not work any longer. Any ideas?

Reply With Quote
  #97 (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

@Richard closed it down to pursue other interests, but hopefully is still around on futures.io (formerly BMT).

Too bad, there was some great stuff posted there.

"If we don't loosen up some money, this sucker is going down." -GW Bush, 2008
“Lack of proof that something is true does not prove that it is not true - when you want to believe.” -Humpty Dumpty, 2014
“The greatest shortcoming of the human race is our inability to understand the exponential function.”
Prof. Albert Bartlett
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #98 (permalink)
 gomi 
Paris
Market Wizard
 
Experience: None
Platform: NinjaTrader
Posts: 1,270 since Oct 2009
Thanks Given: 282
Thanks Received: 4,505


MWinfrey View Post
@Zondor, @Richard

That movethemarkets link does not work any longer. Any ideas?

RWT-004 :: Move the Markets ?

Found it on NT support site
Best practice for programming efficient indicators for the NT Strategy Analyzer? - [AUTOLINK]NinjaTrader[/AUTOLINK] Support Forum

Reply With Quote
  #99 (permalink)
 ntvola 
Orange County, CA / United States
 
Experience: Advanced
Platform: NinjaTrader, Excel/VBA
Broker: IB / Kinetick / eSignal
Trading: Equities
Posts: 17 since Nov 2012
Thanks Given: 12
Thanks Received: 16

I'm learning more about this topic and found this thread really helpful. Thank you!

Here is the movethemarkets PDF "Calling Upon Indicators Efficiently in NinjaScript" (attached)

Also, found this thread which has some more good info on this topic:

Best practice for programming efficient indicators for the NT Strategy Analyzer
Best practice for programming efficient indicators for the NT Strategy Analyzer? - [AUTOLINK]NinjaTrader[/AUTOLINK] Support Forum

Attached Thumbnails
Anyone have any hints for optimizing C# code?-calling-upon-indicators-efficiently-ninjascript.pdf  
Reply With Quote
Thanked by:
  #100 (permalink)
 kbeary33 
Calgary AB
 
Experience: Intermediate
Platform: AmiBroker, IB
Broker: Interactive Brokers
Trading: Stocks
Posts: 10 since Mar 2013
Thanks Given: 5
Thanks Received: 2


How would you go about this "economization" of indicators if you are using a multi-instrument strategy?

ie, if you declared

 
Code
class MyIndicator : Indicator {
private SMA sma1;
private SMA sma2;
...
protected void override OnStartup() {
sma1 = SMA(Close,5);
sma2 = SMA(Close, 13);
...
}
How could you then call sma1 or sma2 for a BarsArray[index] item?

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