From 175b1d11a11079135ae588ed6544d8097b2072f3 Mon Sep 17 00:00:00 2001 From: ttldtor Date: Tue, 31 Oct 2023 20:04:03 +0300 Subject: [PATCH] Session::isTrading Session::getNextSession --- .../isolated/Isolated.hpp | 3 ++ .../dxfeed_graal_cpp_api/schedule/Session.hpp | 26 ++++++++++++++++ samples/cpp/ScheduleSample/src/main.cpp | 18 ++++++----- src/internal/Isolate.cpp | 26 ++++++++++++++++ src/schedule/Session.cpp | 30 +++++++++++++++++++ 5 files changed, 96 insertions(+), 7 deletions(-) diff --git a/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp b/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp index 018a09b1..8c846126 100644 --- a/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp +++ b/include/dxfeed_graal_cpp_api/isolated/Isolated.hpp @@ -112,6 +112,9 @@ struct SessionFilter { struct Session { static /* dxfg_day_t* */ void *getDay(/* dxfg_session_t* */ void *session) noexcept; + static bool isTrading(/* dxfg_session_t* */ void *session) noexcept; + static /* dxfg_session_t* */ void *getNextSession(/* dxfg_session_t* */ void *session, + /* dxfg_session_filter_t* */ void *filter) noexcept; static std::string toString(/* dxfg_session_t* */ void *session) noexcept; }; diff --git a/include/dxfeed_graal_cpp_api/schedule/Session.hpp b/include/dxfeed_graal_cpp_api/schedule/Session.hpp index bdba527b..3985308f 100644 --- a/include/dxfeed_graal_cpp_api/schedule/Session.hpp +++ b/include/dxfeed_graal_cpp_api/schedule/Session.hpp @@ -38,6 +38,32 @@ struct DXFCPP_EXPORT Session { */ Day::Ptr getDay() const noexcept; + /** + * @return true if trading activity is allowed within this session. + * This method is equivalent to expression @ref SessionType::isTrading() "getType()->isTrading()". + *

+ * Some sessions may have zero duration - e.g. indices that post value once a day. + * Such sessions can be of any appropriate type, trading or non-trading. + */ + bool isTrading() const noexcept; + + /** + * Returns following session accepted by specified filter. + * This method may cross the day boundary and return appropriate session from + * following days - up to a year in the future. If no such session was found + * within one year this method will return `Session::Ptr{nullptr}`. + *

+ * To find following trading session of any type use this code: + *

session = session->getNextSession(SessionFilter::TRADING);
+ * To find following regular trading session use this code: + *
session = session->getNextSession(SessionFilter::REGULAR);
+ * + * @param filter The filter to test sessions + * @return The nearest following session that is accepted by the filter or `Session::Ptr{nullptr}` if no such + * session was found within one year + */ + Session::Ptr getNextSession(const SessionFilter &filter) const noexcept; + /** * Returns a string representation of the current object. * diff --git a/samples/cpp/ScheduleSample/src/main.cpp b/samples/cpp/ScheduleSample/src/main.cpp index 2dea4f0a..d5f3447f 100644 --- a/samples/cpp/ScheduleSample/src/main.cpp +++ b/samples/cpp/ScheduleSample/src/main.cpp @@ -145,13 +145,17 @@ void printNextTradingSession(auto &&profile, auto time) { return; } - // if (!session->isTrading()) { - // session = session->getNextSession(SessionFilter::TRADING); - // } - // - // std::cout << "Next trading session for " + profile->getSymbol() + ": " + session->toString() + " in " + - // session->getDay()->toString() - // << std::endl; + if (!session->isTrading()) { + session = session->getNextSession(SessionFilter::TRADING); + } + + if (!session) { + std::cerr << "There is no next trading session for " << profile->getSymbol() << std::endl; + } else { + std::cout << "Next trading session for " + profile->getSymbol() + ": " + session->toString() + " in " + + session->getDay()->toString() + << std::endl; + } } void printNearestTradingSession(auto &&profile, auto time) { diff --git a/src/internal/Isolate.cpp b/src/internal/Isolate.cpp index fbdd86fc..ca5b92da 100644 --- a/src/internal/Isolate.cpp +++ b/src/internal/Isolate.cpp @@ -711,6 +711,32 @@ std::string Day::toString(/* dxfg_day_t* */ void *day) noexcept { nullptr, dxfcpp::bit_cast(session))); } +bool Session::isTrading(/* dxfg_session_t* */ void *session) noexcept { + if (!session) { + return false; + } + + return runIsolatedOrElse( + [](auto threadHandle, auto &&session) { + return dxfg_Session_isTrading(dxfcpp::bit_cast(threadHandle), session) == 1; + }, + false, dxfcpp::bit_cast(session)); +} + +/* dxfg_session_t* */ void *Session::getNextSession(/* dxfg_session_t* */ void *session, + /* dxfg_session_filter_t* */ void *filter) noexcept { + if (!session || !filter) { + return nullptr; + } + + return dxfcpp::bit_cast(runIsolatedOrElse( + [](auto threadHandle, auto &&session, auto &&filter) { + return dxfg_Session_getNextSession(dxfcpp::bit_cast(threadHandle), session, + filter); + }, + nullptr, dxfcpp::bit_cast(session), dxfcpp::bit_cast(filter))); +} + std::string Session::toString(/* dxfg_session_t* */ void *session) noexcept { if (!session) { return dxfcpp::String::EMPTY; diff --git a/src/schedule/Session.cpp b/src/schedule/Session.cpp index 6563b0dd..b46d6ef6 100644 --- a/src/schedule/Session.cpp +++ b/src/schedule/Session.cpp @@ -33,6 +33,36 @@ Day::Ptr Session::getDay() const noexcept { return day; } +bool Session::isTrading() const noexcept { + if (!handle_) { + return false; + } + + return isolated::schedule::Session::isTrading(handle_.get()); +} + +Session::Ptr Session::getNextSession(const SessionFilter &filter) const noexcept { + if (!handle_ || !filter.handle_) { + return {}; + } + + auto graalSession = isolated::schedule::Session::getNextSession(handle_.get(), filter.handle_.get()); + + if (!graalSession) { + return {}; + } + + std::shared_ptr session{new (std::nothrow) Session{}}; + + if (!session) { + return {}; + } + + session->handle_ = JavaObjectHandle(graalSession); + + return session; +} + std::string Session::toString() const noexcept { if (!handle_) { return dxfcpp::String::EMPTY;