简体   繁体   中英

How to make global variable in pinescript(Tradingview)

I am working on creating a trend indicator in tradingview to track which way the trend is going. Specifically, I want a variable that will stay the same over days, but when a certain condition is met it will change. It seems like it should be simple to do, but everytime I try I get thrown into a never ending loop and I can't seem to wrap my head around it. Variable "Trend"


///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")

So basically what I want to do is keep a running total for Trend where each time there are 2 consecutive candles against the trend it will switch to neutral, but as the trend continues to move in 1 direction it can build back up to +-2 (This was we are never more than 2 "pullbacks" away from neutral. I've racked my brain over this for days now, but if anyone has any ideas any help would be appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM