diff --git a/vn.ib/test/test.py b/vn.ib/test/test.py index 337aa8bd..883a7c02 100644 --- a/vn.ib/test/test.py +++ b/vn.ib/test/test.py @@ -1,5 +1,7 @@ # encoding: UTF-8 +import sys + from time import sleep from vnib import IbApi @@ -7,7 +9,7 @@ from vnib import IbApi ######################################################################## class TestApi(IbApi): - """""" + print sys._getframe().f_code.co_name #---------------------------------------------------------------------- def __init__(self): @@ -16,33 +18,279 @@ class TestApi(IbApi): #---------------------------------------------------------------------- def nextValidId(self, orderId): - """""" - print 'next valid id:', orderId + print sys._getframe().f_code.co_name + print locals() #---------------------------------------------------------------------- def currentTime(self, time): - """""" - print 'current time is:', time + print sys._getframe().f_code.co_name + print locals() #---------------------------------------------------------------------- def connectAck(self): - """""" - print 'connection established' + print sys._getframe().f_code.co_name + print locals() #---------------------------------------------------------------------- - def error(self, i, errorCode, errorString): - """""" - print 'error', i, errorCode, errorString - + def error(self, id_, errorCode, errorString): + print sys._getframe().f_code.co_name + print locals() + #---------------------------------------------------------------------- def accountSummary(self, reqId, account, tag, value, curency): - """""" - print 'account summary', reqId, account, tag, value, curency + print sys._getframe().f_code.co_name + print locals() #---------------------------------------------------------------------- def accountSummaryEnd(self, reqId): - """""" - print 'account summary end', reqId + print sys._getframe().f_code.co_name + 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__': diff --git a/vn.ib/test/vnib.pyd b/vn.ib/test/vnib.pyd index 6e101943..6c4050ed 100644 Binary files a/vn.ib/test/vnib.pyd and b/vn.ib/test/vnib.pyd differ diff --git a/vn.ib/vnib/vnib/vnib.cpp b/vn.ib/vnib/vnib/vnib.cpp index 22931a7a..f8052561 100644 --- a/vn.ib/vnib/vnib/vnib.cpp +++ b/vn.ib/vnib/vnib/vnib.cpp @@ -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 expirations, std::set strikes) { PyLock lock; - this->api->securityDefinitionOptionalParameter(reqId, exchange, underlyingConId, tradingClass, multiplier, expirations, strikes); + + //这里因为boost.python中没有提供std::set的封装,因此选择转化成vector + std::vector expirationsVector(expirations.begin(), expirations.end()); + std::vector strikesVector(strikes.begin(), strikes.end()); + + this->api->securityDefinitionOptionalParameter(reqId, exchange, underlyingConId, tradingClass, multiplier, expirationsVector, strikesVector); }; void IbWrapper::securityDefinitionOptionalParameterEnd(int reqId) @@ -390,13 +395,6 @@ void VnIbApi::eDisconnect() this->client->eDisconnect(); }; -int VnIbApi::serverVersion() -{ - //int i = this->client->serverVersion(); - //return i; - return 0; -}; - std::string VnIbApi::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 expirations, std::set strikes) + //virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& tradingClass, const std::string& multiplier, std::set expirations, std::set strikes) + virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& tradingClass, const std::string& multiplier, std::vector expirations, std::vector strikes) { try { @@ -1338,16 +1337,7 @@ struct IbApiWrap : VnIbApi, wrapper < VnIbApi > }; - - -struct TagValue_Wrapper : TagValue -{ - TagValue_Wrapper(PyObject* self_) : self(self_) {} - PyObject* self; -}; - - -// 特殊封装相关 +// 将ibapi自带的智能指针注册到boost.python的环境中,让boost.python实现自动识别 namespace boost { namespace python { template struct pointee< ibapi::shared_ptr > @@ -1362,20 +1352,8 @@ template inline T * get_pointer(ibapi::shared_ptr const & p) return p.get(); } -/* -template -bool ibapi::shared_ptr::operator==(ibapi::shared_ptr const & a) -{ - return a.get() == this->get(); -} - -template -bool ibapi::shared_ptr::operator!=(ibapi::shared_ptr const & a) -{ - return a.get() != this->get(); -} -*/ +// 封装模块 BOOST_PYTHON_MODULE(vnib) { using namespace boost::python; @@ -1387,7 +1365,6 @@ BOOST_PYTHON_MODULE(vnib) class_("IbApi") .def("eConnect", &IbApiWrap::eConnect) .def("eDisconnect", &IbApiWrap::eDisconnect) - .def("serverVersion", &IbApiWrap::serverVersion) .def("TwsConnectionTime", &IbApiWrap::TwsConnectionTime) .def("reqMktData", &IbApiWrap::reqMktData) .def("cancelMktData", &IbApiWrap::cancelMktData) @@ -1500,7 +1477,7 @@ BOOST_PYTHON_MODULE(vnib) .def("securityDefinitionOptionalParameterEnd", pure_virtual(&IbApiWrap::securityDefinitionOptionalParameterEnd)) ; - //结构体封装 + //结构体相关的封装 class_("OrderState") .def_readwrite("status", &OrderState::status) .def_readwrite("initMargin", &OrderState::initMargin) @@ -1583,15 +1560,10 @@ BOOST_PYTHON_MODULE(vnib) ; class_("TagValue") - //class_("TagValue") .def_readwrite("tag", &TagValue::tag) .def_readwrite("value", &TagValue::value) ; - class_("TagValueList") - //class_("TagValueList") - .def(vector_indexing_suite()); //这个true非常重要 - class_("ComboLeg") .def_readwrite("conId", &ComboLeg::conId) .def_readwrite("ratio", &ComboLeg::ratio) @@ -1603,9 +1575,6 @@ BOOST_PYTHON_MODULE(vnib) .def_readwrite("exemptCode", &ComboLeg::exemptCode) ; - class_("ComboLegList") - .def(vector_indexing_suite()); - class_("Contract") .def_readwrite("conId", &Contract::conId) .def_readwrite("symbol", &Contract::symbol) @@ -1662,19 +1631,8 @@ BOOST_PYTHON_MODULE(vnib) .def_readwrite("nextOptionPartial", &ContractDetails::nextOptionPartial) .def_readwrite("notes", &ContractDetails::notes) ; - - enum_("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) - ; - - - //实现不完全,暂时不清楚作用 - class_("OrderCondition") + + class_("OrderCondition") //实现不完全,暂时不清楚作用 .def("toString", &OrderCondition::toString) .def("type", &OrderCondition::type) ; @@ -1683,12 +1641,6 @@ BOOST_PYTHON_MODULE(vnib) .def_readwrite("price", &OrderComboLeg::price) ; - class_("OrderComboLegList") - .def(vector_indexing_suite()); - - class_>>("OrderConditionList") - .def(vector_indexing_suite>, true>()); - class_("Order") .def_readwrite("orderId", &Order::orderId) .def_readwrite("clientId", &Order::clientId) @@ -1804,10 +1756,37 @@ BOOST_PYTHON_MODULE(vnib) .def_readwrite("extOperator", &Order::extOperator) ; - //register_ptr_to_python(); - //register_ptr_to_python(); - //register_ptr_to_python(); + //vector相关的封装 + class_("TagValueList") + .def(vector_indexing_suite()); //这个true非常重要 + class_("ComboLegList") + .def(vector_indexing_suite()); + + class_("OrderComboLegList") + .def(vector_indexing_suite()); + + class_>>("OrderConditionList") + .def(vector_indexing_suite>, true>()); + + class_>>("OrderConditionList") + .def(vector_indexing_suite>, true>()); + + class_>("StringList") + .def(vector_indexing_suite, true>()); + + class_>("DoubleList") + .def(vector_indexing_suite, true>()); + + //enum相关的封装 + enum_("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") .value("BID_SIZE", BID_SIZE) @@ -1886,5 +1865,10 @@ BOOST_PYTHON_MODULE(vnib) .value("DELAYED_OPEN", DELAYED_OPEN) .value("NOT_SET", NOT_SET) ; + + //注册智能指针到boost.python环境中,暂时无用 + //register_ptr_to_python(); + //register_ptr_to_python(); + //register_ptr_to_python(); }; diff --git a/vn.ib/vnib/vnib/vnib.h b/vn.ib/vnib/vnib/vnib.h index c3b17ca4..e79b482d 100644 --- a/vn.ib/vnib/vnib/vnib.h +++ b/vn.ib/vnib/vnib/vnib.h @@ -383,8 +383,11 @@ public: 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 expirations, std::set strikes){}; + virtual void securityDefinitionOptionalParameter(int reqId, const std::string& exchange, int underlyingConId, const std::string& - tradingClass, const std::string& multiplier, std::set expirations, std::set strikes){}; + tradingClass, const std::string& multiplier, std::vector expirations, std::vector strikes){}; virtual void securityDefinitionOptionalParameterEnd(int reqId){}; @@ -396,8 +399,6 @@ public: void eDisconnect(); - int serverVersion(); - std::string TwsConnectionTime(); void reqMktData(TickerId id, const Contract& contract,