-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpine-bot-client.py
152 lines (127 loc) · 4.01 KB
/
pine-bot-client.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
# coding=utf-8
import sys
import os
## Python version check
PYTHON_VERSION = sys.version.split()[0]
version = sys.version_info
if version[0] != 3 or version[1] < 6:
print(f"*** Use Python 3.6 or above: now={PYTHON_VERSION} ***")
sys.exit(1)
## CCXT version check
try:
import ccxt
except ImportError:
print(f"*** CCXT is not found!! ***")
sys.exit(1)
CCXT_VERSION = 'builtin'
if not os.path.exists('ccxt'):
CCXT_VERSION = 'unknown'
try:
import pip
ccxt = list(filter(lambda pkg: pkg.key == 'ccxt', pip.get_installed_distributions()))
if ccxt:
ccxt = ccxt[0]
CCXT_VERSION = ccxt.version
ver = [int(n) for n in ccxt.version.split('.')]
if ver < [1, 17, 376]:
print(f"Recommend CCXT 1.17.376 or above: now={CCXT_VERSION}")
except:
pass
## setup
from logging import getLogger
logger = getLogger()
from util.parameters import load_parameters, load_param_file
BOT_NAME = 'Pine-Bot'
VERSION = '0.1'
## do_help
def do_help ():
s = '''\
[Usage] python pine-bot-client.py <command> [<argument>, ...]
<command> := help|support|init|run
<support>
support
Display supported exchanges
support <exchange>
Display supported markets
support <exchange> <market>
Check given exchange/markt pair is supported
<init>
init <pine script>
Generate parameter json file unde the same directory of given pine script
<run>
run <pine script>
Run Pine script
run <pine script> <additiona parameter json file>
Run Pine script with additional parameters.
'''
print(s)
sys.exit(0)
## Handling command line arguments
class CommandLineError (Exception):
pass
def handle_command_line ():
argc = len(sys.argv)
if argc < 2:
raise CommandLineError("missing <command>. ('help' to show usge)")
# command
command = sys.argv[1]
if command not in ('run', 'init', 'help', 'support'):
raise CommandLineError("invalid command: {}".format(command))
if command == 'help':
return (command, None, None, None)
if command == 'support':
exchange = market = None
if argc > 2:
exchange = sys.argv[2]
if argc > 3:
market = sys.argv[3]
return (command, exchange, market, None)
# pine script
if argc < 3:
raise CommandLineError("missing <pine script>")
pine_fname = sys.argv[2]
pine_str = ''
try:
with open(pine_fname) as f:
pine_str = f.read()
except Exception as e:
raise CommandLineError("fail to read pine script: {}".format(e)) from e
# explicit configuration
params = None
if argc > 3:
try:
load_param_file(sys.argv[3])
except Exception as e:
raise CommandLineError(f"fail to load parameter file: {e}") from e
return (command, pine_fname, pine_str, params)
## Run as a script
if __name__ == '__main__':
from util.logging import enable_logfile
from command.support import do_support
from command.init import do_init
from command.run import do_run
try:
command, *args = handle_command_line()
except CommandLineError as e:
logger.error(e)
logger.info('Type `python {} help` for usage'.format(sys.argv[0]))
sys.exit(1)
if command == 'help':
do_help()
try:
if command == 'support':
params = load_parameters()
do_support(params, args[0], args[1])
else:
pine_fname, pine_str, params = args
if command == 'init':
params = load_parameters(params)
do_init(params, pine_fname, pine_str)
elif command == 'run':
params = load_parameters(params, pine_fname)
enable_logfile(pine_fname, params)
logger.info(f"=== {BOT_NAME} ver.{VERSION} start ===")
do_run(params, pine_fname, pine_str)
except Exception as e:
logger.critical("fail to execute '%s: %s", command, e, exc_info=e)
sys.exit(1)