Skip to content

Instantly share code, notes, and snippets.

@madmongo1
Last active November 23, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madmongo1/268bcf770c1f05f8c3a4e69b715a6882 to your computer and use it in GitHub Desktop.
Save madmongo1/268bcf770c1f05f8c3a4e69b715a6882 to your computer and use it in GitHub Desktop.
net::awaitable< void >
main_window::monitor_deribit_indecies(QMenu *menu)
{
struct index_entry
{
QAction *menu_action = nullptr;
bool
valid() const noexcept
{
return bool(menu_action);
}
};
std::unordered_map< std::string, index_entry > index_map;
auto indecies_feed = trade::deribit_index_price_names(entity_service_);
auto indecies_connection = co_await indecies_feed.subscribe();
auto stopped = false;
auto close_connection = close_signal_.connect([&] {
stopped = true;
indecies_connection.disconnect();
});
BOOST_SCOPE_EXIT_ALL(&) { close_connection.disconnect(); };
while (!stopped)
{
auto snap = co_await indecies_connection.consume();
if (snap.status() == trade::snapshot_status::good)
{
auto to_remove = keys_of(index_map);
for (auto &deribit_index_name : snap.names())
{
auto [base, term] = [&] {
auto term = std::string();
auto und = deribit_index_name.find('_');
auto base =
boost::to_upper_copy(deribit_index_name.substr(0, und));
if (und != std::string::npos)
term = boost::to_upper_copy(
deribit_index_name.substr(und + 1));
return std::make_tuple(std::move(base), std::move(term));
}();
auto index_name = fmt::format("{}/{}", base, term);
auto key = trade::ccy_pair {
.base = trade::currency(base),
.term = trade::currency(term)
};
to_remove.erase(index_name);
auto &entry = index_map[index_name];
if (!entry.valid())
{
entry.menu_action =
menu->addAction(QString::fromStdString(index_name));
QObject::connect(
entry.menu_action,
&QAction::triggered,
this,
[this, key] {
auto pred = [&](deribit::index_rate_widget *p) {
return p->key() == key;
};
auto gen = [&] {
return new deribit::index_rate_widget(
key, entity_service_);
};
activate_mdi_view(mdi_area_, pred, gen);
});
}
else
{
entry.menu_action->setEnabled(true);
}
}
for (auto &index_name : to_remove)
if (auto imap = index_map.find(index_name);
imap != index_map.end())
{
menu->removeAction(imap->second.menu_action);
index_map.erase(imap);
}
}
else
{
for (auto &entry : index_map)
{
entry.second.menu_action->setEnabled(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment