TradingView
Wilson-IV
8 de Nov de 2020 17:41

Array SMA 

Zcash / TetherUSBinance

Descrição

Calcuating SMA on an Array
In this script i show you how to calculate SMA on an array.
Several values are plotted just for illustration.

Steps to follow:
- make sure you have an array with values (source array)
- create a blank array (pref. with the same size)
- call the function array_sma

This function fills the empty array with the SMA values of the source array.

Notas de Lançamento

Updates:
- EMA Array added
- Indexes now inline with Pinescript
Comentários
UnknownUnicorn11728497
@Wilson-IV
Hey, thanks for this. I am currently creating an Optimizer, which calculates the ROI of a Strategy using the Indicator Foundation.

That works now, and I want to compare dozens of moving averages with one another, for which I want to use arrays for.
Once that is added we will use a Order Frequency Filter, meaning we find the strategy that has the most orders and the strategy that got triggered the least and develop a ratio out of that.
We could easily add Slippage and Fees later.
Strategies are more reliable the higher their order frequency is.
Wilson-IV
@hgmahan, Thanks for your message. This script was created in 2020. Nowadays TV has Matrices which can give you more flexibility then arrays. Matrices has MxN dimension while arrays only has (so far i know) 1-dimensional. Imho i think matrices will help you far more better then arrays.
UnknownUnicorn11728497
@Wilson-IV, Thanks for your advice, will try to put it into use. WIll grant you access too once it's done
ckppmarathe
It seems that index = 0 is the oldest value in the time series that the array represents. In normal pinescript series the index = 0 is the newest value.

If this is correct, can you modify the code so that index = 0 gives the newest value to be in line with pinescript?
Wilson-IV
@ckppmarathe, Thank you for your feedback. You are right. E.x. close(0) is the most recent value then close(4), so in this case i need to use index = 0 as the newest value then continue to index = 4 as the oldes.
I will change the code so that index = 0 will be the newest value according the way pinescript works. Thanks again.
ckppmarathe
@Wilson-IV,

Have you revised the code? Are you going to post it?

Thanks and regards.
Wilson-IV
@ckppmarathe, Yes, just did it a few seconds ago.
ckppmarathe
@Wilson-IV,

Are you going to post the revised code soon?
Wilson-IV
@ckppmarathe, The code is already up to date.
Wilson-IV
//===============================================================================
//=== EMA calculation
//===============================================================================
array_ema(array_source, array_sma_source, array_ema_destination, array_size, ema_lenght) =>

for dbl_Temp1 = 0 to array_size - 1

aplha = 2 / (ema_lenght + 1)
sum_ema = 0.
index = round(dbl_Temp1)

if dbl_Temp1 == 0

//Fill only the first array slot with the SMA value
array.set(array_ema_destination, index, array.get(array_sma_source, dbl_Temp1))

else if dbl_Temp1 > 0

//Fill array slots from index = 1 to the last index with EMA value
array.set(array_ema_destination, index, (aplha * array.get(array_source, dbl_Temp1)) + (1 - aplha) * array.get(array_ema_destination, dbl_Temp1 - 1))

//=== Fill array_EMA with EMA values
array_ema(array_test, array_SMA, array_EMA, 5, 2)
Mais