MetaTrader Bridge for Trading API

Traders,

It’s finally here, the much awaited feature ‘MetaTrader Bridge’. Now, all our DotNet libraries supports MetaTrader Bridge. You can seamlessly connect your favourite TA Software ‘MetaTrader’ with your trading account Using API.

Features:

  • Order Requests (Place/Modify/Cancel/Exit etc)
  • Order Details (Status, Filled Price, Filled Qty etc)
  • Symbol Details (Tick Size, LotSize)
  • Position Details (Mtm, Net Positions, Avg Bought Price, Avg Sold Price etc)
  • Supports all exchanges
  • Supports Cover and Bracket 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:

Below instructions are for Upstox API, but it is same for all other Trading API’s. You can watch this video for overview of the below setup.

  1. System Requirements
  2. API subscription
  3. Installation of UpstoxNet
  4. Installation of MetaTraderBridge
  5. MetaTrader Initial Setup
  6. How to call Bridge Functions
  7. List of all Functions

1. System Requirements:

  • Windows 7 or above
  • .Net Framework 4.5.2 or higher
  • MS Office 2007 or above
    (Our library includes RTD server, hence MS office is required. Without MS office you can’t install our Library)

2. API subscription:

Get API subscription from Upstox.
Refer this video  for steps to get API subscription.

3. Installation of UpstoxNet:

UpstoxNet, our DotNet Library for Upstox API. Download the Library from here and follow the install notes included in the downloaded file. This library also includes ‘Hello Upstox v2.0’, our Hello world trading platform built with our library. You can find a desktop shortcut for ‘Hello Upstox v2.0’ post installation. You are going to use this ‘Hello Upstox v2.0’ as a front-end to receive order requests from MetaTrader. Refer this video for installation and initial settings.

4. Installation of MetaTrader Bridge:

Download the Bridge files from here
Extract the contents of the downloaded zip file to a folder.

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

 

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.

Menu --> Tools --> options --> Expert Advisors --> Allow DLL Imports

 

6. How to call Bridge Functions:

In your MQL scripts/Strategy/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’

Example Usage:

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);

 

Bracket 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, alphanumeric, 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);

 

7. List of all Functions:

//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 ) ;

//SYMOCOL 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, alphanumeric, 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, alphanumeric.

 

Note: We don’t have MetaTrader experts at our end, so we will not be able to help you in building trading system with MQL(MetaQuotes Language). You should know MQL to use our Bridge or you can hire any MetaTrader experts to code your logic and bridge functions in MQL. That’s said you should know ‘when to call which Bridge function’ in MQL.

If any bugs/issues in the Bridge functions, you can notify us at help@howutrade.in For all other queries related to MetaTrader/MQL, pls post in the developer forums Zerodha  or Upstox , any fellow developer may help you.

If you are a MetaTrader experts, you may help others by posting sample strategies with Bridge in the developer forum, you may also answer queries related to MeatTrader/MQL in the developer forum.

If you are new to API Trading, please watch all videos in our YouTube playlist ‘Must Watch

Happy API Trading..!
ChokS

ChokS

ChokS

.Net Programmer. AFL Coder. Author of KiteDotNet & KiteXL.

Leave a Reply

Your email address will not be published.