-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.py
58 lines (44 loc) · 1.68 KB
/
simulate.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
import json, time, convert
from decimal import Decimal
from scalper import Scalper
FEE = Decimal('0.0065')
print 'Loading history...'
history = json.load(file('history.raw.json'))[-1::-1]
class Sim():
def __init__(self, history):
self.history = history
def trades(self, since):
return [self.history.pop()]
def depth(self):
price = self.history[-1]['price']
# print price
return {'bids': [{'amount': Decimal('Infinity'),
'price': price * Decimal('0.98')}],
'asks': [{'amount': Decimal('Infinity'),
'price': price * Decimal('1.02')}]
}
def buy(self, amount, price, ttl = None, onProgress = None):
onProgress(None, amount * (1 - FEE), price)
def sell(self, amount, price, ttl = None, onProgress = None):
onProgress(None, amount, price * (1 + FEE))
# scalper = Scalper(Sim(history), Decimal(1), Decimal(0))
# scalper.start()
def sim(start, end):
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': Decimal(x['price_int']) / Decimal(10**5),
'tid': x['tid']},
trades)
bot = Scalper(Sim(trades), Decimal(1), Decimal(0))
try:
bot.start()
except:
print bot.btcs, bot.usds
sim('01/08-2011 00:00', '14/08-2011 23:59')