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

Commit

Permalink
price api overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
CelestialCrafter committed Nov 25, 2023
1 parent 082448f commit 95a01a0
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 108 deletions.
9 changes: 5 additions & 4 deletions algorithms/bollinger_bands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from talib import BBANDS
from price import get_periods

def algorithm(prices, window_size=get_periods(20, 'days'), standard_deviations=2):
upper_band, middle_band, lower_band = BBANDS(prices, window_size, standard_deviations, standard_deviations, 0)
return upper_band, lower_band, middle_band
def algorithm(prices, window_size=(20, 'days'), standard_deviations=2):
print(prices.shape[0])
print(get_periods(*window_size))
return BBANDS(prices, timeperiod=get_periods(*window_size), nbdevup=standard_deviations, nbdevdn=standard_deviations)

def signal(prices, data):
upper_band, lower_band, middle_band = data
upper_band, middle_band, lower_band = data
if prices[-1] > upper_band[-1]:
return 'sell', 1
elif prices[-1] < lower_band[-1]:
Expand Down
7 changes: 3 additions & 4 deletions algorithms/custom_bollinger_rsi.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from price import get_periods
from .bollinger_bands import algorithm as bollinger_bands
from .rsi import algorithm as rsi

def algorithm(prices, window_size_rsi=get_periods(13, 'days'), window_size_bollinger_bands=get_periods(1, 'month')):
upper_band, lower_band, middle_band = bollinger_bands(prices, window_size=window_size_bollinger_bands)
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)
rsi_line = rsi(prices, window_size=window_size_rsi)

return upper_band, lower_band, middle_band, rsi_line
return [*bb_data, rsi_line]

def signal(prices, data):
price = prices[-1]
Expand Down
4 changes: 2 additions & 2 deletions algorithms/rsi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from talib import RSI
from price import get_periods

def algorithm(prices, window_size=get_periods(14, 'days')):
return RSI(prices, window_size)
def algorithm(prices, window_size=(14, 'days')):
return RSI(prices, timeperiod=get_periods(*window_size))

def signal(prices, data, high=70, low=30):
rsi = data
Expand Down
134 changes: 98 additions & 36 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,123 @@ channels:
- conda-forge
- defaults
dependencies:
- _libgcc_mutex=0.1
- _openmp_mutex=4.5
- binutils_linux-64=2.28.1
- blas=1.1
- brotli=1.0.9
- brotli-bin=1.0.9
- bzip2=1.0.8
- ca-certificates=2023.7.22
- intel-openmp=2023.2.0
- libblas=3.9.0
- libcblas=3.9.0
- libffi=3.4.4
- liblapack=3.9.0
- ca-certificates=2023.08.22
- contourpy=1.0.5
- cycler=0.11.0
- cyrus-sasl=2.1.28
- dbus=1.13.18
- expat=2.5.0
- fontconfig=2.14.1
- fonttools=4.25.0
- freetype=2.12.1
- gcc_linux-64=7.2.0
- giflib=5.2.1
- glib=2.69.1
- gst-plugins-base=1.14.1
- gstreamer=1.14.1
- icu=58.2
- jpeg=9e
- kiwisolver=1.4.4
- krb5=1.20.1
- lcms2=2.12
- ld_impl_linux-64=2.38
- lerc=3.0
- libbrotlicommon=1.0.9
- libbrotlidec=1.0.9
- libbrotlienc=1.0.9
- libclang=14.0.6
- libclang13=14.0.6
- libcups=2.4.2
- libdeflate=1.17
- libedit=3.1.20221030
- libevent=2.1.12
- libffi=3.3
- libgcc-ng=13.2.0
- libgfortran-ng=13.2.0
- libgfortran5=13.2.0
- libllvm14=14.0.6
- libopenblas=0.3.24
- libpng=1.6.39
- libpq=12.15
- libstdcxx-ng=11.2.0
- libta-lib=0.4.0
- mkl=2020.4
- openssl=3.0.11
- libtiff=4.5.1
- libuuid=1.41.5
- libwebp=1.3.2
- libwebp-base=1.3.2
- libxcb=1.15
- libxkbcommon=1.0.1
- libxml2=2.10.4
- libxslt=1.1.37
- llvm-openmp=14.0.6
- lz4-c=1.9.4
- matplotlib=3.7.2
- matplotlib-base=3.7.2
- munkres=1.1.4
- mysql=5.7.24
- ncurses=6.4
- nspr=4.35
- nss=3.89.1
- numpy=1.26.0
- numpy-base=1.26.0
- openblas=0.3.24
- openjpeg=2.4.0
- openssl=1.1.1w
- packaging=23.1
- pcre=8.45
- pillow=10.0.1
- pip=23.2.1
- python=3.10.13
- ply=3.11
- pyparsing=3.0.9
- pyqt=5.15.7
- python=3.10.0
- python-dateutil=2.8.2
- python_abi=3.10
- qt-main=5.15.2
- qt-webengine=5.15.9
- qtwebkit=5.212
- readline=8.2
- setuptools=68.0.0
- sip=6.6.2
- six=1.16.0
- sqlite=3.41.2
- ta-lib=0.4.19
- ta-lib=0.4.28
- tk=8.6.12
- vc=14.2
- vs2015_runtime=14.27.29016
- toml=0.10.2
- tornado=6.3.3
- tzdata=2023c
- wheel=0.41.2
- xz=5.4.2
- zlib=1.2.13
- zstd=1.5.5
- pip:
- async-timeout==4.0.3
- blinker==1.6.2
- backtrader==1.9.78.123
- blinker==1.6.3
- certifi==2023.7.22
- charset-normalizer==3.3.0
- click==8.1.7
- colorama==0.4.6
- contourpy==1.1.1
- cycler==0.12.0
- dnspython==2.4.2
- flask==2.3.3
- fonttools==4.43.0
- flask==3.0.0
- idna==3.4
- itsdangerous==2.1.2
- jinja2==3.1.2
- kiwisolver==1.4.5
- markupsafe==2.1.3
- matplotlib==3.8.0
- numpy==1.26.0
- packaging==23.1
- pandas==2.1.1
- pillow==10.0.1
- platformdirs==3.11.0
- pyjwt==2.8.0
- pymongo==4.5.0
- pyparsing==3.1.1
- python-dateutil==2.8.2
- pyqt5-sip==12.11.0
- python-dotenv==1.0.0
- pytz==2023.3.post1
- redis==5.0.0
- requests==2.31.0
- schedule==1.2.1
- six==1.16.0
- tzdata==2023.3
- urllib3==2.0.5
- werkzeug==2.3.7
prefix: C:\Users\Celestial2\miniconda3\envs\auto-trading
- tomli==2.0.1
- urllib3==2.0.6
- waitress==2.1.2
- werkzeug==3.0.0
- yapf==0.40.2
- zipp==3.17.0
prefix: /home/celestial/anaconda3/envs/auto-trading
21 changes: 9 additions & 12 deletions plots/bollinger_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
from algorithms.bollinger_bands import algorithm as boillinger_bands
import plots.colors as colors

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

