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?

  #21 (permalink)
 gomi 
Paris
Market Wizard
 
Experience: None
Platform: NinjaTrader
Posts: 1,270 since Oct 2009
Thanks Given: 282
Thanks Received: 4,505

I'm answering my own question : as a matter of fact you can now develop non-blurry WPF apps with .NET version 4.0
cf Scott Hanselman - WPF and Text Blurriness, now with complete Clarity

This version was released on april 12th 2010, so let's say it's been 11 days that's it's possible to develop a visualization app with WPF.. ;-)

Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
Better Renko Gaps
The Elite Circle
What broker to use for trading palladium futures
Commodities
Cheap historycal L1 data for stocks
Stocks and ETFs
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
 
  #22 (permalink)
 NetTecture 
Szczecin
 
Experience: Intermediate
Platform: Ninja, writing own now
Posts: 211 since Mar 2010

Small font rendering was significantly improoved with 4.0 - you can not tell draw operations to align with physical pixels (good for small things) and a lot was done for making fonts more readable. I assume it helped that they made VIsual Studio in WPF now, so a lot of the problems start appearing in their own applications and THUS GET FIXED

Reply With Quote
  #23 (permalink)
 syxforex 
British Columbia
 
Experience: Advanced
Platform: NINJA
Broker: ZEN
Trading: Crude
Posts: 1,091 since May 2010


Hey Mike,

I was wondering if you would be interested in starting a thread on PC tweaking for traders. I want to learn this stuff and run my PC like a car freak mechanic would run his muscle car..

I'd also like to start on for learning how to tweak NT7, maintain it, and how best to learn C# and what other things you need to know to tweak indicators. I noticed one book on C# in the books thread. Is that a good place to start do you think?

Visit my NexusFi Trade Journal Reply With Quote
  #24 (permalink)
 
waza1960's Avatar
 waza1960 
NSW Australia
 
Experience: Advanced
Platform: MT4/MT5/ Multicharts/Ninja Trader
Broker: gain
Trading: forex
Posts: 33 since May 2010
Thanks Given: 38
Thanks Received: 40

