forked from gdemos01/crypto-trading-ai-bot-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTrader.py
75 lines (61 loc) · 3.59 KB
/
AutoTrader.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
from VirtualAccount import VirtualAccount
import numpy as np
from Config import *
import time
class AutoTrader:
def __init__(self,model):
self.advisor = model
self.account = VirtualAccount()
self.trade_amount = 100
def buy(self):
prev_bought_at = self.account.bought_btc_at # How much did I buy BTC for before
if self.account.usd_balance - self.trade_amount >= 0:
if prev_bought_at == 0 or self.account.last_transaction_was_sell or (prev_bought_at > self.account.btc_price): #or (self.account.btc_price/prev_bought_at -1 > 0.005):
print(">> BUYING $",self.trade_amount," WORTH OF BITCOIN")
self.account.btc_amount += self.trade_amount / self.account.btc_price
self.account.usd_balance -= self.trade_amount
self.account.bought_btc_at = self.account.btc_price
self.account.last_transaction_was_sell = False
else:
print(">> Not worth buying more BTC at the moment")
else:
print(">> Not enough USD left in your account to buy BTC ")
def sell(self):
if self.account.btc_balance - self.trade_amount >= 0:
if self.account.btc_price > self.account.bought_btc_at: # Is it profitable?
print(">> SELLING $",self.trade_amount," WORTH OF BITCOIN")
self.account.btc_amount -= (self.trade_amount / self.account.btc_price)
self.account.usd_balance += self.trade_amount
self.account.last_transaction_was_sell = True
else:
print(">> Declining sale: Not profitable to sell BTC")
else:
print(">> Not enough BTC left in your account to buy USD ")
def runSimulation(self,samples,prices):
print("> Trading Automatically for ",TESTING_MONTHS)
day_count = 0
for i in range(0,len(samples)):
if i % 24 == 0:
day_count += 1
print("#################################################################################################")
print("# Account Balance: $", (self.account.usd_balance + self.account.btc_balance), " BTC: $",
self.account.btc_balance, " USD: $", self.account.usd_balance, "")
print("#################################################################################################")
print("########################################## DAY ",day_count," #########################################")
if i % 6 == 0: # Perform a prediction every 6 hours
prediction = self.advisor.predict(np.array([samples[i]]))
#btc_price = samples[i][len(samples[i])-1]
btc_price = prices[i]
if self.account.btc_price != 0:
self.account.btc_balance = self.account.btc_amount * btc_price
self.account.btc_price = btc_price
if prediction == 1:
self.buy()
else:
self.sell()
self.account.btc_balance = self.account.btc_amount * btc_price
time.sleep(1) # Only for Visual Purposes
print("#################################################################################################")
print("# Account Balance: $", (self.account.usd_balance + self.account.btc_balance), " BTC: $",
self.account.btc_balance, " USD: $", self.account.usd_balance, "")
print("#################################################################################################")