Pine Script/Trading View - Change the Moving Average type used to calculate Stochastic (%K and %D)

Viewed 287

I'm trying to make some modifications to a script, to be able to change the Moving Average type used to calculate the %K and %D, using a drop-down menu in the inputs (with the 'options' parameter).

This is the code that I'm using as the starting point:

RSI1_length = input(title="Length Stoch RSI 1", type=input.integer, defval=14)
RSI2_length = input(title="Length Stoch RSI 2", type=input.integer, defval=14)
rsi2Time = input(title="Multiplier for Stoch_RSI_time_2", type=input.integer, defval=3)

rsi1 = rsi(close, RSI1_length)
rsi1_k = sma(stoch(rsi1, rsi1, rsi1, RSI1_length), 3)
rsi1_d = sma(rsi1_k, 3)
rsi2 = rsi(close, RSI2_length * rsi2Time)
rsi2_k = sma(stoch(rsi2, rsi2, rsi2, RSI2_length * rsi2Time), 3 * rsi2Time)
rsi2_d = sma(rsi2_k, 3 * rsi2Time)

The script will then create labels on crossover and crossunder between Stoch RSI1 and Stoch RSI2.

The code below is the one with my modifications. I added the possibility to choose between different MA calculations using Pine Script built-in functions.

The problem is that with these modifications, the crossover/crossunder labels (when 'MAtype' is set to SMA) are not the same as when I use the 'original' script (the one where all the calculations are made using SMA).

MAType = input(title="Type", defval="SMA", options=["SMA", "HMA"])

//Stoch RSI 1
rsi1 = rsi(close, RSI1_length_A)

ma_k() =>
    if MAType == "SMA"
        sma(stoch(rsi1, rsi1, rsi1, RSI1_length_A), 3)
    else if MAType == "HMA"
        hma(stoch(rsi1, rsi1, rsi1, RSI1_length_A), 3)

rsi1_k = ma_k()

ma_d() =>
    if MAType == "SMA"
        sma(rsi1_k, 3)
    else if MAType == "HMA"
        hma(rsi1_k, 3)

rsi1_d = ma_d()

//Stoch RSI 2
rsi2 = rsi(close, RSI2_length_A * rsi2Time)

ma_k2() =>
    if MAType == "SMA"
        sma(stoch(rsi2, rsi2, rsi2, RSI2_length_A * rsi2Time), 3 * rsi2Time)
    else if MAType == "HMA"
        hma(stoch(rsi2, rsi2, rsi2, RSI2_length_A * rsi2Time), 3 * rsi2Time)

rsi2_k = ma_k2()

ma_d2() =>
    if MAType == "SMA"
        sma(rsi2_k, 3 * rsi2Time)
    else if MAType == "HMA"
        hma(rsi2_k, 3 * rsi2Time)

rsi2_d = ma_d2()

The labels should be at the same positions since it's supposed to use the same calculations. I can't figure what's wrong with my code.

I tried a dozen different things, such as:

  • using 4 separate 'MAtype' inputs (one for each %K and %D for Stoch RSI 1 and 2),

  • return a variable on the last line of the function,

  • list 'MAtype' as an argument in the function parentheses,

  • ...

but no matter what, I cannot get the same 'output' that the original script (I'm only talking of SMA here). The code is 'working' (I'm not getting any error message), but the results are different.

Does anyone have any idea about what causes that? Any help is very much appreciated.

1 Answers

Your code snippet is calculating Moving average functions in local if scope that could lead to missing data and inconsistent results.

f_customMa() function in the example below select the average type based on the MAtype1/2 inputs:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © e2e4mfck
//@version=4
study("Double Stoch with MA selector")

RSI1_length_A = input(title="Length Stoch RSI 1", type=input.integer, defval=14)
RSI2_length_A = input(title="Length Stoch RSI 2", type=input.integer, defval=14)
rsi2Time = input(title="Multiplier for Stoch_RSI_time_2", type=input.integer, defval=3)

MAType1 = input(title="Type1", defval="SMA", options=["SMA", "HMA"])
MAType2 = input(title="Type2", defval="SMA", options=["SMA", "HMA"])


f_customMa(_type, _source, _length) =>
    _result = _type == 'SMA' ? sma(_source, _length) :
      _type == 'HMA' ? hma(_source, _length) : na


//Stoch RSI 1
rsi1 = rsi(close, RSI1_length_A)
rsi1_k = f_customMa(MAType1, stoch(rsi1, rsi1, rsi1, RSI1_length_A), 3)
rsi1_d = f_customMa(MAType1, rsi1_k, 3)

plot(rsi1_k, "K1", color=#2962FF)
plot(rsi1_d, "D1", color=#FF6D00)

//Stoch RSI 2
rsi2 = rsi(close, RSI2_length_A * rsi2Time)
rsi2_k = f_customMa(MAType2, stoch(rsi2, rsi2, rsi2, RSI2_length_A * rsi2Time), 3)
rsi2_d = f_customMa(MAType2, rsi2_k, 3)

plot(rsi2_k, "K2", color=#2962FF)
plot(rsi2_d, "D2", color=#FF6D00)
Related