首页 > 解决方案 > Pine 脚本/交易视图 - 更改用于计算随机指标的移动平均线类型(%K 和 %D)

问题描述

我正在尝试对脚本进行一些修改,以便能够使用输入中的下拉菜单(使用“选项”参数)更改用于计算 %K 和 %D 的移动平均线类型。

这是我用作起点的代码:

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 计算之间进行选择的可能性。

问题在于,通过这些修改,交叉/交叉标签(当“MAtype”设置为 SMA 时)与我使用“原始”脚本时(所有计算均使用 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()

标签应该位于相同的位置,因为它应该使用相同的计算。我无法弄清楚我的代码有什么问题。

我尝试了十几种不同的方法,例如:

但无论如何,我无法获得与原始脚本相同的“输出”(我在这里只谈论 SMA)。该代码正在“工作”(我没有收到任何错误消息),但结果不同。

有谁知道是什么原因造成的?很感谢任何形式的帮助。

标签: pine-script

解决方案


您的代码片段正在计算本地 if 范围内的移动平均函数,这可能导致数据丢失和结果不一致。

下面示例中的 f_customMa() 函数根据 MA​​type1/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)

推荐阅读