如何在pinescript(Tradingview)中使全局变量

时间:2020-06-20 05:15:32

标签: variables abstract pine-script trend

我正在努力在tradingview中创建趋势指标,以跟踪趋势的发展方向。具体来说,我希望变量在几天内保持不变,但是当满足特定条件时它将改变。看起来这应该很简单,但是每次尝试时,我都会陷入一个永无止境的循环中,而且似乎无法将自己的头缠住。变量“趋势”


///Condition
pos = close > open
neg = close < open

pos_cond = pos and pos[1]
neg_cond = neg and neg[1]

///Variables to keep track of trend

Trend = iff(***pos_cond or neg_cond not met***, Trend[1], Trend + real_trend)

trend_change_neg = iff(pos_cond, 1, 0)
trend_change_pos = iff(neg_cond, -1, 0)

real_trend = trend_change_neg + trend_change_pos

Trend = iff(Trend > 2, 2, iff(Trend < -2, -2, Trend))

/////////plots
plotshape(Trend > 0, color = color.green, location = location.top, style = shape.square, title="TrendLong")

plotshape( Trend == 0, color = color.yellow, location = location.top, style = shape.square, title = "TrendNeutral")

plotshape( Trend < 0, color = color.red, location = location.top, style = shape.square, title = "TrendShort")

所以基本上我想要做的是保持趋势的运行总计,每次有2个连续的蜡烛与趋势相对时,它将切换到中性,但是随着趋势继续向1个方向移动,它可以恢复到+ -2(这是我们与中立点之间的距离不超过2个“回撤”。我已经花了好几天时间思考这个问题,但是如果有人有任何想法,将不胜感激。

1 个答案:

答案 0 :(得分:1)