Skip to main content
Arindam Ghosh
Algo Trader
Asked a question 2 years ago

Hi, Can anyone who uses Pine Script, please explain me what this below code is doing step by step? I want to code it in Python. Depth = input(12, title="Depth") Deviation = input(5, title="Deviation") lastlow = 0.0, lasthigh = 0.0 lastlow := nz(lastlow[1]) lasthigh := nz(lasthigh[1]) data(x) => d = security(syminfo.tickerid, timeframe.period, x) d getLow(x, y, z, a) => lastlow = y v = data(x) m = v==lastlow or data(z) - v > a*syminfo.mintick if v!=lastlow lastlow := v if m v := 0.0 [v,lastlow] getHigh(x, y, z, a) => lasthigh = y v = data(x) m = v==lasthigh or v - data(z) > a*syminfo.mintick if v!=lasthigh lasthigh := v if m v := 0.0 [v,lasthigh] [v,e] = getLow(lowest(Depth), lastlow, low, Deviation) lastlow := e zBB = v != 0.0 [v1,e1] = getHigh(highest(Depth), lasthigh, high, Deviation) lasthigh := e1 zSS = v1 != 0.0 zigzagDirection = -1 zigzagHigh = 0 zigzagLow = 0 zigzagDirection := zBB ? 0 : zSS ? 1 : nz(zigzagDirection[1], -1) virtualLow = zigzagLow[1] + 1 if not zBB or (zBB and zigzagDirection == zigzagDirection[1] and low > low[virtualLow]) zigzagLow := nz(zigzagLow[1]) + 1 virtualHigh = zigzagHigh[1] + 1 if not zSS or (zSS and zigzagDirection == zigzagDirection[1] and high < high[virtualHigh]) zigzagHigh := nz(zigzagHigh[1]) + 1 line zigzag =, low[zigzagLow], bar_index-zigzagHigh, high[zigzagHigh],, style=line.style_solid, width=2) if (zigzagDirection == zigzagDirection[1]) line.delete(zigzag[1])

Join FYERS Community to pick others' brains on Trading/Investing

I will recommend you strongly to learn Pinescript, check my reasons below:

TradingView vs Python

1. TradingView has almost 1000x trading users compared to Python. Pinescript is 100x easier than Python.

2. TradingView(TV) has the largest, ready-to-use financial markets data set. It's a heaven for data analysts. In Python you pull data all by yourself and manage database. Its time consuming and takes your focus away from trading.

3. TV has top-class UI with top-class charting; Python has no UI.  

4. It's much easier to debug and keep track of strategy issues in TV. Suppose I have 5 conditions to trigger a BUY. If my Buy is falsely triggered because of improper data, or error in coding anyone of the condition, I can SEE easily on the chart.

5. Time to ideate any strategy, and analyze data is way faster. Suppose a new idea comes to my mind, its very easier on TV to check it on charts before getting into coding and backtesting.  

6. You are never bound to the limitations of TV. You can integrate it with Python or anything

7.Because TV has more users, it has much bigger supporting community for ideas, tutorials, strategies, execution utilities etc.

Get started with pinescript here