forked from shiyindebcd/ProcessTrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateNewProcess_Inheritance.py
294 lines (250 loc) · 17.8 KB
/
CreateNewProcess_Inheritance.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
import os
import re
import pandas as pd
from PySide6 import QtCore
from PySide6.QtGui import QCursor, Qt
from PySide6.QtWidgets import QWidget
from read_write_file import ReadWriteCsv
from UI.CreateNewProcess_dark import Ui_Form as UI_dark
from UI.CreateNewProcess_light import Ui_Form as UI_light
from main import THEME
if THEME == "dark":
UI = UI_dark
else:
UI = UI_light
class NewProcessWindow(QWidget, UI): # 添加新的策略进程窗口类
def __init__(self, parent):
super(NewProcessWindow, self).__init__()
self.parent = parent
print(parent)
self.setupUi(self)
# 不显示标题栏
flags = QtCore.Qt.WindowFlags(Qt.FramelessWindowHint)
self.setWindowFlags(flags)
# 不显示空白边框
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
# 设置透明度
self.setWindowOpacity(0.95)
#
# self.parent.ioModule = ReadWriteCsv() # 创建读写文件对象
self.add_paramer_to_combobox()
self.label_info.setText('命名规则: 进程名(或策略实例名)= 客户名 + 天勤帐户 + 交易所.合约 + 合约周期 + min, '
'\n该名称将作为策略实例的名称,不能和已有的策略实例名重复. 添加时五个元素都不可或缺')
self.Btn_add_process_param.clicked.connect(self.get_process_parameters)
self.Btn_clear_input_process_param.clicked.connect(self.process_parameters_input_clear)
self.comboBox_exchange.activated.connect(self.add_contracts_to_comboBox_symbol) # comboBox 信号槽函数
self.comboBox_contract_type.activated.connect(self.add_contracts_to_comboBox_symbol)
def mousePressEvent(self, e): # 鼠标点击事件
if e.button() == Qt.LeftButton:
self.m_drag = True
self.m_DragPosition = e.globalPosition().toPoint() - self.pos()
e.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
def mouseReleaseEvent(self, e): # 鼠标释放事件
if e.button() == Qt.LeftButton:
self.m_drag = False
self.setCursor(QCursor(Qt.ArrowCursor))
def mouseMoveEvent(self, e): # 鼠标拖动事件
if Qt.LeftButton and self.m_drag:
self.move(e.globalPosition().toPoint() - self.m_DragPosition)
e.accept()
def add_paramer_to_combobox(self): # 将参数添加到combobox中
self.comboBox_select_clients_name.clear()
self.comboBox_select_tq_account.clear()
self.comboBox_select_strategy.clear()
self.add_clients_list_to_combobox()
self.add_tq_account_list_to_combobox()
self.add_strategy_list_to_combobox()
def add_clients_list_to_combobox(self): # 将用户列表添加到combobox中
clients_list = self.get_clients_list()
for item in clients_list:
self.comboBox_select_clients_name.addItem(item)
def get_clients_list(self): # 获取用户列表
clients_list = []
data = self.parent.ioModule.read_csv_file(path='./data/clients.csv')
if data.empty:
clients_list = []
else:
for account in data['clients_name']:
clients_list.append(str(account))
return clients_list
def add_tq_account_list_to_combobox(self): # 将天勤账号列表添加到combobox中
tq_account_list = self.get_tq_account_list()
for item in tq_account_list:
self.comboBox_select_tq_account.addItem(item)
def get_tq_account_list(self): # 获取天勤账号列表
tq_account_list = []
data = self.parent.ioModule.read_csv_file(path='./data/tq_account.csv')
if data.empty:
pass
else:
for index, item in data.iterrows():
tq_account_list.append(str(item['tq_account']))
return tq_account_list
def add_strategy_list_to_combobox(self): # 将策略列表添加到combobox中
strategy_list = self.get_strategy_list(path='./strategys')
for item in strategy_list:
self.comboBox_select_strategy.addItem(item)
pass
def get_strategy_list(self, path): # 从策略文件中自动搜索并获取策略类名 列表
class_name_list = []
try:
file_list = os.listdir(path)
except:
file_list = []
print('the path is not dir')
if file_list:
for file in file_list:
file = os.path.join(path, file)
if os.path.isdir(file):
self.get_strategy_list(file)
else:
if file.endswith('.py'):
with open(file, encoding='utf-8') as f:
for line in f.readlines():
cls_match = re.match(r'class\s(.*?)[\(:]', line) # 用正则表达式寻找class关键字之后的类名
if cls_match:
cls_name = cls_match.group(1)
#判断cls_name是否是和文件名相同的类
if cls_name == file.split('\\')[-1].split('.')[0]:
class_name_list.append(cls_name)
else:
pass
return class_name_list
def add_contracts_to_comboBox_symbol(self):
if self.comboBox_contract_type.currentText():
exchange = self.comboBox_exchange.currentText().split()[-1]
type = self.comboBox_contract_type.currentText().split()[-1]
pth = './available_contracts/' + exchange + '_' + type + '.csv'
if os.path.exists(pth):
data = self.parent.ioModule.read_csv_file(path=pth)
list = data['0'].tolist()
self.comboBox_symbol.clear()
self.comboBox_symbol.addItems(list)
def get_process_parameters(self):
if self.comboBox_select_clients_name.currentText(): #检查是否有用户名
if self.comboBox_select_tq_account.currentText(): # 检查是否有天勤账户
if self.comboBox_select_strategy.currentText(): # 检查是否有策略模板
if self.comboBox_contract_type.currentText(): # 检查合约类型栏是否有为空
if self.comboBox_symbol.currentText(): # 检查合约栏是否有内容
path_contract = ('./available_contracts/' + self.comboBox_exchange.currentText().split()[-1] +
'_' + self.comboBox_contract_type.currentText().split()[-1] + '.csv')
if os.path.exists(path_contract): # 检查是否有合约表文件
data = self.parent.ioModule.read_csv_file(path=path_contract)
list = data['0'].tolist()
if self.comboBox_symbol.currentText() in list: # 检查输入的合约在不在合约表文件里面
if self.symbol_period.text(): # 检查是否有合约周期
process_df = self.parent.ioModule.read_csv_file(path='./data/config.csv')
if len(process_df) > 0:
name_list = process_df['process_name'].tolist()
else:
name_list = []
my_dict = {}
# 进程名(或策略实例名)= 客户名 + 天勤帐户 + 交易所.合约 + 合约周期, 该名称将作为策略实例的名称,不能重复
process_name = (
self.comboBox_select_clients_name.currentText() + '-'
+ self.comboBox_select_tq_account.currentText() + '-'
+ self.comboBox_select_strategy.currentText() + '-'
+ self.comboBox_symbol.currentText() + '-'
+ self.symbol_period.text() + 'min'
)
if process_name not in name_list: # 判断进程名是否已在策略列表中
my_dict['process_name'] = process_name # 进程名称或策略实例名称
my_dict['whether_self_start'] = self.checkBox_whether_self_start.isChecked() # 是否自启动
my_dict['client_name'] = self.comboBox_select_clients_name.currentText() # 客户名称
my_dict['tq_account'] = self.comboBox_select_tq_account.currentText() # 天勤账号
current_tq_account = self.comboBox_select_tq_account.currentText()
TQ_qccount_df = self.parent.ioModule.read_csv_file(path='./data/tq_account.csv')
index = TQ_qccount_df.index[TQ_qccount_df['tq_account'] == current_tq_account] # 获取当前所选项目对应的pd行index
for idx, row in TQ_qccount_df.iterrows():
if idx == index:
my_dict['tq_psd'] = str(row['tq_psd']) # 天勤密码
my_dict['futures_company'] = row['futures_company'] # 期货公司
my_dict['futures_account'] = row['futures_account'] # 期货账号
my_dict['futures_psd'] = row['futures_psd'] # 期货密码
break
my_dict['symbol'] = self.comboBox_symbol.currentText() # 合约代码
my_dict['symbol_period'] = self.symbol_period.text() # 合约周期
my_dict['strategy'] = self.comboBox_select_strategy.currentText() # 策略名称
my_dict['whether_live_trading'] = self.checkBox_whether_live_futures_trading.isChecked() # 是否实盘交易
my_dict['whether_backtest'] = False # 是否回测
my_dict['whether_open_web_services'] = self.checkBox_whether_open_web_services.isChecked()
my_dict['web_port'] = self.web_port.text() # 网页端口
my_dict['trading_status'] = self.checkBox_trading_status.isChecked() # 交易状态标志位,默认True为正常交易,Flase为停止交易,可在策略中通过开关此位来停止或开启交易
my_dict['orientation'] = self.orientation.text() # 交易方向,用于半自动化策略,0为无方向,1或正整数为做多,-1或负整数为做空
my_dict['initial_capital'] = self.initial_capital.text() # 初始资金
my_dict['final_capital'] = self.initial_capital.text() # 最终资金
my_dict['contract_multiples'] = self.contract_multiples.text() # 合约倍数
my_dict['margin_rate'] = self.margin_rate.text() # 保证金率
my_dict['stop_loss'] = self.stop_loss.text() # 止损位
my_dict['stop_profit'] = self.stop_profit.text() # 止盈位
my_dict['long_add_times'] = 0 # 做多累积次数
my_dict['long_current_position'] = 0 # 当前多单持仓
my_dict['first_long_price'] = 0 # 第一次做多价格
my_dict['first_long_deal'] = 0 # 第一次做多成交量
my_dict['short_add_times'] = 0 # 做空累积次数
my_dict['short_current_position'] = 0 # 当前空单持仓
my_dict['first_short_price'] = 0 # 第一次做空价格
my_dict['first_short_deal'] = 0 # 第一次做空成交量
my_dict['whether_open_line'] = False # 是否定义了开仓直线
my_dict['open_line_Coordinates'] = '0,0' # 开仓线坐标
my_dict['whether_close_line'] = False # 是否定义了平仓直线
my_dict['close_line_Coordinates'] = '0,0' # 平仓线坐标
my_dict['CP1'] = self.Customized_parameters1.text() # 自定义参数1 Customized_parameters 为了方便使用,缩写为CP
my_dict['CP2'] = self.Customized_parameters2.text() # 自定义参数2
my_dict['CP3'] = self.Customized_parameters3.text() # 自定义参数3
my_dict['CP4'] = self.Customized_parameters4.text() # 自定义参数4
my_dict['CP5'] = self.Customized_parameters5.text() # 自定义参数5
my_dict['CP6'] = self.Customized_parameters6.text() # 自定义参数6
my_dict['CP7'] = self.Customized_parameters7.text() # 自定义参数7
my_dict['CP8'] = self.Customized_parameters8.text() # 自定义参数8
my_dict['CP9'] = self.Customized_parameters9.text() # 自定义参数9
print('已新添加策略,策略参数为: \n' + str(my_dict))
df = pd.DataFrame(my_dict, index=[0])
self.parent.ioModule.add_dict_to_csv(df, path='./data/config.csv')
self.label_info.setText('新策略实例添加成功: ' + str(my_dict['process_name']))
self.parent.load_process_config()
else:
self.label_info.setText('该进程名(策略实例名) 已在策略列表中,请勿添加相同的策略实例名')
else:
self.label_info.setText('添加一个策略实例时,合约周期不可缺少')
else:
self.label_info.setText('输入的合约名不在天勤的交易列表里,请仔细检查')
else:
self.label_info.setText('没有可用的合约表,请先运行一次天勤行情服务,下载可用合约表')
else:
self.label_info.setText('请先输入或选择合适的合约名再点添加')
else:
self.label_info.setText('请先选择合约类型')
else:
self.label_info.setText('没有可有用策略模板')
else:
self.label_info.setText('请先添加至少一个可用的天勤帐户')
else:
self.label_info.setText('请先添加至少一个用户')
def process_parameters_input_clear(self): # 清空进程参数输入框
self.add_paramer_to_combobox()
self.checkBox_whether_self_start.setChecked(True) # 默认自启动
self.checkBox_whether_live_futures_trading.setChecked(False) # 默认不实盘交易
self.checkBox_whether_open_web_services.setChecked(False) # 默认不需要web服务
self.comboBox_exchange.setCurrentIndex(0) # 默认交易所
self.comboBox_contract_type.setCurrentIndex(-1)
self.comboBox_symbol.clear()
self.symbol_period.clear()
self.initial_capital.clear()
self.web_port.clear()
self.orientation.clear()
self.contract_multiples.clear()
self.margin_rate.clear()
self.stop_loss.clear()
self.stop_profit.clear()
self.Customized_parameters1.clear()
self.Customized_parameters2.clear()
self.Customized_parameters3.clear()
self.Customized_parameters4.clear()
self.Customized_parameters5.clear()
self.Customized_parameters6.clear()
self.Customized_parameters7.clear()
self.Customized_parameters8.clear()
self.Customized_parameters9.clear()