-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeseries_cryptobot_integration_.py
228 lines (182 loc) · 8.55 KB
/
timeseries_cryptobot_integration_.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf-8 -*-
"""Timeseries Cryptobot Integration .ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1P0m45z9OgfVFNNeZPtZ3DCyCQbRnBD4V
"""
import os
import uuid
import numpy as np
import random
import requests
import time
import pandas as pd
from enum import Enum
from prophet import Prophet
from datetime import date, timedelta
# Constants
INITIAL_BALANCE = 1000000 # $1 million
INTERVALS = 5 # seconds
MINIMUM_GROWTH = 0.02 # minimum 2% increase in predicted price to trigger buy
STOP_LOSS_PERCENTAGE = 0.05 # limit loss to 5%
GOAL = 1200000 # $1.2 million
""" Time Series Prediction Functions """
def preprocess_data(data_url):
df = pd.read_csv(data_url)
df = df.drop(['Adj Close'], axis=1)
df.rename(columns={'Date': 'ds', 'Close': 'y'}, inplace=True)
return df
def train_prophet_model(df_train):
m = Prophet(interval_width=0.95, n_changepoints=7)
m.fit(df_train)
return m
def predict_with_prophet(model, future_dates):
forecast = model.predict(future_dates)
return forecast
def prepare_prophet_input_data(model, days_to_predict=1):
future = model.make_future_dataframe(periods=days_to_predict)
return future
def predict_future_price(model, days_to_predict=60):
input_data = prepare_prophet_input_data(model, days_to_predict)
predicted_price = predict_with_prophet(model, input_data)
# gets the predicted price for tomorrow
tmr = str(date.today() + timedelta(days=1))
return predicted_price.loc[predicted_price['ds'] == tmr, 'yhat'].values[0]
class BitcoinTransaction:
def __init__(self, transaction_type, price, amount, volume, profit_or_loss=None, transaction_trigger=None):
self.transaction_type = transaction_type
self.price = price
self.amount = amount
self.volume = volume
self.profit_or_loss = profit_or_loss
self.transaction_trigger = transaction_trigger
self.transaction_id = uuid.uuid4()
def __str__(self):
return f'Transaction ID: {self.transaction_id}, Transaction Type: {self.transaction_type}, Price: {self.price}, Amount: {self.amount} BTC, Volume: {self.volume}, Profit/Loss: {self.profit_or_loss}, Transaction Trigger: {self.transaction_trigger}'
def __repr__(self):
return f"({str(self)})"
class TransactionTypes(Enum):
BUY = 1
SELL = 2
""" Trading Bot Functions """
def get_price():
url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest"
headers = {"X-CMC_PRO_API_KEY": "91d6c06b-2f3c-458e-98b2-3e0de816e413"}
params = {"symbol": "BTC"}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
return data["data"]["BTC"]["quote"]["USD"]["price"]
else:
return None
def take_decision(current_price, predicted_price, balance, buy_order, sell_order):
# no buy orders placed yet
if buy_order is None:
# Check if price is predicted to grow beyond minimum required growth
if predicted_price >= current_price * (1 + MINIMUM_GROWTH):
# Check if we have sufficient balance
if balance > 0:
# Calculate the volume of BTC we can buy with our balance and the dollar amount
volume = balance / current_price
amount = balance
# Place the buy order
trigger = "Predicted future growth"
buy_order = BitcoinTransaction(TransactionTypes.BUY, current_price, amount, volume, trigger)
print("New buy order placed:", buy_order)
balance -= amount
else:
print("Insufficient balance to place a new buy order")
else:
print(f"Predicted future price does not meet the minimum growth requirement ({MINIMUM_GROWTH * 100}%) for buy trigger")
elif sell_order is None:
# Check if current price has fallen to trigger stoploss sell, minimize loss
if current_price <= buy_order.price * (1 - STOP_LOSS_PERCENTAGE):
volume = buy_order.volume
amount = current_price * volume
# calculate the loss (negative profit) incurred in this sell order
profit_or_loss = volume * (current_price - buy_order.price)
trigger = "Current price triggered stoploss"
sell_order = BitcoinTransaction(
TransactionTypes.SELL, current_price, amount, volume, profit_or_loss, trigger)
print("New sell order placed:", sell_order)
balance += amount
# Check if investment goal has been reached
elif balance + (current_price * buy_order.volume) >= GOAL:
volume = buy_order.volume
amount = current_price * volume
# calculate the profit/loss incurred in this sell order
profit_or_loss = volume * (current_price - buy_order.price)
trigger = "Investment goal reached"
sell_order = BitcoinTransaction(
TransactionTypes.SELL, current_price, amount, volume, profit_or_loss, trigger)
print("New sell order placed:", sell_order)
balance += amount
# Check if predicted future price will fall below stoploss of current price, prevent possible loss
elif predicted_price <= current_price * (1 - STOP_LOSS_PERCENTAGE):
volume = buy_order.volume
amount = current_price * volume
# calculate the profit/loss incurred in this sell order
profit_or_loss = volume * (current_price - buy_order.price)
trigger = "Predicted future price triggered stoploss"
sell_order = BitcoinTransaction(
TransactionTypes.SELL, current_price, amount, volume, profit_or_loss, trigger)
print("New sell order placed:", sell_order)
balance += amount
else:
print("Waiting for price to reach sell threshold")
return buy_order, sell_order, balance
# Main program
def main():
balance = INITIAL_BALANCE
buy_order = None
sell_order = None
goal_reached = False
# url = 'https://raw.githubusercontent.com/yetanotherpassword/COMS4507/main/BTC-USD.csv'
# update this url to new dataset for future retraining.
url = 'https://raw.githubusercontent.com/AnsonCNS/COMS4507/main/BTC-USD_2023-05-07.csv'
preprocessed_data = preprocess_data(url)
last_day_index = len(preprocessed_data.index)
# split dataset into 85% training (311/365 days), 15% testing (54/365 days)
df_train = preprocessed_data.head(last_day_index - 54)
model = train_prophet_model(df_train)
transaction_record = []
profit_and_loss_record = []
#FIXME the line below is for experimental demonstration, remove line to get real prices
price = get_price()
# keep running the bot if there is positive balance or an existing buy order has been placed.
while (balance > 0 or buy_order) and not goal_reached:
# FIXME
# uncomment the following line to get real prices
# current_price = get_price()
# FIXME
# the following line is for experimental demonstration, remove line to get real prices
current_price = random.randint(int(price*(1-0.1)), int(price*(1+0.1)))
if current_price is not None:
print("Current price of BTC: $", current_price)
predicted_price = predict_future_price(model)
print("Predicted future price of BTC: $", predicted_price)
buy_order, sell_order, balance = take_decision(
current_price, predicted_price, balance, buy_order, sell_order
)
if sell_order:
# record transaction
transaction_record.append(sell_order)
print("Sell order fulfilled. Profit: $", sell_order.profit_or_loss)
profit_and_loss_record.append({sell_order.transaction_id: sell_order.profit_or_loss})
# reset orders
buy_order = None
sell_order = None
elif buy_order:
# record transaction
transaction_record.append(buy_order)
if balance >= GOAL:
goal_reached = True
print("Investment goal reached! Stop trading.")
print("Remaining balance (USD): $", balance, "\n")
else:
print("Error getting price from CoinMarketCap API")
time.sleep(INTERVALS)
print("Transaction record:", transaction_record)
print("Profit and Loss:", profit_and_loss_record)
if __name__ == '__main__':
main()