Reputation: 1716
I'm converting a Pinescript Indicator to python to use it with pandas:
Here is pinescrip code:
RSI_Period = input(14,title='RSI')
SF = input(5,title='Slow Factor')
QQE=input(4.236)
Wilders_Period = RSI_Period * 2 - 1
Rsi = rsi(close,RSI_Period)
RsiMa = ema(Rsi, SF)
AtrRsi = abs(RsiMa[1] - RsiMa)
MaAtrRsi = ema(AtrRsi, Wilders_Period)
dar = ema(MaAtrRsi,Wilders_Period) * QQE
DeltaFastAtrRsi= dar
RSIndex=RsiMa
newshortband= RSIndex + DeltaFastAtrRsi
newlongband= RSIndex - DeltaFastAtrRsi
longband=RSIndex[1] > longband[1] and RSIndex > longband[1]?
max(longband[1],newlongband):newlongband
shortband=RSIndex[1] < shortband[1] and RSIndex < shortband[1]?
min(shortband[1], newshortband):newshortband
trend=cross(RSIndex, shortband[1])?1:cross(longband[1], RSIndex)?-1:nz(trend[1],1)
FastAtrRsiTL = trend==1? longband: shortband
plot(FastAtrRsiTL,color=red)
plot(RsiMa,color=yellow)
Now this is what I got so far in python:
RSIPeriod = 14
SF = 5
QQE=4.236
WildersPeriod = RSIPeriod * 2 - 1
df['RSI'] = ta.RSI(df['Price'], RSIPeriod)
df['RSI_ma'] = ta.EMA(df['RSI'], SF)
df['ATRrsi'] = abs(df['RSI_ma'].shift(1) - df['RSI_ma'])
df['MaATRrsi'] = ta.EMA(df['ATRrsi'], WildersPeriod)
df['dar'] = ta.EMA(df['MaATRrsi'], WildersPeriod) * QQE
df['newshortband'] = df['RSI_ma'] + df['dar']
df['newlongband'] = df['RSI_ma'] - df['dar']
df['longband'] = 0.0
df['longband'] = np.where( df['RSI_ma'].shift(1) > df['longband'].shift(1) and df['RSI_ma'] > df['longband'].shift(1),
max(df['longband'].shift(1) ,df['newlongband']), df['newlongband'])
df['shortband'] = 0.0
df['shortband'] = np.where( df['RSI_ma'].shift(1) < df['shortband'].shift(1) and df['RSI_ma'] < df['shortband'].shift(1),
max(df['shortband'].shift(1) ,df['newshortband']), df['newshortband'])
df['trend'] = np.where(df['RSI_ma'] > df['dar'].shift(1), 1, -1)
df['FastAtrRsiTL'] = np.where(df['trend'] == 1, df['longband'], df['shortband'])
And this is the error: ----> 4 df['longband'] = np.where( df['RSI_ma'].shift(1) > df['longband'].shift(1) and df['RSI_ma'] > df['longband'].shift(1), 5 max(df['longband'].shift(1) ,df['newlongband']), df['newlongband']) 6 df['shortband'] = 0.0
~\Anaconda3\envs\finance\lib\site-packages\pandas\core\generic.py in nonzero(self) 1571 raise ValueError("The truth value of a {0} is ambiguous. " 1572 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." -> 1573 .format(self.class.name)) 1574 1575 bool = nonzero
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Upvotes: 0
Views: 2096
Reputation: 1
I've managed to create a python function which takes a QQE indicator from TradingView. The input is a Pandas.DataFrame which needs to have the column 'Close' that contains the Close price of the candles.
You can ofcourse change the input to fit your own parameters and needs.
def QQE(_candles):
#https://www.tradingview.com/script/tJ6vtBBe-QQE/
Close = _candles.Close
Fast=2.6180
Slow=4.2360
RSI=14
SF=2
def WiMA(src, length):
MA_s = [0]
for i, x in enumerate(src):
MA_s.append( (x + (MA_s[i] * (length-1)))/length )
return MA_s
def crossovers(p1,p2):
a = []
for i in range(1,min(len(p1),len(p2))):
if p1[i] < p2[i] and not p1[i-1] < p2[i-1]:
a.append(True)
elif p1[i] > p2[i] and not p1[i-1] > p2[i-1]:
a.append(True)
else:
a.append(False)
return a
RSIndex = ta.ema(ta.rsi(Close, RSI, fillna=0), SF, fillna=0)
TR = [0]
for i in range(1, len(Close)):
TH = RSIndex[i-1] if RSIndex[i-1] > RSIndex[i] else RSIndex[i]
TL = RSIndex[i-1] if RSIndex[i-1] < RSIndex[i] else RSIndex[i]
TR.append(TH - TL)
AtrRsi= WiMA(TR, 14)
SmoothedAtrRsi= WiMA(AtrRsi, 14)
# FastQQE
DeltaFastAtrRsi = [x*Fast for x in SmoothedAtrRsi]
newlongband = [x - i for x, i in zip(RSIndex,DeltaFastAtrRsi)]
longband = [0]
for i in range(1,len(RSIndex)):
if RSIndex[i-1] > longband[i-1] and RSIndex[i] > longband[i-1]:
longband.append(max(longband[i-1],newlongband[i]))
else:
longband.append(newlongband[i])
newshortband = [x + i for x, i in zip(RSIndex,DeltaFastAtrRsi)]
shortband = [0]
for i in range(1,len(RSIndex)):
if RSIndex[i-1] < shortband[i-1] and RSIndex[i] < shortband[i-1]:
shortband.append(min(shortband[i-1],newshortband[i]))
else:
shortband.append(newshortband[i])
trend = [0,0]
shortbandCross = crossovers(RSIndex, [0]+shortband)
longbandCross = crossovers([0]+longband, RSIndex)
for i in range(1,len(shortbandCross)):
if shortbandCross[i] == True:
trend.append(1)
elif longbandCross[i] == True:
trend.append(-1)
else:
trend.append(trend[i])
FastAtrRsiTL = [longband[i] if trend[i] == 1 else shortband[i] for i in range(len(trend))]
# SlowQQE
DeltaSlowAtrRsi = [x*Slow for x in SmoothedAtrRsi]
newlongband1 = [x - i for x, i in zip(RSIndex,DeltaSlowAtrRsi)]
longband1 = [0]
for i in range(1,len(RSIndex)):
if RSIndex[i-1] > longband1[i-1] and RSIndex[i] > longband1[i-1]:
longband1.append(max(longband1[i-1],newlongband1[i]))
else:
longband1.append(newlongband1[i])
newshortband1 = [x + i for x, i in zip(RSIndex,DeltaSlowAtrRsi)]
shortband1 = [0]
for i in range(1,len(RSIndex)):
if RSIndex[i-1] < shortband1[i-1] and RSIndex[i] < shortband1[i-1]:
shortband1.append(min(shortband1[i-1],newshortband1[i]))
else:
shortband1.append(newshortband1[i])
trend1 = [0,0]
shortbandCross1 = crossovers(RSIndex, [0]+shortband1)
longbandCross1 = crossovers([0]+longband1, RSIndex)
for i in range(1,len(shortbandCross1)):
if shortbandCross1[i] == True:
trend1.append(1)
elif longbandCross1[i] == True:
trend1.append(-1)
else:
trend1.append(trend1[i])
SlowAtrRsiTL = [longband1[i] if trend1[i] == 1 else shortband1[i] for i in range(len(trend1))]
return FastAtrRsiTL, SlowAtrRsiTL
Upvotes: 0