Merge branch 'temp_arham' of bitbucket.org:bluetradesys/spark into temp_arham

This commit is contained in:
arhamGIT committed 2023-04-26 12:33:50 -04:00
commit 7f07f5a69b
4 files changed
+241 -12

No files matched your search

+2
View File
@@ -57,6 +57,8 @@ bts::api::Venue DomainSecurityUtils::convert(api::domain::Venue venue) {
return api::Venue::VENUE_BINANCE_USDS_FUTURES;
case DOMAIN_VENUE(SIM):
return api::Venue::VENUE_SIM;
case DOMAIN_VENUE(COINBASE):
return api::Venue::VENUE_COINBASE;
case DOMAIN_VENUE(UNSPECIFIED):
return api::VENUE_UNSET;
default:
@@ -1,3 +1,4 @@
#include "lib/md/coinbase/CoinbaseSpot.h"
#ifdef SPARK_ENABLE_CCAPI
#include "CoinbaseSecurityRequestor.h"
#include "lib/api/domain/SecurityType.pb.h"
@@ -62,13 +63,13 @@ static ccapi::Request exchange_info_req() {
return req;
}
// static vector<string> get_sec_descs(const vector<Security> &sec_ptrs) {
// vector<string> descs{sec_ptrs.size()};
// std::transform(
// sec_ptrs.begin(), sec_ptrs.end(), descs.begin(),
// [](const auto &s) { return s.desc(); });
// return descs;
// }
static vector<string> get_sec_descs(const vector<Security> &sec_ptrs) {
vector<string> descs{sec_ptrs.size()};
std::transform(
sec_ptrs.begin(), sec_ptrs.end(), descs.begin(),
[](const auto &s) { return s.desc(); });
return descs;
}
static void validate_response(const ccapi::Event &event, bool is_expected) {
bts_assert_msgf(
@@ -158,6 +159,7 @@ struct CoinbaseSecurityRequestor::PImpl {
};
REMOVE_IF(added_secs, is_sec_excluded);
// Assign security ids
std::cout << types.size();
for (auto &sec : added_secs) {
// Since the ids haven't been set yet, we should expect them to be 0
bts_assert(sec.id().value() == 0);
@@ -184,13 +186,14 @@ struct CoinbaseSecurityRequestor::PImpl {
vector<Security> added_secs;
for (const auto *msg : msgs) {
bts_assert_msgf(
msg->getType() == ccapi::Message::Type::GENERIC_PUBLIC_REQUEST,
msg->getType() == ccapi::Message::Type::GET_INSTRUMENTS,
"Received msg w/ unexpected type:\n{}", msg->toStringPretty());
parse_secs<T>(*msg, types, added_secs);
}
// auto added_sec_descs = get_sec_descs(added_secs);
// LOG(md, Info, "Received Binance US securities:\n{}",
// fmt::to_string(fmt::join(added_sec_descs, "\n")));
LOG(md, Info, "Arhams Code: here {} {}", msgs.size(), added_secs.size());
auto added_sec_descs = get_sec_descs(added_secs);
LOG(md, Info, "Received Binance US securities:\n{}",
fmt::to_string(fmt::join(added_sec_descs, "\n")));
callback(added_secs);
}
@@ -210,7 +213,7 @@ struct CoinbaseSecurityRequestor::PImpl {
LOG(md, Diag, "Received Binance US exchange info (spots):\n{}",
event.toStringPretty());
std::cout << msgs.size();
// update_securities<BinanceSpot>(msgs, {SecurityType::SPOT});
update_securities<CoinbaseSpot>(msgs, {SEC_TYPE(SPOT)});
exch_info_req_pending = false;
if (external_req_pending && !is_busy()) {
request();
+162
View File
@@ -0,0 +1,162 @@
#ifdef SPARK_ENABLE_CCAPI
#include "CoinbaseSpot.h"
#include "lib/api/domain/Currency.pb.h"
#include "lib/api/domain/Venue.pb.h"
#include "lib/core/TableFormatter.h"
#include "lib/domain/Price.h"
#include "lib/domain/Quantity.h"
#include "lib/domain/Security.h"
#include "lib/io/JsonReader.h"
#include "lib/logging/logging.h"
#include <ccapi_cpp/ccapi_message.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include <sstream>
namespace bts::md::coinbase {
using bts::api::domain::SecurityType;
using bts::api::domain::Venue;
using bts::domain::Quantity;
using bts::domain::Security;
using bts::domain::SpotSecurity;
using bts::io::JsonReader;
using std::string;
using std::stringstream;
using std::vector;
#define SEC_TYPE(x) bts::api::domain::SECURITY_TYPE_##x
// static std::string deduce_sym_from_name(const std::string &name) {
// std::regex word_regex("(\\w+)");
// auto words_begin =
// std::sregex_iterator(name.begin(), name.end(), word_regex);
// auto words_end = std::sregex_iterator();
// if (words_begin != words_end) {
// return words_begin->str();
// }
// return "";
// }
// static std::string deduce_sym(const CoinbaseSpot &bspot) {
// if (!bspot.base_asset.empty()) {
// return bspot.base_asset;
// }
// return deduce_sym_from_name(bspot.symbol);
// }
void CoinbaseSpot::parse(
const ccapi::Message &msg, vector<Security> &securities, Venue venue) {
const auto &sec_elements = msg.getElementList();
for (const auto &sec_el : sec_elements) {
const auto &sec_props = sec_el.getNameValueMap();
auto &sec = securities.emplace_back();
auto &scomm = sec.security_common;
scomm.type = SEC_TYPE(SPOT);
scomm.venue = venue;
scomm.exchange_sec_id = sec_props.at("BASE_ASSET");
scomm.desc = sec_props.at("INSTRUMENT");
scomm.symbol = sec_props.at("BASE_ASSET");
scomm.asset = scomm.symbol;
scomm.group = scomm.symbol;
sec.security.emplace<SpotSecurity>();
LOG(md, Info, "Arhams Code: sec el {} {} {} {}",
sec_props.at("INSTRUMENT"), sec.to_string(true), securities.size(),
venue);
}
}
// void CoinbaseSpot::parse(
// const ccapi::Message &msg, vector<Security> &securities, Venue venue) {
// const auto &sec_elements = msg.getElementList();
// for (const auto &sec_el : sec_elements) {
// const auto &sec_props = sec_el.getNameValueMap();
// auto it = sec_props.find(CCAPI_HTTP_BODY);
// if (it == sec_props.end()) {
// continue;
// }
// const auto &body = it->second;
// const auto js = nlohmann::json::parse(body);
// JsonReader jr{&js};
// vector<CoinbaseSpot> binance_spots;
// if (!jr.read("symbols", binance_spots)) {
// const auto js_str = js.dump(2);
// LOG(md, Error,
// "Invalid json. Expected field 'symbols' to contain list of spot "
// "secs:\n{}",
// js_str);
// continue;
// }
// for (const auto &bspot : binance_spots) {
// auto &sec = securities.emplace_back();
// auto &scomm = sec.security_common;
// scomm.type = SEC_TYPE(SPOT);
// scomm.venue = venue;
// scomm.exchange_sec_id = bspot.symbol;
// scomm.desc = bspot.symbol;
// scomm.symbol = deduce_sym(bspot);
// scomm.asset = scomm.symbol;
// scomm.group = scomm.symbol;
// // scomm.min_price_increment = bspot.filter_tuple.price.tick_size;
// // scomm.min_quantity = bspot.filter_tuple.lot_size.min_qty;
// // scomm.min_quantity_increment = bspot.filter_tuple.lot_size.step_size;
// // scomm.currency = bspot.quote_asset.to_currency();
// sec.security.emplace<SpotSecurity>();
// LOG(md, Diag, "Converted BinanceSpot:\n{}\nto domain::Security:\n{}",
// bspot, sec.to_string(true));
// }
// }
// }
void from_json(const nlohmann::json &j, CoinbaseSpot &sec) {
JsonReader jr{&j};
jr.read("symbol", sec.symbol);
// jr.read("status", sec.status);
jr.read("baseAsset", sec.base_asset);
jr.read("baseAssetPrecision", sec.base_asset_precision);
// jr.read("quoteAsset", sec.quote_asset);
jr.read("quotePrecision", sec.quote_precision);
jr.read("quoteAssetPrecision", sec.quote_asset_precision);
// jr.read("orderTypes", sec.order_types);
jr.read("icebergAllowed", sec.iceberg_allowed);
jr.read("ocoAllowed", sec.oco_allowed);
jr.read("quoteOrderQtyMarketAllowed", sec.quote_order_qty_market_allowed);
jr.read("allowTrailingStop", sec.allow_trailing_stop);
jr.read("cancelReplaceAllowed", sec.cancel_replace_allowed);
jr.read("isSpotTradingAllowed", sec.is_spot_trading_allowed);
jr.read("isMarginTradingAllowed", sec.is_margin_trading_allowed);
// jr.read("permissions", sec.permissions);
// jr.read("filters", sec.filter_tuple);
}
void to_json(nlohmann::json &j, const CoinbaseSpot &spot) {
j = nlohmann::json{
{"symbol", spot.symbol},
// {"status", spot.status},
{"baseAsset", spot.base_asset},
{"baseAssetPrecision", spot.base_asset_precision},
// {"quoteAsset", spot.quote_asset},
{"quotePrecision", spot.quote_precision},
{"quoteAssetPrecision", spot.quote_asset_precision},
// {"orderTypes", spot.order_types},
{"icebergAllowed", spot.iceberg_allowed},
{"ocoAllowed", spot.oco_allowed},
{"quoteOrderQtyMarketAllowed", spot.quote_order_qty_market_allowed},
{"allowTrailingStop", spot.allow_trailing_stop},
{"cancelReplaceAllowed", spot.cancel_replace_allowed},
{"isSpotTradingAllowed", spot.is_spot_trading_allowed},
{"isMarginTradingAllowed", spot.is_margin_trading_allowed},
// {"permissions", spot.permissions},
// {"filterTuple", spot.filter_tuple}
};
}
std::ostream &operator<<(std::ostream &out, const CoinbaseSpot &spot) noexcept {
out << spot.symbol;
return out;
}
} // namespace bts::md::coinbase
#endif
+62
View File
@@ -0,0 +1,62 @@
#pragma once
#ifdef SPARK_ENABLE_CCAPI
#include "lib/api/domain/Venue.h"
// #include "lib/md/binance/BinanceContractType.h"
// #include "lib/md/binance/BinanceFilterTuple.h"
// #include "lib/md/binance/BinanceInstrumentStatus.h"
// #include "lib/md/binance/BinanceOrderType.h"
// #include "lib/md/binance/BinancePermission.h"
// #include "lib/md/binance/BinanceQuoteAsset.h"
#include <map>
#include <nlohmann/json.hpp>
#include <ostream>
#include <string>
#include <vector>
namespace bts::domain {
class Security;
}
namespace ccapi {
class Message;
}
namespace bts::md::coinbase {
/**
* @brief Represents a spot security at Binance
*/
class CoinbaseSpot {
public:
std::string symbol;
// BinanceInstrumentStatus status;
std::string base_asset;
double base_asset_precision;
// BinanceQuoteAsset quote_asset;
double quote_precision;
double quote_asset_precision;
// std::vector<BinanceOrderType> order_types;
bool iceberg_allowed;
bool oco_allowed;
bool quote_order_qty_market_allowed;
bool allow_trailing_stop;
bool cancel_replace_allowed;
bool is_spot_trading_allowed;
bool is_margin_trading_allowed;
// std::vector<BinancePermission> permissions;
// BinanceFilterTuple filter_tuple;
static void parse(
const ccapi::Message &msg,
std::vector<domain::Security> &securities,
api::domain::Venue venue = api::domain::Venue::VENUE_COINBASE);
};
void from_json(const nlohmann::json &j, CoinbaseSpot &spot);
void to_json(nlohmann::json &j, const CoinbaseSpot &spot);
std::ostream &operator<<(std::ostream &out, const CoinbaseSpot &spot) noexcept;
} // namespace bts::md::coinbase
#endif