Skip to content

Commit 4a7d59b

Browse files
committedOct 11, 2022
make it compile and rename indicators
1 parent 71630a3 commit 4a7d59b

13 files changed

+26082
-8
lines changed
 

Diff for: ‎custom_indicators/BollingerBandsBandwitch.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from backtrader import bt
2+
3+
class BollingerBandsBandwitch(bt.ind.BollingerBands):
4+
5+
'''
6+
Extends the Bollinger Bands with a Percentage line and a Bandwitch indicator
7+
'''
8+
lines = ('bandwitch',)
9+
plotlines = dict(bandwitch=dict(_name='%Bwtch')) # display the line as %B on chart
10+
11+
def __init__(self):
12+
super(BollingerBandsBandwitch, self).__init__()
13+
self.l.bandwitch = 100 * (self.l.top - self.l.bot) / self.l.mid
14+
pass
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: ‎data/Source 1/EURUSD_M15_2020.csv

+24,999
Large diffs are not rendered by default.

Diff for: ‎data/Source 1/EURUSD_M15_2020_1kbar.csv

+1,000
Large diffs are not rendered by default.

Diff for: ‎finplotWindow.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from pyqtgraph.graphicsItems.LegendItem import LegendItem
44

5-
from indicators import ichimoku
6-
from indicators import rsi
7-
from indicators import stochastic
8-
from indicators import stochasticRsi
9-
from indicators import sma
10-
from indicators import ema
5+
from custom_indicators import ichimoku
6+
from custom_indicators import rsi
7+
from custom_indicators import stochastic
8+
from custom_indicators import stochasticRsi
9+
from custom_indicators import sma
10+
from custom_indicators import ema
1111

1212
sys.path.append('../finplot')
1313
import finplot as fplt

Diff for: ‎strategies/AiStableBaselinesModel.py

+63-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
import numpy as np
2626
from enum import Enum
2727

28+
import pandas as pd
29+
30+
from custom_indicators import BollingerBandsBandwitch
31+
2832
# action list
2933
class Action(Enum):
3034
HOLD=0
@@ -62,6 +66,25 @@ def __init__(self, *argv):
6266
self.atr = bt.indicators.ATR(self.data, period=self.p.atrperiod)
6367

6468
self.stochastic = bt.ind.stochastic.Stochastic(self.data)
69+
self.rsi = bt.ind.RelativeStrengthIndex(self.data)
70+
self.bbands = bt.ind.BollingerBands(self.data)
71+
self.bbandsPct = bt.ind.BollingerBandsPct(self.data)
72+
self.bbandsBandwitch = BollingerBandsBandwitch.BollingerBandsBandwitch(self.data)
73+
74+
self.sma_200 = bt.ind.MovingAverageSimple(self.data,period=200)
75+
self.sma_150 = bt.ind.MovingAverageSimple(self.data,period=150)
76+
self.sma_100 = bt.ind.MovingAverageSimple(self.data,period=100)
77+
self.sma_50 = bt.ind.MovingAverageSimple(self.data,period=50)
78+
self.sma_21 = bt.ind.MovingAverageSimple(self.data,period=21)
79+
80+
self.ema_200 = bt.ind.ExponentialMovingAverage(self.data,period=200)
81+
self.ema_100 = bt.ind.ExponentialMovingAverage(self.data,period=100)
82+
self.ema_50 = bt.ind.ExponentialMovingAverage(self.data,period=50)
83+
self.ema_26 = bt.ind.ExponentialMovingAverage(self.data,period=26)
84+
self.ema_12 = bt.ind.ExponentialMovingAverage(self.data,period=12)
85+
self.ema_9 = bt.ind.ExponentialMovingAverage(self.data,period=9)
86+
87+
self.macd = bt.ind.MACDHisto(self.data)
6588

6689
pass
6790

@@ -76,6 +99,11 @@ def next(self):
7699

77100
self.obseravation = self.next_observation()
78101

102+
# Do nothing if a parameter is not valid yet (basically wait for all idicators to be loaded)
103+
if pd.isna(self.obseravation).any():
104+
print("Waiting indicators")
105+
return
106+
79107
# Prepare data for Model
80108
action, _states = self.model.predict(self.obseravation) # deterministic=True
81109

@@ -112,10 +140,43 @@ def next_observation(self):
112140
# https://stackoverflow.com/questions/53979199/tensorflow-keras-returning-multiple-predictions-while-expecting-one
113141

114142
# Ichimoku
115-
inputs = [ self.ichimoku.tenkan[0], self.ichimoku.kijun[0], self.ichimoku.senkou[0], self.ichimoku.senkou_lead[0], self.ichimoku.chikou[0] ]
143+
#inputs = [ self.ichimoku.tenkan[0], self.ichimoku.kijun[0], self.ichimoku.senkou[0], self.ichimoku.senkou_lead[0], self.ichimoku.chikou[0] ]
144+
145+
# OHLCV
146+
inputs = [ self.data.open[0],self.data.high[0],self.data.low[0],self.data.close[0],self.data.volume[0] ]
116147

117148
# Stochastic
118-
inputs = inputs + [self.stochastic.percK[0] / 100.0, self.stochastic.percD[0] / 100.0]
149+
inputs = inputs + [self.stochastic.percK[0], self.stochastic.percD[0]]
150+
151+
# Rsi
152+
inputs = inputs + [self.rsi.rsi[0]]
153+
154+
# bbands
155+
inputs = inputs + [self.bbands.bot[0]] # BBL
156+
inputs = inputs + [self.bbands.mid[0]] # BBM
157+
inputs = inputs + [self.bbands.top[0]] # BBU
158+
inputs = inputs + [self.bbandsBandwitch.bandwitch[0]] # BBB
159+
inputs = inputs + [self.bbandsPct.pctb[0]] # BBP
160+
161+
# sma
162+
inputs = inputs + [self.sma_200.sma[0]]
163+
inputs = inputs + [self.sma_150.sma[0]]
164+
inputs = inputs + [self.sma_100.sma[0]]
165+
inputs = inputs + [self.sma_50.sma[0]]
166+
inputs = inputs + [self.sma_21.sma[0]]
167+
168+
# ema
169+
inputs = inputs + [self.ema_200.ema[0]]
170+
inputs = inputs + [self.ema_100.ema[0]]
171+
inputs = inputs + [self.ema_50.ema[0]]
172+
inputs = inputs + [self.ema_26.ema[0]]
173+
inputs = inputs + [self.ema_12.ema[0]]
174+
inputs = inputs + [self.ema_9.ema[0]]
175+
176+
# macd
177+
inputs = inputs + [self.macd.macd[0]] # MACD
178+
inputs = inputs + [self.macd.histo[0]] # MACD histo
179+
inputs = inputs + [self.macd.signal[0]] # MACD signal
119180

120181
return np.array(inputs)
121182

0 commit comments

Comments
 (0)