forked from kungfu-origin/kungfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwingchun
executable file
·123 lines (104 loc) · 4.45 KB
/
wingchun
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
#!/usr/bin/python -u
'''
Copyright [2017] [taurus.ai]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import os, sys, argparse
def setup_environment_variables():
fin = open('/opt/kungfu/master/etc/sysconfig/kungfu')
for line in fin.readlines():
ss = line.strip().split('=')
if len(ss) == 2:
os.environ[ss[0]] = ss[1].strip()
class WingchunEntrance(object):
def __init__(self):
parser = argparse.ArgumentParser(
description='Wingchun Command Tool',
usage='''wingchun <command> [<args>]
The most commonly used wingchun commands are:
td start trade engine
md start market data engine
strategy start a strategy
pos handle position block for td
report report order/trade for strategy
help help on writing strategy
''')
if len(sys.argv) == 1:
print parser.usage
return
parser.add_argument('command', help='Subcommand to run')
# parse_args defaults to [1:] for args, but you need to
# exclude the rest of the args too, or validation will fail
args = parser.parse_args(sys.argv[1:2])
if not hasattr(self, args.command):
print 'Unrecognized command'
parser.print_help()
exit(1)
# use dispatch pattern to invoke method with same name
getattr(self, args.command)()
def run_engine(self, tp):
engine_parser = argparse.ArgumentParser(
description='Engine Parsing Tool',
usage='''wingchun td/md name''')
engine_parser.add_argument('name', help='name of engine')
args = engine_parser.parse_args(sys.argv[2:])
kungfu.wingchun.run_engine(args.name, tp)
def td(self):
self.run_engine('td')
def md(self):
self.run_engine('md')
def strategy(self):
parser = argparse.ArgumentParser(
description='wingchun strategy',
usage='''wingchun strategy -n strategy_name -p file_path''')
parser.add_argument('-n', '--name', type=str, help='name of strategy (StrategyName_ProductId_PMId)')
parser.add_argument('-p', '--path', type=str, help='path of the strategy py file')
args = parser.parse_args(sys.argv[2:])
kungfu.wingchun.run_strategy(args.name, args.path)
def pos(self):
os.system(os.environ["WINGCHUN_HOME"] + "/bin/td_pos_tool " + ' '.join(sys.argv[2:]))
def help(self):
if len(sys.argv) >= 3:
if sys.argv[2] == 'report':
os.system(os.environ["WINGCHUN_HOME"] + '/bin/wc_printer -h')
return
elif sys.argv[2] == 'pos':
os.system(os.environ["WINGCHUN_HOME"] + "/bin/td_pos_tool -h")
return
cmd_append = 'help_' + str(sys.argv[2])
if hasattr(kungfu.wingchun.wc_configs, cmd_append):
getattr(kungfu.wingchun.wc_configs, cmd_append)()
return
kungfu.wingchun.wc_configs.help()
def report(self):
if len(sys.argv) >= 3:
os.system(os.environ["WINGCHUN_HOME"] + '/bin/wc_printer ' + ' '.join(sys.argv[2:]))
else:
files = os.listdir('/shared/kungfu/journal/user')
strs = {}
for fn in files:
ss = fn.split('.')
if len(ss) == 2 and ss[1].isdigit():
if not (ss[0] in strs):
strs[ss[0]] = []
strs[ss[0]].append(ss[1])
print 'Please specify strategy name with "-n".'
print 'For more details, please check "wingchun report -h"'
print '#%3s %14s: [%s]' % ('idx', 'strategy_name', 'td1_idx, $td2_idx, ...')
for idx, str in enumerate(strs):
print '%3s %15s: [%s]' % (idx + 1, str, ', '.join(strs[str]))
if __name__ == '__main__':
if "WINGCHUN_HOME" in os.environ:
import kungfu.wingchun
WingchunEntrance()
else:
setup_environment_variables()
os.system(' '.join(sys.argv))