1. 封装部分vn.ib中的结构体

2. 添加MainWindow的一键恢复窗口布局功能
This commit is contained in:
chenxy123 2016-10-19 23:45:49 +08:00
parent 6ed2a4eed9
commit 5869b45dd4
6 changed files with 203 additions and 20 deletions

View File

@ -90,6 +90,17 @@ public:
use_ = Use(); use_ = Use();
} }
//封装添加
bool operator==(const shared_ptr<X> const & a)
{
return a.get() == this->get();
}
bool operator!=(const shared_ptr<X> const & a)
{
return a.get() != this->get();
}
private: private:
X *ptr_; X *ptr_;

Binary file not shown.

View File

@ -1,5 +1,5 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<SubscriptionDataContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:Microsoft.VisualStudio.WindowsAzure.CommonAzureTools.Authentication.CacheManagement"> <SubscriptionDataContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:Microsoft.VisualStudio.WindowsAzure.CommonAzureTools.Authentication.CacheManagement">
<Items /> <Items />
<TokenCache>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAhuIZgFnYcU6wvp5DI4FozAAAAAACAAAAAAAQZgAAAAEAACAAAAAP+ZejtWt08T4CHAObG1FMrnjuDrDgxp3Kgz3p0GFTiAAAAAAOgAAAAAIAACAAAADnqXDqufrv721M+KR/tLuCYzN6ynSphcMeiL+hw9/zfBAAAAA0xC0ncTgoWKgyHhV01sFjQAAAAHPVrjN2gp4P8U/lDH6S7DXnH0r85ILsW5BB0pf0XLyQTdX5KoNz8//d+jBnTKg15QzO9mS8Bc1TVIuzVWasdjI=</TokenCache> <TokenCache>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAhuIZgFnYcU6wvp5DI4FozAAAAAACAAAAAAAQZgAAAAEAACAAAACfxQZLqGgIlYaHRNvaMP1104wne69yDm3hT5jSkViJrwAAAAAOgAAAAAIAACAAAACEoYIxmu9X+z/pKyvKYVXdWTl6zqCrHe3ExYUwAcJwxxAAAABGePzLyKMDeM4WJVJQQCN+QAAAAEywH3Ecv+0PlTkK8x594oWHzQCnUgiXkPjdiAJHqE3sO/L19xiNs2JqW9giCiOVPNL2VS6Pue9CQLjRouK8HHE=</TokenCache>
</SubscriptionDataContainer> </SubscriptionDataContainer>

View File

