-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
143 lines (108 loc) · 3.51 KB
/
main.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
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
import asyncio
import yaml
import sys
import click
import logging as log
from market_data_collector import MarketDataCollector
from orderbook_manager import OrderbookManager
@click.group()
@click.option('--config', '-c', default="config.yml", type=click.File())
@click.option('--endpoint', '-e', default="https://api.qtrade.io", help='qtrade backend endpoint')
@click.option('--keyfile', '-f', default="lpbot_hmac.txt", help='a file with the hmac key', type=click.File('r'))
@click.option('--verbose', '-v', default=False, is_flag=True)
@click.pass_context
def cli(ctx, config, endpoint, keyfile, verbose):
log_level = "DEBUG" if verbose is True else "INFO"
root = log.getLogger()
root.setLevel(log_level)
handler = log.StreamHandler(sys.stdout)
handler.setLevel(log_level)
formatter = log.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
hmac_key = keyfile.read().strip()
config = yaml.load(config)
ctx.obj['mdc'] = MarketDataCollector(config['market_data_collector'])
ctx.obj['obm'] = OrderbookManager(
endpoint, hmac_key, config['orderbook_manager'])
@cli.command()
@click.pass_context
def run(ctx):
loop = asyncio.get_event_loop()
try:
loop.create_task(ctx.obj['obm'].monitor())
loop.create_task(ctx.obj['mdc'].daemon())
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
print("Closing Loop")
loop.close()
@cli.command()
@click.pass_context
def mdc(ctx):
loop = asyncio.get_event_loop()
loop.create_task(ctx.obj['mdc'].daemon())
loop.run_forever()
@cli.command()
@click.pass_context
def obm(ctx):
loop = asyncio.get_event_loop()
loop.create_task(ctx.obj['obm'].monitor())
loop.run_forever()
@cli.command()
@click.pass_context
def balances_test(ctx):
print(ctx.obj['obm'].api.balances_merged())
@cli.command()
@click.pass_context
def compute_allocations_test(ctx):
print(ctx.obj['obm'].compute_allocations())
@cli.command()
@click.pass_context
def allocate_orders_test(ctx):
allocs = ctx.obj['obm'].compute_allocations()
a = allocs.popitem()[1]
print(ctx.obj['obm'].allocate_orders(a[1], a[0]))
@cli.command()
@click.pass_context
def price_orders_test(ctx):
allocs = ctx.obj['obm'].compute_allocations()
m, a = allocs.popitem()
print(ctx.obj['obm'].price_orders(
ctx.obj['obm'].allocate_orders(a[0], a[1], m), 0.0000033, 0.0000032))
@cli.command()
@click.pass_context
def update_orders_test(ctx):
ctx.obj['obm'].update_orders()
@cli.command()
@click.pass_context
def cancel_all(ctx):
ctx.obj['obm'].api.cancel_all_orders()
@cli.command()
@click.pass_context
def rebalance_test(ctx):
ctx.obj['mdc'].update_tickers()
ctx.obj['mdc'].update_midpoints()
print(ctx.obj['obm'].generate_orders(force_rebalance=False))
@cli.command()
@click.pass_context
def estimate_account_value(ctx):
ctx.obj['mdc'].update_tickers()
ctx.obj['mdc'].update_midpoints()
print(ctx.obj['obm'].estimate_account_value())
@cli.command()
@click.pass_context
def estimate_account_gain(ctx):
ctx.obj['mdc'].update_tickers()
ctx.obj['mdc'].update_midpoints()
btc_val, usd_val = ctx.obj['obm'].estimate_account_value()
print(ctx.obj['obm'].estimate_account_gain(btc_val))
@cli.command()
@click.pass_context
def trade_tracking_test(ctx):
print(ctx.obj['obm'].boot_trades())
print(ctx.obj['obm'].check_for_trades())
if __name__ == "__main__":
cli(obj={})