Hello,
I am looking for a way to reduce the CPU usage of the pc by writing an optimized code of an indicator.
I know that PRT runs an indicator for every tick arriving on the instrument and, within this, it executes the indicator for every bar on the worktable.
That is, if I have 1000 bars in the work plan, for every incoming tick PRT executes the indicator 1000 times.
My indicatorit already uses calculateonlastbars = 20 so for every tick it run 20 times, one for last 20 bars, and draws objects with DRAW commands only if islastbarupdate.
But the use of CPU is very high due to complex calculation.
because object drawing are very static (starting from a certain date in the past), it would be sufficient to activate drawing not for each tick,
but:
– at each new opening bar or every 10 seconds
– in any case when the market is closed (because no new tick arriving to force draw).
in other words, there is a way to recognize if we are at a closed market period, and therefore if ticks do not arrive?
I can always draw, or I am at an open market and therefore I can count on the fact that the ticks arrive and then use
currentsecond to make the indicator work every 10 seconds?
Or its’ possible to know the my pc Time and date at indicator execution? I found only reference to closing bar time/date/timestamp.
thank you.
JSParticipant
Veteran
Hi
@robocop
If I understand your problem correctly and you only want to have your indicator updated 1 time every 10 seconds, you can use:
TimeFrame(10 seconds, UpdateOnClose)
Hi @robocop
If I understand your problem correctly and you only want to have your indicator updated 1 time every 10 seconds, you can use:
TimeFrame(10 seconds, UpdateOnClose)
Your suggestion is not applicable because MultiTimeFrame require that the timeframe of the chart is below Timeframe coded (eg. chart TF 5 second).
MyIndicators runs in Timeframe 1h and above (2h, 4h, Day, Week, etc) and i want to update not at every tick on market open (e.g. 10 second, at close/open bar, etrc) and always at market close.
JSParticipant
Veteran
Okay, maybe you can make the complex calculations less complex in a smart way…
For example, I use the standard deviation in my calculations and have found a way to calculate this standard deviation much faster…
Okay, maybe you can make the complex calculations less complex in a smart way…
In my opinion the right way is dont make any calculation if we know is redundant, not make a light, but redundant, calculation.
Anyway, i will think about it. Meanwhile if you know the answer to my original question, i appreciate it.
JSParticipant
Veteran
I’m not talking about redundant calculations, but sometimes you can manipulate the equations/calculations in such a way that they work much faster.
Calculating the standard deviation in this way is much faster than with the standard method…
Mu = Average[n](Close)
MuSquared = Square(Mu)
XiSquared = Square(Close)
StdRS = SQRT(1/n * Summation[n](XiSquared) - MuSquared)
Return StdRS[n]
JSParticipant
Veteran
I would not know any other solution for this.
(Normally, using “CalculateOnLastBars” should significantly reduce your CPU usage)
You can use a simple conditions based on time in conjunction with islastbarupdate:
In this example, I’m making a new calculation every X seconds:
X = 6
if islastbarupdate and timestamp-lastcalc>=X then
//do some calculations
indi = close
lastcalc=timestamp
endif
return indi
Link to Nicolas code added as Log 334 here …
Snippet Link Library