#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();
string SetStaticIP(string ip, string ip_flag);
string ModifyStaticIP(string ip, string ip_flag);
string GetStaticIP();
string ApplyStaticIP();
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);
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();
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();
string GetFundLimits();
string CalculateMargin(string exchange_segment, string transaction_type, string product_type, string security_id, long quantity, double price, double trigger_price);
string ManageKillSwitch(string kill_switch_status);
string ParseOrderId(string response);
string ParseOrderStatus(string response);
#import
input string InpApiUrl = "https://api.dhan.co/v2";
input string InpAccessToken = "PASTE_YOUR_ACCESS_TOKEN";
input string InpClientId = "PASTE_YOUR_CLIENT_ID";
input bool InpDebug = true;
input bool InpStaticIpEnabled = false;
input string InpStaticIp = "";
input string InpStaticIpFlag = "PRIMARY";
input string InpExchangeSegment = "NSE_EQ";
input string InpProductType = "INTRADAY";
input string InpSecurityId = "1333";
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 ===");
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));
Print("Sample finished.");
}