forked from tymyx1019/financial_data_pools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautotrade.py
227 lines (193 loc) · 6.98 KB
/
autotrade.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
import io
import time
import pyperclip
from time import sleep
from pywinauto import clipboard
from pywinauto import mouse
import pandas as pd
import warnings
from pywinauto.application import Application
import subprocess
import sys
sys.path.append(".")
from stock_financial.comm_funcs import Singleton
warnings.filterwarnings('ignore')
class AutoTrade(Singleton):
app = None
def start_trade(self):
# if self.app is None:
# 关闭同花顺
subprocess.call('taskkill /F /IM xiadan.exe', creationflags=0x08000000)
time.sleep(0.1)
self.app = Application(backend="win32").start(r"C:\同花顺软件\同花顺\xiadan.exe")
time.sleep(0.1)
self.app_ths = self.app.window(title=u'网上股票交易系统5.0')
return self.app_ths
def common(self):
account = self.app_ths.window(best_match=u'ComboBox2').texts()[0]
name = self.app_ths.window(best_match=u'StatusBar').texts()[3]
print(account)
print(name)
def shx(self):
self.app_ths.post_message(0x111, 32790, 0)
sleep(0.02)
def sell_stock(self, code):
"""
卖出股票
:param code: 股票代码
:return:
"""
self.app_ths.post_message(0x111, 162, 0)
# shx()
self.app_ths.Edit1.set_focus()
time.sleep(0.02)
pyperclip.copy(code)
self.app_ths.Edit1.post_message(0x0302, 12, 0)
time.sleep(0.02)
amount_str = self.app_ths.static2.texts()[0]
if amount_str != '' and amount_str != '0':
amount = int(amount_str)
if amount > 0:
pyperclip.copy(amount) # 全部卖出
self.app_ths.Edit3.post_message(0x0302, 12, 0)
time.sleep(0.01)
i = 0
while self.app_ths.static2.texts()[0] != '':
self.app_ths.button1.click()
time.sleep(0.01)
i = i + 1
if i >= 1:
break
else:
return
def buy_stock(self, amount, code):
"""
买入股票
:param amount: 买入股票数量
:param code: 股票代码
:return:
"""
self.app_ths.post_message(0x111, 161, 0)
self.shx()
self.app_ths.Edit1.set_focus()
time.sleep(0.02)
pyperclip.copy(code)
self.app_ths.Edit1.post_message(0x0302, 12, 0)
time.sleep(0.01)
mouse.click(button='left', coords=(496, 151)) # 卖二
# app_ths.post_message(0x111, 161, 0)
pyperclip.copy(amount)
self.app_ths.Edit3.post_message(0x0302, 12, 0)
time.sleep(0.01)
# self.app_ths.button1.click()(369,324),(429,345)
# (445, 363)-(505, 384),(408, 324)-(468, 345)
mouse.click(button='left', coords=(445, 330))
time.sleep(0.1)
def button43(self, code):
# a.post_message(0x111, 1691, 380898) # 新增帐户
self.app_ths.post_message(0x111, 512, 0) # 双向委托
time.sleep(0.02)
self.app_ths.Edit1.set_focus()
pyperclip.copy(code)
self.app_ths.Edit1.post_message(0x0302, 12, 0)
time.sleep(0.01)
self.app_ths.print_control_identifiers(depth=4)
self.app_ths.Edit4.post_message(0x0302, 12, 0)
def withdraw_buy(self):
"""
撤买
:return:
"""
self.app_ths.post_message(0x111, 163, 0)
self.shx()
# a.Edit1.set_focus()
time.sleep(0.02)
self.app_ths.Button3.click()
def withdraw_sell(self):
"""
撤卖
:return:
"""
self.app_ths.post_message(0x111, 163, 0)
self.shx()
time.sleep(0.02)
self.app_ths.Button5.click()
def copy_frozen(self):
"""
定位到撤单按钮
:return:
"""
self.app_ths.post_message(0x111, 163, 0)
self.shx()
self.app_ths.CVirtualGridCtrl.post_message(0x111, 57634, 0)
sleep(0.03)
def get_data_frozen(self):
"""
获取同花顺撤单股票明细
读取ListView中的信息
:return: 清洗后的数据
"""
self.copy_frozen()
data = clipboard.GetData()
df = pd.read_csv(io.StringIO(data), delimiter='\t', na_filter=False)
df = pd.DataFrame(df, columns=[u'证券代码', u'证券名称', u'操作', u'委托数量'])
df.set_index([u'证券代码'], inplace=True)
print(df)
def get_available_amount(self):
# 获取可用金额
self.app_ths.post_message(0x111, 165, 0)
self.shx()
available_amount = self.app_ths.static6.texts()[0]
return float(available_amount)
def get_frozen_funds(self):
"""
获取冻结资金
:return:
"""
self.app_ths.post_message(0x111, 165, 0)
self.shx()
frozen_assets = self.app_ths.static5.texts()[0]
return float(frozen_assets)
def stock_market_value(self):
"""
获取股票市值
获取总资金total assets
:return:
"""
self.app_ths.post_message(0x111, 165, 0)
self.shx()
stock_values = self.app_ths.static11.texts()[0] # 获取股票市值
return float(stock_values)
def get_total_assets(self):
"""
获取总资金total assets
:return:
"""
self.app_ths.post_message(0x111, 165, 0)
# shx()
total_assets = self.app_ths.static12.texts()[0] # 获取总资金total assets
available_amount = self.app_ths.static6.texts()[0] # 可用余额
return float(total_assets), float(available_amount)
def copyto(self):
self.app_ths.post_message(0x111, 165, 0)
self.app_ths.CVirtualGridCtrl.post_message(0x111, 57634, 0)
sleep(0.1)
def get_position_data(self):
"""
#获取同花顺持仓股票明细
读取ListView中的信息
:return: 清洗后的数据
[{'明细': '', '证券代码': '002024', '证券名称': '苏宁易购', '股票余额': 0, '可用余额': 0, '冻结数量': 0, '成本价': 0.0, '市价': 4.66, '盈亏': 21.37, '盈亏比例': 0.0, '市值': 0.0, '交易市场': '深圳A股', 'Unnamed: 12': ''}, {'明细': '', '证券代码': '300536', '证券名称': '农尚环境', '股票余额': 3100, '可用余额': 3100, '冻结数量': 0, '成本价': -26.875, '市价': 20.96, '盈亏': 148289.21, '盈亏比例': 0.0, '市值': 64976.0, '交易市场': '深圳A股', 'Unnamed: 12': ''}]
"""
self.copyto()
data1 = clipboard.GetData()
data = data1.replace("(%)", "")
df = pd.read_csv(io.StringIO(data), delimiter='\t', na_filter=False)
ret = df.to_dict('records')
for i in ret:
i[u'证券代码'] = str(i[u'证券代码']).zfill(6)
return ret
if __name__ == '__main__':
# 示例 买入000001,100股
ths_trade = AutoTrade()
data = ths_trade.buy_stock(amount=100, code='000001')