Created
May 13, 2019 23:08
-
-
Save qharlie/7881392cdbdbca8c13c3ed6dec820900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import backtrader as bt | |
from collections import defaultdict | |
class KitchenSinkAnalyzer(bt.Analyzer): | |
params = dict(sma_period=10, slow_sma_period=30, rsi_period=14, mean_period=10, moment_period=12, percent_period=15) | |
def start(self): | |
self.smas = {data: bt.indicators.SMA(data, period=self.p.sma_period) | |
for data in self.datas} | |
self.slow_smas = {data: bt.indicators.SMA(data, period=self.p.slow_sma_period) | |
for data in self.datas} | |
self.rsis = {data: bt.indicators.RelativeStrengthIndex(data, period=self.p.rsi_period) | |
for data in self.datas} | |
self.mean_deviations = {data: bt.indicators.StandardDeviation(data, period=self.p.mean_period) | |
for data in self.datas} | |
self.momentums = {data: bt.indicators.Momentum(data, period=self.p.moment_period) | |
for data in self.datas} | |
self.moms_oscs = {data: bt.indicators.MomentumOscillator(data, period=self.p.moment_period) | |
for data in self.datas} | |
self.percent_change = {data: bt.indicators.PercentChange(data, period=self.p.percent_period) | |
for data in self.datas} | |
self.downdays = {data: bt.indicators.DownDay(data, period=self.p.sma_period) | |
for data in self.datas} | |
self.downmoves = {data: bt.indicators.DownMove(data) | |
for data in self.datas} | |
self.updays = {data: bt.indicators.UpDay(data, period=self.p.sma_period) | |
for data in self.datas} | |
self.upmoves = {data: bt.indicators.UpMove(data) | |
for data in self.datas} | |
self.plusdi = {data: bt.indicators.PlusDirectionalIndicator(data, period=self.p.rsi_period) | |
for data in self.datas} | |
self.rateofchange = {data: bt.indicators.RateOfChange(data) | |
for data in self.datas} | |
self.rmis = {data: bt.indicators.RelativeMomentumIndex(data) | |
for data in self.datas} | |
self.trix = {data: bt.indicators.Trix(data) | |
for data in self.datas} | |
self.rets['symbols'] = defaultdict(dict) | |
def stop(self): | |
self.rets['smas'] = list() | |
symbols = self.rets['symbols'] | |
for data, sma in self.smas.items(): | |
status = 'SMA_SIDEWAYS' | |
symbol = symbols[data._name] | |
if data > sma: # if data.close[0] > sma[0] | |
status = 'SMA_OVER' | |
else: | |
status = 'SMA_UNDER' | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
node = dt, data._name, status, sma[0], data.close[0] | |
self.rets['smas'].append(node) | |
symbol['sma_status'] = status | |
symbol['sma_value'] = sma[0] | |
self.rets['sma_slows'] = list() | |
for data, sma in self.slow_smas.items(): | |
status = 'SMA_SIDEWAYS' | |
symbol = symbols[data._name] | |
if data > sma: # if data.close[0] > sma[0] | |
status = 'SMA_OVER' | |
else: | |
status = 'SMA_UNDER' | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
node = dt, data._name, status, sma[0], data.close[0] | |
self.rets['sma_slows'].append(node) | |
symbol['sma_slows_status'] = status | |
symbol['sma_slows_value'] = sma[0] | |
self.rets['rsis'] = list() | |
for data, rsi in self.rsis.items(): | |
status = 'RSI_EVEN' | |
symbol = symbols[data._name] | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
if rsi >= 65: # if data.close[0] > rsi[0] | |
status = 'RSI_OVER' | |
elif rsi <= 35: | |
status = 'RSI_UNDER' | |
node = dt, data._name, status, rsi[0] | |
symbol['rsi_status'] = status | |
symbol['rsi_value'] = rsi[0] | |
self.rets['rsis'].append(node) | |
self.rets['mean_deviations'] = list() | |
for data, md in self.mean_deviations.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'STD_DEVIATION' | |
node = (dt, data._name, status, md[0]) | |
symbol['mean_deviation_status'] = status | |
symbol['mean_deviation_value'] = md[0] | |
self.rets['mean_deviations'].append(node) | |
self.rets['momentums'] = list() | |
for data, ms in self.momentums.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'MOMENTUM' | |
node = (dt, data._name, status, ms[0]) | |
symbol['momentums_status'] = status | |
symbol['momentums_value'] = ms[0] | |
self.rets['momentums'].append(node) | |
self.rets['moms_ocsc'] = list() | |
for data, mo in self.moms_oscs.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'MOMENTUM_OSCILATORS' | |
node = (dt, data._name, status, mo[0]) | |
symbol['moms_ocsc_status'] = status | |
symbol['moms_ocsc_value'] = mo[0] | |
self.rets['moms_ocsc'].append(node) | |
self.rets['percent_change'] = list() | |
for data, pc in self.percent_change.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'PERCENT_CHANGE' | |
node = (dt, data._name, status, pc[0]) | |
symbol['percent_change_status'] = status | |
symbol['percent_change_value'] = pc[0] | |
self.rets['percent_change'].append(node) | |
self.rets['downdays'] = list() | |
for data, pc in self.downdays.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'DOWNDAYS' | |
node = (dt, data._name, status, pc[0]) | |
symbol['downdays_status'] = status | |
symbol['downdays_value'] = pc[0] | |
self.rets['downdays'].append(node) | |
self.rets['downmoves'] = list() | |
for data, pc in self.downmoves.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'DOWNMOVES' | |
node = (dt, data._name, status, pc[0]) | |
symbol['downmoves_status'] = status | |
symbol['downmoves_value'] = pc[0] | |
self.rets['downmoves'].append(node) | |
self.rets['updays'] = list() | |
for data, pc in self.updays.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'UPDAYS' | |
node = (dt, data._name, status, pc[0]) | |
symbol['updays_status'] = status | |
symbol['updays_value'] = pc[0] | |
self.rets['updays'].append(node) | |
self.rets['upmoves'] = list() | |
for data, pc in self.upmoves.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'UPMOVES' | |
node = (dt, data._name, status, pc[0]) | |
symbol['upmoves_status'] = status | |
symbol['upmoves_value'] = pc[0] | |
self.rets['upmoves'].append(node) | |
self.rets['plusdi'] = list() | |
for data, pc in self.plusdi.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'plusdi' | |
node = (dt, data._name, status, pc[0]) | |
symbol['plusdi_status'] = status | |
symbol['plusdi_value'] = pc[0] | |
self.rets['plusdi'].append(node) | |
self.rets['rateofchange'] = list() | |
for data, pc in self.rateofchange.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'rateofchange' | |
node = (dt, data._name, status, pc[0]) | |
symbol['rateofchange_status'] = status | |
symbol['rateofchange_value'] = pc[0] | |
self.rets['rateofchange'].append(node) | |
self.rets['rmis'] = list() | |
for data, pc in self.rmis.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'rmis' | |
node = (dt, data._name, status, pc[0]) | |
symbol['rmis_status'] = status | |
symbol['rmis_value'] = pc[0] | |
self.rets['rmis'].append(node) | |
self.rets['trix'] = list() | |
for data, pc in self.trix.items(): | |
dt = '{}:{}'.format(data.datetime.date(0), data.datetime.time(0)) | |
symbol = symbols[data._name] | |
status = 'trix' | |
node = (dt, data._name, status, pc[0]) | |
symbol['trix_status'] = status | |
symbol['trix_value'] = pc[0] | |
self.rets['trix'].append(node) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment