/
bitbot.py
executable file
·117 lines (98 loc) · 3.02 KB
/
bitbot.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
#!/usr/bin/python3
import datetime
import getopt
import time
import sys
import re
sys.path.append('./source')
sys.path.append('./api')
import bbCfg
import apiKraken
import apiBacktest
import bbClasses
import bbFunctions
from bbKeys import *
def main(argv=None):
testFlag, btFlag, quiet = argParser(argv)
# Use data from exchange
if btFlag == 0:
API = apiKraken.API(keyKraken, secKraken)
t = bbClasses.trader(bbCfg.logFileName)
# Backtest: Use data from logfile
else:
API = apiBacktest.API()
with open(bbCfg.logFileNameBT, 'w') as logBT:
logBT.write('Time,Bid,Ask,EUR,BTC,Trade,minPrice,maxPrice\n')
t = bbClasses.trader(bbCfg.logFileNameBT)
p = bbClasses.portfolio(100,0)
m = bbClasses.marketData('Null', 500, 500)
if btFlag == 1 and testFlag == 0 and not quiet:
# Progress bar
for i in range(bbFunctions.progressBarLength()):
sys.stdout.write('|')
sys.stdout.write('\n')
sys.stdout.flush
while True:
mainLoop(m, p, t, API, testFlag, btFlag, quiet)
if testFlag:
break
elif btFlag == 1 and API.line == bbFunctions.file_len(bbCfg.logFileName):
break
if not btFlag:
timeNow = datetime.datetime.now()
delay = (10 - (timeNow.minute)%10) * 60 - timeNow.second
time.sleep(delay)
if not quiet:
print('\n')
def mainLoop(m, p, t, api, testFlag, btFlag, quiet):
api.getBalance(m, p, t)
api.getPrices(m, t, t.minTrade)
if btFlag != 1:
t.stopLoss(m, p)
t.updateBounds(m)
t.calcBaseWeight(m)
t.calcMomentum(m)
t.checkAllin(m, btFlag)
t.checkOverride()
t.calcCoinsToTrade(m, p)
t.checkTradeSize(m, p)
if testFlag == 1:
bbFunctions.printTermLine(m, p, t)
elif btFlag == 1:
bbFunctions.printLogLine(m, p, t, bbCfg.logFileNameBT, bounds = 1)
if api.line % bbCfg.progressBar == 0 and not quiet:
sys.stdout.write('|')
sys.stdout.flush()
else:
if t.suspend == 1:
print('Trading suspended!')
elif t.suspend == 0 and t.error == 0:
api.cancelOrders(t)
api.placeOrder(m, t)
bbFunctions.printTermLine(m, p, t)
bbFunctions.printStatus(m, p, t)
bbFunctions.printLogLine(m, p, t, bbCfg.logFileName)
bbFunctions.drawPlot(m, t)
if abs(t.coinsToTrade) > 0:
bbFunctions.printLogLine(m, p, t, bbCfg.txFileName)
if t.error != 0:
t.handle_error(m)
def argParser(argv):
testFlag = 0
btFlag = 0
quiet = 0
if argv is None:
argv = sys.argv[1:]
else:
argv = argv.split()
opts, args = getopt.getopt(argv, 'btq')
for o, a in opts:
if o == '-b':
btFlag = 1
elif o == '-t':
testFlag = 1
elif o == '-q':
quiet = 1
return testFlag, btFlag, quiet
if __name__ == "__main__":
main()