Pine Script/Trading 视图 - 更改用于计算随机指标(%K 和 %D)的移动平均线类型
Pine Script/Trading View - Change the Moving Average type used to calculate Stochastic (%K and %D)
我正在尝试对脚本进行一些修改,以便能够使用输入中的下拉菜单(使用 'options' 参数).
这是我用作起点的代码:
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)
脚本随后将在 Stoch RSI1 和 Stoch RSI2 之间的交叉点和下交叉点创建标签。
下面的代码是我修改过的。我添加了使用 Pine Script 内置函数在不同 MA 计算之间进行选择的可能性。
问题是,经过这些修改后,crossover/crossunder 标签(当 'MAtype' 设置为 SMA 时)与我使用 'original' 脚本(那个其中所有计算均使用 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()
标签应该在相同的位置,因为它应该使用相同的计算。我不知道我的代码有什么问题。
我尝试了十几种不同的东西,例如:
使用 4 个单独的 'MAtype' 输入(Stoch RSI 1 和 2 每个 %K 和 %D),
return函数最后一行的一个变量,
list 'MAtype'作为函数括号中的参数,
...
但无论如何,我无法获得与原始脚本相同的 'output'(我在这里只谈论 SMA)。代码是 'working'(我没有收到任何错误消息),但结果不同。
有人知道是什么原因造成的吗?非常感谢任何帮助。
您的代码片段正在计算局部 if 范围内的移动平均函数,这可能会导致数据丢失和结果不一致。
下面示例中的 f_customMa() 函数 select 基于 MAtype1/2 输入的平均类型:
// 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)
我正在尝试对脚本进行一些修改,以便能够使用输入中的下拉菜单(使用 'options' 参数).
这是我用作起点的代码:
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)
脚本随后将在 Stoch RSI1 和 Stoch RSI2 之间的交叉点和下交叉点创建标签。
下面的代码是我修改过的。我添加了使用 Pine Script 内置函数在不同 MA 计算之间进行选择的可能性。
问题是,经过这些修改后,crossover/crossunder 标签(当 'MAtype' 设置为 SMA 时)与我使用 'original' 脚本(那个其中所有计算均使用 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()
标签应该在相同的位置,因为它应该使用相同的计算。我不知道我的代码有什么问题。
我尝试了十几种不同的东西,例如:
使用 4 个单独的 'MAtype' 输入(Stoch RSI 1 和 2 每个 %K 和 %D),
return函数最后一行的一个变量,
list 'MAtype'作为函数括号中的参数,
...
但无论如何,我无法获得与原始脚本相同的 'output'(我在这里只谈论 SMA)。代码是 'working'(我没有收到任何错误消息),但结果不同。
有人知道是什么原因造成的吗?非常感谢任何帮助。
您的代码片段正在计算局部 if 范围内的移动平均函数,这可能会导致数据丢失和结果不一致。
下面示例中的f_customMa() 函数 select 基于 MAtype1/2 输入的平均类型:
// 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)