-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPLS_TestTradeBuy.py
More file actions
executable file
·144 lines (119 loc) · 3.91 KB
/
PLS_TestTradeBuy.py
File metadata and controls
executable file
·144 lines (119 loc) · 3.91 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
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
'''
Created on 2015年9月9日
#脚本说明: !!!
#
#仿真交易环境下,向svr提交一个增强限价单: 买入港股00700 /100股/ 价格150.000
#并回显定单的状态, 全部成交或交易异常,程序退出
#
@author: futu
'''
import socket
import json
import string
import sys
import threading
import time
##################################################################################################
#timer定时器的一个实现
class Timer(threading.Thread):
"""
very simple but useless timer.
"""
def __init__(self, seconds):
self.runTime = seconds
threading.Thread.__init__(self)
def run(self):
time.sleep(self.runTime)
print "Buzzzz!! Time's up!"
class CountDownTimer(Timer):
"""
a timer that can counts down the seconds.
"""
def run(self):
counter = self.runTime
for sec in range(self.runTime):
time.sleep(1.0)
counter -= 1
class CountDownExec(CountDownTimer):
"""
a timer that execute an action at the end of the timer run.
"""
def __init__(self, seconds, action, args=[]):
self.args = args
self.action = action
CountDownTimer.__init__(self, seconds)
def run(self):
CountDownTimer.run(self)
self.action(self.args)
##################################################################################################
#futnn plubin会开启本地监听服务端
# 请求及发送数据都是jason格式, 具体详见插件的协议文档
host="localhost"
port=11111
global g_buy, g_lid, g_sid, g_end
#是否发送了请求
g_buy = 0
#订单本地id
g_lid = 0
#订单svr id
g_sid = 0
#是否结束了
g_end = 0
#socket
g_s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
g_s.connect((host,port))
def fun_TestTrade(args=[]):
print "enter fun testTrade ..."
req1 = {'Protocol':'6003','ReqParam':{'EnvType':'1', 'Cookie':'8888','OrderSide':'0','OrderType':'0','Price':'150000','Qty':'100','StockCode':'00700'},'Version':'1'}
global g_buy, g_lid, g_sid, g_end
if g_buy == 0:
str = json.dumps(req1) + "\r\n"
g_s.send(str)
g_buy = 1
print "Wait trade order respons ..."
rsp = g_s.recv(4096)
arstr = rsp.split("\r\n")
for str in arstr:
if str != "":
print str
decode = json.loads(str)
pro = decode["Protocol"]
#解析发送请求
if pro == "6003":
cookie = decode["RetData"]["Cookie"]
if cookie == "8888":
print "My Req Return"
g_lid = decode["RetData"]["LocalID"]
err = decode["ErrCode"]
if err != "0":
print "err=%s %s"%(err, decode["ErrDesc"])
g_end = 1
elif pro =="6001":
lid = decode["RetData"]["LocalID"]
if lid == g_lid:
g_sid = decode["RetData"]["OrderID"]
dtqty = decode["RetData"]["DealtQty"]
qty = decode["RetData"]["Qty"]
print "order status changed id=%s dealtQuty=%s"%(g_sid, dtqty)
if dtqty == qty:
g_end = 1
print "order finish!!"
print str
elif pro == "6002":
sid = decode["RetData"]["OrderID"]
if g_sid == sid:
print "order Err:%s"%(decode["RetData"]["OrderErrNotifyHK"])
g_end = 1
print str
print ""
if g_end == 0:
t1 = CountDownExec(2, fun_TestTrade)
t1.start()
else:
g_s.close()
print "test trade all over"
print "leave fun testTrade ...\n"
t1 = CountDownExec(2, fun_TestTrade)
t1.start()