//+------------------------------------------------------------------+ //| Sample_Dhan.mq5 | //| Sample script exercising the DhanHQ v2 API bridge. | //| | //| BEFORE RUNNING: | //| 1. Compile Library_Dhan.mq5 into MQL5\Libraries\Library_Dhan.ex5 | //| 2. In MetaTrader 5: Tools -> Options -> Expert Advisors, | //| tick "Allow WebRequest for listed URL" and add: | //| https://api.dhan.co | //| 3. Generate a 24h Access Token from web.dhan.co (My Profile -> | //| Access DhanHQ APIs) and copy your Dhan Client ID. | //| Docs: https://docs.dhanhq.co/api/v2/guides/authentication | //| 4. Static IP whitelisting is MANDATORY for order placement/ | //| modification/cancellation per SEBI rules. Set InpStaticIpEnabled //| = true and provide your ISP static IP, or configure it once | //| from web.dhan.co and leave it disabled here. | //| Docs: https://docs.dhanhq.co/api/v2/authentication/set-ip | //+------------------------------------------------------------------+ #property copyright "Copyright 2026" #property link "https://docs.dhanhq.co" #property version "1.0" #property script_show_inputs struct DhanConfig { string api_url; string access_token; string client_id; bool static_ip_enabled; string static_ip; string static_ip_flag; bool debug; }; #import "..\Libraries\Library_Dhan.ex5" void Dhan_Init(DhanConfig &config); string GetProfile(); // Static IP string SetStaticIP(string ip, string ip_flag); string ModifyStaticIP(string ip, string ip_flag); string GetStaticIP(); string ApplyStaticIP(); // Orders string PlaceOrder(string transaction_type, string exchange_segment, string product_type, string order_type, string validity, string security_id, long quantity, double price, double trigger_price, long disclosed_quantity, bool after_market_order, string amo_time, string correlation_id); string OrderMarket(string transaction_type, string exchange_segment, string product_type, string security_id, long quantity); string OrderLimit(string transaction_type, string exchange_segment, string product_type, string security_id, long quantity, double price, string validity); string OrderStopLoss(string transaction_type, string exchange_segment, string product_type, string security_id, long quantity, double price, double trigger_price); string OrderStopLossMarket(string transaction_type, string exchange_segment, string product_type, string security_id, long quantity, double trigger_price); string ModifyOrder(string order_id, string order_type, string validity, long quantity, double price, double trigger_price, long disclosed_quantity, string leg_name); string CancelOrder(string order_id); string GetOrders(); string GetOrderById(string order_id); string GetOrderByCorrelationId(string correlation_id); string SliceOrder(string transaction_type, string exchange_segment, string product_type, string order_type, string validity, string security_id, long quantity, double price, double trigger_price, long disclosed_quantity, bool after_market_order, string amo_time, string correlation_id); string GetTrades(); string GetTradesByOrderId(string order_id); // Super orders string PlaceSuperOrder(string transaction_type, string exchange_segment, string product_type, string order_type, string security_id, long quantity, double price, double target_price, double stop_loss_price, double trailing_jump, string correlation_id); string ModifySuperOrder(string order_id, string order_type, string leg_name, long quantity, double price, double target_price, double stop_loss_price, double trailing_jump); string CancelSuperOrderLeg(string order_id, string order_leg); string GetSuperOrders(); // Portfolio string GetHoldings(); string GetPositions(); string ConvertPosition(string exchange_segment, string from_product_type, string to_product_type, string position_type, string security_id, long convert_qty); string ExitAllPositions(); // Funds string GetFundLimits(); string CalculateMargin(string exchange_segment, string transaction_type, string product_type, string security_id, long quantity, double price, double trigger_price); // Trader's control string ManageKillSwitch(string kill_switch_status); // Helpers string ParseOrderId(string response); string ParseOrderStatus(string response); #import //--- Connection / auth input string InpApiUrl = "https://api.dhan.co/v2"; input string InpAccessToken = "PASTE_YOUR_ACCESS_TOKEN"; // 24h JWT from web.dhan.co input string InpClientId = "PASTE_YOUR_CLIENT_ID"; // Dhan Client ID input bool InpDebug = true; //--- Static IP (mandatory for order placement/modify/cancel) input bool InpStaticIpEnabled = false; // enable/disable IP whitelisting input string InpStaticIp = ""; // your ISP static IPv4/IPv6 input string InpStaticIpFlag = "PRIMARY"; // PRIMARY or SECONDARY //--- Sample order parameters input string InpExchangeSegment = "NSE_EQ"; // e.g. NSE_EQ, NSE_FNO input string InpProductType = "INTRADAY"; // CNC, INTRADAY, MARGIN, MTF input string InpSecurityId = "1333"; // 1333 = HDFC Bank on NSE_EQ input long InpQuantity = 1; input double InpLimitPrice = 1500.0; //+------------------------------------------------------------------+ void OnStart() { DhanConfig cfg; cfg.api_url = InpApiUrl; cfg.access_token = InpAccessToken; cfg.client_id = InpClientId; cfg.static_ip_enabled = InpStaticIpEnabled; cfg.static_ip = InpStaticIp; cfg.static_ip_flag = InpStaticIpFlag; cfg.debug = InpDebug; Dhan_Init(cfg); Print("=== Authentication check ==="); Print("Profile: ", GetProfile()); Print("=== Static IP setup ==="); // ApplyStaticIP() is a no-op unless InpStaticIpEnabled = true. Print("ApplyStaticIP: ", ApplyStaticIP()); Print("Current IPs: ", GetStaticIP()); Print("=== Account / Funds ==="); Print("Fund limits: ", GetFundLimits()); Print("Positions: ", GetPositions()); Print("Holdings: ", GetHoldings()); Print("=== Order book / Trade book ==="); Print("Orders: ", GetOrders()); Print("Trades: ", GetTrades()); Print("=== Margin calculation ==="); Print("Margin: ", CalculateMargin(InpExchangeSegment, "BUY", InpProductType, InpSecurityId, InpQuantity, InpLimitPrice, 0)); // --------------------------------------------------------------- // The trading calls below are commented out for safety. Uncomment // to place REAL orders. Static IP whitelisting must be active. // --------------------------------------------------------------- // // string limitResp = OrderLimit("BUY", InpExchangeSegment, InpProductType, // InpSecurityId, InpQuantity, InpLimitPrice, "DAY"); // Print("Limit order: ", limitResp); // string orderId = ParseOrderId(limitResp); // Print("New order id: ", orderId, " status=", ParseOrderStatus(limitResp)); // // Print("Market order: ", OrderMarket("BUY", InpExchangeSegment, InpProductType, // InpSecurityId, InpQuantity)); // // Print("Stop-loss order: ", OrderStopLoss("SELL", InpExchangeSegment, InpProductType, // InpSecurityId, InpQuantity, InpLimitPrice * 0.95, InpLimitPrice * 0.96)); // // Print("Stop-loss market: ", OrderStopLossMarket("SELL", InpExchangeSegment, InpProductType, // InpSecurityId, InpQuantity, InpLimitPrice * 0.95)); // // if(StringLen(orderId) > 0) { // Print("Order by id: ", GetOrderById(orderId)); // Print("Modify order: ", ModifyOrder(orderId, "LIMIT", "DAY", InpQuantity, // InpLimitPrice * 1.01, 0, 0, "")); // Print("Trades for order: ", GetTradesByOrderId(orderId)); // Print("Cancel order: ", CancelOrder(orderId)); // } // // Print("Slice order: ", SliceOrder("BUY", "NSE_FNO", InpProductType, "LIMIT", "DAY", // InpSecurityId, 1800, InpLimitPrice, 0, 0, false, "", "")); // // Print("=== Super order (bracket) ==="); // string superResp = PlaceSuperOrder("BUY", InpExchangeSegment, InpProductType, "LIMIT", // InpSecurityId, InpQuantity, InpLimitPrice, // InpLimitPrice * 1.05, InpLimitPrice * 0.95, 1.0, ""); // Print("Super order: ", superResp); // string superId = ParseOrderId(superResp); // if(StringLen(superId) > 0) { // Print("Modify super (target leg): ", ModifySuperOrder(superId, "LIMIT", "TARGET_LEG", // 0, 0, InpLimitPrice * 1.06, 0, 0)); // Print("Cancel super target leg: ", CancelSuperOrderLeg(superId, "TARGET_LEG")); // } // Print("Super orders: ", GetSuperOrders()); // // Print("=== Portfolio management ==="); // Print("Convert position: ", ConvertPosition(InpExchangeSegment, "INTRADAY", "CNC", // "LONG", InpSecurityId, InpQuantity)); // Print("Exit all positions: ", ExitAllPositions()); // // Print("=== Trader's control ==="); // Print("Kill switch ACTIVATE: ", ManageKillSwitch("ACTIVATE")); Print("Sample finished."); } //+------------------------------------------------------------------+


