MetaTrader Bridge for Upstox API

It’s finally here, the much awaited feature ‘MetaTrader Bridge’. Now, our EtaTrader v1.0 supports MetaTrader Bridge. You can seamlessly connect your favorite technical analysis software ‘MetaTrader’ with your Upstox trading account using API.

Features:

Order Requests (Place/Modify/Cancel/Exit etc)
Order Details (Status, Filled Price, Filled Qty etc)
Symbol Details (Tick Size, Lot Size)
Position Details (Mtm, Net Positions, Avg Bought Price, Avg Sold Price etc)
Supports all exchanges
Supports Cover and OCO order
Based on Tcp-Ip Client-Server model

With MetaTrader Bridge, you can fully manage your orders and positions right from MetaTrader.

Getting Started with MetaTrader Bridge:

1. System Requirements
2. API subscription from Upstox
3. Installation of EtaTrader For Upstox API
4. Installation of MetaTrader Bridge
5. MetaTrader Initial Setup
6. List of Functions
7. Example Usage

1. System Requirements:

Windows 7 or above
.Net Framework 4.5.2 or higher
MS Office 2007 or above (Because EtaTrader includes RTD server)

2. API subscription:

API subscription from Upstox
https://developer.upstox.com/

3. Installation of EtaTrader:

Download and install EtaTrader For Upstox API from https://howutrade.in/downloads/. EtaTrader will receive order requests from MetaTrader through Bridge.

4. Installation of MetaTrader Bridge:

Bridge DLL is available in the downloaded zip file. Check for ‘Developers\Metatrader’ folder.

(a). If you are using MetaTrader-32bit then copy the ‘MTBridgeUpstoxNet.dll’ file inside the ’32-bit’ folder to

C:\Users\[USERNAME]\AppData\Roaming\MetaQuotes\Terminal\[HASH]\MQL5\Libraries

If you are using MetaTrader-64bit then copy the ‘MTBridgeUpstoxNet.dll’ file inside the ’64-bit’ folder to
C:\Users\[USERNAME]\AppData\Roaming\MetaQuotes\Terminal\[HASH]\MQL5\Libraries

(b). Copy the ‘UpstoxNetBridge.mqh’ file to
C:\Users\[USER]\AppData\Roaming\MetaQuotes\Terminal\[HASH]\MQL5\Include

This ‘UpstoxNetBridge.mqh’ file contains list of all functions supported by the bridge.

5. MetaTrader Initial Setup:

You need to enable ‘Allow DLL Imports’ in MetaTrader to call the functions present in the bridge DLL. (If not enabled already)
Menu --> Tools --> options --> Expert Advisors --> Allow DLL Imports

6. List of Functions:

Below is the list of functions supported by MetaTrader Bridge for Upstox API.

//ORDER DETAILS
string GetOrderCTag(string Exch, string TrdSym, string CTag, bool IsLive=true);
string GetOrderIds(string Exch, string TrdSym, string ProdType);
string GetOrderIdsBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
string GetLastOrderId(string Exch, string TrdSym, string ProdType, bool IsLive=true);

string GetChildOrders(string OrderId, bool IsLive=true);
string GetOrderDiscQty(string OrderId, bool IsLive=true);
string GetOrderExch(string OrderId, bool IsLive=true);
string GetOrderExchId(string OrderId, bool IsLive=true);
datetime GetOrderExchTime(string OrderId, bool IsLive=true);
string GetOrderExchToken(string OrderId, bool IsLive=true);
double GetOrderFilledPrice(string OrderId, bool IsLive=true);
int GetOrderFilledQty(string OrderId, bool IsLive=true);
string GetOrderInstToken(string OrderId, bool IsLive=true);
string GetOrderMessage(string OrderId, bool IsLive=true);
string GetOrderParentId(string OrderId, bool IsLive=true);
int GetOrderPendingQty(string OrderId, bool IsLive=true);
double GetOrderPrice(string OrderId, bool IsLive=true);
string GetOrderProdType(string OrderId, bool IsLive=true);
int GetOrderQty(string OrderId, bool IsLive=true);
string GetOrderStatus(string OrderId, bool IsLive=true);
string GetOrderTag(string OrderId, bool IsLive=true);
datetime GetOrderTime(string OrderId, bool IsLive=true);
string GetOrderTrans(string OrderId, bool IsLive=true);
string GetOrderTrdSym(string OrderId, bool IsLive=true);
double GetOrderTriggerPrice(string OrderId, bool IsLive=true);
string GetOrderType(string OrderId, bool IsLive=true);
datetime GetOrderValidDate(string OrderId, bool IsLive=true);
string GetOrderVariety(string OrderId, bool IsLive=true);

