RicardoSantos

Function Simple Moving Average

207 2 99
Function describing Simple Moving Average
Remove from Favorite Scripts Add to Favorite Scripts
//@version=2
study(title='Function Simple Moving Average', overlay=true)
src = input(close)
length = input(10)

f_sma(_src, _length)=>
    _length_adjusted = _length < 1 ? 1 : _length
    _sum = 0
    for _i = 0 to (_length_adjusted - 1)
        _sum := _sum + _src[_i]
    _return = _sum / _length_adjusted

plot(sma(src, length), color=color(gray, 0), linewidth=5)
plot(f_sma(src, length), color=color(blue, 0), linewidth=1)

Thank you Richard: Why is there an "_" before every variable, function and constant in your formula? Michael
Reply
RicardoSantos PRO StockSwinger
5 months ago
just a way to identify local variables inside a function/loop/multiline statement, personal variable naming method.
Reply
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out