Reducing CPU load of NinjaTrader - NinjaTrader Programming | futures io social day trading
futures io futures trading


Reducing CPU load of NinjaTrader
Updated: Views / Replies:8,860 / 37
Created: by Fat Tails Attachments:3

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
 3  
 
Thread Tools Search this Thread
 

Reducing CPU load of NinjaTrader

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

Reducing CPU load of NinjaTrader

I use quite a few indicators for my workspaces, and as I do not use multiple quadcore processors, I want to limit the CPU load caused by NinjaTrader. Besides setting most of the custom indicator to "CalcOnBarClose = true", I am currently experimenting with the custom plots of some indicators. Even if an indicator is set to "CalcOnBarClose = false", the custom plot seems to be triggered with each incoming tick. If calculations are performed within that plot, this may slow down NinjaTrader.

So this is my idea: Collect the values and the GraphicPath objects to be plotted, store them in a cache and retrieve them from there without recalculating them with every incoming tick. Only perform calculations, if

(a) a new bar has been completed or
(b) the vertical or horizontal compression of the chart has been changed by the user

I really do not know whether this makes sense, but anyhow I have applied the concept to the default pivots indicator of NinjaTrader. This model could be applied to other indicators as well, which use custom plots and which do not need to recalculate the plots with every incoming tick.


These are the changes that I made:

Variables

 
Code
 
private bool initPlot = false;
private GraphicsPath[] path = new GraphicsPath[7];
private int[] cacheLastX = newint[7];
private int[] cacheLastY = newint[7];
private double cacheMax = 0.0;
private double cacheMin = 0.0;
private DateTime cacheLastBarTime = Cbi.Globals.MinDate;
private DateTime cacheFirstBarTime = Cbi.Globals.MinDate;

public override void Plot()

 
Code
{
DateTime firstBarTime = Bars.GetTime(ChartControl.FirstBarPainted); 
DateTime lastBarTime = (Bars.Count == 0) ? Cbi.Globals.MaxDate : ChartControl.EquidistantBars ?
Bars.GetTime(Math.Min(Bars.Count - 1, ChartControl.LastBarPainted)) 
: Bars.GetTime(Math.Min(Bars.Count - 1, Bars.GetBar(ChartControl.LastBarTimePainted)));
if (!initPlot || cacheLastBarTime != lastBarTime || cacheFirstBarTime != firstBarTime || min !=cacheMin || max != cacheMax)
{
// calculations performed here
}
cacheMax = max;
cacheMin = min;
cacheLastBarTime = lastBarTime;
cacheFirstBarTime = firstBarTime;
initPlot = true;
float textHeight = ChartControl.Font.GetHeight();
// now plotting values retrieved from Cache
for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++)
{
Gui.Chart.Plot plot = Plots[seriesCount];
SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
SolidBrush brush = brushes[seriesCount];
if (brush.Color != plot.Pen.Color) 
brush = new SolidBrush(plot.Pen.Color);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawPath(plot.Pen, path[seriesCount]);
graphics.SmoothingMode = oldSmoothingMode;
graphics.DrawString(plot.Name, ChartControl.Font, brush, cacheLastX[seriesCount], cacheLastY[seriesCount] - textHeight / 2, stringFormatFar);
}
}
Does this make sense? I have attached the modified indicator and am now looking for some pundits

- to validate or invalidate the idea
- and, maybe, test the indicator attached

Thank you for your help.

Attached Files
Register to download File Type: zip PivotsEnhanced.zip (5.9 KB, 22 views)
Reply With Quote
The following 4 users say Thank You to Fat Tails for this post:
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Madison, WI
 
Futures Experience: Advanced
Platform: Sierra Charts, ALT
Favorite Futures: ES
 
aslan's Avatar
 
Posts: 614 since Jan 2010
Thanks: 342 given, 1,077 received


First, Plot() does not get called for every tick. It can, but if there is a burst of ticks, the Plot() call should get delayed and only called after the display update interval specified in the chart properties.

