TradingView
m_b_round
2 mar 2023 14:39

Centred Moving Average 

APEUSDT Perpetual ContractBybit

Descrizione

Based around the Centered Moving Average as published by Vailant-Hero this script is revised and improved to aid with execution time & server load. For full description follow the link as above, as Valiant-Hero explains the idea perfectly well.

While the original script worked fine for small values of length, once length was extended significantly or chart timeframe set to short values then the script is prone to exceeding computation requirements. The original script was attempting to delete and re-draw (length x 3) lines on the chart for each tick. In addition to server load, once length is greater than 167 (500/3) then the first drawn lines start disappearing, so the predicted values no longer appear connected to the offset averages calculated from the candle data. A further error resulted with larger values of "length" and future data selected, in that the script would try and move lines more than 500 bars into the future.

Improvements and major code changes
  • All values for the predicted moving average lines are calculated from a single run through of the data, rather than having to loop back through the data "length" times (and then through it again "length" times if you selected double moving average). Each loop also inefficiently calculated the sum of "length" values by recalling each one individually.
  • Number of lines are thus reduced so that we're never attempting to plot more than "max_lines_count" onto the chart. User is able to select the granularity of the lines - more sections will mean a smoother line but at the expense of processing speed.
  • No matter the combination of "length" and the selected granularity of the lines, no line will be drawn if its endpoint would be more than 500 bars in the future.
  • Code for "Double SMA" only affected the predicted data values, rather than affecting the historic calculations (and standard deviation calcs) as well as the predictions. This has been included and results in much smoother lines when "Double Moving Average" is selected.
  • Striped lines for the predicted values - firstly to make it obvious where the "predictions" begin, and also because they look funky.

Note di rilascio

Minor code update such that predicted values are only calculated on the last bar - as they're not needed before that point. Hopefully further improve execution times.

Note di rilascio

Thanks to BarefootJoey for the bug report - odd values for length now calculate without throwing an error.

Also built in some more configuration options from a screenshot they showed me where the lines were dashed rather than striped. So there's now a config option for dashed lines, which will also reduce the script overhead.

Still seeing occasional errors with the amount of computing power when length is set to huge numbers (> 1000) so the next edition may be a rewrite to only delete / move lines if they have actually changed position rather than needlessly deleting and redrawing on each tick.
Commenti
Bull_Bear003
Interesting script! May I ask how you find/calculate the values for the projected dashed lines (not "Show Future")?

Also, the indicator doesn't show the values for the dashed lines (again with "Show Future" unticked). How can I read the value easily without pointing the cursor to the dashed line?
m_b_round
@Bull_Bear003, The original idea was taken from @ValiantHero who explains it better than I can (see the info in the script) but I'll try and explain here. If you have a sequence of 40 candle close values, and you want a moving average length 10, the average of candles 20-29 makes more sense to be used as the average at the time of candle 25 than it does at candle 29. So for historic data that's not a problem, you just shift all the averages to the left by length/2 bars. However that then leaves you with length/2 bars of current data for which you don't have an average. This is where the solid lines stop and the dashed lines start (before the show future bit), as to calculate these, we take the last bar for which we have an average that we can calculate (so in our example above that's candles 31-40, which would be displaced to be the value plotted above bar 35). We then use the close of bar 40 again to work out the average for bar 36 (which becomes the average of 32-40 + 40 again),. Bar 37 has 33-40 + 40 + 40 and so on.

Values for the dashed lines aren't plots, they're actually lines drawn on the chart, which is why they don't show the values. It might be possible to put a label to the right of the end of the dashed lines if that would help ? Of course it could only show the values for the current bar, but that's better than nothing I guess?
BarefootJoey
Wonderful job, Mike. Beautifully debugged & improved. Thank you so much for this!!
Altro