@ -4,6 +4,13 @@
#include "StdAfx.h" #include "StdAfx.h"
#include "vnib.h" #include "vnib.h"
#include "Contract.h"
#include "OrderState.h"
#include "Execution.h"
#include "CommissionReport.h"
#include "ScannerSubscription.h"
#include "TagValue.h"
///------------------------------------------------------------------------------------- ///-------------------------------------------------------------------------------------
///C++的回调函数将数据保存到队列中 ///C++的回调函数将数据保存到队列中
@ -416,10 +423,52 @@ struct IbApiWrap : VnIbApi, wrapper < VnIbApi >
}; };
struct TagValue_Wrapper : TagValue
{
TagValue_Wrapper(PyObject* self_) : self(self_) {}
PyObject* self;
};
// 特殊封装相关
namespace boost {
namespace python {
template <class T> struct pointee< ibapi::shared_ptr<T> >
{
typedef T type;
};
}
}
template<class T> inline T * get_pointer(ibapi::shared_ptr<T> const & p)
{
return p.get();
}
/*
template<class T>
bool ibapi::shared_ptr::operator==(ibapi::shared_ptr<T> const & a)
{
return a.get() == this->get();
}
template<class T>
bool ibapi::shared_ptr::operator!=(ibapi::shared_ptr<T> const & a)
{
return a.get() != this->get();
}
*/
BOOST_PYTHON_MODULE(vnib) BOOST_PYTHON_MODULE(vnib)
{ {
PyEval_InitThreads(); //导入时运行保证先创建GIL using namespace boost::python;
//导入时运行保证先创建GIL
PyEval_InitThreads();
//API类封装
class_<IbApiWrap, boost::noncopyable>("IbApi") class_<IbApiWrap, boost::noncopyable>("IbApi")
.def("eConnect", &IbApiWrap::eConnect) .def("eConnect", &IbApiWrap::eConnect)
.def("eDisconnect", &IbApiWrap::eDisconnect) .def("eDisconnect", &IbApiWrap::eDisconnect)
@ -435,5 +484,111 @@ BOOST_PYTHON_MODULE(vnib)
.def("accountSummary", pure_virtual(&IbApiWrap::accountSummary)) .def("accountSummary", pure_virtual(&IbApiWrap::accountSummary))
.def("accountSummaryEnd", pure_virtual(&IbApiWrap::accountSummaryEnd)) .def("accountSummaryEnd", pure_virtual(&IbApiWrap::accountSummaryEnd))
; ;
//结构体封装
class_<OrderState>("OrderState")
.def_readwrite("status", &OrderState::status)
.def_readwrite("initMargin", &OrderState::initMargin)
.def_readwrite("maintMargin", &OrderState::maintMargin)
.def_readwrite("equityWithLoan", &OrderState::equityWithLoan)
.def_readwrite("commission", &OrderState::commission)
.def_readwrite("minCommission", &OrderState::minCommission)
.def_readwrite("maxCommission", &OrderState::maxCommission)
.def_readwrite("commissionCurrency", &OrderState::commissionCurrency)
.def_readwrite("warningText ", &OrderState::warningText)
;
class_<Execution>("Execution")
.def_readwrite("execId", &Execution::execId)
.def_readwrite("time", &Execution::time)
.def_readwrite("acctNumber", &Execution::acctNumber)
.def_readwrite("exchange", &Execution::exchange)
.def_readwrite("side", &Execution::side)
.def_readwrite("shares", &Execution::shares)
.def_readwrite("price", &Execution::price)
.def_readwrite("permId", &Execution::permId)
.def_readwrite("clientId ", &Execution::clientId)
.def_readwrite("orderId ", &Execution::orderId)
.def_readwrite("liquidation ", &Execution::liquidation)
.def_readwrite("cumQty ", &Execution::cumQty)
.def_readwrite("avgPrice ", &Execution::avgPrice)
.def_readwrite("orderRef ", &Execution::orderRef)
.def_readwrite("evRule ", &Execution::evRule)
.def_readwrite("evMultiplier ", &Execution::evMultiplier)
.def_readwrite("modelCode ", &Execution::modelCode)
;
class_<UnderComp>("UnderComp")
.def_readwrite("conId", &UnderComp::conId)
.def_readwrite("delta", &UnderComp::delta)
.def_readwrite("price", &UnderComp::price)
;
class_<CommissionReport>("CommissionReport")
.def_readwrite("execId", &CommissionReport::execId)
.def_readwrite("commission", &CommissionReport::commission)
.def_readwrite("currency", &CommissionReport::currency)
.def_readwrite("realizedPNL", &CommissionReport::realizedPNL)
.def_readwrite("yield", &CommissionReport::yield)
.def_readwrite("yieldRedemptionDate", &CommissionReport::yieldRedemptionDate)
;
class_<ExecutionFilter>("ExecutionFilter")
.def_readwrite("m_clientId", &ExecutionFilter::m_clientId)
.def_readwrite("m_acctCode", &ExecutionFilter::m_acctCode)
.def_readwrite("m_time", &ExecutionFilter::m_time)
.def_readwrite("m_symbol", &ExecutionFilter::m_symbol)
.def_readwrite("m_secType", &ExecutionFilter::m_secType)
.def_readwrite("m_exchange", &ExecutionFilter::m_exchange)
.def_readwrite("m_side", &ExecutionFilter::m_side)
;
class_<ScannerSubscription>("ScannerSubscription")
.def_readwrite("numberOfRows", &ScannerSubscription::numberOfRows)
.def_readwrite("instrument", &ScannerSubscription::instrument)
.def_readwrite("locationCode", &ScannerSubscription::locationCode)
.def_readwrite("scanCode", &ScannerSubscription::scanCode)
.def_readwrite("abovePrice", &ScannerSubscription::abovePrice)
.def_readwrite("belowPrice", &ScannerSubscription::belowPrice)
.def_readwrite("aboveVolume", &ScannerSubscription::aboveVolume)
.def_readwrite("marketCapAbove", &ScannerSubscription::marketCapAbove)
.def_readwrite("marketCapBelow", &ScannerSubscription::marketCapBelow)
.def_readwrite("moodyRatingAbove", &ScannerSubscription::moodyRatingAbove)
.def_readwrite("moodyRatingBelow", &ScannerSubscription::moodyRatingBelow)
.def_readwrite("spRatingAbove", &ScannerSubscription::spRatingAbove)
.def_readwrite("spRatingBelow", &ScannerSubscription::spRatingBelow)
.def_readwrite("maturityDateAbove", &ScannerSubscription::maturityDateAbove)
.def_readwrite("maturityDateBelow", &ScannerSubscription::maturityDateBelow)
.def_readwrite("couponRateAbove", &ScannerSubscription::couponRateAbove)
.def_readwrite("couponRateBelow", &ScannerSubscription::couponRateBelow)
.def_readwrite("excludeConvertible", &ScannerSubscription::excludeConvertible)
.def_readwrite("averageOptionVolumeAbove", &ScannerSubscription::averageOptionVolumeAbove)
.def_readwrite("scannerSettingPairs", &ScannerSubscription::scannerSettingPairs)
.def_readwrite("stockTypeFilter", &ScannerSubscription::stockTypeFilter)
;
class_<TagValue, TagValueSPtr>("TagValue")
//class_<TagValue>("TagValue")
.def_readwrite("tag", &TagValue::tag)
.def_readwrite("value", &TagValue::value)
;
class_<TagValueList, TagValueListSPtr>("TagValueList")
//class_<TagValueList>("TagValueList")
.def(vector_indexing_suite<TagValueList, true>()); //这个true非常重要
//register_ptr_to_python<TagValueSPtr>();
//register_ptr_to_python<TagValueListSPtr>();
class_<ComboLeg>("ComboLeg")
.def_readwrite("conId", &ComboLeg::conId)
.def_readwrite("ratio", &ComboLeg::ratio)
.def_readwrite("action", &ComboLeg::action)
.def_readwrite("exchange", &ComboLeg::exchange)
.def_readwrite("openClose", &ComboLeg::openClose)
.def_readwrite("shortSaleSlot", &ComboLeg::shortSaleSlot)
.def_readwrite("designatedLocation", &ComboLeg::designatedLocation)
.def_readwrite("exemptCode", &ComboLeg::exemptCode)
;
}; };

