TASC 2022.05 Relative Strength Exponential Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PineCodersTASC

//  TASC Issue: May 2022 - Vol. 40, Issue 6
//     Article: Relative Strength Moving Averages
//              Part 1: The Relative Strength 
//              Exponential Moving Average (RS EMA)
//  Article By: Vitali Apirine
//    Language: TradingView's Pine Script v5
// Provided By: PineCoders, for tradingview.com

//@version=5
indicator('TASC 2022.05 Relative Strength EMA', 'RSEMA', true)

float src   = input.source(close,  'Source:')
int periods = input.int(50,    'EMA Length:', minval=14)
int     pds = input.int(50,     'RS Length:', minval=4)
float  mltp = input.int(10, 'RS Multiplier:', minval=0)

rsema(float source     = close, 
 simple int emaPeriod  = 50, 
 simple int  rsPeriod  = 50, 
      float multiplier = 10.0
     ) =>
    var float mltp1 = 2.0 / (emaPeriod + 1.0)
    var float coef1 = 2.0 /  (rsPeriod + 1.0)
    var float coef2 = 1.0 - coef1
    float diff = source - nz(source[1], source)
    float cup  = diff > 0 ?          diff  : 0.0
    float cdn  = diff < 0 ? math.abs(diff) : 0.0
    float acup = na, acup := coef1 * cup + coef2 * nz(acup[1])
    float acdn = na, acdn := coef1 * cdn + coef2 * nz(acdn[1])
    float rs   = math.abs(acup - acdn) / (acup + acdn)
    float rate = mltp1 * (1.0 + nz(rs, 0.00001) * multiplier)
    float rsma = na
    rsma := rate * source + (1.0 - rate) * nz(rsma[1], source)
    rsma

float rsema =  rsema(src, periods, pds, mltp)

plot(rsema, title='RS EMA', color=#009900, linewidth=2)
