pycoin

PyCoin is an automated stock trading application that uses the alpaca trading api. It takes in data over 10 second intervals and uses an LSTM model (retrained every 15 minutes) to make a decision on what the predicted next price will be, and makes takes an action in that second depending on the decision outputted by the model. It uses Tensorflow and Keras to make a prediction model.

Trading Logic

The trading logic used for this is based on the derivative of the predictions graph. Currently the algorithm is able to correctly predict whether the crypto price is increasing or decreasing no less than 80% of the time. I have found the model to perform best with the lookback set to 1, epochs between 10 and 15, and units around 256. I also found that the size of the rolling dataset works best between 1800 to 2400 datapoints. According to graph error, it looks as though the model needs to be retrained every half an hour to an hour at minimum. As far as structure goes – the main class is StonkTrader. It incorporates CrytoPredictor and AlpacaTrader and brings them together in one class. This is then referenced by app.py and put together with the web server for a fully functioned release. All current data can be viewed on the webserver at http://localhost:5000. Currently, the application is now ported to use the Alpaca stock trading api. Although the algorithm I designed works well, and the predictions are more than accurate, the fees associated with trading crypto nullify the gains. Since there are no fees for trading stocks, I switched to the Alpaca api. See ‘issues’ for any open issues.

Endpoint: app.py

Installation and Build

First install Docker, Git, and Python.

$ python3 -m venv container  # create virtual environment
$ source container/bin/activate  # activate venv
$ ./install  # install dependencies to venv
-- make changes --
$ ./build [x.x.x]  # build and push to remote
$ docker run  -p 5000:5000 ckinateder/pycoin:[x.x.x]  # run container

Sample Console

Console

Sample Output

$ python3 app.py ltc usd 600

--------------------------------------------------------------------------------
@ 12/29/2020 18:53:55
--------------------------------------------------------------------------------
n-1: $129.3600 (actual)
n: $129.2700 (actual)

n-1: $129.1327 (predicted)
n: $129.0323 (predicted)
n+1: $129.1236 (predicted)

actual (previous) d/dx: -0.0090

predicted (previous) d/dx: -0.0100
predicted (next) d/dx: 0.0091

predicted action: buy
-------------------------------------------------------------------------------- 

+ Balance:
  + 0.00 USD
  + 4.73753803 LTC
   (bought)
+ Total net: 2.070%

* Using 460.36 MB of memory
Saved model to disk
Model trained and saved in 105.28s
Last model trained at 2020-12-29 18:52:16.116808-05:00

+ Recieved response with ['Ticker', 'pair=xltczusd']
+ Saved response at time 2020-12-29 18:54:06.126143-05:00 to file data/ltc-usd_kraken.csv

Dataset loaded into frame in 0.01s
Loaded model from disk

--------------------------------------------------------------------------------
@ 12/29/2020 18:54:07
--------------------------------------------------------------------------------
n-1: $129.2700 (actual)
n: $129.2700 (actual)

n-1: $129.3077 (predicted)
n: $129.4065 (predicted)
n+1: $129.3176 (predicted)

actual (previous) d/dx: 0.0000

predicted (previous) d/dx: 0.0099
predicted (next) d/dx: -0.0089

predicted action: hold
-------------------------------------------------------------------------------- 

+ Balance:
  + 0.00 USD
  + 4.73753803 LTC (valued at 612.42 USD)
   (holding)
+ Total net: 2.070%

* Using 464.12 MB of memory

Last model trained at 2020-12-29 18:52:16.116808-05:00

Charts

Note that these are just samples from my tests and may not be the direct result of the ouptput immediately above.
Historical hourly Bitcoin prices – Hourly prices Historical prices + predicted with actual prices – Predictions Historical prices + predicted with actual prices (zoomed in) – Zoomed Predictions Rate of change of predicted prices – Slope Perent error in rate of change of predicted prices – Error