Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·219 lines (196 loc) · 6.71 KB
/
Copy pathrun.py
File metadata and controls
executable file
·219 lines (196 loc) · 6.71 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
""" It's a TradeBot """
import configparser
import curses
from httplib import HTTPException
import os
import signal
import socket
from ssl import SSLError
import sys
import time
from tradebot import TradeBot
def on_exit(sig, func=None):
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
curses.curs_set(1)
sys.exit()
def initialize():
# Initialize curses screen
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
curses.curs_set(0)
# Initialize screen
stdscr.addstr(0, 0, "=" * 82)
stdscr.addstr(1, 36, "BTC-E BOT")
stdscr.addstr(2, 0, "-" * 81)
stdscr.addstr(4, 0, "=" * 81)
stdscr.addstr(21, 0, " " * 70)
stdscr.addstr(22, 0, "=" * 82)
for i in range(1, 22):
stdscr.addstr(i, 0, "|")
stdscr.addstr(i, 81, "|")
# Initialize top bar labels
stdscr.addstr(3, 2, "SIM", curses.A_BOLD)
stdscr.addstr(3, 12, "VERBOSE", curses.A_BOLD)
stdscr.addstr(3, 24, "WAIT", curses.A_BOLD)
stdscr.addstr(3, 35, "PAIR", curses.A_BOLD)
stdscr.addstr(3, 50, "THRESH", curses.A_BOLD)
stdscr.addstr(3, 65, "TRADE", curses.A_BOLD)
stdscr.addstr(3, 6, "[ ]")
stdscr.addstr(3, 20, "[ ]")
stdscr.addstr(3, 29, "[ ]")
stdscr.addstr(3, 40, "[ ]")
stdscr.addstr(3, 57, "[ ]")
stdscr.addstr(3, 71, "[ ]")
# Initialize main area labels
stdscr.addstr(5, 2, "BALANCE:", curses.A_BOLD)
stdscr.addstr(5, 67, "PRICE HISTORY", curses.A_UNDERLINE)
stdscr.addstr(6, 57, "CURRENT ->")
stdscr.addstr(7, 2, "STATE:", curses.A_BOLD)
stdscr.addstr(8, 2, "TRADE INCREMENT:", curses.A_BOLD)
stdscr.addstr(9, 2, "TRADE THRESHOLD:", curses.A_BOLD)
stdscr.addstr(10, 2, "AVERAGE PRICE:", curses.A_BOLD)
stdscr.addstr(19, 2, "ORDER LIST ( )", curses.A_UNDERLINE)
stdscr.addstr(12, 2, "TRADE HISTORY", curses.A_UNDERLINE)
stdscr.refresh()
return stdscr
def update(stdscr):
curr1 = tradebot.curr[0].upper()
curr2 = tradebot.curr[1].upper()
stdscr.addstr(1, 56, "%s" % time.asctime())
(state, thresh) = tradebot.get_state()
if state == "buy":
thresh = "< %0.06f" % thresh
elif state == "sell":
thresh = "> %0.06f" % thresh
elif state == "build":
thresh = "%0.06f" % thresh
stdscr.addstr(9, 20, "%s %s" % (thresh, curr2))
stdscr.addstr(10, 20, "%f %s" % (tradebot.average_price(), curr2))
stdscr.addstr(5, 12, "%f %s / %f %s"
% (tradebot.get_balance(1), curr1, tradebot.get_balance(2),curr2))
stdscr.addstr(7, 20, "%s " % state.upper())
stdscr.addstr(8, 20, "%f %s" % (tradebot.get_trade_cost(), curr1))
# Top Bar values
sim = "OFF"
if tradebot.simulation:
sim = "ON"
stdscr.addstr(3, 7, "%3s" % sim)
stdscr.addstr(3, 21, "%s" % args['verbosity'][:1])
stdscr.addstr(3, 30, "%3d" % tradebot.wait)
stdscr.addstr(3, 41, "%s_%s" % (tradebot.curr[0], tradebot.curr[1]))
stdscr.addstr(3, 58, "%.02f%%" % (tradebot.trade_threshold * 100))
stdscr.addstr(3, 72, "%6.02f%%" % (tradebot.trade_increment * 100))
# Price History
line = 6
history = tradebot.get_price_history()
for item in history:
stdscr.addstr(line, 68, "%f %s" % (item, curr2))
line += 1
if line > 21:
break
# Completed trades
history = tradebot.get_trade_history()
line = 13
for item in history:
stdscr.addstr(line, 2, "%s: %s %f @ %.05f %s " % (item.timestamp,
item.type,
item.amount,
item.rate,
curr2))
line += 1
# Order list
orders = tradebot.get_orders()
stdscr.addstr(19, 14, "%2d" % len(orders))
line = 20
stdscr.addstr(20, 2, " " * 40)
stdscr.addstr(21, 2, " " * 40)
for order in orders:
stdscr.addstr(line, 2, "%s %f @ %.05f %s" % (order.type,
order.amount,
order.rate,
curr2))
line += 1
if line > 21:
break
stdscr.refresh()
signal.signal(signal.SIGQUIT, on_exit)
signal.signal(signal.SIGTERM, on_exit)
signal.signal(signal.SIGINT, on_exit)
errlog = 'error.log'
config = configparser.ConfigParser()
config.read('tradebot.conf')
args = {}
if 'api_file' in config['BTC-E']:
args['api_file'] = str(config['BTC-E']['api_file'])
else:
sys.stderr.write('api_file not defined')
sys.exit(1)
with open(args['api_file']) as f:
args['api_key'] = f.readline().strip()
if 'increment' in config['TRADE']:
args['trade_increment'] = float(config['TRADE']['increment'])
else:
args['trade_increment'] = 0.012
if 'threshold' in config['TRADE']:
args['trade_threshold'] = float(config['TRADE']['threshold'])
else:
args['trade_threshold'] = 0.006
if 'pair' in config['BTC-E']:
args['pair'] = str(config['BTC-E']['pair'])
else:
args['pair'] = 'ltc_btc'
if 'wait' in config['TRADE']:
args['wait'] = int(config['TRADE']['refresh'])
else:
args['wait'] = 15
if 'simulation' in config['MAIN']:
args['simulation'] = str(config['MAIN']['simulation'])
else:
args['simulation'] = 'off'
if 'verbosity' in config['MAIN']:
args['verbosity'] = config['MAIN']['verbosity'].upper()
else:
args['verbosity'] = "ERROR"
if 'logfile' in config['MAIN']:
args['logfile'] = str(config['MAIN']['logfile'])
else:
args['logfile'] = 'tradebot.log'
if 'db' in config['MAIN']:
args['db'] = str(config['MAIN']['db'])
else:
args['db'] = 'tradebot.db'
sys.stderr = open(errlog, "w")
tradebot = TradeBot(args)
stdscr = initialize()
while True:
try:
stdscr.addstr(21, 2, " " * 70)
tradebot.refresh_price()
for i in range(tradebot.wait):
update(stdscr)
time.sleep(1)
except (SSLError, HTTPException, ValueError, socket.error):
curses.start_color()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
for i in range(60):
stdscr.addstr(21, 2, "Failed to connect to exchange. Retrying " \
"in %d" % i, curses.color_pair(1))
except Exception as e:
curses.nocbreak()
#stdscr.keypad(0)
curses.echo()
curses.endwin()
curses.curs_set(1)
import traceback
type_, value_, traceback_ = sys.exc_info()
for line in traceback.format_tb(traceback_):
sys.stderr.write(line)
sys.stderr.write(e.__class__.__name__ + ": ")
sys.stderr.write(e.message)
sys.exit()