I think your idea has merit, but you have to be careful since you don't know what Ninja is really doing behind the scenes. For example, what if the chart required a refresh, but your cached values were right so you did not do a refresh? I think this is a rare occurrence, but I just wanted to point out that you may not have complete control since Ninja does not really document this functionality.

How much CPU are you using? Is it only an issue during a quick burst?

Reply With Quote
The following user says Thank You to aslan for this post:
 
  #4 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,653 since Mar 2010
Thanks: 4,226 given, 25,602 received
Forum Reputation: Legendary


aslan View Post
First, Plot() does not get called for every tick. It can, but if there is a burst of ticks, the Plot() call should get delayed and only called after the display update interval specified in the chart properties.

Thanks for the answer. First, I was not aware that I could set the display update interval via chart properties. Are you sure that this setting also applies to custom plots? I had a bad experience with the pivots at some stage, where they caused about 30% processor load. Citing Josh from NinjaTrader:

There were calculations being done in the Plot method of the Pivots indicator that was expensive performance wise and it was being done multiple times. So, yes, it was moved away to be less intensive. Development says it is unlikely you would run into the same issue with your own programmings.

The problem was solved by moving away those calculations in a subroutine, which is called conditionally.


aslan View Post
I think your idea has merit, but you have to be careful since you don't know what Ninja is really doing behind the scenes. For example, what if the chart required a refresh, but your cached values were right so you did not do a refresh? I think this is a rare occurrence, but I just wanted to point out that you may not have complete control since Ninja does not really document this functionality.

I think that the chart will be refreshed anyhow, and that the changed code only affects the indicator selectively. I also have tested vertical and horizontal chart compression with and without the conditions that I added in the beginning of the Plot() override.


aslan View Post
How much CPU are you using? Is it only an issue during a quick burst?

My CPU load is between 25% to 30% with IB feed, which is a compressed feed. So if I used Zenfire or any other real feed, it would be limit. It is not quick bursts but fluctuating between 15% and 30% for the workspace.

I am also afraid that other users might encounter issues, if they use some of the indicators that I have coded. Many of them use the graphic engine of the pivots indicator to display horizontal lines. So if you put five or six of these on your charts, and if you have three or four charts open, the impact can be significant.


Last edited by Fat Tails; November 30th, 2010 at 03:31 PM.
Reply With Quote
The following user says Thank You to Fat Tails for this post:
 
  #5 (permalink)
Elite Member
Madison, WI
 
Futures Experience: Advanced
Platform: Sierra Charts, ALT
Favorite Futures: ES
 
aslan's Avatar
 
Posts: 614 since Jan 2010
Thanks: 342 given, 1,077 received

Yes, the update interval should affect custom plots as well, because your plot routine is basically just taking the place of the standard Ninja one, it does not know the difference.

Plotting is an area that you do need to be very careful for performance reasons because it has the potential to do a lot of work for a single tick. Allocating resources like brushes and pens is not a good idea, and copies should be kept around. When looping, be sure you are only doing the min amount work necessary.

Ninja is notorious for being rather lazy all over the the place (they have gotten better in NT7 though). You can still see it today, if you go look at their implementations of bar styles (candlesticks etc). They do a lot of little stuff that is not that big a deal by themselves, but they add up rather quickly.

In indicators, there are lots of little tricks you can play (see @fluxsmith ind), but there are trade offs.

At the end of the day, when I decide something is sticking around, I go in and do a deep dive and optimize it so I know I never need to go back and look at it. I do not do this with most indicators though, because I don't use many.

Reply With Quote
The following 2 users say Thank You to aslan for this post:
 
  #6 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,653 since Mar 2010
Thanks: 4,226 given, 25,602 received
Forum Reputation: Legendary


aslan View Post
Yes, the update interval should affect custom plots as well, because your plot routine is basically just taking the place of the standard Ninja one, it does not know the difference.

Plotting is an area that you do need to be very careful for performance reasons because it has the potential to do a lot of work for a single tick. Allocating resources like brushes and pens is not a good idea, and copies should be kept around. When looping, be sure you are only doing the min amount work necessary.

So if Plot() is only called once per chart update interval selected, the impact of the modifications I have done will be small - unless there are some power users who need refresh intervals set to below 0.1 sec, because otherwise they feel that they miss part of the action....

Knowing this, the question is

- whether the modifcation made will contribute to speed in a significant way
- whether it works correctly
- whether it can be improved

At the moment it is just an idea that I wanted to check. If it does not pay off, I might well decide not to go any further with this. I am not running out of ideas. But first want to explore the feasible.

Reply With Quote
The following user says Thank You to Fat Tails for this post:
 
  #7 (permalink)
Elite Member
Madison, WI
 
Futures Experience: Advanced
Platform: Sierra Charts, ALT
Favorite Futures: ES
 
aslan's Avatar
 
Posts: 614 since Jan 2010
Thanks: 342 given, 1,077 received


Fat Tails View Post
So if Plot() is only called once per chart update interval selected, the impact of the modifications I have done will be small - unless there are some power users who need refresh intervals set to below 0.1 sec, because otherwise they feel that they miss part of the action....

That is the theory, but some indicators force an update (i.e. want to see every update). These are rare, and are discouraged for obvious reasons. 0.1 is the smallest value they allow, so some use the force method to see more updates. In reality though, we really dont know what NT is doing. I suspect there are other cases that cause an immediate update, like auto-scale changes or a new bar being added.

Quoting 
Knowing this, the question is

- whether the modifcation made will contribute to speed in a significant way

I would suspect that what you are trying to do will be better when you are under load, which is when you want it, but if you are using the update interval and not forcing updates every tick, then the gain will be minimal.

Reply With Quote
The following user says Thank You to aslan for this post:
 
  #8 (permalink)
 Vendor: www.traderwerks.com 
Taipei Taiwan
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Optimus
Favorite Futures: TW
 
Posts: 693 since Jun 2009
Thanks: 440 given, 440 received


Fat Tails View Post

My CPU load is between 25% to 30% with IB feed, which is a compressed feed. So if I used Zenfire or any other real feed, it would be limit. It is not quick bursts but fluctuating between 15% and 30% for the workspace.

When I ran the IB feed a long , long time ago, I put TWS on a different computer. TWS was pretty bad back then, taking up a lot of CPU. I have no idea about now.

On a single machine with TWS and Ninja pretty much anything is faster.

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

If I use DrawRectangle() or DrawLine() or any of the other Draw() methods in OnBarUpdate(), will display update intervals as fixed via chart properties apply or will it be calculated with every incoming tick?

Reply With Quote
 
  #10 (permalink)
Market Wizard
virginia
 
Futures Experience: Intermediate
Platform: ninja
Favorite Futures: ES
 
cory's Avatar
 
Posts: 5,220 since Jun 2009
Thanks: 628 given, 6,318 received
Forum Reputation: Legendary



Fat Tails View Post
If I use DrawRectangle() or DrawLine() or any of the other Draw() methods in OnBarUpdate(), will display update intervals as fixed via chart properties apply or will it be calculated with every incoming tick?

if it calc on every tick use fitsttickofbar should show an improvement.

Reply With Quote

Reply



futures io > > > > > Reducing CPU load of NinjaTrader

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Am looking for ELCollections.dll that has been modified for multi-threaded cpu's. sigmatrader EasyLanguage Programming 2 August 12th, 2011 06:02 PM
SEC Reducing Market Volitility tdaman News and Current Events 0 April 6th, 2011 03:32 AM
NT 7 & Multicore CPU max-td NinjaTrader 19 January 27th, 2011 07:30 AM
Slippage - need advice on reducing it jackyd Traders Hideout 7 July 6th, 2009 03:24 AM


All times are GMT -4. The time now is 07:09 AM.

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-18 in 0.17 seconds with 20 queries on phoenix via your IP 54.163.209.109