//POSITION DETAILS
double GetAvgBoughtPrice(string Exch, string TrdSym);
double GetAvgBoughtPriceBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
double GetAvgBoughtPriceProduct(string Exch, string TrdSym, string ProdType);
double GetAvgSoldPrice(string Exch, string TrdSym);
double GetAvgSoldPriceBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
double GetAvgSoldPriceProduct(string Exch, string TrdSym, string ProdType);
int GetBoughtQty(string Exch, string TrdSym);
int GetBoughtQtyBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
int GetBoughtQtyProduct(string Exch, string TrdSym, string ProdType);
int GetSoldQty(string Exch, string TrdSym);
int GetSoldQtyBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
int GetSoldQtyProduct(string Exch, string TrdSym, string ProdType);

double GetTotalMtm(string Exch);
double GetTotalMtmBridge(string StgyCode, string Source, bool IsLive);
double GetMtm(string Exch, string TrdSym);
double GetMtmBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
double GetMtmProduct(string Exch, string TrdSym, string ProdType);
int GetNetQty(string Exch, string TrdSym);
int GetNetQtyBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
int GetNetQtyProduct(string Exch, string TrdSym, string ProdType);

int GetTotalPositionsBridge(string StgyCode, string Source, bool IsLive);
string GetPositionsListBridge(string StgyCode, string Source, bool IsLive);
int GetClosedPositionsBridge(string StgyCode, string Source, bool IsLive);
int GetOpenPositionsBridge(string StgyCode, string Source, bool IsLive);
int GetEntryTradesBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
int GetLongTradesBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
int GetShortTradesBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);
int GetTotalTradesBridge(string Exch, string TrdSym, string StgyCode, string Source, bool IsLive);

//SYMBOL DETAILS
int GetLotSize(string Exch, string TrdSym);
double GetTickSize(string Exch, string TrdSym);

//BASIC DETAILS
string GetConnectedClient();
bool TestConnection();

//ORDER REQUESTS
string CancelAmoBridge(string OrderId, bool IsAsync=false);
string CancelOCOMainBridge(string OrderId, bool IsAsync=false);
string CancelCOMainBridge(string OrderId, bool IsAsync=false);
string CancelSimpleOrderBridge(string OrderId, bool IsAsync=false);
string ExitOCOBridge(string OrderId, bool IsAsync=false, string StgyCode="ZZ", bool IsLive=true);
string ExitCOBridge(string OrderId, bool IsAsync=false, string StgyCode="ZZ", bool IsLive=true);
string ModifyOCOMainBridge(string OrderId, int Qty=0, double LmtPrice=0, double TrgPrice=0, bool IsAsync=false);
string ModifyOCOSlBridge(string OrderId, double TrgPrice, bool IsAsync=false);
string ModifyOCOTgtBridge(string OrderId, double LmtPrice, bool IsAsync=false);
string ModifyCOSlBridge(string OrderId, double TrgPrice, bool IsAsync=false);
string ModifySimpleOrderBridge(string OrderId, string OrdType="", int Qty=0, double LmtPrice=0, double TrgPrice=0, bool IsAsync=false, int DiscQty=0);
string PlaceAmoBridge(string Exch, string TrdSym, string SignalType, string OrdType, int Qty, string ProdType, double LmtPrice=0, double TrgPrice=0, string Validity="DAY", string CTag="LAST", int DiscQty=0, bool IsLive=true, string StgyCode="ZZ", bool IsAsync=false, string TagAPI="");
string PlaceOCOBridge(string Exch, string TrdSym, string SignalType, int Qty, double LmtPrice, double SqOffValue, double StoplossValue, int TrailTicks=0, string OrdType="L", double TrgPrice=0, string CTag="LAST", bool IsLive=true, string StgyCode="ZZ", bool IsAsync=false, string TagAPI="");
string PlaceCOBridge(string Exch, string TrdSym, string SignalType, int Qty, double StoplossPrice, string OrdType="M", double LmtPrice=0, string CTag="LAST", bool IsLive=true, string StgyCode="ZZ", bool IsAsync=false, string TagAPI="");
string PlaceSimpleOrderBridge(string Exch, string TrdSym, string SignalType, string OrdType, int Qty, string ProdType, double LmtPrice=0, double TrgPrice=0, string Validity="DAY", string CTag="LAST", int DiscQty=0, bool IsLive=true, string StgyCode="ZZ", bool IsAsync=false, string TagAPI="");

