Skip to content

Commit

Permalink
InstrumentProfileType
Browse files Browse the repository at this point in the history
  • Loading branch information
ttldtor committed Oct 20, 2023
1 parent 1318971 commit b5dc6c2
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 41 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ set(dxFeedGraalCxxApi_ApiOsub_Sources

set(dxFeedGraalCxxApi_Ipf_Sources
src/ipf/InstrumentProfile.cpp
src/ipf/InstrumentProfileType.cpp
src/ipf/InstrumentProfileField.cpp
src/ipf/InstrumentProfileReader.cpp
src/ipf/live/InstrumentProfileCollector.cpp
Expand Down
44 changes: 44 additions & 0 deletions include/dxfeed_graal_cpp_api/ipf/InstrumentProfileType.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include "../internal/Conf.hpp"

#include "../internal/Common.hpp"

#include <cstdint>

namespace dxfcpp {

/**
* Defines standard types of InstrumentProfile. Note that other (unknown) types
* can be used without listing in this class - use it for convenience only.
* Please see <b>Instrument Profile Format</b> documentation for complete description.
*/
struct InstrumentProfileType : Enum<InstrumentProfileType, std::uint32_t> {
using Enum::Enum;

static const DXFCPP_EXPORT InstrumentProfileType CURRENCY;
static const DXFCPP_EXPORT InstrumentProfileType FOREX;
static const DXFCPP_EXPORT InstrumentProfileType BOND;
static const DXFCPP_EXPORT InstrumentProfileType INDEX;
static const DXFCPP_EXPORT InstrumentProfileType STOCK;
static const DXFCPP_EXPORT InstrumentProfileType ETF;
static const DXFCPP_EXPORT InstrumentProfileType MUTUAL_FUND;
static const DXFCPP_EXPORT InstrumentProfileType MONEY_MARKET_FUND;
static const DXFCPP_EXPORT InstrumentProfileType PRODUCT;
static const DXFCPP_EXPORT InstrumentProfileType FUTURE;
static const DXFCPP_EXPORT InstrumentProfileType OPTION;
static const DXFCPP_EXPORT InstrumentProfileType WARRANT;
static const DXFCPP_EXPORT InstrumentProfileType CFD;
static const DXFCPP_EXPORT InstrumentProfileType SPREAD;
static const DXFCPP_EXPORT InstrumentProfileType OTHER;
static const DXFCPP_EXPORT InstrumentProfileType REMOVED;
};

template <>
const std::unordered_map<InstrumentProfileType::CodeType, std::reference_wrapper<const InstrumentProfileType>>
InstrumentProfileType::ParentType::ALL;

} // namespace dxfcpp
1 change: 1 addition & 0 deletions include/dxfeed_graal_cpp_api/ipf/IpfModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "InstrumentProfileField.hpp"
#include "InstrumentProfile.hpp"
#include "InstrumentProfileReader.hpp"
#include "InstrumentProfileType.hpp"

#include "live/InstrumentProfileCollector.hpp"
#include "live/InstrumentProfileConnection.hpp"
72 changes: 31 additions & 41 deletions samples/cpp/DxFeedLiveIpfSample/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,52 +55,42 @@ int main(int argc, char *argv[]) {
std::unordered_map<std::string, std::shared_ptr<InstrumentProfile>> profiles{};
std::mutex mutex{};

collector->addUpdateListener([](auto &&profiles) -> void {
for (const auto &p : profiles) {
if (p->getType() == "REMOVED") {
std::cout << p->getSymbol() + ": " + p->getType() + "\n";
// It is possible to add listener after connection is started - updates will not be missed in this case
collector->addUpdateListener([&profiles, &mutex, self = collector](auto &&instruments) {
std::cout << "\nInstrument Profiles:" << std::endl;
// We can observe REMOVED elements - need to add necessary filtering
// See javadoc for InstrumentProfileCollector for more details

// (1) We can either process instrument profile updates manually
for (auto &&profile : instruments) {
std::lock_guard lock{mutex};

if (InstrumentProfileType::REMOVED.getName() == profile->getType()) {
// Profile was removed - remove it from our data model
profiles.erase(profile->getSymbol());
} else {
std::cout << p->getSymbol() + " (" + p->getDescription() + ")\n";
// Profile was updated - collector only notifies us if profile was changed
profiles[profile->getSymbol()] = profile;
}
};

{
std::lock_guard lock{mutex};
std::cout << "Total number of profiles (1): " + std::to_string(profiles.size()) << std::endl;
}
//
// // (2) or access the concurrent view of instrument profiles
// std::unordered_set<std::string> symbols =
// StreamSupport.stream(collector->view().spliterator(), false)
// .filter(profile->!InstrumentProfileType.REMOVED.name().equals(profile.getType()))
// .map(InstrumentProfile::getSymbol)
// .collect(Collectors.toSet());
// std::cout << "Total number of profiles (2): " + std::to_string(symbols.size()) << std::endl;
//
std::cout << "Last modified: " + dxfcpp::formatTimeStampWithMillisWithTimeZone(self->getLastUpdateTime())
<< std::endl;
});

//
// // It is possible to add listener after connection is started - updates will not be missed in this case
// collector.addUpdateListener([&profiles, &mutex](auto &&instruments) {
// std::cout << "\nInstrument Profiles:" << std::endl;
// // We can observe REMOVED elements - need to add necessary filtering
// // See javadoc for InstrumentProfileCollector for more details
//
// // (1) We can either process instrument profile updates manually
// for (auto &&profile : instruments) {
// std::lock_guard lock{mutex};
//
// if (InstrumentProfileType::REMOVED->getName() == profile->getType()) {
// // Profile was removed - remove it from our data model
// profiles.erase(profile->getSymbol());
// } else {
// // Profile was updated - collector only notifies us if profile was changed
// profiles[profile.getSymbol()] = profile;
// }
// };
//
// {
// std::lock_guard lock{mutex};
// std::cout << "Total number of profiles (1): " + std::to_string(profiles.size()) << std::endl;
// }
//
// // (2) or access the concurrent view of instrument profiles
// std::unordered_set<std::string> symbols =
// StreamSupport.stream(collector->view().spliterator(), false)
// .filter(profile->!InstrumentProfileType.REMOVED.name().equals(profile.getType()))
// .map(InstrumentProfile::getSymbol)
// .collect(Collectors.toSet());
// std::cout << "Total number of profiles (2): " + std::to_string(symbols.size()) << std::endl;
//
// std::cout << "Last modified: " + new Date(collector->getLastUpdateTime()) << std::endl;
// });
//
std::this_thread::sleep_for(std::chrono::days(365));

connection->close();
Expand Down
52 changes: 52 additions & 0 deletions src/ipf/InstrumentProfileType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023 Devexperts LLC.
// SPDX-License-Identifier: MPL-2.0

#include <dxfg_api.h>

#include <dxfeed_graal_c_api/api.h>
#include <dxfeed_graal_cpp_api/api.hpp>

#include <cstring>
#include <memory>
#include <utf8.h>
#include <utility>

namespace dxfcpp {

const InstrumentProfileType InstrumentProfileType::CURRENCY{0, "CURRENCY"};
const InstrumentProfileType InstrumentProfileType::FOREX{1, "FOREX"};
const InstrumentProfileType InstrumentProfileType::BOND{2, "BOND"};
const InstrumentProfileType InstrumentProfileType::INDEX{3, "INDEX"};
const InstrumentProfileType InstrumentProfileType::STOCK{4, "STOCK"};
const InstrumentProfileType InstrumentProfileType::ETF{5, "ETF"};
const InstrumentProfileType InstrumentProfileType::MUTUAL_FUND{6, "MUTUAL_FUND"};
const InstrumentProfileType InstrumentProfileType::MONEY_MARKET_FUND{7, "MONEY_MARKET_FUND"};
const InstrumentProfileType InstrumentProfileType::PRODUCT{8, "PRODUCT"};
const InstrumentProfileType InstrumentProfileType::FUTURE{9, "FUTURE"};
const InstrumentProfileType InstrumentProfileType::OPTION{10, "OPTION"};
const InstrumentProfileType InstrumentProfileType::WARRANT{11, "WARRANT"};
const InstrumentProfileType InstrumentProfileType::CFD{12, "CFD"};
const InstrumentProfileType InstrumentProfileType::SPREAD{13, "SPREAD"};
const InstrumentProfileType InstrumentProfileType::OTHER{14, "OTHER"};
const InstrumentProfileType InstrumentProfileType::REMOVED{15, "REMOVED"};

template <>
const std::unordered_map<InstrumentProfileType::CodeType, std::reference_wrapper<const InstrumentProfileType>>
InstrumentProfileType::ParentType::ALL =
[](std::initializer_list<std::reference_wrapper<const InstrumentProfileType>> orders) {
std::unordered_map<InstrumentProfileType::CodeType, std::reference_wrapper<const InstrumentProfileType>>
result{};

for (auto &&o : orders) {
result.emplace(o.get().getCode(), o);
}

return result;
}({InstrumentProfileType::CURRENCY, InstrumentProfileType::FOREX, InstrumentProfileType::BOND,
InstrumentProfileType::INDEX, InstrumentProfileType::STOCK, InstrumentProfileType::ETF,
InstrumentProfileType::MUTUAL_FUND, InstrumentProfileType::MONEY_MARKET_FUND, InstrumentProfileType::PRODUCT,
InstrumentProfileType::FUTURE, InstrumentProfileType::OPTION, InstrumentProfileType::WARRANT,
InstrumentProfileType::CFD, InstrumentProfileType::SPREAD, InstrumentProfileType::OTHER,
InstrumentProfileType::REMOVED});

} // namespace dxfcpp

0 comments on commit b5dc6c2

Please sign in to comment.