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.