-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decision.py
110 lines (79 loc) · 2.44 KB
/
Decision.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
from Bank import Bank
from datetime import datetime
import sys
from GDAX import GDAX
class Decision(object):
""""""
def __init__(self, bank, coin = 'BTC-USD', output_file = "output_file.txt"):
# 0 = start, 1 = buy, 2 = sell
self.state = 0
# Bank information
self.bank = bank
# Price previously purchased crypto
self.bought_at = 0
# The coin-USD pair
self.coin = coin
# The name of the file to putput info to
self.output_file = output_file
def sell(self, price_crypto):
if self.state !=2 :
print "Error, trying to sell on a buy"
return
print " Selling at: " + str(price_crypto)
self.bank.sell(price_crypto)
if self.bank.dead:
print "Bank is dead!!"
sys.exit(0)
self.bought_at = 0
now = datetime.now()
samantha = open(self.output_file, "a")
samantha.write('===Sold at : {day:02d}-{hour:02d}-{second:02d}===\n'.format(day = now.day, hour = now.hour, second = now.second))
samantha.write(self.bank.current_info())
samantha.close()
def buy(self, price_crypto):
if self.state !=1:
return "Error, trying to buy on a sell"
print " Buying at: " + str(price_crypto)
self.bank.buy(price_crypto)
if self.bank.dead:
print "Bank is dead!!"
sys.exit(0)
self.bought_at = price_crypto
now = datetime.now()
samantha = open(self.output_file, "a")
samantha.write('===Bought at : \n {day:02d}-{hour:02d}-{second:02d}===\n'.format(day = now.day, hour = now.hour, second = now.second))
samantha.write(self.bank.current_info())
samantha.close()
def get_delta(self):
return 0.000
def start_state(self, EMA, SMA, price_crypto):
delta = self.get_delta()
if EMA < (SMA - delta * SMA):
# Switch to buy state
self.state = 1
return
# Waiting to buy (EMA > SMA)
def wait_to_buy(self, EMA, SMA, price_crypto):
delta = self.get_delta()
if EMA > (SMA + SMA*delta):
self.buy(price_crypto)
self.state = 2
return
# Waiting to sell (EMA < SMA)
def wait_to_sell(self, EMA, SMA, price_crypto):
delta = self.get_delta()
if EMA < (SMA - SMA*delta):
if price_crypto > self.bought_at:
self.sell(price_crypto)
self.state = 1
return
def get_state(self):
return self.state
# Think carefully
def make_decision(self, EMA, SMA, price_crypto):
if self.state == 0 :
self.start_state(EMA, SMA, price_crypto),
if self.state == 1 :
self.wait_to_buy(EMA, SMA, price_crypto),
if self.state == 2 :
self.wait_to_sell(EMA, SMA, price_crypto)