Hoping somebody can help me here.I'm running N6.5 using a VPS (windows server2003 750mb Ram single core.I run a simulation with 12 strategies for a month went perfectly.
I've started trading live 3 days ago and I suddenly get these messages saying that ninja was unable to submit an order due to not enough free threads in the thread Pool.
I contacted Ninja support forum and they say its a VPS problem I contact the VPS people and they say that its not their problem. I can't find any relevant info on the net.
I was wondering if the RAM being less than recommended was an issue (I don't use the VPS for any thing other than simulation and live automated trading).Maybe I need to upgrade to windows server2008 or multiple cores. opinion or advise welcome

Visit my NexusFi Trade Journal Reply With Quote
  #25 (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,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,661


waza1960 View Post
Hoping somebody can help me here.I'm running N6.5 using a VPS (windows server2003 750mb Ram single core.I run a simulation with 12 strategies for a month went perfectly.
I've started trading live 3 days ago and I suddenly get these messages saying that ninja was unable to submit an order due to not enough free threads in the thread Pool.
I contacted Ninja support forum and they say its a VPS problem I contact the VPS people and they say that its not their problem. I can't find any relevant info on the net.
I was wondering if the RAM being less than recommended was an issue (I don't use the VPS for any thing other than simulation and live automated trading).Maybe I need to upgrade to windows server2008 or multiple cores. opinion or advise welcome

750mb ram is not enough to run NinjaTrader. I assume your home machine has 2GB or more, hopefully 4GB or more, just run the strategy live from home for a day and see if you have the same problems.

Server 2008 isn't going to do anything except add more services and slow things down. Your best bet is XP or Windows Seven, and 2GB of ram if you are using NT 6.5.

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
  #26 (permalink)
 NickA 
London
 
Experience: None
Platform: MC & Ninja mainly
Posts: 135 since Jan 2010
Thanks Given: 5
Thanks Received: 50

Try to use online algorithms wherever possible. Online algorithm - Wikipedia, the free encyclopedia
For example put any of the VWAP algorithms on a 1 tick chart of the ES to see what I mean!

Essentially you want to avoid looping through the data as much as possible. A good example is a simple moving average. The way most new to programming would tackle that is some sort of loop to iterate over the last N bars and sum the total. Really you should

add close[0]/period
subtract close[period]/period

That is about the simplest type of optimisation but wherever you see a loop you want to try and look at eliminating it if possible. There are always places where you are iterating through ticks or bars or something or other.

I know very little about .NET but have started to try and get up to speed on it. Having said that I hear one area to be wary of is garbage collection (as with any managed framework), it is widely written about.

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


waza1960 View Post
I've started trading live 3 days ago and I suddenly get these messages saying that ninja was unable to submit an order due to not enough free threads in the thread Pool.

This is called thread starvation. The .NET runtime allocates one thread pool per process (i.e. NT). In .NET2.0 the thread pool has a default size of 25 worker threads per available processor. In .NET3.5 Microsoft increased this to 250 worker threads. However, NT6.5 runs on .NET2.0 so you are only getting 25 worker threads.

Just found your post on the NT support forum. From the screenshot I can see you are getting an InvalidOperationException when NT invokes HttpWebRequest.BeginGetRequestStream. This is a common problem which NT should be aware of.

See this link...

Contention, poor performance, and deadlocks when you make Web service requests from ASP.NET applications

Try adding the following line of code to the initialisation method of your NT strategy.

 
Code
System.Threading.ThreadPool.SetMaxThreads(250, 1000);
See if that helps. It'll bump up the number of threads in the pool.


waza1960 View Post
I contacted Ninja support forum and they say its a VPS problem I contact the VPS people and they say that its not their problem.

This is a software issue, not a VPS issue. Send that link to NT and tell them to pull their finger out and look at their code rather than being so quick to point their finger at the VPS. They should be providing support for their product, not me. I suspect they are making numerous HttpWebRequest calls without understanding the ramifications for the thread pool. On a powerful machine you won't notice a problem because the worker threads will be freed up quickly but on a less powerful machine with limited CPU resources (such as your VPS) you've got a problem.


waza1960 View Post
I was wondering if the RAM being less than recommended was an issue (I don't use the VPS for any thing other than simulation and live automated trading).

This problem is completely unconnected to the RAM. But yes, you need a couple of Gig really to run NT comfortably. It's a memory hungry beast.


waza1960 View Post
Maybe I need to upgrade to windows server2008 or multiple cores. opinion or advise welcome

Stick with Win2003. There's no point in you upgrading to Win2008. Any increased CPU resources (such as additional cores) will definitely help. It sounds like you have a fairly low powered VPS instance and that's going to mean you don't get much CPU time allocated to you. Either get yourself a more powerful VPS or ditch the VPS entirely and run NT on a dedicated box instead (my preference).

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

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

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #29 (permalink)
 NickA 
London
 
Experience: None
Platform: MC & Ninja mainly
Posts: 135 since Jan 2010
Thanks Given: 5
Thanks Received: 50

"The problem is in the caching scheme that Ninjatrader uses for momoization. First off, it's a simple linear search!"

Hmm that is precisely the kind of thing I was eluding too above. You really don't want to be looping through 'stuff'. Loops are bad. Of course if ninja is doing it internally you have problems (as Richard discovered)

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


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

A good test case is the BigMike CollectiveMA. It makes six calls to external MA's.

I already changed it as Richard suggested, and I can easily see that it loads much faster now.

These changes are very easy to make, only taking a few minutes. When you think about it, doing that in an indicator that uses six calls to external MA's will cut the resources used by the indicator to a small fraction of the original demands.

Follow me on Twitter Visit my NexusFi Trade Journal 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