Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
plot to algo file
Browse files Browse the repository at this point in the history
  • Loading branch information
fox committed Dec 7, 2023
1 parent 9061dc2 commit 3e62073
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 69 deletions.
21 changes: 21 additions & 0 deletions algorithms/bollinger_bands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from talib import BBANDS
from price import get_periods, get_max_periods
import matplotlib.pyplot as plt
import plots.colors as colors


def algorithm(prices, window_size=(20, 'days'), standard_deviations=2):
periods = get_periods(*window_size)
Expand All @@ -18,3 +21,21 @@ def signal(prices, data):
return 'buy', 0.5

return 'no_action', 0

def plot(prices, timestamps, **kwargs):
upper_band, middle_band, lower_band = algorithm(prices, window_size=(20, 'days'), **kwargs)

plt.fill_between(timestamps, upper_band, lower_band, color='grey', alpha=0.3)

# Price/SMA
plt.plot(timestamps, prices, color=colors.mainline())
plt.plot(timestamps, middle_band, color=colors.secondaryline())

# Buy/Sell Signals
upper_condition = prices >= upper_band
lower_condition = prices <= lower_band

plt.scatter(timestamps[upper_condition], prices[upper_condition], color=colors.upper())
plt.scatter(timestamps[lower_condition], prices[lower_condition], color=colors.lower())

return plt
27 changes: 27 additions & 0 deletions algorithms/custom_bollinger_rsi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from .bollinger_bands import algorithm as bollinger_bands
from .rsi import algorithm as rsi
import matplotlib.pyplot as plt
import plots.colors as colors
from matplotlib.gridspec import GridSpec
from algorithms.rsi import plot as rsi_plot
from algorithms.bollinger_bands import plot as bollinger_bands_plot


def algorithm(prices, window_size_rsi=(13, 'days'), window_size_bollinger_bands=(1, 'month')):
bb_data = bollinger_bands(prices, window_size=window_size_bollinger_bands)
Expand All @@ -18,3 +24,24 @@ def signal(prices, data):
return 'sell', 1

return 'no_action', 0

def plot(prices, timestamps, **kwargs):
gs = GridSpec(3, 1, figure=plt.gcf())

plt.subplot(gs[0, :])
plt.plot(timestamps, prices, color=colors.mainline())

upper_band, middle_band, lower_band, rsi_line = algorithm(prices, **kwargs)
sliced_prices = prices[:min(upper_band.shape[0], rsi_line.shape[0])]

upper_condition = (prices >= upper_band) & (rsi_line >= 70)
lower_condition = (prices >= lower_band) & (30 >= rsi_line)
plt.scatter(timestamps[upper_condition], sliced_prices[upper_condition], color=colors.upper())
plt.scatter(timestamps[lower_condition], sliced_prices[lower_condition], color=colors.lower())

plt.subplot(gs[-1, :])
rsi_plot(prices, timestamps)
plt.subplot(gs[-2, :])
bollinger_bands_plot(prices, timestamps)

return plt
18 changes: 18 additions & 0 deletions algorithms/rsi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from talib import RSI
from price import get_periods, get_max_periods
import numpy as np
import matplotlib.pyplot as plt
import plots.colors as colors

def algorithm(prices, window_size=(14, 'days')):
periods = get_periods(*window_size)
Expand All @@ -21,3 +24,18 @@ def signal(prices, data, high=70, low=30):
return 'buy', strength

return 'no_action', 0

def plot(prices, timestamps, **kwargs):
rsi_line = algorithm(prices, **kwargs)

plt.plot(timestamps, rsi_line, color=colors.mainline())

# Thresholds
upper = np.full(rsi_line.shape, 70)
lower = np.full(rsi_line.shape, 30)

plt.fill_between(timestamps, upper, lower, color='grey', alpha=0.3)
plt.plot(timestamps, upper, linestyle='dashed', color=colors.upper())
plt.plot(timestamps, lower, linestyle='dashed', color=colors.lower())

return plt
22 changes: 0 additions & 22 deletions plots/bollinger_bands.py

This file was deleted.

27 changes: 0 additions & 27 deletions plots/custom_bollinger_rsi.py

This file was deleted.

19 changes: 0 additions & 19 deletions plots/rsi.py

This file was deleted.

2 changes: 1 addition & 1 deletion views/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def plot(algorithm):
timestamps = np.arange(timestamps[-1] - interval_timedelta * timestamps.shape[0], timestamps[-1], interval_timedelta)

try:
import_module(f'plots.{algorithm}').plot(prices, timestamps)
import_module(f'algorithms.{algorithm}').plot(prices, timestamps)
except Exception as error:
return str(error), 400

Expand Down

0 comments on commit 3e62073

Please sign in to comment.