yuwangying / pyctp Goto Github PK
View Code? Open in Web Editor NEWCTP with python3.4.3
CTP with python3.4.3
使用不同的交易员登录,例如1601,1602,1603交易员登录,登录之后,可以看到其他交易员名下的账户。
期望:每个交易员只能看到自己名下的期货账户。
tableView中checkBox在修改的时候状态闪变
期货账户无策略,鼠标右击删除,后台报错。
log:
Traceback (most recent call last):
File "C:\Users\yuanpengfei\Desktop\PyCTP-PyCTP_Dev\PyCTP_Client\PyCTP_ClientCore\QAccountWidget.py", line 711, in slot_action_del_strategy
print(">>> QAccountWidget.slot_action_del_strategy() i_strategy.get_user_id()=", i_strategy.get_user_id(), "i_strategy.get_strategy_id()=", i_strategy.get_strategy_id(), "self.__clicked_status['user_id']=", self.__clicked_status['user_id'], "self.__clicked_status['strategy_id']=", self.__clicked_status['strategy_id'])
AttributeError: 'QAccountWidget' object has no attribute '_QAccountWidget__clicked_status'
双击任务栏图标应显示出窗口
小蜜蜂账户策略总开关与TS端策略状态不一致。
测试步骤:
1:打开小蜜蜂客户端,设置某个期货账户策略参数,开启总开关,期货账户策略总开关,然后TS端开始发单,快期可以看到有报单;
2:小蜜蜂不做任何操作直接退出(账户策略总开关已经是打开状态),然后退出TS端,快期终端手动平仓,清空持仓;
3:重启TS端,发现启动后上次账户立马开启发单;
4:打开小蜜蜂客户端,发现账户下策略总开关显示还是未开启状态,其实已经打开。
盘中交易,快期已经有报单,但是小蜜蜂不显示新开的报单。
退出小蜜蜂重启小蜜蜂之后又显示了。
待增加登录失败弹窗提醒
总账户和单账户下的开始策略文字使人困惑,容易引发误操作。
建议总账户下改名:交易员开关
期货账户改名:账户开关
如题,User.py代码中get_commission方法:
def get_commission(self, instrument_id, exchange_id):
# 获取品种代码,例如cu、zn
if exchange_id in ['SHFE', 'CFFEX', 'DZCE']:
commodity_id = instrument_id[:2]
elif exchange_id in ['DCE']:
commodity_id = instrument_id[:1]
if commodity_id not in self.__dict_commission:
...
假如前两种判断都不成立,第三种逻辑判断,commodity_id属于直接使用变量,那么启动过程将会报错。
UnboundLocalError: local variable 'commodity_id' referenced before assignment
我修改如下,将变量变为self变量,然后bug不存在,系统正常启动:
def get_commission(self, instrument_id, exchange_id):
# 获取品种代码,例如cu、zn
if exchange_id in ['SHFE', 'CFFEX', 'DZCE']:
self.commodity_id = instrument_id[:2]
elif exchange_id in ['DCE']:
self.commodity_id = instrument_id[:1]
if self.commodity_id not in self.__dict_commission:
暂时不支持非上期所品种持仓
server socket crash, 客户端应当提示用户网络连接故障。
当客户端遇到如下socket异常:
SocketManager.RecvN() [WinError 10053] 你的主机中的软件中止了一个已建立的连接。
客户端理应弹窗提示。
User子进程,删除策略时退订行情
b'063802' 连接交易前置 0
b'063802' 登陆交易账号 0
b'063802' 交易日 20161228
b'063802' 投资者代码 063802
Exception in thread Thread-1:
Traceback (most recent call last):
File "D:\Python34\lib\threading.py", line 920, in _bootstrap_inner
self.run()
File "D:\Python34\lib\threading.py", line 868, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\yuanpengfei\Desktop\PyCTP-PyCTP_Dev\PyCTP_Client\PyCTP_ClientCore\CTPManager.py", line 75, in start_init
self.create_user(i)
File "C:\Users\yuanpengfei\Desktop\PyCTP-PyCTP_Dev\PyCTP_Client\PyCTP_ClientCore\CTPManager.py", line 115, in create_user
obj_user = User(dict_arguments, ctp_manager=self)
File "C:\Users\yuanpengfei\Desktop\PyCTP-PyCTP_Dev\PyCTP_Client\PyCTP_ClientCore\User.py", line 84, in init
self.QryTrade() # 获取user的Trade记录
File "C:\Users\yuanpengfei\Desktop\PyCTP-PyCTP_Dev\PyCTP_Client\PyCTP_ClientCore\User.py", line 284, in QryTrade
print(">>> User.QryTrade() self.__listQryTrade=", self.__listQryTrade, type(self.__listQryTrade), len(self.__listQryTrade))
TypeError: object of type 'int' has no len()
Strategy.py文件
#初始化持仓变量
def init_position(self):
...
字段与协议不匹配导致出错,导致KeyError
SocketManager.RecvN() [WinError 10054] 远程主机强迫关闭了一个现有的连接。 30734 0
QPixmap: It is not safe to use pixmaps outside the GUI thread
xml配置文件适合已发布的程序进行ip地址更改。
好处:
期货账户tab的资金条显示期货账户的盈亏统计,包含但不限于套利系统的委托单
测试环境:TS端从来没操作过包含cu1707,cu1709两个合约相关的策略
现象:启动TS端,小蜜蜂新建策略,合约id为:cu1707, cu1709,关闭小蜜蜂,重新启动,发现报错。
报错日志:
Strategy.statistics_for_trade() user_id = 091921 strategy_id = 01 既不属于A也不属于B合约的trade
Strategy.OnRtnTrade() user_id = 091921 strategy_id = 01 时间过滤,跳过持仓量统计
Exception in thread Thread-4:
Traceback (most recent call last):
File "D:\Python34\lib\threading.py", line 920, in _bootstrap_inner
self.run()
File "D:\Python34\lib\threading.py", line 868, in run
self._target(*self._args, **self._kwargs)
File "D:\CTP\PyCTP\PyCTP_Client\PyCTP_ClientCore\User.py", line 908, in timer_queue_put
'DataMain': self.get_table_widget_data() # 进程通信:发送策略信息
File "D:\CTP\PyCTP\PyCTP_Client\PyCTP_ClientCore\User.py", line 772, in get_table_widget_data
strategy_statistics = self.__dict_strategy[strategy_id].get_statistics()
File "D:\CTP\PyCTP\PyCTP_Client\PyCTP_ClientCore\Strategy.py", line 1916, in get_statistics
'profit_position': self.count_profit_position(), # 持仓盈亏
File "D:\CTP\PyCTP\PyCTP_Client\PyCTP_ClientCore\Strategy.py", line 1890, in count_profit_position
self.__profit_position += trade['ProfitPosition']
KeyError: 'ProfitPosition'
Strategy.OnRtnTrade() user_id = 091921 strategy_id = 01 时间过滤,跳过持仓量统计
Strategy.statistics_for_trade() user_id = 091921 strategy_id = 01 既不属于A也不属于B合约的trade
log日志:
bee_start_error.txt
点击“设置持仓”按钮到收到回报之间不刷新groupBox的持仓指标
在主进程里通过multiprocessing.Process创建两个账户进程,每个进程进行轮询即可,轮询可以采用Queue方式进行读取,有消息就处理
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 12 00:31:14 2016
"""
import sys
import time
import os
import threading
import chardet
import pandas as pd
from pandas import Series, DataFrame
import FunctionLog
from Trade import PyCTP_Trader
from Market import PyCTP_Market
import Utils
import uuid
import multiprocessing
#多进程函数
def my_function(x):
# time.sleep(1)
# print('My Unique Id: {0}'.format(uuid.uuid1()))
print(">>> process name = ", multiprocessing.current_process())
print("x = ", x)
if x == 0:
BrokerID = b'9999'
UserID = b'058176' # 063802
Password = b'669822' # 123456
ExchangeID = b'SHFE'
listInstrumentID = [b'cu1706', b'cu1709']
InstrumentID = b'cu1701'
trader = PyCTP_Trader.CreateFtdcTraderApi(b'tmp/_tmp_t_') # Trade实例
market = PyCTP_Market.CreateFtdcMdApi(b'tmp/_tmp_m_') # Market实例
print('连接交易前置', Utils.code_transform(trader.Connect(b'tcp://180.168.146.187:10000')))
print('连接行情前置', Utils.code_transform(market.Connect(b'tcp://180.168.146.187:10010')))
print('交易账号登陆', Utils.code_transform(trader.Login(BrokerID, UserID, Password)))
print('交易账号登陆', Utils.code_transform(market.Login(BrokerID, UserID, Password)))
print('交易日', Utils.code_transform(trader.GetTradingDay()))
print('设置投资者代码', Utils.code_transform(trader.setInvestorID(UserID)))
time.sleep(1)
print('查询账户信息\n', Utils.code_transform(trader.QryInvestor()))
while True:
# print('查询账户信息')
print("In process ", multiprocessing.current_process(), '查询账户信息', Utils.code_transform(trader.QryInvestor()))
print("\n")
time.sleep(2)
elif x == 1:
BrokerID = b'9999'
UserID = b'063802' # 063802
Password = b'123456' # 123456
ExchangeID = b'SHFE'
listInstrumentID = [b'cu1706', b'cu1709']
InstrumentID = b'cu1701'
trader = PyCTP_Trader.CreateFtdcTraderApi(b'tmp/_tmp_t_') # Trade实例
market = PyCTP_Market.CreateFtdcMdApi(b'tmp/_tmp_m_') # Market实例
print('连接交易前置', Utils.code_transform(trader.Connect(b'tcp://180.168.146.187:10000')))
print('连接行情前置', Utils.code_transform(market.Connect(b'tcp://180.168.146.187:10010')))
print('交易账号登陆', Utils.code_transform(trader.Login(BrokerID, UserID, Password)))
print('交易账号登陆', Utils.code_transform(market.Login(BrokerID, UserID, Password)))
print('交易日', Utils.code_transform(trader.GetTradingDay()))
print('设置投资者代码', Utils.code_transform(trader.setInvestorID(UserID)))
time.sleep(1)
while True:
# print('查询账户信息')
print("In process ", multiprocessing.current_process(), '查询账户信息', Utils.code_transform(trader.QryInvestor()))
print("\n")
time.sleep(2)
def __main__():
print("\n\n>>> In process ", multiprocessing.current_process())
for x in range(2):
process = multiprocessing.Process(target=my_function, args=(x,))
process.daemon = True
process.start()
# process.join()
BrokerID = b'9999'
UserID = b'063802' # 063802
Password = b'123456' # 123456
ExchangeID = b'SHFE'
listInstrumentID = [b'cu1706', b'cu1709']
InstrumentID = b'cu1701'
trader = PyCTP_Trader.CreateFtdcTraderApi(b'tmp/_tmp_t_') # Trade实例
market = PyCTP_Market.CreateFtdcMdApi(b'tmp/_tmp_m_') # Market实例
print('连接交易前置', Utils.code_transform(trader.Connect(b'tcp://180.168.146.187:10000')))
print('连接行情前置', Utils.code_transform(market.Connect(b'tcp://180.168.146.187:10011')))
print('交易账号登陆', Utils.code_transform(trader.Login(BrokerID, UserID, Password)))
print('交易账号登陆', Utils.code_transform(market.Login(BrokerID, UserID, Password)))
print('交易日', Utils.code_transform(trader.GetTradingDay()))
print('设置投资者代码', Utils.code_transform(trader.setInvestorID(UserID)))
# 调试OrderInsert
while True:
print(">>> In process ", multiprocessing.current_process())
time.sleep(2)
time.sleep(2.0)
print('退订行情:', market.UnSubMarketData(listInstrumentID))
print('交易账号登出', trader.Logout())
print('行情账号登出', market.Logout())
if __name__ == '__main__':
__main__()
价差止损设置为1,价差偏离2个跳,程序没有止损,快期没有报单
界面A撤单B撤单布局要设计的更宽一点,内容显示不全
平仓盈亏-持仓盈亏!=净盈亏
重启客户端的数据与重启之前的数据一样错误。
测试步骤:
1:添加策略id:"06",
2:删除策略id“06”,
3:添加策略id:"06"
结果:
界面无响应,Python后台提示重复创建id
期望结果:
可以添加进去,如果遇到重复添加,应该消息框提示.
鼠标右击添加新策略,发现报错。
log:
Traceback (most recent call last):
File "C:\Users\yuanpengfei\Desktop\PyCTP-PyCTP_Dev\PyCTP_Client\PyCTP_ClientCore\QNewStrategy.py", line 123, in on_pushButton_ok_clicked
'order_algorithm': self.__socket_manager.get_list_algorithm_info()[0]['name'], # 下单算法
IndexError: list index out of range
所有账户tab的资金条显示所有期货账户的盈亏统计求和
socket断开连接应主动提醒,且发送socket指令时被客户端弹窗拒绝,否则导致sokcet错误程序崩溃。
中金所品种跨期套利的策略有持仓,将策略设置持仓为非零,TS正常接收和处理了修改持仓指令,TS发给小蜜蜂的设置持仓回报消息的值也是正确的,小蜜蜂界面和内核将设置持仓的各个持仓值全部为0.
tableView中右击鼠标,选择“添加策略”
非上期所的平今平昨标志注意:
通过小蜜蜂套利系统发单,平今和平仓标志分别为3、4,如果是通过快期平仓或平今,标志都是1.
小蜜蜂需要处理平仓标志为1、3、4的所有情况。
针对协议msgtype = 15,msgtype = 17两个,注意TradeServer发来的字段中字符类型在Python代码中的处理
'''
In [6]: ord('a')
Out[6]: 97
In [7]: chr(49)
Out[7]: '1'
'''
通过快期平仓的平仓标志位可能为1,转换为3或4,目的为兼容小蜜蜂套利系统
程序窗口全部关闭后,进程并未关掉,程序里依然有线程运行。
夜盘开盘一段时间后,小蜜蜂仓位和TS端不一致。
此时点击删除策略,无法从列表里移除。
重启小蜜蜂,该策略从列表里删除。
为了提升性能,小蜜蜂可以考虑去掉外接mongo数据库功能,用xml,excel文件进行数据持久化工作。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.