-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastsim.py
117 lines (97 loc) · 3.19 KB
/
fastsim.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
import json, time, convert
FEE = 0.0053
WINDOW = 60
MIN = 1
MARGIN_BUY = 0.015
MARGIN_SELL = 0.015
print 'Loading history...'
history = json.load(file('history.raw.json'))
class HrBot:
def __init__(self):
self.btc = True
self.price = 100000000
self.trades = []
def update(self, trades):
self.trades += trades
# offset = time.time() - WINDOW
cutoff = trades[-1]['date'] - WINDOW
self.trades = filter(lambda x: x['date'] > cutoff, self.trades)
# if len(self.trades) > WINDOW:
# self.trades = self.trades[-WINDOW:]
def onTrade(self, amount, price):
self.btc = not self.btc
self.price = price
def action(self, prices):
# print 'got:', len(self.trades)
if len(self.trades) < MIN:
return 0
# price, amount, tid, date
# t = self.trades[-WINDOW:]
# movavg = sum(map(lambda x: x['price'], t)) / WINDOW
movavg = sum(map(lambda x: x['price'], self.trades)) / len(self.trades)
buy = prices['buy']
sell = prices['sell']
margin_buy = MARGIN_BUY
margin_sell = MARGIN_SELL
if buy < self.price:
pass
margin_buy *= 0.8
if sell > self.price:
pass
margin_sell *= 0.8
if not self.btc and buy < movavg * (1 - margin_buy):
# return (self.pool - self.balance)
return 1
elif self.btc and sell > movavg / (1 - margin_sell):
# return -self.balance
return -1
return 0
def sim(start, end):
bot = HrBot()
btc = 1 * 10**8
usd = 0
f = "%d/%m-%Y %H:%M"
start = time.mktime(time.strptime(start, f))
end = time.mktime(time.strptime(end, f))
trades = filter(lambda x: x['date'] >= start and x['date'] <= end,
history
)
print '%s -- %s:' % \
(time.ctime(trades[0]['date']), time.ctime(trades[-1]['date'])),
trades = map(lambda x: {'date': x['date'],
'price': int(x['price_int'])},
trades)
for t in trades:
bot.update([t])
p = t['price']
buy = p * 1.02
sell = p * 0.98
x = bot.action({'buy': buy, 'sell': sell})
if x != 0:
bot.onTrade(x, 0)
if x < 0:
# print "sell", sell
usd = (1 - FEE) * (btc * sell) / (10 ** 8)
btc = 0
else:
# print "buy", buy
btc = (1 - FEE) * usd / buy * (10 ** 8)
usd = 0
if btc == 0:
btc = usd / trades[-1]['price']
else:
btc /= 10 ** 8
print "%.2f%%" % ((btc - 1) * 100)
# for m in range(7, 13):
# d = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# start = "01/%d-2010 00:00" % m
# end = "%d/%d-2010 23:59" % (d[m], m)
# sim(start, end)
for m in range(1, 10):
d = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
start = "01/%d-2011 00:00" % m
end = "%d/%d-2011 23:59" % (d[m], m)
sim(start, end)
# sim('01/05-2011 00:00', '15/08-2011 23:59')
# sim('01/08-2011 00:00', '15/08-2011 23:59')
# sim('15/08-2011 00:00', '22/08-2011 23:59')