Add timestamp to TickerPrice
It's almost always 0, it's only filled when bitcoinity replies with a "trade" data, other jsons don't give any time stamps
This commit is contained in:
parent
1aed72ded4
commit
b3b77d5cad
5 changed files with 43 additions and 8 deletions
|
@ -8,13 +8,13 @@ class App {
|
|||
|
||||
start() {
|
||||
var price_quick = Bitcoinity.ticker_quick()
|
||||
System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange)")
|
||||
System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange) timestamp %(price_quick.timestamp)")
|
||||
|
||||
var price_currency = Bitcoinity.ticker_currency(_currency)
|
||||
System.print("Ticker (%(_currency)): %(price_currency.price) %(price_currency.currency) on %(price_currency.exchange)")
|
||||
System.print("Ticker (%(_currency)): %(price_currency.price) %(price_currency.currency) on %(price_currency.exchange) timestamp %(price_currency.timestamp)")
|
||||
|
||||
var price = Bitcoinity.ticker(_exchange, _currency)
|
||||
System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange)")
|
||||
System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange) timestamp %(price.timestamp)")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include <simdjson.h>
|
||||
#include <string_view>
|
||||
#include <cstdint>
|
||||
|
||||
namespace duck {
|
||||
namespace detail {
|
||||
|
@ -76,6 +77,11 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
template <typename... Keys>
|
||||
std::optional<std::uint64_t> get_uint64 (Keys&&... keys) const {
|
||||
return this->get<std::uint64_t>(std::forward<Keys>(keys)...);
|
||||
}
|
||||
|
||||
template <typename... Keys>
|
||||
bool has_key (Keys&&... keys) const {
|
||||
return !!this->get<simdjson::ondemand::object>(std::forward<Keys>(keys)...);
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include <wrenpp/vm_fun.hpp>
|
||||
#include <wrenpp/callback_manager.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
namespace duck {
|
||||
|
||||
|
@ -75,8 +76,6 @@ std::string make_user_agent_string() {
|
|||
} //unnamed namespace
|
||||
|
||||
TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view currency, BitcoinityMatchType match_type) {
|
||||
static const TickerPrice error_price {0.0, "", ""};
|
||||
|
||||
duck::BitcoinityReader bitcoinity{
|
||||
make_user_agent_string(),
|
||||
exchange,
|
||||
|
@ -90,6 +89,7 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view
|
|||
// {"event":"new_msg","payload":{"data":{"currency":"USD","exchange_name":"bitfinex","trade":{"amount":0.0486,"date":1652471682,"exchange_name":"bitfinex","price":30072}}},"ref":null,"topic":"webs:markets_bitfinex_USD"}
|
||||
|
||||
double price_out;
|
||||
std::uint64_t timestamp_out = 0;
|
||||
std::optional<double> price;
|
||||
auto msg = bitcoinity.read();
|
||||
|
||||
|
@ -106,6 +106,10 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view
|
|||
continue; //keep trying, sometimes I get a message like:
|
||||
//{"event":"new_msg","payload":{"data":{"connected_count":1355}},"ref":null,"topic":"webs:markets"}
|
||||
|
||||
auto timestamp = msg.payload.get_uint64("data", "trade", "date");
|
||||
if (timestamp)
|
||||
timestamp_out = *timestamp;
|
||||
|
||||
std::string_view currency_out = msg.payload.get_string_or_empty("data", "currency");
|
||||
std::string_view exchange_out = msg.payload.get_string_or_empty("data", "exchange_name");
|
||||
switch (match_type) {
|
||||
|
@ -125,7 +129,12 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view
|
|||
break;
|
||||
}
|
||||
|
||||
return {price_out, std::string(currency_out), std::string(exchange_out)};
|
||||
return TickerPrice{
|
||||
price_out,
|
||||
timestamp_out,
|
||||
std::string(currency_out),
|
||||
std::string(exchange_out)
|
||||
};
|
||||
} while (true);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,14 +27,21 @@ R"wren(foreign class TickerPrice {
|
|||
construct new() { }
|
||||
|
||||
foreign price
|
||||
foreign timestamp
|
||||
foreign currency
|
||||
foreign exchange
|
||||
foreign is_valid
|
||||
}
|
||||
)wren"};
|
||||
|
||||
TickerPrice::TickerPrice (double price, std::string&& currency, std::string&& exchange) :
|
||||
TickerPrice::TickerPrice (
|
||||
double price,
|
||||
std::uint64_t timestamp,
|
||||
std::string&& currency,
|
||||
std::string&& exchange
|
||||
) :
|
||||
m_price(price),
|
||||
m_timestamp(timestamp),
|
||||
m_currency(std::move(currency)),
|
||||
m_exchange(std::move(exchange))
|
||||
{}
|
||||
|
@ -43,6 +50,10 @@ void TickerPrice::set_price (double price) {
|
|||
m_price = price;
|
||||
}
|
||||
|
||||
void TickerPrice::set_timestamp(std::uint64_t timestamp) {
|
||||
m_timestamp = timestamp;
|
||||
}
|
||||
|
||||
void TickerPrice::set_currency (std::string&& currency) {
|
||||
m_currency = std::move(currency);
|
||||
}
|
||||
|
@ -55,6 +66,10 @@ double TickerPrice::price() const noexcept {
|
|||
return m_price;
|
||||
}
|
||||
|
||||
std::uint64_t TickerPrice::timestamp() const noexcept {
|
||||
return m_timestamp;
|
||||
}
|
||||
|
||||
const char* TickerPrice::currency() const noexcept {
|
||||
return m_currency.c_str();
|
||||
}
|
||||
|
@ -70,6 +85,7 @@ bool TickerPrice::is_valid() const noexcept {
|
|||
void register_ticker_price_to_wren(wren::VM& vm) {
|
||||
vm.callback_manager()
|
||||
.add_callback(false, "ticker_price", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>())
|
||||
.add_callback(false, "ticker_price", "TickerPrice", "timestamp", wren::make_method_bindable<&TickerPrice::timestamp>())
|
||||
.add_callback(false, "ticker_price", "TickerPrice", "currency", wren::make_method_bindable<&TickerPrice::currency>())
|
||||
.add_callback(false, "ticker_price", "TickerPrice", "is_valid", wren::make_method_bindable<&TickerPrice::is_valid>())
|
||||
.add_callback(false, "ticker_price", "TickerPrice", "exchange", wren::make_method_bindable<&TickerPrice::exchange>());
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
|
||||
namespace wren {
|
||||
class VM;
|
||||
|
@ -27,18 +28,21 @@ namespace duck {
|
|||
class TickerPrice {
|
||||
public:
|
||||
TickerPrice() = default;
|
||||
TickerPrice (double price, std::string&& currency, std::string&& exchange);
|
||||
TickerPrice (double price, std::uint64_t timestamp, std::string&& currency, std::string&& exchange);
|
||||
|
||||
void set_price (double price);
|
||||
void set_timestamp (std::uint64_t timestamp);
|
||||
void set_currency (std::string&& currency);
|
||||
void set_exchange (std::string&& exchange);
|
||||
double price() const noexcept;
|
||||
std::uint64_t timestamp() const noexcept;
|
||||
const char* currency() const noexcept;
|
||||
const char* exchange() const noexcept;
|
||||
bool is_valid() const noexcept;
|
||||
|
||||
private:
|
||||
double m_price{};
|
||||
std::uint64_t m_timestamp{};
|
||||
std::string m_currency;
|
||||
std::string m_exchange;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue