NexusFi: Find Your Edge


Home Menu

 





Reducing CPU load of NinjaTrader


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Fat Tails with 14 posts (17 thanks)
    2. looks_two aslan with 5 posts (5 thanks)
    3. looks_3 Tarkus11 with 3 posts (2 thanks)
    4. looks_4 Big Mike with 3 posts (0 thanks)
      Best Posters
    1. looks_one RJay with 3 thanks per post
    2. looks_two Fat Tails with 1.2 thanks per post
    3. looks_3 aslan with 1 thanks per post
    4. looks_4 Tarkus11 with 0.7 thanks per post
    1. trending_up 16,293 views
    2. thumb_up 29 thanks given
    3. group 7 followers
    1. forum 37 posts
    2. attach_file 3 attachments




 
Search this Thread

Reducing CPU load of NinjaTrader

  #1 (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,102

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
Elite Membership required to download: PivotsEnhanced.zip
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Trade idea based off three indicators.
Traders Hideout
REcommedations for programming help
Sierra Chart
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
Pivot Indicator like the old SwingTemp by Big Mike
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
18 thanks
  #3 (permalink)
 
aslan's Avatar
 aslan 
Madison, WI
 
Experience: Advanced
Platform: ALT
Trading: ES
Posts: 625 since Jan 2010
Thanks Given: 356
Thanks Received: 1,127


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
Thanked by:
  #4 (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,102


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.

Started this thread Reply With Quote
Thanked by:
  #5 (permalink)
 
aslan's Avatar
 aslan 
Madison, WI
 
Experience: Advanced
Platform: ALT
Trading: ES
Posts: 625 since Jan 2010
Thanks Given: 356
Thanks Received: 1,127

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
Thanked by:
  #6 (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,102


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.

Started this thread Reply With Quote
Thanked by:
  #7 (permalink)
 
aslan's Avatar
 aslan 
Madison, WI
 
Experience: Advanced
Platform: ALT
Trading: ES
Posts: 625 since Jan 2010
Thanks Given: 356
Thanks Received: 1,127


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
Thanked by:
  #8 (permalink)
 traderwerks   is a Vendor
 
Posts: 692 since Jun 2009
Thanks Given: 436
Thanks Received: 465


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)
 
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,102

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?

Started this thread Reply With Quote
  #10 (permalink)
 
cory's Avatar
 cory 
virginia
 
Experience: Intermediate
Platform: ninja
Trading: NQ
Posts: 6,098 since Jun 2009
Thanks Given: 877
Thanks Received: 8,090



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




Last Updated on November 8, 2011


© 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