View File

@ -9,10 +9,17 @@
#include <boost/python/module.hpp> //python封装 #include <boost/python/module.hpp> //python封装
#include <boost/python/def.hpp> //python封装 #include <boost/python/def.hpp> //python封装
#include <boost/python/object.hpp> //python封装 #include <boost/python/object.hpp> //python封装
#include <boost/python/register_ptr_to_python.hpp> //Python封装
#include <boost/python/suite/indexing/vector_indexing_suite.hpp> //Python封装
#include <boost/python.hpp> //python封装 #include <boost/python.hpp> //python封装
#include <boost/thread.hpp> //任务队列的线程功能 #include <boost/thread.hpp> //任务队列的线程功能
#include <boost/bind.hpp> //任务队列的线程功能 #include <boost/bind.hpp> //任务队列的线程功能
#include <boost/shared_ptr.hpp>
//API //API
#include "EWrapper.h" #include "EWrapper.h"
#include "EClientSocket.h" #include "EClientSocket.h"
@ -50,6 +57,14 @@ public:
}; };
///-------------------------------------------------------------------------------------
///强制转化相关
///-------------------------------------------------------------------------------------
boost::python::list tagvaluelist_to_pylist();
TagValueListSPtr pylist_to_tagvaluelist();
///------------------------------------------------------------------------------------- ///-------------------------------------------------------------------------------------
///声明类 ///声明类
///------------------------------------------------------------------------------------- ///-------------------------------------------------------------------------------------
@ -58,7 +73,6 @@ class VnIbApi;
class IbWrapper; class IbWrapper;
///------------------------------------------------------------------------------------- ///-------------------------------------------------------------------------------------
///C++ SPI的回调函数方法实现 ///C++ SPI的回调函数方法实现
///------------------------------------------------------------------------------------- ///-------------------------------------------------------------------------------------

View File

