FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
fix: market info tab now works for spot and perp symbols
tilakpatel22632b3cc741cf
diff --git a/fincept-cpp/scripts/exchange/fetch_funding_rate.py b/fincept-cpp/scripts/exchange/fetch_funding_rate.pyindex 1bc0d5246..c3f390efb 100644--- a/fincept-cpp/scripts/exchange/fetch_funding_rate.py+++ b/fincept-cpp/scripts/exchange/fetch_funding_rate.py@@ -1,34 +1,76 @@"""Fetch current funding rate for a perpetual futures symbol.+Works with both spot symbols (BTC/USDT) and perp symbols (BTC/USDT:USDT).+For spot symbols on exchanges that use unified format, tries the perp equivalent.+Usage: python fetch_funding_rate.py <exchange_id> <symbol>"""import sys-from exchange_client import make_exchange, output_success, output_error, run_with_error_handling+from exchange_client import make_exchange, output_success, run_with_error_handling+++def to_perp_symbol(symbol: str) -> str:+ """Convert spot symbol to linear perp equivalent if needed.+ BTC/USDT -> BTC/USDT:USDT (Binance unified format)+ """+ if ":" not in symbol and "/" in symbol:+ quote = symbol.split("/")[1]+ return f"{symbol}:{quote}"+ return symbol+@run_with_error_handlingdef main():if len(sys.argv) < 3:- output_error("Usage: fetch_funding_rate.py <exchange_id> <symbol>", "INVALID_ARGS")+ output_success({+ "symbol": "",+ "funding_rate": None,+ "funding_timestamp": None,+ "next_funding_timestamp": None,+ "mark_price": None,+ "index_price": None,+ })+ returnexchange_id = sys.argv[1]symbol = sys.argv[2]exchange = make_exchange(exchange_id)- exchange.options["defaultType"] = "swap"+ exchange.load_markets()++ # Try perp symbol first, fall back to original+ perp_symbol = to_perp_symbol(symbol)+ tried = []++ for sym in ([perp_symbol, symbol] if perp_symbol != symbol else [symbol]):+ tried.append(sym)+ try:+ # Use swap market type for perp symbols+ exchange.options["defaultType"] = "swap"+ rate = exchange.fetch_funding_rate(sym)+ output_success({+ "symbol": sym,+ "funding_rate": rate.get("fundingRate"),+ "funding_timestamp": rate.get("fundingTimestamp"),+ "next_funding_timestamp": rate.get("nextFundingTimestamp"),+ "mark_price": rate.get("markPrice"),+ "index_price": rate.get("indexPrice"),+ "interest_rate": rate.get("interestRate"),+ })+ return+ except Exception:+ continue++ # Not available for this symbol/exchange (common for spot-only exchanges)+ output_success({+ "symbol": symbol,+ "funding_rate": None,+ "funding_timestamp": None,+ "next_funding_timestamp": None,+ "mark_price": None,+ "index_price": None,+ })- try:- rate = exchange.fetch_funding_rate(symbol)- output_success({- "symbol": symbol,- "funding_rate": rate.get("fundingRate"),- "funding_timestamp": rate.get("fundingTimestamp"),- "next_funding_timestamp": rate.get("nextFundingTimestamp"),- "mark_price": rate.get("markPrice"),- "index_price": rate.get("indexPrice"),- "interest_rate": rate.get("interestRate"),- })- except Exception as e:- output_error(f"Funding rate not available: {e}", "NOT_SUPPORTED")if __name__ == "__main__":main()diff --git a/fincept-cpp/scripts/exchange/fetch_open_interest.py b/fincept-cpp/scripts/exchange/fetch_open_interest.pyindex 922663e0d..1a4448cf0 100644--- a/fincept-cpp/scripts/exchange/fetch_open_interest.py+++ b/fincept-cpp/scripts/exchange/fetch_open_interest.py@@ -1,31 +1,57 @@"""Fetch open interest for a perpetual futures symbol.+Works with both spot symbols (BTC/USDT) and perp symbols (BTC/USDT:USDT).+Usage: python fetch_open_interest.py <exchange_id> <symbol>"""import sys-from exchange_client import make_exchange, output_success, output_error, run_with_error_handling+from exchange_client import make_exchange, output_success, run_with_error_handling+++def to_perp_symbol(symbol: str) -> str:+ """Convert spot symbol to linear perp equivalent if needed."""+ if ":" not in symbol and "/" in symbol:+ quote = symbol.split("/")[1]+ return f"{symbol}:{quote}"+ return symbol+@run_with_error_handlingdef main():if len(sys.argv) < 3:- output_error("Usage: fetch_open_interest.py <exchange_id> <symbol>", "INVALID_ARGS")+ output_success({"symbol": "", "open_interest": None, "open_interest_value": None, "timestamp": None})+ returnexchange_id = sys.argv[1]symbol = sys.argv[2]exchange = make_exchange(exchange_id)- exchange.options["defaultType"] = "swap"-- try:- oi = exchange.fetch_open_interest(symbol)- output_success({- "symbol": symbol,- "open_interest": oi.get("openInterestAmount"),- "open_interest_value": oi.get("openInterestValue"),- "timestamp": oi.get("timestamp"),- })- except Exception as e:- output_error(f"Open interest not available: {e}", "NOT_SUPPORTED")+ exchange.load_markets()++ perp_symbol = to_perp_symbol(symbol)++ for sym in ([perp_symbol, symbol] if perp_symbol != symbol else [symbol]):+ try:+ exchange.options["defaultType"] = "swap"+ oi = exchange.fetch_open_interest(sym)+ output_success({+ "symbol": sym,+ "open_interest": oi.get("openInterestAmount"),+ "open_interest_value": oi.get("openInterestValue"),+ "timestamp": oi.get("timestamp"),+ })+ return+ except Exception:+ continue++ # Not available for this symbol/exchange+ output_success({+ "symbol": symbol,+ "open_interest": None,+ "open_interest_value": None,+ "timestamp": None,+ })+if __name__ == "__main__":main()diff --git a/fincept-cpp/src/screens/crypto_trading/crypto_trading_screen.cpp b/fincept-cpp/src/screens/crypto_trading/crypto_trading_screen.cppindex 02be3850d..2f43de4f9 100644--- a/fincept-cpp/src/screens/crypto_trading/crypto_trading_screen.cpp+++ b/fincept-cpp/src/screens/crypto_trading/crypto_trading_screen.cpp@@ -10,6 +10,7 @@#include <cmath>#include <ctime>#include <algorithm>+#include <chrono>#include <thread>namespace fincept::crypto {@@ -2002,51 +2003,92 @@ void CryptoTradingScreen::render_market_info_tab() {if (market_info_fetching_) {ImGui::TextColored(TEXT_DIM, "Loading market info...");} else {- ImGui::TextColored(TEXT_DIM, "No market info available (spot mode)");+ // Not yet fetched — show a trigger hint+ ImGui::TextColored(TEXT_DIM, "Fetching market info...");}return;}ImGui::Columns(2, "##mktinfo_cols", false);+ // --- Fees (always available for any symbol) ---+ ImGui::TextColored(TEXT_DIM, "Maker Fee");+ if (market_info_.maker_fee > 0.0) {+ std::snprintf(buf, sizeof(buf), "%.4f%%", market_info_.maker_fee * 100.0);+ ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ } else {+ ImGui::TextColored(TEXT_DIM, "N/A");+ }+ ImGui::Spacing();++ ImGui::TextColored(TEXT_DIM, "Taker Fee");+ if (market_info_.taker_fee > 0.0) {+ std::snprintf(buf, sizeof(buf), "%.4f%%", market_info_.taker_fee * 100.0);+ ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ } else {+ ImGui::TextColored(TEXT_DIM, "N/A");+ }+ ImGui::Spacing();++ // --- Perp-only fields (show N/A for spot) ---ImGui::TextColored(TEXT_DIM, "Funding Rate");- std::snprintf(buf, sizeof(buf), "%.4f%%", market_info_.funding_rate * 100);- ImVec4 fr_col = market_info_.funding_rate >= 0 ? MARKET_GREEN : MARKET_RED;- ImGui::TextColored(fr_col, "%s", buf);+ if (market_info_.funding_rate != 0.0) {+ std::snprintf(buf, sizeof(buf), "%.4f%%", market_info_.funding_rate * 100.0);+ ImVec4 fr_col = market_info_.funding_rate >= 0 ? MARKET_GREEN : MARKET_RED;+ ImGui::TextColored(fr_col, "%s", buf);+ } else {+ ImGui::TextColored(TEXT_DIM, "N/A (spot)");+ }ImGui::Spacing();ImGui::TextColored(TEXT_DIM, "Mark Price");- std::snprintf(buf, sizeof(buf), "%.2f", market_info_.mark_price);- ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ if (market_info_.mark_price > 0.0) {+ std::snprintf(buf, sizeof(buf), "%.4f", market_info_.mark_price);+ ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ } else {+ ImGui::TextColored(TEXT_DIM, "N/A (spot)");+ }ImGui::Spacing();ImGui::TextColored(TEXT_DIM, "Index Price");- std::snprintf(buf, sizeof(buf), "%.2f", market_info_.index_price);- ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ if (market_info_.index_price > 0.0) {+ std::snprintf(buf, sizeof(buf), "%.4f", market_info_.index_price);+ ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ } else {+ ImGui::TextColored(TEXT_DIM, "N/A (spot)");+ }ImGui::NextColumn();ImGui::TextColored(TEXT_DIM, "Open Interest");- std::snprintf(buf, sizeof(buf), "%.2f", market_info_.open_interest);- ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ if (market_info_.open_interest > 0.0) {+ std::snprintf(buf, sizeof(buf), "%.2f", market_info_.open_interest);+ ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ } else {+ ImGui::TextColored(TEXT_DIM, "N/A (spot)");+ }ImGui::Spacing();ImGui::TextColored(TEXT_DIM, "OI Value (USD)");- double oi_val = market_info_.open_interest_value;- if (oi_val > 1e9) std::snprintf(buf, sizeof(buf), "%.2fB", oi_val / 1e9);- else if (oi_val > 1e6) std::snprintf(buf, sizeof(buf), "%.2fM", oi_val / 1e6);- else std::snprintf(buf, sizeof(buf), "%.0f", oi_val);- ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ if (market_info_.open_interest_value > 0.0) {+ const double oi_val = market_info_.open_interest_value;+ if (oi_val > 1e9) std::snprintf(buf, sizeof(buf), "%.2fB", oi_val / 1e9);+ else if (oi_val > 1e6) std::snprintf(buf, sizeof(buf), "%.2fM", oi_val / 1e6);+ else std::snprintf(buf, sizeof(buf), "%.0f", oi_val);+ ImGui::TextColored(TEXT_PRIMARY, "%s", buf);+ } else {+ ImGui::TextColored(TEXT_DIM, "N/A (spot)");+ }ImGui::Spacing();if (market_info_.next_funding_time > 0) {ImGui::TextColored(TEXT_DIM, "Next Funding");- int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(+ const int64_t now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();- int64_t diff_s = (market_info_.next_funding_time - now_ms) / 1000;+ const int64_t diff_s = (market_info_.next_funding_time - now_ms) / 1000;if (diff_s > 0) {- int h = (int)(diff_s / 3600);- int m = (int)((diff_s % 3600) / 60);+ const int h = static_cast<int>(diff_s / 3600);+ const int m = static_cast<int>((diff_s % 3600) / 60);std::snprintf(buf, sizeof(buf), "%dh %dm", h, m);} else {std::snprintf(buf, sizeof(buf), "Now");@@ -2055,6 +2097,10 @@ void CryptoTradingScreen::render_market_info_tab() {}ImGui::Columns(1);++ // Refresh hint+ ImGui::Spacing();+ ImGui::TextColored(TEXT_DIM, "(refreshes every %.0fs)", MARKET_INFO_INTERVAL);}void CryptoTradingScreen::async_fetch_market_info() {@@ -2063,21 +2109,40 @@ void CryptoTradingScreen::async_fetch_market_info() {const std::string sym = selected_symbol_;launch_async(std::thread([this, sym]() {+ auto& svc = trading::ExchangeService::instance();++ // Fetch funding rate + OI (perp only — scripts return nulls gracefully for spot)+ const auto fr = svc.fetch_funding_rate(sym);+ const auto oi = svc.fetch_open_interest(sym);++ // Fetch trading fees — always available for any symbol on any exchange.+ // fetch_trading_fees.py with a symbol arg returns: {"symbol":..., "maker":..., "taker":...}+ double maker_fee = 0.0;+ double taker_fee = 0.0;try {- auto& svc = trading::ExchangeService::instance();- auto fr = svc.fetch_funding_rate(sym);- auto oi = svc.fetch_open_interest(sym);+ const auto fees_j = svc.fetch_trading_fees(sym);+ if (fees_j.contains("data")) {+ const auto& d = fees_j["data"];+ if (d.contains("maker") && !d["maker"].is_null())+ maker_fee = d["maker"].get<double>();+ if (d.contains("taker") && !d["taker"].is_null())+ taker_fee = d["taker"].get<double>();+ }+ } catch (const std::exception& e) {+ LOG_DEBUG(TAG, "Trading fees fetch failed: %s", e.what());+ }+ {std::lock_guard<std::mutex> lock(data_mutex_);- market_info_.funding_rate = fr.funding_rate;- market_info_.mark_price = fr.mark_price;- market_info_.index_price = fr.index_price;- market_info_.next_funding_time = fr.next_funding_timestamp;- market_info_.open_interest = oi.open_interest;+ market_info_.funding_rate = fr.funding_rate;+ market_info_.mark_price = fr.mark_price;+ market_info_.index_price = fr.index_price;+ market_info_.next_funding_time = fr.next_funding_timestamp;+ market_info_.open_interest = oi.open_interest;market_info_.open_interest_value = oi.open_interest_value;- market_info_.has_data = true;- } catch (const std::exception& e) {- LOG_DEBUG("CryptoTrading", "Market info fetch failed: %s", e.what());+ market_info_.maker_fee = maker_fee;+ market_info_.taker_fee = taker_fee;+ market_info_.has_data = true; // always mark ready after fetch attempt}market_info_fetching_ = false;}));