增加vn.ib的部分测试代码

This commit is contained in:
chenxy123 2016-10-29 00:16:47 +08:00
parent 2e92f5c891
commit 45ce784cfa
4 changed files with 315 additions and 82 deletions

View File

@ -1,5 +1,7 @@
# encoding: UTF-8 # encoding: UTF-8
import sys
from time import sleep from time import sleep
from vnib import IbApi from vnib import IbApi
@ -7,7 +9,7 @@ from vnib import IbApi
######################################################################## ########################################################################
class TestApi(IbApi): class TestApi(IbApi):
"""""" print sys._getframe().f_code.co_name
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def __init__(self): def __init__(self):
@ -16,33 +18,279 @@ class TestApi(IbApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def nextValidId(self, orderId): def nextValidId(self, orderId):
"""""" print sys._getframe().f_code.co_name
print 'next valid id:', orderId print locals()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def currentTime(self, time): def currentTime(self, time):
"""""" print sys._getframe().f_code.co_name
print 'current time is:', time print locals()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def connectAck(self): def connectAck(self):
"""""" print sys._getframe().f_code.co_name
print 'connection established' print locals()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def error(self, i, errorCode, errorString): def error(self, id_, errorCode, errorString):
"""""" print sys._getframe().f_code.co_name
print 'error', i, errorCode, errorString print locals()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def accountSummary(self, reqId, account, tag, value, curency): def accountSummary(self, reqId, account, tag, value, curency):
"""""" print sys._getframe().f_code.co_name
print 'account summary', reqId, account, tag, value, curency print locals()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def accountSummaryEnd(self, reqId): def accountSummaryEnd(self, reqId):
"""""" print sys._getframe().f_code.co_name
print 'account summary end', reqId print locals()
#----------------------------------------------------------------------
def tickPrice(self, tickerId, field, price, canAutoExecute):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def tickSize(self, tickerId, field, size):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def tickOptionComputation(self, tickerId, tickType, impliedVol, delta, optPrice, pvDividend, gamma, vega, theta, undPrice):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def tickGeneric(self, tickerId, tickType, value):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def tickString(self, tickerId, tickType, value):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def tickEFP(self, tickerId, tickType, basisPoints, formattedBasisPoints, totalDividends, holdDays, futureLastTradeDate, dividendImpact, dividendsToLastTradeDate):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def openOrder(self, orderId, contract, order, orderState):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def openOrderEnd(self):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def winError(self, str_, lastError):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def connectionClosed(self):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def updateAccountValue(self, key, val, currency, accountName):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def updatePortfolio(self, contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accountName):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def updateAccountTime(self, timeStamp):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def accountDownloadEnd(self, accountName):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def contractDetails(self, reqId, contractDetails):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def bondContractDetails(self, reqId, contractDetails):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def contractDetailsEnd(self, reqId):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def execDetails(self, reqId, contract, execution):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def execDetailsEnd(self, reqId):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def updateMktDepth(self, id_, position, operation, side, price, size):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def updateMktDepthL2(self, id_, position, marketMaker, operation, side, price, size):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def updateNewsBulletin(self, msgId, msgType, newsMessage, originExch):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def managedAccounts(self, accountsList):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def receiveFA(self, pFaDataType, cxml):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def historicalData(self, reqId, date, open_, high, low, close, volume, barCount, WAP, hasGaps):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def scannerParameters(self, xml):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def scannerData(self, reqId, rank, contractDetails, distance, benchmark, projection, legsStr):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def scannerDataEnd(self, reqId):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def realtimeBar(self, reqId, time, open_, high, low, close, volume, wap, count):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def fundamentalData(self, reqId, data):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def deltaNeutralValidation(self, reqId, underComp):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def tickSnapshotEnd(self, reqId):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def marketDataType(self, reqId, marketDataType):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def commissionReport(self, commissionReport):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def position(self, account, contract, position, avgCost):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def positionEnd(self):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def verifyMessageAPI(self, apiData):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def verifyCompleted(self, isSuccessful, errorText):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def displayGroupList(self, reqId, groups):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def displayGroupUpdated(self, reqId, contractInfo):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def verifyAndAuthMessageAPI(self, apiData, xyzChallange):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def verifyAndAuthCompleted(self, isSuccessful, errorText):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def positionMulti(self, reqId, account, modelCode, contract, pos, avgCost):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def positionMultiEnd(self, reqId):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def accountUpdateMulti(self, reqId, account, modelCode, key, value, currency):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def accountUpdateMultiEnd(self, reqId):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def securityDefinitionOptionalParameter(self, reqId, exchange, underlyingConId, tradingClass, multiplier, expirations, strikes):
print sys._getframe().f_code.co_name
print locals()
#----------------------------------------------------------------------
def securityDefinitionOptionalParameterEnd(self, reqId):
print sys._getframe().f_code.co_name
print locals()
if __name__ == '__main__': if __name__ == '__main__':

Binary file not shown.

View File

@ -338,7 +338,12 @@ void IbWrapper::accountUpdateMultiEnd(int reqId)
void IbWrapper::securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& tradingClass, const std::string& multiplier, std::set<std::string> expirations, std::set<double> strikes) void IbWrapper::securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& tradingClass, const std::string& multiplier, std::set<std::string> expirations, std::set<double> strikes)
{ {
PyLock lock; PyLock lock;
this->api->securityDefinitionOptionalParameter(reqId, exchange, underlyingConId, tradingClass, multiplier, expirations, strikes);
//这里因为boost.python中没有提供std::set的封装因此选择转化成vector
std::vector<std::string> expirationsVector(expirations.begin(), expirations.end());
std::vector<double> strikesVector(strikes.begin(), strikes.end());
this->api->securityDefinitionOptionalParameter(reqId, exchange, underlyingConId, tradingClass, multiplier, expirationsVector, strikesVector);
}; };
void IbWrapper::securityDefinitionOptionalParameterEnd(int reqId) void IbWrapper::securityDefinitionOptionalParameterEnd(int reqId)
@ -390,13 +395,6 @@ void VnIbApi::eDisconnect()
this->client->eDisconnect(); this->client->eDisconnect();
}; };
int VnIbApi::serverVersion()
{
//int i = this->client->serverVersion();
//return i;
return 0;
};
std::string VnIbApi::TwsConnectionTime() std::string VnIbApi::TwsConnectionTime()
{ {
return this->client->TwsConnectionTime(); return this->client->TwsConnectionTime();
@ -1311,7 +1309,8 @@ struct IbApiWrap : VnIbApi, wrapper < VnIbApi >
} }
}; };
virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& tradingClass, const std::string& multiplier, std::set<std::string> expirations, std::set<double> strikes) //virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& tradingClass, const std::string& multiplier, std::set<std::string> expirations, std::set<double> strikes)
virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& tradingClass, const std::string& multiplier, std::vector<std::string> expirations, std::vector<double> strikes)
{ {
try try
{ {
@ -1338,16 +1337,7 @@ struct IbApiWrap : VnIbApi, wrapper < VnIbApi >
}; };
// 将ibapi自带的智能指针注册到boost.python的环境中让boost.python实现自动识别
struct TagValue_Wrapper : TagValue
{
TagValue_Wrapper(PyObject* self_) : self(self_) {}
PyObject* self;
};
// 特殊封装相关
namespace boost { namespace boost {
namespace python { namespace python {
template <class T> struct pointee< ibapi::shared_ptr<T> > template <class T> struct pointee< ibapi::shared_ptr<T> >
@ -1362,20 +1352,8 @@ template<class T> inline T * get_pointer(ibapi::shared_ptr<T> const & p)
return p.get(); 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)
{ {
using namespace boost::python; using namespace boost::python;
@ -1387,7 +1365,6 @@ BOOST_PYTHON_MODULE(vnib)
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)
.def("serverVersion", &IbApiWrap::serverVersion)
.def("TwsConnectionTime", &IbApiWrap::TwsConnectionTime) .def("TwsConnectionTime", &IbApiWrap::TwsConnectionTime)
.def("reqMktData", &IbApiWrap::reqMktData) .def("reqMktData", &IbApiWrap::reqMktData)
.def("cancelMktData", &IbApiWrap::cancelMktData) .def("cancelMktData", &IbApiWrap::cancelMktData)
@ -1500,7 +1477,7 @@ BOOST_PYTHON_MODULE(vnib)
.def("securityDefinitionOptionalParameterEnd", pure_virtual(&IbApiWrap::securityDefinitionOptionalParameterEnd)) .def("securityDefinitionOptionalParameterEnd", pure_virtual(&IbApiWrap::securityDefinitionOptionalParameterEnd))
; ;
//结构体封装 //结构体相关的封装
class_<OrderState>("OrderState") class_<OrderState>("OrderState")
.def_readwrite("status", &OrderState::status) .def_readwrite("status", &OrderState::status)
.def_readwrite("initMargin", &OrderState::initMargin) .def_readwrite("initMargin", &OrderState::initMargin)
@ -1583,15 +1560,10 @@ BOOST_PYTHON_MODULE(vnib)
; ;
class_<TagValue, TagValueSPtr>("TagValue") class_<TagValue, TagValueSPtr>("TagValue")
//class_<TagValue>("TagValue")
.def_readwrite("tag", &TagValue::tag) .def_readwrite("tag", &TagValue::tag)
.def_readwrite("value", &TagValue::value) .def_readwrite("value", &TagValue::value)
; ;
class_<TagValueList, TagValueListSPtr>("TagValueList")
//class_<TagValueList>("TagValueList")
.def(vector_indexing_suite<TagValueList, true>()); //这个true非常重要
class_<ComboLeg, ComboLegSPtr>("ComboLeg") class_<ComboLeg, ComboLegSPtr>("ComboLeg")
.def_readwrite("conId", &ComboLeg::conId) .def_readwrite("conId", &ComboLeg::conId)
.def_readwrite("ratio", &ComboLeg::ratio) .def_readwrite("ratio", &ComboLeg::ratio)
@ -1603,9 +1575,6 @@ BOOST_PYTHON_MODULE(vnib)
.def_readwrite("exemptCode", &ComboLeg::exemptCode) .def_readwrite("exemptCode", &ComboLeg::exemptCode)
; ;
class_<Contract::ComboLegList, Contract::ComboLegListSPtr>("ComboLegList")
.def(vector_indexing_suite<Contract::ComboLegList, true>());
class_<Contract>("Contract") class_<Contract>("Contract")
.def_readwrite("conId", &Contract::conId) .def_readwrite("conId", &Contract::conId)
.def_readwrite("symbol", &Contract::symbol) .def_readwrite("symbol", &Contract::symbol)
@ -1662,19 +1631,8 @@ BOOST_PYTHON_MODULE(vnib)
.def_readwrite("nextOptionPartial", &ContractDetails::nextOptionPartial) .def_readwrite("nextOptionPartial", &ContractDetails::nextOptionPartial)
.def_readwrite("notes", &ContractDetails::notes) .def_readwrite("notes", &ContractDetails::notes)
; ;
enum_<OrderCondition::OrderConditionType>("OrderConditionType") class_<OrderCondition>("OrderCondition") //实现不完全,暂时不清楚作用
.value("Price", OrderCondition::Price)
.value("Time", OrderCondition::Time)
.value("Margin", OrderCondition::Margin)
.value("Execution", OrderCondition::Execution)
.value("Volume", OrderCondition::Volume)
.value("PercentChange", OrderCondition::PercentChange)
;
//实现不完全,暂时不清楚作用
class_<OrderCondition>("OrderCondition")
.def("toString", &OrderCondition::toString) .def("toString", &OrderCondition::toString)
.def("type", &OrderCondition::type) .def("type", &OrderCondition::type)
; ;
@ -1683,12 +1641,6 @@ BOOST_PYTHON_MODULE(vnib)
.def_readwrite("price", &OrderComboLeg::price) .def_readwrite("price", &OrderComboLeg::price)
; ;
class_<Order::OrderComboLegList, Order::OrderComboLegListSPtr>("OrderComboLegList")
.def(vector_indexing_suite<Order::OrderComboLegList, true>());
class_<std::vector<ibapi::shared_ptr<OrderCondition>>>("OrderConditionList")
.def(vector_indexing_suite<std::vector<ibapi::shared_ptr<OrderCondition>>, true>());
class_<Order>("Order") class_<Order>("Order")
.def_readwrite("orderId", &Order::orderId) .def_readwrite("orderId", &Order::orderId)
.def_readwrite("clientId", &Order::clientId) .def_readwrite("clientId", &Order::clientId)
@ -1804,10 +1756,37 @@ BOOST_PYTHON_MODULE(vnib)
.def_readwrite("extOperator", &Order::extOperator) .def_readwrite("extOperator", &Order::extOperator)
; ;
//register_ptr_to_python<TagValueSPtr>(); //vector相关的封装
//register_ptr_to_python<TagValueListSPtr>(); class_<TagValueList, TagValueListSPtr>("TagValueList")
//register_ptr_to_python<OrderComboLegSPtr>(); .def(vector_indexing_suite<TagValueList, true>()); //这个true非常重要
class_<Contract::ComboLegList, Contract::ComboLegListSPtr>("ComboLegList")
.def(vector_indexing_suite<Contract::ComboLegList, true>());
class_<Order::OrderComboLegList, Order::OrderComboLegListSPtr>("OrderComboLegList")
.def(vector_indexing_suite<Order::OrderComboLegList, true>());
class_<std::vector<ibapi::shared_ptr<OrderCondition>>>("OrderConditionList")
.def(vector_indexing_suite<std::vector<ibapi::shared_ptr<OrderCondition>>, true>());
class_<std::vector<ibapi::shared_ptr<OrderCondition>>>("OrderConditionList")
.def(vector_indexing_suite<std::vector<ibapi::shared_ptr<OrderCondition>>, true>());
class_<std::vector<std::string>>("StringList")
.def(vector_indexing_suite<std::vector<std::string>, true>());
class_<std::vector<double>>("DoubleList")
.def(vector_indexing_suite<std::vector<double>, true>());
//enum相关的封装
enum_<OrderCondition::OrderConditionType>("OrderConditionType")
.value("Price", OrderCondition::Price)
.value("Time", OrderCondition::Time)
.value("Margin", OrderCondition::Margin)
.value("Execution", OrderCondition::Execution)
.value("Volume", OrderCondition::Volume)
.value("PercentChange", OrderCondition::PercentChange)
;
enum_<TickType>("TickType") enum_<TickType>("TickType")
.value("BID_SIZE", BID_SIZE) .value("BID_SIZE", BID_SIZE)
@ -1886,5 +1865,10 @@ BOOST_PYTHON_MODULE(vnib)
.value("DELAYED_OPEN", DELAYED_OPEN) .value("DELAYED_OPEN", DELAYED_OPEN)
.value("NOT_SET", NOT_SET) .value("NOT_SET", NOT_SET)
; ;
//注册智能指针到boost.python环境中暂时无用
//register_ptr_to_python<TagValueSPtr>();
//register_ptr_to_python<TagValueListSPtr>();
//register_ptr_to_python<OrderComboLegSPtr>();
}; };

View File

@ -383,8 +383,11 @@ public:
virtual void accountUpdateMultiEnd(int reqId){}; virtual void accountUpdateMultiEnd(int reqId){};
//virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string&
// tradingClass, const std::string& multiplier, std::set<std::string> expirations, std::set<double> strikes){};
virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string&
tradingClass, const std::string& multiplier, std::set<std::string> expirations, std::set<double> strikes){}; tradingClass, const std::string& multiplier, std::vector<std::string> expirations, std::vector<double> strikes){};
virtual void securityDefinitionOptionalParameterEnd(int reqId){}; virtual void securityDefinitionOptionalParameterEnd(int reqId){};
@ -396,8 +399,6 @@ public:
void eDisconnect(); void eDisconnect();
int serverVersion();
std::string TwsConnectionTime(); std::string TwsConnectionTime();
void reqMktData(TickerId id, const Contract& contract, void reqMktData(TickerId id, const Contract& contract,