OpenAlgo MQL5 Functions
Below is the API documentation for the OpenAlgoAPI functions provided in your MQL5 header file:
OpenAlgoAPI Function Documentation
All six functions are void. They print the request and the OpenAlgo response to the Experts log rather than returning a value, so read the log (or the OpenAlgo order book) to confirm what happened.
Each function takes apiUrlParam as the base URL of your OpenAlgo instance, for example http://127.0.0.1:5000, and appends the endpoint path itself.
PlaceOrder
Places an order with the specified parameters by sending a POST request to /api/v1/placeorder.
void PlaceOrder(string actionParam,
int quantityParam,
string apiUrlParam,
string apiKeyParam,
string strategyParam,
string symbolParam,
Exchanges exchangeParam,
ProductTypes productParam,
PriceTypes priceTypeParam,
double priceParam = 0,
double triggerPriceParam = 0,
int disclosedQuantityParam = 0)Parameters:
actionParam: The trade action, either "BUY" or "SELL".quantityParam: The quantity of the asset to trade, in units and not lots.apiUrlParam: The base URL of the OpenAlgo API.apiKeyParam: The OpenAlgo API key, generated on the API Key page of the dashboard. This is not your broker's API key.strategyParam: The name of the trading strategy. OpenAlgo records it against every order.symbolParam: The OpenAlgo symbol for the instrument being traded.exchangeParam: AnExchangesenum value (NSE, NFO, CDS, BSE, BFO, BCD, MCX, NCDEX).productParam: AProductTypesenum value (CNC, NRML, MIS).priceTypeParam: APriceTypesenum value (MARKET, LIMIT, SL, SLM). SLM is sent asSL-M.priceParam(optional): The price at which to place the order. Required for LIMIT and SL.triggerPriceParam(optional): The trigger price for SL and SL-M orders.disclosedQuantityParam(optional): The disclosed quantity of the order.
The three optional numeric parameters are only written into the JSON body when they are greater than zero, so leaving them at their defaults keeps the payload minimal.
Usage:
PlaceSmartOrder
Places an order that considers the current open position size and matches the position size given in the position book. Sends a POST request to /api/v1/placesmartorder.
Parameters:
Inherits all parameters from
PlaceOrder.positionSizeParam: The target position size. It sits third, immediately afterquantityParam.
OpenAlgo compares positionSizeParam with your live open position for that symbol, exchange and product, then places only the difference. A target of 0 flattens the position, and in that case OpenAlgo works out the direction itself, so actionParam and quantityParam are ignored. If the position already matches the target, no order goes to the broker.
Usage:
ModifyOrder
Modifies an existing order with new parameters. Sends a POST request to /api/v1/modifyorder.
Parameters:
orderidParam: The ID of the order to be modified, as returned by OpenAlgo when the order was placed.priceParam: Mandatory here, unlike inPlaceOrder. It sits fourth, right afterquantityParam.The remaining parameters match
PlaceOrder.
Note the tail of the argument list: in ModifyOrder the optional disclosedQuantityParam comes BEFORE triggerPriceParam, which is the reverse of PlaceOrder and PlaceSmartOrder. /api/v1/modifyorder requires every field, so this function always sends price, quantity, disclosed quantity and trigger price, even when they are zero.
Usage:
CancelOrder
Cancels an existing order. Sends a POST request to /api/v1/cancelorder.
Parameters:
orderidParam: The ID of the order to be canceled.apiUrlParam: The base URL of the OpenAlgo API.apiKeyParam: The OpenAlgo API key.strategyParam: The name of the trading strategy.
Usage:
ClosePosition
Closes all open positions associated with a given strategy. Sends a POST request to /api/v1/closeposition.
Parameters:
apiUrlParam: The base URL of the OpenAlgo API.apiKeyParam: The OpenAlgo API key.strategyParam: The name of the trading strategy to close positions for.
Usage:
CancelAllOrders
Cancels all orders associated with a given strategy. Sends a POST request to /api/v1/cancelallorder.
Parameters:
apiUrlParam: The base URL of the OpenAlgo API.apiKeyParam: The OpenAlgo API key.strategyParam: The name of the trading strategy to cancel orders for.
Usage:
Notes
Replace placeholder values like
"your_api_key"and"http://127.0.0.1:5000"with the actual key and URL for your setup.symbolParamis the OpenAlgo symbol, not the MetaTrader symbol. Equities carry no-EQsuffix, so useSBIN, notSBIN-EQ.The transport is
wininet.dll, so Allow DLL imports must be enabled for the EA. MQL5'sWebRequest()allowlist is not involved.OpenAlgo rate limits order placement to 10 requests per second. Guard your EA against firing on every tick.
This library covers order management only. Market data, the order book, positions and holdings are reached by calling the OpenAlgo REST API directly.
Last updated