Skip to content

Commit

Permalink
market histories saved from index prices
Browse files Browse the repository at this point in the history
  • Loading branch information
George Burry committed Aug 15, 2021
1 parent 463650e commit 2feb6d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
31 changes: 27 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import json
import decimal
import requests
import statistics
Expand All @@ -22,6 +23,7 @@ class Bot:

def __init__(
self,
histories_fname='histories',
num_samples=20,
num_std=2,
positive_multiplier=1.003,
Expand All @@ -35,6 +37,7 @@ def __init__(
self.client.stark_private_key = STARK_PRIVATE_KEY
self.coinbase_api = 'https://api.pro.coinbase.com'
self.market = None
self.histories_fname = histories_fname
self.num_samples = num_samples
self.num_std = num_std
self.positive_multiplier = positive_multiplier
Expand All @@ -50,11 +53,31 @@ def __init__(
self.sell_orders = []
self.get_account()

def load_all_histories(self):
with open(self.histories_fname + '.json', 'r') as f:
histories = json.load(f)
return histories

def load_market_history(self):
histories = self.load_all_histories()
return histories[self.market] if histories.get(self.market) else []

def save_market_history(self, data):
histories = self.load_all_histories()
histories[self.market] = data
with open(self.histories_fname + '.json', 'w') as f:
json.dump(histories, f)

def get_price_history(self):
endpoint = f'/products/{self.market}/candles'
r = requests.get(self.coinbase_api + endpoint)
data = r.json()[:self.num_samples][::-1]
self.price_history = [float(x[4]) for x in data]
# endpoint = f'/products/{self.market}/candles'
# r = requests.get(self.coinbase_api + endpoint)
# data = r.json()[:self.num_samples][::-1]
# self.price_history = [float(x[4]) for x in data]

history = self.load_market_history()
history.append(float(self.market_info['indexPrice']))
self.price_history = history[-20:]
self.save_market_history(self.price_history)

def calculate_price_stats(self):
self.mean_price = statistics.mean(self.price_history)
Expand Down
1 change: 1 addition & 0 deletions histories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 2feb6d0

Please sign in to comment.