forked from terryh/autotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddeclient.py
executable file
·243 lines (204 loc) · 6.61 KB
/
ddeclient.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A tool listen to DDE SERVER
ddecleint.py [options] outputfile
-i interval the timer for check the strategy, default 0.5 second
--config=ddeconfig file file name must end with .py encoding utf-8
example of config file:
DDESERVERS = [
{
'server': u'DDEEXCEL',
'topic': u'FUTURETXFA0',
'price': u'市價',
'total': u'累計交易量',
'time': u'時間',
},
#{
# 'server': u'',
# 'topic': u'',
# 'price': u'',
# 'total': u'',
# 'time': u'',
# },
]
this function need to install win32 package for python
Author: TerryH, email: terryh.tp at gmail.com,
License: BSD
"""
import time
import datetime
import sys
import codecs
import getopt
import re
import codecs
re_dde = re.compile("DDESERVERS.?=.?\[.*?\]",re.DOTALL)
try:
import win32ui
import dde
except:
print "You must have pywin32 install, http://sourceforge.net/projects/pywin32/"
sys.exit()
try:
import cPickle as pickle
except:
import pickle
class DDEClient(object):
def __init__(self,server="",topic=""):
# having our dde conversation fifrst
self.ddeclient = dde.CreateServer()
self.ddeclient.Create("AutoTrader")
self.conversation = dde.CreateConversation(self.ddeclient)
self.ddeserver = ""
self.ddedata = ""
self.configdict={}
self.status = 0
if server and topic:
self.ddeserver = server
self.ddedata = topic
self.connect()
def resetclient(self):
# having our dde conversation fifrst
try:
self.ddeclient.Destroy()
except:
pass
self.ddeclient = dde.CreateServer()
self.ddeclient.Create("AutoClient")
self.conversation = dde.CreateConversation(self.ddeclient)
def disconnect(self):
# having our dde conversation fifrst
try:
# clean up
self.ddeclient.Destroy()
self.ddeserver = ""
self.ddedata = ""
except:
pass
def connect(self, server="", topic=""):
if server and topic:
self.ddeserver = server
self.ddedata = topic
if self.ddeserver and self.ddedata:
try:
if self.conversation.Connected()==0:
self.conversation.ConnectTo(self.ddeserver,self.ddedata)
self.status = 1
except:
print "DDE connetion fail"
self.status = 0
def request(self, item=u""):
if self.status and item:
# FIXME because self.conversation.Connected() is break, so we use our own self.status
try:
value = self.conversation.Request(item)
return value
except:
print "DDE request %s fail" % (item)
self.status=0
else:
while not self.status:
print "DDE connection fail or no topic"
self.resetclient()
self.connect()
time.sleep(5)
def dde_query(dde_list=[],out=""):
if dde_list and out:
# reset some variabale
content=""
cl = [0,0,0]
dt = 0
HH = 0
MM = 0
SS = 0
price = 0
volume = 0
dirty = 0
dde_dt = 0
dde_price = 0
dde_volume = 0
nl = []
try:
fp = file(out)
content = fp.read()
fp.close()
except:
fp = open(out,"w")
fp.close()
# the out file format is pickle in python list [time(),int(price),int(volume)]
if fp:
if content:
cl = pickle.loads(content)
dde_volume = cl[2]
for dd in dde_list:
tt = dd.request(dd.configdict['time'])
try:
pp = int(float(dd.request(dd.configdict['price'])))
except:
pp = 0
try:
vv = int(float(dd.request(dd.configdict['total'])))
except:
vv = 0
if tt:
if tt.find(":")>0:
# like HH:MM:SS
m1 = tt.find(":")
m2 = tt.rfind(":")
HH = int(tt[m1-2:m1])
MM = int(tt[m1+1:m2])
SS = int(tt[m2+1:m2+3])
elif len(tt)>=5 and len(tt)<=6:
SS=int(tt[-2:])
MM=int(tt[-4:-2])
HH=int(tt[-6:-4])
tt = datetime.time(HH,MM,SS)
nl = [tt,pp,vv]
#print nl
if vv != dde_volume :
# volume bigger than update
fp = open(out,"w")
pickle.dump(nl,fp)
fp.close()
dde_volume = vv
if __name__ == '__main__':
# take from HTS for example
#server,topic = "DDEEXCEL","FUTUREMXFA0"
#DDE = DDEClient(server,topic)
#print DDE.request(u"時間")
opts, args = getopt.getopt(sys.argv[1:], "i:",
["config=",])
if len(args) > 0:
interval = 0.5
config = ""
out = args[0]
cmd = ''
for o, a in opts:
if o in ("-i"):
interval = float(a)
elif o in ("--config"):
config = a
if config and out and interval:
if config:
ddeconfig = codecs.open(config, encoding='utf-8').read()
if re_dde.search(ddeconfig):
cmd = re_dde.search(ddeconfig).group()
exec(cmd)
dde_list = []
# FIXME find no way to create two dde Server at same time
DDESERVERS = DDESERVERS[:1]
for c in DDESERVERS:
if "server" in c and "topic" in c and 'price' in c and \
'time' in c and 'total' in c:
#print c['server'],c['topic']
DDE = DDEClient(c['server'],c['topic'])
DDE.configdict = c
dde_list.append(DDE)
if dde_list:
while True:
dde_query(dde_list,out)
time.sleep(interval)
else:
print __doc__
sys.exit()