首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将thinkscript的AdaptiveEMA转换为pine脚本

将thinkscript的AdaptiveEMA转换为pine脚本
EN

Stack Overflow用户
提问于 2021-12-29 18:51:55
回答 1查看 106关注 0票数 0

Thinkscript有一个看似简单的函数来计算一个自适应EMA。然而,它使用了thinkscript内置的CompoundValue函数,这在松树脚本中是不可用的。

我在用松树脚本重新创建CompoundValue函数时遇到了困难。它应该是递归的,但我在另一个答案中看到,它实际上是某种嵌套的if语句。

AdaptiveEMA的thinkscript代码:

代码语言:javascript
复制
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));

我试图将其作为一个函数,以便在一个图表上有两个AdaptiveEMAs,并按如下方式调用它们:

代码语言:javascript
复制
plot AEMAshort = adaptiveEMA(close, 25, 25);
plot AEMAlong = adaptiveEMA(close, 50, 50);

让我对CompoundValue感兴趣的是,第一个参数是第一个参数。从thinkscript参考:

根据以下规则计算复合值:如果条号大于长度,则返回可见数据值,否则返回历史数据值。此函数用于递归初始化学习。

如果CompoundValue脚本中的第一个参数是1,那么除了一天中的第一分钟之外,barindex几乎总是大于1吗?

我如何在松树脚本中分解CompoundValue函数并在这里使用它?任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-30 08:54:14

代码语言:javascript
复制
//@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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70523733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档