@ -23,7 +23,7 @@ class MainWindow(QtGui.QMainWindow):
self.widgetDict = {} # 用来保存子窗口的字典 self.widgetDict = {} # 用来保存子窗口的字典
self.initUi() self.initUi()
self.loadWindowSettings() self.loadWindowSettings('custom')
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def initUi(self): def initUi(self):
@ -55,6 +55,9 @@ class MainWindow(QtGui.QMainWindow):
# 连接组件之间的信号 # 连接组件之间的信号
widgetPositionM.itemDoubleClicked.connect(widgetTradingW.closePosition) widgetPositionM.itemDoubleClicked.connect(widgetTradingW.closePosition)
# 保存默认设置
self.saveWindowSettings('default')
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def initMenu(self): def initMenu(self):
"""初始化菜单""" """初始化菜单"""
@ -65,9 +68,6 @@ class MainWindow(QtGui.QMainWindow):
connectLtsAction = QtGui.QAction(u'连接LTS', self) connectLtsAction = QtGui.QAction(u'连接LTS', self)
connectLtsAction.triggered.connect(self.connectLts) connectLtsAction.triggered.connect(self.connectLts)
connectXtpAction = QtGui.QAction(u'连接XTP', self)
connectXtpAction.triggered.connect(self.connectXtp)
connectKsotpAction = QtGui.QAction(u'连接金仕达期权', self) connectKsotpAction = QtGui.QAction(u'连接金仕达期权', self)
connectKsotpAction.triggered.connect(self.connectKsotp) connectKsotpAction.triggered.connect(self.connectKsotp)
@ -119,6 +119,9 @@ class MainWindow(QtGui.QMainWindow):
rmAction = QtGui.QAction(u'风险管理', self) rmAction = QtGui.QAction(u'风险管理', self)
rmAction.triggered.connect(self.openRm) rmAction.triggered.connect(self.openRm)
restoreAction = QtGui.QAction(u'还原窗口', self)
restoreAction.triggered.connect(self.restoreWindow)
# 创建菜单 # 创建菜单
menubar = self.menuBar() menubar = self.menuBar()
@ -128,8 +131,6 @@ class MainWindow(QtGui.QMainWindow):
sysMenu.addAction(connectCtpAction) sysMenu.addAction(connectCtpAction)
if 'LTS' in self.mainEngine.gatewayDict: if 'LTS' in self.mainEngine.gatewayDict:
sysMenu.addAction(connectLtsAction) sysMenu.addAction(connectLtsAction)
if 'XTP' in self.mainEngine.gatewayDict:
sysMenu.addAction(connectXtpAction)
if 'FEMAS' in self.mainEngine.gatewayDict: if 'FEMAS' in self.mainEngine.gatewayDict:
sysMenu.addAction(connectFemasAction) sysMenu.addAction(connectFemasAction)
if 'XSPEED' in self.mainEngine.gatewayDict: if 'XSPEED' in self.mainEngine.gatewayDict:
@ -166,6 +167,7 @@ class MainWindow(QtGui.QMainWindow):
# 帮助 # 帮助
helpMenu = menubar.addMenu(u'帮助') helpMenu = menubar.addMenu(u'帮助')
helpMenu.addAction(restoreAction)
helpMenu.addAction(aboutAction) helpMenu.addAction(aboutAction)
helpMenu.addAction(testAction) helpMenu.addAction(testAction)
@ -209,11 +211,6 @@ class MainWindow(QtGui.QMainWindow):
"""连接LTS接口""" """连接LTS接口"""
self.mainEngine.connect('LTS') self.mainEngine.connect('LTS')
#----------------------------------------------------------------------
def connectXtp(self):
"""连接XTP接口"""
self.mainEngine.connect('XTP')
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def connectKsotp(self): def connectKsotp(self):
"""连接金仕达期权接口""" """连接金仕达期权接口"""
@ -320,7 +317,7 @@ class MainWindow(QtGui.QMainWindow):
if reply == QtGui.QMessageBox.Yes: if reply == QtGui.QMessageBox.Yes:
for widget in self.widgetDict.values(): for widget in self.widgetDict.values():
widget.close() widget.close()
self.saveWindowSettings() self.saveWindowSettings('custom')
self.mainEngine.exit() self.mainEngine.exit()
event.accept() event.accept()
@ -339,16 +336,16 @@ class MainWindow(QtGui.QMainWindow):
return widget, dock return widget, dock
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def saveWindowSettings(self): def saveWindowSettings(self, settingName):
"""保存窗口设置""" """保存窗口设置"""
settings = QtCore.QSettings('vn.py', 'vn.trader') settings = QtCore.QSettings('vn.trader', settingName)
settings.setValue('state', self.saveState()) settings.setValue('state', self.saveState())
settings.setValue('geometry', self.saveGeometry()) settings.setValue('geometry', self.saveGeometry())
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def loadWindowSettings(self): def loadWindowSettings(self, settingName):
"""载入窗口设置""" """载入窗口设置"""
settings = QtCore.QSettings('vn.py', 'vn.trader') settings = QtCore.QSettings('vn.trader', settingName)
# 这里由于PyQt4的版本不同settings.value('state')调用返回的结果可能是: # 这里由于PyQt4的版本不同settings.value('state')调用返回的结果可能是:
# 1. None初次调用注册表里无相应记录因此为空 # 1. None初次调用注册表里无相应记录因此为空
# 2. QByteArray比较新的PyQt4 # 2. QByteArray比较新的PyQt4
@ -361,6 +358,12 @@ class MainWindow(QtGui.QMainWindow):
except AttributeError: except AttributeError:
pass pass
#----------------------------------------------------------------------
def restoreWindow(self):
"""还原默认窗口设置(还原停靠组件位置)"""
self.loadWindowSettings('default')
self.showMaximized()
######################################################################## ########################################################################
class AboutWidget(QtGui.QDialog): class AboutWidget(QtGui.QDialog):