@backtrader I'll forgive you your bluntness. I know C#, javascript and ruby, just python is new for me. And I did indeed simplify the code before posting but apparently left some in.
Latest posts made by Rudi
-
RE: In need of support with more indicators in a screener
-
In need of support with more indicators in a screener
I've been following the blog post on creating a screener on the basis of an Analyzer. Background being that i want to screen the S&P500 stocks at end of day to scan if a stock shows the signals I'm looking for.
I've been trying several ways, but my python skills are not sufficient I think. Below my try, I would appreciate if somebody could point me in the right direction.class Screener(bt.Analyzer): params = dict(period=10) def start(self): self.smas = [bt.indicators.SMA(self.datas, period=self.p.period) for data in self.datas] self.trend_ema = [bt.indicators.ExponentialMovingAverage(data, period=200) for data in self.datas] self.adx = [bt.indicators.ADX(data, period=10) for data in self.datas] self.result = zip(self.datas, self.smas, self.crsi, self.trend_ema, self.adx) def stop(self): self.rets['over'] = list() self.rets['under'] = list() for value, sma, trend_ema, adx in self.result node = data._name, data.close[0], crsi[0] if value > trend_ema and adx >= 30 self.rets['over'].append(node)
-
One for the cookbook: On Balance Volume
@backtrader Not developed by myself but a valuable indicator. Credits go to backtest rookies
class OnBalanceVolume(bt.Indicator): alias = 'OBV' lines = ('obv') params = ( ('length', 12), ) plotlines = dict( obv=dict( _name='OBV', color='purple', alpha=0.50 ) ) def __init__(self): # Plot a horizontal Line self.plotinfo.plotyhlines = [0] def next(self): # Aliases to avoid long lines c = self.data.close v = self.data.volume obv = self.lines.obv if c[0] > c[-1]: obv[0] = obv[-1] + v[0] elif c[0] < c[-1]: obv[0] = obv[-1] - v[0] else: obv[0] = obv[-1]
-
RE: Would appreciate some help with Connors RSI
Brilliant @backtrader ! I'll make sure to add indicators once I get the hang of creating them!
-
RE: Would appreciate some help with Connors RSI
@ab_trader thanks for looking into this. I changed the code as below (calculating the RSI myself in the
init()
, but still getting the rsi over the streak back as anan
. Is this not what you meant?class ConnorsRSI(bt.Indicator): lines = ('crsi','rsi','streak', 'streak_rsi','prank') params = ( ('rsi_length', 3), ('streak_length', 2), ('loopback_length', 100), ) plotlines = dict( obv=dict( _name='crsi', color='purple', alpha=0.50 ) ) def updown(self): upday=False downday=False if self.data[0] > self.data[-1]: upday = True else: upday = False if self.data[0] < self.data[-1]: downday = True else: downday = False if upday and self.lines.streak[-1] >= 0: self.lines.streak[0] = self.lines.streak[-1] + 1 elif upday and self.lines.streak[-1] <= 0: self.lines.streak[0] = 1 elif downday and self.lines.streak[-1] <= 0: self.lines.streak[0] = self.lines.streak[-1] - 1 elif downday and self.lines.streak[-1] >= 0: self.lines.streak[0] = -1 elif self.data[0] == self.data[-1]: self.lines.streak[0] = 0 else: self.lines.streak[0] = 0 def __init__(self): # Plot a horizontal Line self.plotinfo.plotyhlines = [0] self.lines.rsi = bt.indicators.RSI(self.data, period=self.params.rsi_length) self.lines.prank = bt.indicators.PercentRank(self.data, period=self.params.loopback_length) # calculate rsi over streak upday = bt.indicators.UpDay(self.lines.streak, period=self.params.streak_length) downday = bt.indicators.DownDay(self.lines.streak, period=self.params.streak_length) maup = bt.indicators.MovAv.Smoothed(upday, period=self.params.streak_length) madown = bt.indicators.MovAv.Smoothed(downday, period=self.params.streak_length) rs = maup / madown self.lines.streak_rsi = 100.0 - 100.0 / (1.0 + rs) def next(self): self.updown() def nextstart(self): self.lines.streak[0] = 0
Thanks for your support!
-
Would appreciate some help with Connors RSI
Hi,
I'm new to backorder and new to python, so please bare with me.
I'm trying to implement an indicator for the ConnorsRSI. At a certain point in the algorithm I need to take the RSI of a line that I create, and it gives me back nan instead of an rsi value. I have no clue where I go wrong, and hope that somebody can point me in the right direction.
Please feel free to give other hints and tips if I make obvious coding mistakes.
The code for the indicator itself is:class ConnorsRSI(bt.Indicator): lines = ('crsi','rsi','streak','streak_rsi', 'prank') params = ( ('rsi_length', 3), ('streak_length', 2), ('loopback_length', 100), ) plotlines = dict( obv=dict( _name='crsi', color='purple', alpha=0.50 ) ) def updown(self): upday=False downday=False if self.data[0] > self.data[-1]: upday = True else: upday = False if self.data[0] < self.data[-1]: downday = True else: downday = False if upday and self.lines.streak[-1] >= 0: self.lines.streak[0] = self.lines.streak[-1] + 1 elif upday and self.lines.streak[-1] <= 0: self.lines.streak[0] = 1 elif downday and self.lines.streak[-1] <= 0: self.lines.streak[0] = self.lines.streak[-1] - 1 elif downday and self.lines.streak[-1] >= 0: self.lines.streak[0] = -1 elif self.data[0] == self.data[-1]: self.lines.streak[0] = 0 else: self.lines.streak[0] = 0 def __init__(self): # Plot a horizontal Line self.plotinfo.plotyhlines = [0] self.lines.rsi = bt.indicators.RSI(self.data, period=self.params.rsi_length) self.lines.prank = bt.indicators.PercentRank(self.data, period=self.params.loopback_length) def next(self): self.updown() self.lines.streak_rsi = bt.indicators.RSI(self.lines.streak, period=self.params.streak_length) def nextstart(self): self.lines.streak[0] = 0
The issue is in the 'next', where I calculate (or want to calculate) the RSI value over the streaks. The output I get is:
2016-05-25: Prev: 48.76: Close: 49.26, rsi: 79.21353438233095 Streak: 0.0 Streak_rsi: nan PercentRank: 59 2016-05-26: Prev: 49.26: Close: 49.04, rsi: 68.50800494096566 Streak: -1.0 Streak_rsi: nan PercentRank: 56 2016-05-27: Prev: 49.04: Close: 49.45, rsi: 77.14326450516853 Streak: 1.0 Streak_rsi: nan PercentRank: 64 2016-05-31: Prev: 49.45: Close: 50.09, rsi: 86.08028735088246 Streak: 2.0 Streak_rsi: nan PercentRank: 70 2016-06-01: Prev: 50.09: Close: 49.95, rsi: 76.29219423676898 Streak: -1.0 Streak_rsi: nan PercentRank: 69 2016-06-02: Prev: 49.95: Close: 49.6, rsi: 53.48552312948757 Streak: -2.0 Streak_rsi: nan PercentRank: 65 2016-06-03: Prev: 49.6: Close: 48.95, rsi: 29.183093574328538 Streak: -3.0 Streak_rsi: nan PercentRank: 52 2016-06-06: Prev: 48.95: Close: 49.27, rsi: 46.97498518905785 Streak: 1.0 Streak_rsi: nan PercentRank: 57 2016-06-07: Prev: 49.27: Close: 49.24, rsi: 45.37197817316539 Streak: -1.0 Streak_rsi: nan PercentRank: 56 2016-06-08: Prev: 49.24: Close: 49.18, rsi: 41.15842240824419 Streak: -2.0 Streak_rsi: nan PercentRank: 55
Thanks!