Remarks:

Source : “XLBRIDGE”, “AMIBRIDGE”, “MTBRIDGE”, “WINBRIDGE”
Exch : “NSE_EQ”, “NSE_FO”, “BSE_EQ”, “NCD_FO”, “BCD_FO”, “MCX_FO”
OrdType : “M”, “L”, “SL”, “SL-M”
ProdType : “I”, “D”
SignalType : “BUY”, “SELL”, “SHORT”, “COVER”
validity : “DAY”, “IOC”
StgyCode : Must be 2 character, alpha-numeric, unique string to identify a strategy.
CTag : 3-15 chracters, alphanumeric, unique string to identify a order
IsLive : True to place order in Live Market or False for Paper Trading
TagAPI : 3-8 characters, alpha-numeric.

7. Example Usage:

In your MQL scripts (Strategy and Expert Advisor), you need to include the Bridge functions file ‘UpstoxNetBridge.mqh’ at the top.

#include <UpstoxNetBridge.mqh>

Once included, you can call all the bridge functions present in the ‘UpstoxNetBridge.mqh’

Simple Orders

//get latest prices
double Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);

//variables for Order
string Exch="NSE_EQ";
string TrdSym="AXISBANK"; //can be generated dynamically from Symbol()
int Qty=156; //Workout this as required
string OrdType="L"; //Change to M if want to place in Market
string ProdType="I"; //Intraday
string Validity="DAY";
string StgyCode="T1";
bool IsLive=true; //Live Trade

//Place a BUY order (Long Entry)
string BuyOrderId=PlaceSimpleOrderBridge(Exch,TrdSym,"BUY",OrdType,Qty,ProdType,Ask,0,Validity,"LONGENTRY1",0,IsLive,StgyCode,false);

//Place a SELL order (Long Exit: Target/Stop Hit/SqOff Time/Reverse Signal)
string SellOrderId=PlaceSimpleOrderBridge(Exch,TrdSym,"SELL",OrdType,Qty,ProdType,Bid,0,Validity,"LONGEXIT1",0,IsLive,StgyCode,false);

//Place a SHORT order (Short Entry)
string ShortOrderId=PlaceSimpleOrderBridge(Exch,TrdSym,"SHORT",OrdType,Qty,ProdType,Bid,0,Validity,"SHORTENTRY1",0,IsLive,StgyCode,false);

//Place a COVER order (Short Exit: Target/Stop Hit/SqOff Time/Reverse Signal)
string CoverOrderId=PlaceSimpleOrderBridge(Exch,TrdSym,"COVER",OrdType,Qty,ProdType,Ask,0,Validity,"SHORTEXIT1",0,IsLive,StgyCode,false);

Cover Orders

//get latest prices
double Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);

//variables for Order
string Exch="NSE_EQ";
string TrdSym="AXISBANK";//can be generated dynamically from Symbol()
int Qty=156;//Workout this as required
string OrdType="M";
string StgyCode="T1";
bool IsLive=true;//Live Trade
double StopPct=1.25;//Stoploss Percentage
double BuySlPrice=Ask - (Ask * (StopPct/100));
double ShortSlPrice=Bid + (Bid * (StopPct/100));

//This tag will be used to retrieve the order id of parent order while exiting.
//CTag should be 3-15 characters, alphanumeric, unique to the particular order
string BuyCTag="LONGENTRY";
string ShortCTag="SHORTENTRY";

//Place a BUY order (Long Entry)
string BuyOrderId=PlaceCOBridge(Exch,TrdSym,"BUY",Qty,BuySlPrice,OrdType,0,BuyCTag,IsLive,StgyCode,false);

//Exit the Long Position (Long Exit: Target/Stop Hit/SqOff Time/Reverse Signal)
//Since the stoploss orders are placed/managed by OMS for Cover Order
//We just need to get the order id of the stoploss order and
//call the ExitCO functions to close the Long position

