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
#pragma once
#include "trading/BrokerInterface.h"
#include "trading/adapter/BrokerEnumMap.h"
#include "trading/brokers/BrokerHttp.h"
#include <cmath>
namespace fincept::trading {
class AngelOneBroker : public IBroker {
public:
BrokerId id() const override { return BrokerId::AngelOne; }
const char* name() const override { return "AngelOne"; }
const char* base_url() const override { return "https://apiconnect.angelone.in"; }
const char* ws_adapter_name() const override { return "angelone"; }
BrokerProfile profile() const override {
return BrokerProfile{
.id = "angelone",
.display_name = "Angel One",
.region = "IN",
.currency = "INR",
.credential_fields =
{
{CredentialField::ClientCode, "CLIENT CODE", "Enter login/client ID...", false},
{CredentialField::ApiKey, "API KEY", "Enter API key...", false},
{CredentialField::ApiSecret, "MPIN", "Enter MPIN/PIN...", true},
{CredentialField::AuthCode, "TOTP SECRET", "Enter base32 TOTP secret...", false},
},
.exchanges = {"NSE", "BSE", "NFO", "MCX", "NCDEX"},
.product_types =
{
{"Intraday (MIS)", ProductType::Intraday},
{"Delivery (CNC)", ProductType::Delivery},
{"Margin (NRML)", ProductType::Margin},
},
.supports_intraday = true,
.supports_bracket_order = false,
.supports_cover_order = false,
.has_native_paper = false,
.default_paper_balance = 1000000.0,
.default_watchlist = {"HDFCBANK", "ICICIBANK", "SBIN", "TCS", "INFY", "RELIANCE", "TATASTEEL", "BHARTIARTL",
"ITC", "LT"},
.default_symbol = "RELIANCE",
.default_exchange = "NSE",
.brokerage_info = "\u20B920/order flat",
};
}
TokenExchangeResponse exchange_token(const QString& api_key, const QString& api_secret,
const QString& auth_code) override;
// Angel One JWTs expire ~2.5h; silent refresh replays the stored TOTP login.
bool supports_silent_refresh() const override { return true; }
TokenExchangeResponse refresh_session(const BrokerCredentials& creds) override;
// SmartAPI producttype on positions/orders: DELIVERY, CARRYFORWARD (NRML),
// MARGIN (= MTF on Angel One — ao_enum_map sends MTF as "MARGIN"), INTRADAY.
ProductType product_from_wire(const QString& wire_product) const override;
// Decimal places a price for this instrument must be sent with so it is not
// rounded off the exchange tick grid (CDS/BCD currency derivatives tick in
// ₹0.0025 → 4 dp; equity/F&O ticks of ₹0.05/0.01 stay at 2 dp).
static int price_decimals_for(const QString& symbol, const QString& exchange);
static int decimals_for_tick(double tick) {
if (!(tick > 0.0) || !std::isfinite(tick))
return 2;
for (int d = 0; d <= 6; ++d) {
const double scaled = tick * std::pow(10.0, d);
if (std::fabs(scaled - std::round(scaled)) < 1e-6)
return d;
}
return 6;
}
OrderPlaceResponse place_order(const BrokerCredentials& creds, const UnifiedOrder& order) override;
ApiResponse<QJsonObject> modify_order(const BrokerCredentials& creds, const QString& order_id,
const QJsonObject& mods) override;
ApiResponse<QJsonObject> cancel_order(const BrokerCredentials& creds, const QString& order_id) override;
ApiResponse<QVector<BrokerOrderInfo>> get_orders(const BrokerCredentials& creds) override;
ApiResponse<QJsonObject> get_trade_book(const BrokerCredentials& creds) override;
ApiResponse<QVector<BrokerPosition>> get_positions(const BrokerCredentials& creds) override;
ApiResponse<QVector<BrokerHolding>> get_holdings(const BrokerCredentials& creds) override;
ApiResponse<BrokerFunds> get_funds(const BrokerCredentials& creds) override;
ApiResponse<QVector<BrokerQuote>> get_quotes(const BrokerCredentials& creds,
const QVector<QString>& symbols) override;
// --- Multi-quote & Market Depth ---
ApiResponse<QVector<BrokerQuote>> get_multi_quotes(const BrokerCredentials& creds,
const QVector<QPair<QString, QString>>& symbols) override;
ApiResponse<MarketDepth> get_market_depth(const BrokerCredentials& creds, const QString& symbol,
const QString& exchange) override;
ApiResponse<QVector<BrokerCandle>> get_history(const BrokerCredentials& creds, const QString& symbol,
const QString& resolution, const QString& from_date,
const QString& to_date) override;
ApiResponse<OrderMargin> get_order_margins(const BrokerCredentials& creds, const UnifiedOrder& order) override;
ApiResponse<BasketMargin> get_basket_margins(const BrokerCredentials& creds,
const QVector<UnifiedOrder>& orders) override;
static bool is_token_expired(const BrokerHttpResponse& resp);
static QString checked_error(const BrokerHttpResponse& resp, const QString& fallback);
/// Refresh the JWT pair using the stored refresh_token. On success the
/// updated tokens are returned in the result; caller persists them.
/// Endpoint: POST /rest/auth/angelbroking/jwt/v1/generateTokens.
TokenExchangeResponse refresh_token(const BrokerCredentials& creds);
/// Look up the numeric instrument token for a symbol+exchange.
/// Returns 0 if not found. Used by AngelOneWebSocket subscription.
static quint32 lookup_token_int(const QString& symbol, const QString& exchange);
protected:
QMap<QString, QString> auth_headers(const BrokerCredentials& creds) const override;
private:
static QString generate_totp(const QString& base32_secret);
static QString lookup_token(const QString& symbol, const QString& exchange);
static const BrokerEnumMap<QString>& ao_enum_map();
static QString ao_variety(OrderType t, bool amo);
};
} // namespace fincept::trading