将 thinkscript 的 AdaptiveEMA 转换为 pine 脚本
Convert thinkscript's AdaptiveEMA to pine script
Thinkscript 有一个看似简单的函数来计算自适应 EMA。但是,它使用了thinkscript内置的CompoundValue函数,这在Pine Script中是不可用的。
我在 Pine Script 中重新创建 CompoundValue 函数时遇到问题。它应该是递归的,但我在另一个答案中看到它实际上是某种嵌套的 if 语句。
AdaptiveEMA 的 thinkscript 代码:
input price = close;
input length = 10;
input highLowLength = 10;
def multiplier1 = 2 / (length + 1);
def multiplier2 = AbsValue((close - Lowest(low, highLowLength)) - (Highest(high, highLowLength) - close)) / (Highest(high, highLowLength) - Lowest(low, highLowLength));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (price - ma[1]), Average(price, length));
plot AEMA = ma;
AEMA.SetDefaultColor(GetColor(1));
我正在尝试使它成为一个函数,这样我就可以在一个图表上有两个 AdaptiveEMA 并像这样调用它们:
plot AEMAshort = adaptiveEMA(close, 25, 25);
plot AEMAlong = adaptiveEMA(close, 50, 50);
让我对 CompoundValue 感到厌烦的事情之一是第一个参数是第一个参数。来自 thinkscript 参考资料:
Calculates a compound value according to following rule: if a bar
number is greater than length then the visible data value is returned,
otherwise the historical data value is returned. This function is used
to initialize studies with recursion.
如果 AdapativeEMA 脚本中 CompoundValue 的第一个参数是 1,除了一天的第一分钟外,barindex 不是几乎总是大于 1 吗?
我怎样才能分解 Pine Script 中的 CompoundValue 函数并在此处使用它?
任何帮助将不胜感激。
//@version=5
indicator("adaptive ema", overlay = true)
price1 = input.source(close, title = "price 1")
price2 = input.source(close, title = "price 2")
length1 = input.int(10, title = "length 1")
highLowLength1 = input.int(10, title = "highlow length 1")
length2 = input.int(20, title = "length 2")
highLowLength2 = input.int(20, title = "highlow length 2")
f_adaptiveEMA(_price, _length, _highLowLength) =>
_multiplier1 = 2 / (_length + 1)
_multiplier2 = math.abs((_price - ta.lowest(low, _highLowLength)) - (ta.highest(high, _highLowLength) - _price)) / (ta.highest(high, _highLowLength) - ta.lowest(low, _highLowLength))
_alpha = _multiplier1 * (1 + _multiplier2)
float _ma = na
_ma := _price * _alpha + (1 - _alpha) * nz(_ma[1], _price)
_ma
ma1 = f_adaptiveEMA(price1, length1, highLowLength1)
ma2 = f_adaptiveEMA(price2, length2, highLowLength2)
plot(ma1, color = color.yellow)
plot(ma2, color = color.red)
Thinkscript 有一个看似简单的函数来计算自适应 EMA。但是,它使用了thinkscript内置的CompoundValue函数,这在Pine Script中是不可用的。
我在 Pine Script 中重新创建 CompoundValue 函数时遇到问题。它应该是递归的,但我在另一个答案中看到它实际上是某种嵌套的 if 语句。
AdaptiveEMA 的 thinkscript 代码:
input price = close;
input length = 10;
input highLowLength = 10;
def multiplier1 = 2 / (length + 1);
def multiplier2 = AbsValue((close - Lowest(low, highLowLength)) - (Highest(high, highLowLength) - close)) / (Highest(high, highLowLength) - Lowest(low, highLowLength));
def alpha = multiplier1 * (1 + multiplier2);
def ma = CompoundValue(1, ma[1] + alpha * (price - ma[1]), Average(price, length));
plot AEMA = ma;
AEMA.SetDefaultColor(GetColor(1));
我正在尝试使它成为一个函数,这样我就可以在一个图表上有两个 AdaptiveEMA 并像这样调用它们:
plot AEMAshort = adaptiveEMA(close, 25, 25);
plot AEMAlong = adaptiveEMA(close, 50, 50);
让我对 CompoundValue 感到厌烦的事情之一是第一个参数是第一个参数。来自 thinkscript 参考资料:
Calculates a compound value according to following rule: if a bar number is greater than length then the visible data value is returned, otherwise the historical data value is returned. This function is used to initialize studies with recursion.
如果 AdapativeEMA 脚本中 CompoundValue 的第一个参数是 1,除了一天的第一分钟外,barindex 不是几乎总是大于 1 吗?
我怎样才能分解 Pine Script 中的 CompoundValue 函数并在此处使用它? 任何帮助将不胜感激。
//@version=5
indicator("adaptive ema", overlay = true)
price1 = input.source(close, title = "price 1")
price2 = input.source(close, title = "price 2")
length1 = input.int(10, title = "length 1")
highLowLength1 = input.int(10, title = "highlow length 1")
length2 = input.int(20, title = "length 2")
highLowLength2 = input.int(20, title = "highlow length 2")
f_adaptiveEMA(_price, _length, _highLowLength) =>
_multiplier1 = 2 / (_length + 1)
_multiplier2 = math.abs((_price - ta.lowest(low, _highLowLength)) - (ta.highest(high, _highLowLength) - _price)) / (ta.highest(high, _highLowLength) - ta.lowest(low, _highLowLength))
_alpha = _multiplier1 * (1 + _multiplier2)
float _ma = na
_ma := _price * _alpha + (1 - _alpha) * nz(_ma[1], _price)
_ma
ma1 = f_adaptiveEMA(price1, length1, highLowLength1)
ma2 = f_adaptiveEMA(price2, length2, highLowLength2)
plot(ma1, color = color.yellow)
plot(ma2, color = color.red)