TradingView
CrackingCryptocurrency
28 feb 2022 16:43

MovingAveragesLibrary 

Bitcoin / U.S. dollarBitstamp

Beskrivning

Library "MovingAveragesLibrary"
This is a library allowing one to select between many different Moving Average formulas to smooth out any float variable.

You can use this library to apply a Moving Average function to any series of data as long as your source is a float.

The default application would be for applying Moving Averages onto your chart. However, the scope of this library is beyond that. Any indicator or strategy you are building can benefit from this library.

You can apply different types of smoothing and moving average functions to your indicators, momentum oscillators, average true range calculations, support and resistance zones, envelope bands, channels, and anything you can think of to attempt to smooth out noise while finding a delicate balance against lag.

If you are developing an indicator, you can use the 'ave_func' to allow your users to select any Moving Average for any function or variable by creating an input string with the following structure:

var_name = input.string(<title>, <defval>, <options>)

Where the types of Moving Average you would like to be provided would be included in options.

Example:

i_ma_type = input.string(title = "Moving Average Type", defval = "Hull Moving Average", options = )

Where you would add after options the strings I have included for you at the top of the PineScript for your convenience.

Then for the output you desire, simply call 'ave_func' like so:

ma = ave_func(source, length, i_ma_type)

Now the plotted Moving Average will be the same as what you or your users select from the Input.

ema(src, len) Exponential Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: Float value.

sma(src, len) Simple Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: Float value.

rma(src, len) Relative Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: Float value.

wma(src, len) Weighted Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: Float value.

dv2(len) Donchian V2 function.
  Parameters:
    len: Lookback length to use.
  Returns: Open + Close / 2 for the selected length.

ModFilt(src, len) Modular Filter smoothing function.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: Float value.

EDSMA(src, len) Ehlers Dynamic Smoothed Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: EDSMA smoothing.

dema(x, t) Double Exponential Moving Average.
  Parameters:
    x: Series to use ('close' is used if no argument is supplied).
    t: Lookback length to use.
  Returns: DEMA smoothing.

tema(src, len) Triple Exponential Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: TEMA smoothing.

smma(x, t) Smoothed Moving Average.
  Parameters:
    x: Series to use ('close' is used if no argument is supplied).
    t: Lookback length to use.
  Returns: SMMA smoothing.

vwma(x, t) Volume Weighted Moving Average.
  Parameters:
    x: Series to use ('close' is used if no argument is supplied).
    t: Lookback length to use.
  Returns: VWMA smoothing.

hullma(x, t) Hull Moving Average.
  Parameters:
    x: Series to use ('close' is used if no argument is supplied).
    t: Lookback length to use.
  Returns: Hull smoothing.

covwma(x, t) Coefficient of Variation Weighted Moving Average.
  Parameters:
    x: Series to use ('close' is used if no argument is supplied).
    t: Lookback length to use.
  Returns: COVWMA smoothing.

frama(x, t) Fractal Reactive Moving Average.
  Parameters:
    x: Series to use ('close' is used if no argument is supplied).
    t: Lookback length to use.
  Returns: FRAMA smoothing.

kama(x, t) Kaufman's Adaptive Moving Average.
  Parameters:
    x: Series to use ('close' is used if no argument is supplied).
    t: Lookback length to use.
  Returns: KAMA smoothing.

donchian(len) Donchian Calculation.
  Parameters:
    len: Lookback length to use.
  Returns: Average of the highest price and the lowest price for the specified look-back period.

tma(src, len) Triangular Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: TMA smoothing.

VAMA(src, len) Volatility Adjusted Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: VAMA smoothing.

Jurik(src, len) Jurik Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: JMA smoothing.

MCG(src, len) McGinley smoothing.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: McGinley smoothing.

zlema(series, length) Zero Lag Exponential Moving Average.
  Parameters:
    series: Series to use ('close' is used if no argument is supplied).
    length: Lookback length to use.
  Returns: ZLEMA smoothing.

xema(src, len) Optimized Exponential Moving Average.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    len: Lookback length to use.
  Returns: XEMA smoothing.

EhlersSuperSmoother(src, lower) Ehlers Super Smoother.
  Parameters:
    src: Series to use ('close' is used if no argument is supplied).
    lower: Smoothing value to use.
  Returns: Ehlers Super smoothing.

