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

Commit

Permalink
Merge pull request #33 from auto-trading-temp-name/algorithms
Browse files Browse the repository at this point in the history
custom_bollinger_rsi, and price plots
  • Loading branch information
CelestialCrafter authored Nov 18, 2023
2 parents d15ae20 + aa5a0a1 commit c0165ea
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
18 changes: 18 additions & 0 deletions algorithms/custom_bollinger_rsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from .bollinger_bands import algorithm as bollinger_bands
from .rsi import algorithm as rsi

def algorithm(prices, rsi_window=13, bollinger_window=30):
upper_band, lower_band, middle_band = bollinger_bands(prices, window_size=bollinger_window)
rsi_line = rsi(prices, window_size=rsi_window)

return upper_band, lower_band, middle_band, rsi_line

def signal(prices, data):
price = prices[-1]
upper_band, lower_band, middle_band, rsi_line = data

if price >= lower_band[-1] and 30 >= rsi_line[-1]:
return 'buy', 0.5

if price >= upper_band[-1] and rsi_line[-1] >= 70:
return 'sell', 1
30 changes: 30 additions & 0 deletions plots/custom_bollinger_rsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from algorithms.custom_bollinger_rsi import algorithm as custom_bollinger_rsi
from plots.rsi import plot as rsi
from plots.bollinger_bands import plot as bollinger_bands
import plots.colors as colors

def plot(prices):
gs = GridSpec(2, 2, figure=plt.gcf())

plt.subplot(gs[0, :])
indicies = np.arange(0, prices.shape[0])
plt.plot(indicies, prices, color=colors.mainline())

upper_band, lower_band, middle_band, rsi_line = custom_bollinger_rsi(prices)
sliced_prices = prices[:min(upper_band.shape[0], rsi_line.shape[0])]
indicies = np.arange(0, indicies.shape[0])

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

plt.subplot(gs[-1, :1])
rsi(prices)
plt.subplot(gs[-1, 1:2])
bollinger_bands(prices)

return plt
10 changes: 5 additions & 5 deletions plots/rsi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import plots.colors as colors

def plot(prices):
data = rsi(prices)
indicies = np.arange(0, data.shape[0])
rsi_line = rsi(prices)
indicies = np.arange(0, rsi_line.shape[0])

plt.plot(indicies, data, color=colors.mainline())
plt.plot(indicies, rsi_line, color=colors.mainline())

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

plt.fill_between(indicies, upper, lower, color='grey', alpha=0.3)
plt.plot(indicies, upper, linestyle='dashed', color=colors.upper())
Expand Down
13 changes: 8 additions & 5 deletions views/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ def plot(algorithm):
config = json.load(open('config.json', 'r'))
prices = get_prices(config['pair'], interval=config['interval'])

if algorithm not in get_algorithms():
if algorithm not in ['price', *get_algorithms()]:
return 'Invalid Algorithm', 404
import_module(f'plots.{algorithm}').plot(prices)

ax = plt.gca()
ax.tick_params(color=colors.outline(), labelcolor=colors.outline())
for spine in ax.spines.values():
spine.set_edgecolor(colors.outline())
axes = plt.gcf().get_axes()
for axis in axes:
axis.tick_params(color=colors.outline(), labelcolor=colors.outline())
for spine in axis.spines.values():
spine.set_edgecolor(colors.outline())

plt.tight_layout()

# Save plot into buffer instead of the FS
svg_buffer = io.StringIO()
Expand Down

0 comments on commit c0165ea

Please sign in to comment.