'Tradingview Pinescript to Python Conversion
I've been trying to convert the following Pinescript to Python without much success. I've been able to convert most of it but where I'm getting stuck is snippet surrounded by the "***" below. I've tried picking it apart line by line and I tried different permutations in Python and I can't get it to work fully. Has anyone attempted something similar? This is the original link
https://www.tradingview.com/script/n8AGnIZd-Divergence-for-Many-Indicators-v4/
Pinescript Code
positive_regular_positive_hidden_divergence(src, cond)=>
divlen = 0
prsc = source == "Close" ? close : low
// if indicators higher than last value and close price is higher than las close
if dontconfirm or src > src[1] or close > close[1]
startpoint = dontconfirm ? 0 : 1 // don't check last candle
// we search last 15 PPs
for x = 0 to maxpp - 1
len = bar_index - array.get(pl_positions, x) + prd
// if we reach non valued array element or arrived 101. or previous bars then we don't search more
if array.get(pl_positions, x) == 0 or len > maxbars
break
if len > 5 and
((cond == 1 and src[startpoint] > src[len] and prsc[startpoint] < nz(array.get(pl_vals, x))) or
(cond == 2 and src[startpoint] < src[len] and prsc[startpoint] > nz(array.get(pl_vals, x))))
slope1 = (src[startpoint] - src[len]) / (len - startpoint)
virtual_line1 = src[startpoint] - slope1
slope2 = (close[startpoint] - close[len]) / (len - startpoint)
virtual_line2 = close[startpoint] - slope2
arrived = true
***for y = 1 + startpoint to len - 1
if src[y] < virtual_line1 or nz(close[y]) < virtual_line2
arrived := false
break
virtual_line1 := virtual_line1 - slope1
virtual_line2 := virtual_line2 - slope2
if arrived
divlen := len
break***
divlen
Solution 1:[1]
This was a pain but I figured it out. The looping construct was exactly what it looks like. The issues were in my original code. the below works fine but is slow. Can anyone suggest a faster looping mechanism?
for idx in df.index:
try:
for y in range(2,int(df['len'][idx])):
if df['calcinner'][idx] == 1:
if ((df[metric][idx - y] < df['vline1'][idx]) | (df['ha_close'][idx - y] < df['vline2'][idx]))==True:
df['arrived'][idx] = 0
break
df['vline1'][idx] = df['vline1'][idx] - df['slope1'][idx]
df['vline2'][idx] = df['vline2'][idx] - df['slope2'][idx]
if df['calcinner'][idx] == 1 & df['arrived'][idx] == 1:
df['diverge'][idx] = df['diverge'][idx] + df['len'][idx]
except:
print('skip this line')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
