Skip to content

Commit

Permalink
Session::isTrading
Browse files Browse the repository at this point in the history
Session::getNextSession
  • Loading branch information
ttldtor committed Oct 31, 2023
1 parent f0f64be commit 175b1d1
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
3 changes: 3 additions & 0 deletions include/dxfeed_graal_cpp_api/isolated/Isolated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
26 changes: 26 additions & 0 deletions include/dxfeed_graal_cpp_api/schedule/Session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ struct DXFCPP_EXPORT Session {
*/
Day::Ptr getDay() const noexcept;

/**
* @return <code>true</code> if trading activity is allowed within this session.
* This method is equivalent to expression @ref SessionType::isTrading() "getType()->isTrading()".
* <p>
* 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}`.
* <p>
* To find following trading session of any type use this code:
* <pre>session = session->getNextSession(SessionFilter::TRADING);</pre>
* To find following regular trading session use this code:
* <pre>session = session->getNextSession(SessionFilter::REGULAR);</pre>
*
* @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.
*
Expand Down
18 changes: 11 additions & 7 deletions samples/cpp/ScheduleSample/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions src/internal/Isolate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,32 @@ std::string Day::toString(/* dxfg_day_t* */ void *day) noexcept {
nullptr, dxfcpp::bit_cast<dxfg_session_t *>(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<graal_isolatethread_t *>(threadHandle), session) == 1;
},
false, dxfcpp::bit_cast<dxfg_session_t *>(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<void *>(runIsolatedOrElse(
[](auto threadHandle, auto &&session, auto &&filter) {
return dxfg_Session_getNextSession(dxfcpp::bit_cast<graal_isolatethread_t *>(threadHandle), session,
filter);
},
nullptr, dxfcpp::bit_cast<dxfg_session_t *>(session), dxfcpp::bit_cast<dxfg_session_filter_t *>(filter)));
}

std::string Session::toString(/* dxfg_session_t* */ void *session) noexcept {
if (!session) {
return dxfcpp::String::EMPTY;
Expand Down
30 changes: 30 additions & 0 deletions src/schedule/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> session{new (std::nothrow) Session{}};

if (!session) {
return {};
}

session->handle_ = JavaObjectHandle<Session>(graalSession);

return session;
}

std::string Session::toString() const noexcept {
if (!handle_) {
return dxfcpp::String::EMPTY;
Expand Down

0 comments on commit 175b1d1

Please sign in to comment.