Skip to content

Commit

Permalink
fibonacci bollinger bands
Browse files Browse the repository at this point in the history
  • Loading branch information
whittlem committed Mar 17, 2021
1 parent 1318596 commit 2e53386
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
31 changes: 31 additions & 0 deletions models/Trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def addAll(self):
self.addEMA(26)
self.addGoldenCross()
self.addDeathCross()
self.addFibonacciBollingerBands()

self.addRSI(14)
self.addMACD()
Expand Down Expand Up @@ -364,6 +365,36 @@ def calculateRelativeStrengthIndex(self, series, interval=14):

return rsi

def addFibonacciBollingerBands(self, interval=20, multiplier=3):
"""Adds Fibonacci Bollinger Bands."""

if not isinstance(interval, int):
raise TypeError('Interval integer required.')

if not isinstance(multiplier, int):
raise TypeError('Multiplier integer required.')

tp = (self.df['high'] + self.df['low'] + self.df['close']) / 3
sma = tp.rolling(interval).mean()
sd = multiplier * tp.rolling(interval).std()

sma = sma.fillna(0)
sd = sd.fillna(0)

self.df['fbb_mid'] = sma
self.df['fbb_upper0_236'] = sma + (0.236 * sd)
self.df['fbb_upper0_382'] = sma + (0.382 * sd)
self.df['fbb_upper0_5'] = sma + (0.5 * sd)
self.df['fbb_upper0_618'] = sma + (0.618 * sd)
self.df['fbb_upper0_764'] = sma + (0.764 * sd)
self.df['fbb_upper1'] = sma + (1 * sd)
self.df['fbb_lower0_236'] = sma - (0.236 * sd)
self.df['fbb_lower0_382'] = sma - (0.382 * sd)
self.df['fbb_lower0_5'] = sma - (0.5 * sd)
self.df['fbb_lower0_618'] = sma - (0.618 * sd)
self.df['fbb_lower0_764'] = sma - (0.764 * sd)
self.df['fbb_lower1'] = sma - (1 * sd)

def movingAverageConvergenceDivergence(self):
"""Calculates the Moving Average Convergence Divergence (MACD)"""

Expand Down
41 changes: 41 additions & 0 deletions views/TradingGraphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,47 @@ def renderBuySellSignalEMA1226MACD(self, saveFile='', saveOnly=False):
if saveOnly == False:
plt.show()

def renderFibonacciBollingerBands(self, period=50, saveFile='', saveOnly=False):
"""Render FibonacciBollingerBands"""

if not isinstance(period, int):
raise TypeError('Period parameter is not perioderic.')

if period < 1 or period > len(self.df):
raise ValueError('Period is out of range')

df_subset = self.df.iloc[-period::]

plt.subplot(111)
plt.suptitle(df_subset.iloc[0]['market'] + ' | ' + str(df_subset.iloc[0]['granularity']), fontsize=12)
plt.plot(df_subset.fbb_upper0_236, label="23.6%", color="blue")
plt.plot(df_subset.fbb_lower0_236, label="-23.6%", color="blue")
plt.plot(df_subset.fbb_upper0_382, label="38.2%", color="green")
plt.plot(df_subset.fbb_lower0_382, label="3-8.2%", color="green")
plt.plot(df_subset.fbb_upper0_5, label="50%", color="cyan")
plt.plot(df_subset.fbb_lower0_5, label="-50%", color="cyan")
plt.plot(df_subset.fbb_upper0_618, label="61.8%", color="pink")
plt.plot(df_subset.fbb_lower0_618, label="-61.8%", color="pink")
plt.plot(df_subset.fbb_upper0_764, label="76.4%", color="red")
plt.plot(df_subset.fbb_lower0_764, label="-76.4%", color="red")
plt.plot(df_subset.fbb_upper1, label="100%", color="magenta")
plt.plot(df_subset.fbb_lower1, label="-100%", color="magenta")
plt.plot(df_subset.fbb_mid, label="mid", color="orange")
plt.plot(df_subset.close, label="price", color="black")
plt.legend()
plt.ylabel('Price')
plt.xticks(rotation=90)
plt.tight_layout()

try:
if saveFile != '':
plt.savefig(saveFile)
except OSError:
raise SystemExit('Unable to save: ', saveFile)

if saveOnly == False:
plt.show()

def renderPriceEMA12EMA26(self, saveFile='', saveOnly=False):
"""Render the price, EMA12 and EMA26
Expand Down

0 comments on commit 2e53386

Please sign in to comment.