EhlersEmaSmoother(sig, smoothK, smoothP) Ehlers EMA Smoother.
  Parameters:
    sig: Series to use ('close' is used if no argument is supplied).
    smoothK: Lookback length to use.
    smoothP: Smothing value to use.
  Returns: Ehlers EMA smoothing.

ave_func(in_src, in_len, in_type) Returns the source after running it through a Moving Average function.
  Parameters:
    in_src: Series to use ('close' is used if no argument is supplied).
    in_len: Lookback period to be used for the Moving Average function.
    in_type: Type of Moving Average function to use. Must have a string input to select the options from that MUST match the type-casing in the function below.
  Returns: The source as a float after running it through the Moving Average function.
Kommentarer
greysevere
Love this library -
Quick question though, is there no way to use the historical operator with these? I'm trying to pull old values on the smma.
CrackingCryptocurrency
@greysevere, Thanks! Glad you find it helpful.

I'm not too familiar with your specific use case, and unfortunately can't offer any advice off the top of my head. Can you share the operation you're trying to pull off and I can give you my thoughts? (ie: share the code line)
greysevere
@CrackingCryptocurrency, Hey! Thanks for the reply. Actually, I figured it out. I just had an error in my code that was causing it not to work as expected. I was just referring to the standard historical brackets to go back x number of bars for the data you need. Your library works fine, I was just doing it wrong. HAHA.
Thanks again! I use this library in all of my indicators/studies. I'm mostly just building to learn how the market moves right now, and plan to get into more elaborate uses down the road.
A_Traders_Edge
Thank you for this. Especially the universal Smoothing Function. Makes things a LOT easier. Surprised no other libraries have this. Either way....WAY TO GO!!!
CrackingCryptocurrency
@chasinalts, Thank you! :)
StevyHa
Thank you for this library. I implemented it in my script watching your youtube video. Can you please add Tilson Moving Average (T3) ?

I also added Multi time frame .. here is the way I did it following your video on youtube

//Main MA
import CrackingCryptocurrency/MovingAveragesLibrary/1 as ccma
MainMaEnabled = input.bool(defval = true, title = 'BelowAbove MA Enabled?' , group = 'Below Above MA Strategy')
BelowAbove_MA_TF = input.timeframe(title='BelowAbove T/Frame', defval='D' , group = 'Below Above MA Strategy')
MA_Type = input.string(title= "BelowAbove MA Type", defval="Relative Moving Average" , options=[
"Arnaud Legoux Moving Average",
"Double Exponential Moving Average",
"Donchian",
"Donchian v2",
"Ehlers EMA Smoother",
"Ehlers Dynamic Smoothed Moving Average",
"Exponential Moving Average",
"Exponential Moving Average Optimized",
"Fractal Adaptive Moving Average",
"Hull Moving Average",
"Jurik Moving Average",
"Kaufman's Adaptive Moving Average",
"Least Squares Moving Average",
"McGinley",
"Modular Filter",
"Relative Moving Average",
"Simple Moving Average",
"Smoothed Moving Average",
"Triple Exponential Moving Average",
"Triangular Moving Average",
"Volatility Adjusted Moving Average",
"Volume Weighted Moving Average",
"Weighted Moving Average",
"Zero-Lag Exponential Moving Average",
"Coefficient of Variation Weighted Moving Average"
],group = 'Below Above MA Strategy')
MainMALen = input.int(defval = 20 , step= 1 , title = 'BelowAbove MA Length', minval= 2 , group = 'Below Above MA Strategy')

//Calculations
RSMainMA = ccma.ave_func(close, MainMALen,MA_Type)
//outputs
MainMA = request.security(syminfo.tickerid, BelowAbove_MA_TF, RSMainMA[barstate.isconfirmed ? 0 : 1])
plot(series = MainMaEnabled ? MainMA : na, color=color.new(color.white, 0) ,title = 'Main EMA', linewidth = 1, style = plot.style_line)
junkie190690
Thank you! Is there a way to adjust additional parameters for ma's i.e. KAMA's alpha etc? Or is that not possible through libraries?
Mer