indicies = np.arange(0, upper_band.shape[0])
sliced_prices = prices[:upper_band.shape[0]]

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

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

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

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.scatter(timestamps[upper_condition], prices[upper_condition], color=colors.upper())
plt.scatter(timestamps[lower_condition], prices[lower_condition], color=colors.lower())

return plt
17 changes: 7 additions & 10 deletions plots/custom_bollinger_rsi.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import numpy as np
import matplotlib.pyplot as plt
import plots.colors as colors
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

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

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

upper_band, lower_band, middle_band, rsi_line = custom_bollinger_rsi(prices, **kwargs)
upper_band, middle_band, lower_band, rsi_line = custom_bollinger_rsi(prices, **kwargs)
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.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(prices)
rsi(prices, timestamps)
plt.subplot(gs[-2, :])
bollinger_bands(prices)
bollinger_bands(prices, timestamps)

return plt
5 changes: 2 additions & 3 deletions plots/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import matplotlib.pyplot as plt
import plots.colors as colors

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

return plt
13 changes: 6 additions & 7 deletions plots/rsi.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import numpy as np
import matplotlib.pyplot as plt
from algorithms.rsi import algorithm as rsi
import plots.colors as colors
from algorithms.rsi import algorithm as rsi

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

plt.plot(indicies, rsi_line, color=colors.mainline())
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(indicies, upper, lower, color='grey', alpha=0.3)
plt.plot(indicies, upper, linestyle='dashed', color=colors.upper())
plt.plot(indicies, lower, linestyle='dashed', color=colors.lower())
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
9 changes: 4 additions & 5 deletions plots/worth.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import matplotlib.pyplot as plt
import plots.colors as colors
import datetime as dt
import numpy as np
from datetime import datetime as dt

def plot(timestamps, values):
def plot(values, timestamps):
values = np.array(values)
# times = [ dt.datetime.fromtimestamp(timestamp) for timestamp in timestamps ]
times = np.arange(0, values.shape[0]) * 5
plt.plot(times, values, color=colors.upper())
timestamps = [dt.fromtimestamp(timestamp) for timestamp in timestamps]
plt.plot(timestamps, values, color=colors.upper())

return plt
Loading

0 comments on commit 95a01a0

Please sign in to comment.