-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_stagging_bot.py
162 lines (149 loc) · 5.41 KB
/
run_stagging_bot.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
import os
import sys
curdir = os.path.abspath(os.curdir)
sys.path.append(os.path.join(curdir, "internal"))
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import argparse
import asyncio
import sys
import time
import traceback
from internal.bot.stagging_bot import BinanceStaggingBot
from internal.db import init_instance as init_db_instance
from internal.db import instance as db_instance
from internal.utils.loguru_logger import init_global_logger
from internal.utils.global_vars import set_config, get_config
from loguru import logger as loguru_logger
# Coroutine to be invoked when the event loop is shutting down.
_cleanup_coroutine = None
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--verbose",
action="store_true",
help="output debug-level message"
)
parser.add_argument(
"--conf",
type=str,
default="./etc/stagging_bot.json",
help="the bot config file",
)
subparsers = parser.add_subparsers(
title="BINANCE_STAGGING_BOT",
dest="action",
help="action to perform"
)
balances_parser = subparsers.add_parser("balances")
_ = balances_parser
orders_parser = subparsers.add_parser("orders")
orders_parser.add_argument(
"--symbol",
type=str,
help="coin symbol, like BTCUSDT",
required=True,
)
orders_parser.add_argument(
"--limit",
type=int,
default=1,
help="recent N orders",
)
trade_parser = subparsers.add_parser("trade")
trade_parser.add_argument(
"--symbol",
type=str,
help="coin symbol, like BTCUSDT",
required=True,
)
trade_parser.add_argument(
"--side",
type=str,
default="BUY",
choices=["BUY"],
help="action to trade: BUY",
required=True,
)
trade_parser.add_argument(
"--quantity",
type=int,
help="the amount you wants to spend of the quote asset",
required=True,
)
trade_parser.add_argument(
"--when",
type=int,
help="the absolute unix timestamp when you want to trade",
)
trade_parser.add_argument(
"--elapse",
type=int,
help="the relative unix timestamp when you want to trade",
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
action = args.action
set_config(args.conf)
conf = get_config()
init_global_logger()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
bot = BinanceStaggingBot(use_proxy=False, use_testnet=True)
_cleanup_coroutine = bot.close
try:
task = asyncio.ensure_future(bot.is_ready())
ready = loop.run_until_complete(task)
if ready:
if action == "balances":
task = asyncio.ensure_future(bot.show_balances())
loop.run_until_complete(task)
elif action == "orders":
task = asyncio.ensure_future(bot.show_recent_n_orders(sym=args.symbol, n=args.limit))
loop.run_until_complete(task)
elif action == "trade":
# Setup mongodb connection (pool).
try:
init_db_instance(
client_conf={
"endpoint": conf["mongodb"]["endpoint"],
"username": conf["mongodb"]["username"],
"password": conf["mongodb"]["password"],
"auth_mechanism": conf["mongodb"]["auth_mechanism"],
"database": conf["mongodb"]["database"],
"collection": conf["mongodb"]["collection"],
},
io_loop=loop,
)
except Exception as e:
loguru_logger.error(f"Failed to setup mongodb connection (pool), err{e}.")
sys.exit(-1)
task = asyncio.ensure_future(db_instance().is_connected())
connected = loop.run_until_complete(task)
if connected:
loguru_logger.info("Setup mongodb connection (pool).")
else:
loguru_logger.error("Cannot setup mongodb connection (pool).")
sys.exit(-1)
if args.when is not None and args.when > 0:
task = asyncio.ensure_future(bot.trade(sym=args.symbol, quantity=args.quantity, when=args.when, side=args.side))
loop.run_until_complete(task)
elif args.elapse is not None and args.elapse > 0:
task = asyncio.ensure_future(bot.trade(sym=args.symbol, quantity=args.quantity, when=int(time.time()) + args.elapse, side=args.side))
loop.run_until_complete(task)
except Exception:
traceback.print_exc()
finally:
tasks = []
if _cleanup_coroutine is not None:
tasks.append(asyncio.ensure_future(_cleanup_coroutine()))
if action == "trade":
# Release mongodb connection (pool).
db_instance().close()
# NOTE: Wait 250 ms for the underlying connections to close.
# https://docs.aiohttp.org/en/stable/client_advanced.html#Graceful_Shutdown
loop.run_until_complete(asyncio.sleep(0.250))
loop.close()