//get the entry order id
string BuyEntryOrderId=GetOrderCTag(Exch,TrdSym,BuyCTag,IsLive);

//get the stoploss order id
string BuySlOrderId=GetChildOrders(BuyEntryOrderId);

//call the ExitCO function to exit the Long Position
string ExitLong=ExitCOBridge(BuySlOrderId,false,StgyCode,IsLive);

//Place a SHORT order (Short Entry)
string ShortOrderId=PlaceCOBridge(Exch,TrdSym,"SHORT",Qty,ShortSlPrice,OrdType,0,ShortCTag,IsLive,StgyCode,false);

//Exit the Short Position (Short Exit: Target/Stop Hit/SqOff Time/Reverse Signal)
//Since the stoploss orders are placed/managed by OMS for Cover Order
//We just need to get the order id of the stoploss order and
//call the ExitCO functions to close the Short position

//get the entry order id
string ShortEntryOrderId=GetOrderCTag(Exch,TrdSym,ShortCTag,IsLive);

//get the stoploss order id
string ShortSlOrderId=GetChildOrders(ShortEntryOrderId);

//call the ExitCO function to exit the Short Position
string ExitShort=ExitCOBridge(ShortSlOrderId,false,StgyCode,IsLive);

OCO Orders

//get latest prices
double Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);

//variables for Order
string Exch="NSE_EQ";
string TrdSym="AXISBANK"; //can be generated dynamically from Symbol()
int Qty=156; //Workout this as required
string OrdType="L";
string StgyCode="T1";
bool IsLive=true; //Live Trade
double StopPct=1.25; //Stoploss Percentage
double TgtPct=1.75; //Target Percentage
double BuySlPrice=Ask - (Ask * (StopPct/100));
double BuyTgtPrice=Ask + (Ask * (TgtPct/100));
double ShortSlPrice=Bid + (Bid * (StopPct/100));
double ShortTgtPrice=Bid - (Bid * (TgtPct/100));

//calculate target and stoploss in points as OCO accepts only points
double BuyTgtPoints=BuyTgtPrice - Ask;
double BuySlPoints=Ask - BuySlPrice;
double ShortTgtPoints=Bid - ShortTgtPrice;
double ShortSlPoints=ShortSlPrice - Bid;

//This tag will be used to retrieve the order id of parent order while exiting.
//CTag should be 3-15 characters, alpha-numeric, unique to the particular order
string BuyCTag="LONGENTRY";
string ShortCTag="SHORTENTRY";

//Place a BUY order (Long Entry)
string BuyOrderId=PlaceOCOBridge(Exch,TrdSym,"BUY",Qty,Ask,BuyTgtPoints,BuySlPoints,0,OrdType,0,BuyCTag,IsLive,StgyCode,false);

//Exit the Long Position (Long Exit: Target/Stop Hit/SqOff Time/Reverse Signal)
//Since the stoploss and target orders are placed/managed by OMS for Cover Order
//We just need to get the order id of the stoploss order and
//call the ExitOCO functions to close the Long position

//get the entry order id
string BuyEntryOrderId=GetOrderCTag(Exch,TrdSym,BuyCTag,IsLive);

//get the stoploss order id
string BuySlOrderId=GetChildOrders(BuyEntryOrderId);

//call the ExitOCO function to exit the Long Position
string ExitLong=ExitOCOBridge(BuySlOrderId,false,StgyCode,IsLive);

//Place a SHORT order (Short Entry)
string ShortOrderId=PlaceOCOBridge(Exch,TrdSym,"SHORT",Qty,Bid,ShortTgtPoints,ShortSlPoints,0,OrdType,0,ShortCTag,IsLive,StgyCode,false);

//Exit the Short Position (Short Exit: Target/Stop Hit/SqOff Time/Reverse Signal)
//Since the stoploss and target orders are placed/managed by OMS for Cover Order
//We just need to get the order id of the stoploss order and
//call the ExitOCO functions to close the Short position

//get the entry order id
string ShortEntryOrderId=GetOrderCTag(Exch,TrdSym,ShortCTag,IsLive);

//get the stoploss order id
string ShortSlOrderId=GetChildOrders(ShortEntryOrderId);

//call the ExitOCO function to exit the Short Position
string ExitShort=ExitOCOBridge(ShortSlOrderId,false,StgyCode,IsLive);