Skip to content

Commit

Permalink
[EN-7314] Implement the DXPublisher: OptionSale + Summary
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed Aug 22, 2023
1 parent 3a14303 commit 1690f12
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 59 deletions.
7 changes: 5 additions & 2 deletions include/dxfeed_graal_cpp_api/event/market/OptionSale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ class DXFCPP_EXPORT OptionSale final : public MarketEvent, public IndexedEvent {
double underlyingPrice = math::NaN;
double volatility = math::NaN;
double delta = math::NaN;
std::string optionSymbol;
std::string optionSymbol{};
};

Data data_{};

void fillData(void *graalNative) noexcept override;
void fillGraalData(void *graalNative) const noexcept override;
static void freeGraalData(void *graalNative) noexcept;

static std::shared_ptr<OptionSale> fromGraal(void *graalNative) noexcept;
//TODO: implement
void* toGraal() const noexcept;
static void freeGraal(void* graalNative) noexcept;

Expand Down
3 changes: 3 additions & 0 deletions include/dxfeed_graal_cpp_api/event/market/Summary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class DXFCPP_EXPORT Summary final : public MarketEvent, public LastingEvent {

Data data_{};

void fillData(void *graalNative) noexcept override;
void fillGraalData(void *graalNative) const noexcept override;

static std::shared_ptr<Summary> fromGraal(void *graalNative) noexcept;
//TODO: implement
void* toGraal() const noexcept;
Expand Down
122 changes: 90 additions & 32 deletions src/event/market/OptionSale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,86 @@ namespace dxfcpp {

const EventTypeEnum &OptionSale::TYPE = EventTypeEnum::OPTION_SALE;

void OptionSale::fillData(void *graalNative) noexcept {
if (graalNative == nullptr) {
return;
}

MarketEvent::fillData(graalNative);

auto graalOptionSale = static_cast<dxfg_option_sale_t *>(graalNative);

data_ = {
.eventFlags = graalOptionSale->event_flags,
.index = graalOptionSale->index,
.timeSequence = graalOptionSale->time_sequence,
.timeNanoPart = graalOptionSale->time_nano_part,
.exchangeCode = graalOptionSale->exchange_code,
.price = graalOptionSale->price,
.size = graalOptionSale->size,
.bidPrice = graalOptionSale->bid_price,
.askPrice = graalOptionSale->ask_price,
.exchangeSaleConditions = dxfcpp::toString(graalOptionSale->exchange_sale_conditions),
.flags = graalOptionSale->flags,
.underlyingPrice = graalOptionSale->underlying_price,
.volatility = graalOptionSale->volatility,
.delta = graalOptionSale->delta,
.optionSymbol = dxfcpp::toString(graalOptionSale->option_symbol),
};
}

void OptionSale::fillGraalData(void *graalNative) const noexcept {
if (graalNative == nullptr) {
return;
}

MarketEvent::fillGraalData(graalNative);

auto graalOptionSale = static_cast<dxfg_option_sale_t *>(graalNative);

graalOptionSale->event_flags = data_.eventFlags;
graalOptionSale->index = data_.index;
graalOptionSale->time_sequence = data_.timeSequence;
graalOptionSale->time_nano_part = data_.timeNanoPart;
graalOptionSale->exchange_code = data_.exchangeCode;
graalOptionSale->price = data_.price;
graalOptionSale->size = data_.size;
graalOptionSale->bid_price = data_.bidPrice;
graalOptionSale->ask_price = data_.askPrice;
graalOptionSale->exchange_sale_conditions = createCString(data_.exchangeSaleConditions);
graalOptionSale->flags = data_.flags;
graalOptionSale->underlying_price = data_.underlyingPrice;
graalOptionSale->volatility = data_.volatility;
graalOptionSale->delta = data_.delta;
graalOptionSale->option_symbol = createCString(data_.optionSymbol);
}

void OptionSale::freeGraalData(void *graalNative) noexcept {
if (graalNative == nullptr) {
return;
}

MarketEvent::freeGraalData(graalNative);

auto graalOptionSale = static_cast<dxfg_option_sale_t *>(graalNative);

delete[] graalOptionSale->exchange_sale_conditions;
delete[] graalOptionSale->option_symbol;
}

std::shared_ptr<OptionSale> OptionSale::fromGraal(void *graalNative) noexcept {
if (!graalNative) {
return {};
}

auto eventType = bit_cast<dxfg_event_type_t *>(graalNative);

if (eventType->clazz != DXFG_EVENT_OPTION_SALE) {
if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != dxfg_event_clazz_t::DXFG_EVENT_OPTION_SALE) {
return {};
}

try {
auto graalOptionSale = bit_cast<dxfg_option_sale_t *>(graalNative);
auto optionSale = std::make_shared<OptionSale>(dxfcpp::toString(graalOptionSale->market_event.event_symbol));

optionSale->setEventTime(graalOptionSale->market_event.event_time);
optionSale->data_ = {
graalOptionSale->event_flags,
graalOptionSale->index,
graalOptionSale->time_sequence,
graalOptionSale->time_nano_part,
graalOptionSale->exchange_code,
graalOptionSale->price,
graalOptionSale->size,
graalOptionSale->bid_price,
graalOptionSale->ask_price,
dxfcpp::toString(graalOptionSale->exchange_sale_conditions),
graalOptionSale->flags,
graalOptionSale->underlying_price,
graalOptionSale->volatility,
graalOptionSale->delta,
dxfcpp::toString(graalOptionSale->option_symbol),
};
auto optionSale = std::make_shared<OptionSale>();

optionSale->fillData(graalNative);

return optionSale;
} catch (...) {
Expand Down Expand Up @@ -80,25 +127,36 @@ std::string OptionSale::toString() const noexcept {
}

void *OptionSale::toGraal() const noexcept {
return nullptr;
if constexpr (Debugger::isDebug) {
Debugger::debug(toString() + "::toGraal()");
}

auto *graalOptionSale = new (std::nothrow)
dxfg_option_sale_t{.market_event = {.event_type = {.clazz = dxfg_event_clazz_t::DXFG_EVENT_OPTION_SALE}}};

if (!graalOptionSale) {
// TODO: error handling

return nullptr;
}

fillGraalData(static_cast<void *>(graalOptionSale));

return static_cast<void *>(graalOptionSale);
}

void OptionSale::freeGraal(void *graalNative) noexcept {
if (!graalNative) {
return;
}

auto eventType = bit_cast<dxfg_event_type_t *>(graalNative);

if (eventType->clazz != DXFG_EVENT_OPTION_SALE) {
if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != dxfg_event_clazz_t::DXFG_EVENT_OPTION_SALE) {
return;
}

auto graalOptionSale = bit_cast<dxfg_option_sale_t *>(graalNative);
auto graalOptionSale = static_cast<dxfg_option_sale_t *>(graalNative);

delete[] graalOptionSale->market_event.event_symbol;
delete[] graalOptionSale->exchange_sale_conditions;
delete[] graalOptionSale->option_symbol;
freeGraalData(graalNative);

delete graalOptionSale;
}
Expand Down
4 changes: 2 additions & 2 deletions src/event/market/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ std::shared_ptr<Profile> Profile::fromGraal(void *graalNative) noexcept {
return {};
}

if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != DXFG_EVENT_PROFILE) {
if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != dxfg_event_clazz_t::DXFG_EVENT_PROFILE) {
return {};
}

Expand Down Expand Up @@ -150,7 +150,7 @@ void Profile::freeGraal(void *graalNative) noexcept {
return;
}

if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != DXFG_EVENT_PROFILE) {
if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != dxfg_event_clazz_t::DXFG_EVENT_PROFILE) {
return;
}

Expand Down
90 changes: 67 additions & 23 deletions src/event/market/Summary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,63 @@ namespace dxfcpp {

const EventTypeEnum &Summary::TYPE = EventTypeEnum::SUMMARY;

void Summary::fillData(void *graalNative) noexcept {
if (graalNative == nullptr) {
return;
}

MarketEvent::fillData(graalNative);

auto graalSummary = static_cast<dxfg_summary_t *>(graalNative);

data_ = {
.dayId = graalSummary->day_id,
.dayOpenPrice = graalSummary->day_open_price,
.dayHighPrice = graalSummary->day_high_price,
.dayLowPrice = graalSummary->day_low_price,
.dayClosePrice = graalSummary->day_close_price,
.prevDayId = graalSummary->prev_day_id,
.prevDayClosePrice = graalSummary->prev_day_close_price,
.prevDayVolume = graalSummary->prev_day_volume,
.openInterest = graalSummary->open_interest,
.flags = graalSummary->flags,
};
}

void Summary::fillGraalData(void *graalNative) const noexcept {
if (graalNative == nullptr) {
return;
}

MarketEvent::fillGraalData(graalNative);

auto graalSummary = static_cast<dxfg_summary_t *>(graalNative);

graalSummary->day_id = data_.dayId;
graalSummary->day_open_price = data_.dayOpenPrice;
graalSummary->day_high_price = data_.dayHighPrice;
graalSummary->day_low_price = data_.dayLowPrice;
graalSummary->day_close_price = data_.dayClosePrice;
graalSummary->prev_day_id = data_.prevDayId;
graalSummary->prev_day_close_price = data_.prevDayClosePrice;
graalSummary->prev_day_volume = data_.prevDayVolume;
graalSummary->open_interest = data_.openInterest;
graalSummary->flags = data_.flags;
}

std::shared_ptr<Summary> Summary::fromGraal(void *graalNative) noexcept {
if (!graalNative) {
return {};
}

auto eventType = bit_cast<dxfg_event_type_t *>(graalNative);

if (eventType->clazz != DXFG_EVENT_SUMMARY) {
if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != dxfg_event_clazz_t::DXFG_EVENT_SUMMARY) {
return {};
}

try {
auto graalSummary = bit_cast<dxfg_summary_t *>(graalNative);
auto summary = std::make_shared<Summary>(dxfcpp::toString(graalSummary->market_event.event_symbol));

summary->setEventTime(graalSummary->market_event.event_time);
summary->data_ = {graalSummary->day_id,
graalSummary->day_open_price,
graalSummary->day_high_price,
graalSummary->day_low_price,
graalSummary->day_close_price,
graalSummary->prev_day_id,
graalSummary->prev_day_close_price,
graalSummary->prev_day_volume,
graalSummary->open_interest,
graalSummary->flags};
auto summary = std::make_shared<Summary>();

summary->fillData(graalNative);

return summary;
} catch (...) {
Expand All @@ -68,23 +99,36 @@ std::string Summary::toString() const noexcept {
}

void *Summary::toGraal() const noexcept {
return nullptr;
if constexpr (Debugger::isDebug) {
Debugger::debug(toString() + "::toGraal()");
}

auto *graalSummary = new (std::nothrow)
dxfg_summary_t{.market_event = {.event_type = {.clazz = dxfg_event_clazz_t::DXFG_EVENT_SUMMARY}}};

if (!graalSummary) {
// TODO: error handling

return nullptr;
}

fillGraalData(static_cast<void *>(graalSummary));

return static_cast<void *>(graalSummary);
}

void Summary::freeGraal(void *graalNative) noexcept {
if (!graalNative) {
return;
}

auto eventType = bit_cast<dxfg_event_type_t *>(graalNative);

if (eventType->clazz != DXFG_EVENT_SUMMARY) {
if (static_cast<dxfg_event_type_t *>(graalNative)->clazz != dxfg_event_clazz_t::DXFG_EVENT_SUMMARY) {
return;
}

auto graalSummary = bit_cast<dxfg_summary_t *>(graalNative);
auto graalSummary = static_cast<dxfg_summary_t *>(graalNative);

delete[] graalSummary->market_event.event_symbol;
MarketEvent::freeGraalData(graalNative);

delete graalSummary;
}
Expand Down

0 comments on commit 1690f12

Please sign in to comment.