Skip to content

Commit d80ce96

Browse files
authored
Merge pull request #10 from hanlonlab/development
added context manager to trader api
2 parents 94d67e9 + 8fd3395 commit d80ce96

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

include/Trader.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ class Trader {
8888
.def("on_execution_updated", &Trader::onExecutionUpdated, py::arg("cb"))
8989
.def("on_portfolio_item_updated", &Trader::onPortfolioItemUpdated, py::arg("cb"))
9090
.def("on_portfolio_summary_updated", &Trader::onPortfolioSummaryUpdated, py::arg("cb"))
91-
.def("on_waiting_list_updated", &Trader::onWaitingListUpdated, py::arg("cb"));
91+
.def("on_waiting_list_updated", &Trader::onWaitingListUpdated, py::arg("cb"))
92+
.def("__enter__", &Trader::enter, py::return_value_policy::reference)
93+
.def("__exit__", &Trader::exit);
9294
}
9395

9496
Trader();
@@ -159,6 +161,10 @@ class Trader {
159161
void onPortfolioSummaryUpdated(const std::function<void(Trader*)>& cb);
160162
void onPortfolioItemUpdated(const std::function<void(Trader*, const std::string&)>& cb);
161163
void onWaitingListUpdated(const std::function<void(Trader*)>& cb);
164+
;
165+
// context manager methods
166+
auto enter() -> Trader*;
167+
auto exit(py::args args, const py::kwargs& kwargs) -> bool;
162168

163169
private:
164170
shift::FIXInitiator& m_initiator;

src/Trader.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,14 @@ void Trader::onWaitingListUpdated(const std::function<void(Trader*)>& cb)
326326
{
327327
m_client->waitingListUpdatedCb = cb;
328328
}
329+
330+
auto Trader::enter() -> Trader*
331+
{
332+
return this;
333+
}
334+
335+
auto Trader::exit(py::args args, const py::kwargs& kwargs) -> bool
336+
{
337+
this->disconnect();
338+
return false;
339+
}

0 commit comments

Comments
 (0)