Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

Exiting Cover Order and Bracket Order for a Particular Trading Instrument

1 min read

Bracket Order and Cover Order are multi-legged orders and canceling the bracket order and cover order for a particular trading instrument there are no direct mechanism available from the API provisions supplied by the brokers. Hence here is the Amibroker AFL code to do the exit by reading the orderbook and processing the pending cover order and bracket order from the orderbook.

Supported Brokers : Aliceblue, Tradejini, Zebu

Why Exiting Cover Order and Bracket Order automatically is required?

To Build a Intraday stop and reverse models using Bracket Order and Cover Order with protection.

To implement the Exit Bracket Order I had designed a prototype model which can be later used in your AFL code to simply your coding requirements.

Parameter Controls

We can classify the modules into two types

1)Main Module (Exit Bracket and Cover Orders.afl) – used to cancel/exit the BO and CO orders for a particular trading instrument

2)Header Module (ExitBOCOOrders.afl) – used to parse the orderbook and to cancel the BO and CO orders (this AFL code needs to be placed under the Amibroker\Formulas\Include folder)

Main Module (Exit Bracket and Cover Orders.afl)

#include <ExitBOCOOrders.afl>


Symbol = ParamStr("Symbol","RELIANCE-EQ");
Pcode = ParamList("Pcode","BO|CO",1);
trigger = ParamTrigger("Trigger Button","Cancel Orders");

if(trigger)
{
resp = GetOrderBook();
CancelOrder(resp,Symbol,Pcode); //output in string format
}

Header Module (ExitBOCOOrders.afl)

//store the file under the name - ExitBOCOOrders.afl under the Amibroker\Formulas\Include folder

uid = ParamStr("Client ID","TS2499");
user_apikey = ParamStr("user_apikey","86cbef19e7e61ccee91e497690d5814e"); //Enter your API key here
api_secret = ParamStr("api_secret","8e8b207de43446c65f379bf2145b62fc"); //Enter your API secret key here
s_prdt_ali = "BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML";
broker = ParamStr("Broker","tj"); //Broker Short Code - ab - aliceblue, tj - tradejini, zb - zebu, en - enrich
ver = ParamStr("API Version","1.0");

RequestTimedRefresh(1,False);

function GetOrderBook()
{

algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{\"uid\":\""+uid+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"OrderBook",api_data,broker,ver);
_TRACE(" Order Book  : " +resp);
return resp;
}

function ExitOrder(Symbol,OrderType,Nstordno)
{

if(OrderType=="BO")
{
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{\"nestordernumber\":\""+Nstordno+"\",\"SyomOrderId\":\""+""+"\",\"status\":\""+"pending"+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"ExitBOOrder",api_data,broker,ver);

}

if(OrderType=="CO")
{
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{\"uid\":\""+uid+"\",\"NestOrd\":\""+Nstordno+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"ExitCOOrder",api_data,broker,ver);

}


return resp;
}

function CancelOrder(resp,Tsym,Pcode)
{

//Initialization
flag = 0;

stringinfo = "Nstordno";

for( item = 0; ( sym = StrExtract( resp, item,'{' )) != ""; item++ )
{

sym = StrTrim(sym," ");
Tsym = StrTrim(Tsym," ");

if(Pcode == "BO");
{
ordermatch = StrFind(sym,Tsym) AND StrFind(sym,Pcode) AND StrFind(sym,"complete");
}

if(Pcode == "CO");
{
ordermatch = StrFind(sym,Tsym) AND StrFind(sym,Pcode) AND (StrFind(sym,"complete") OR StrFind(sym,"open"));
}


if(ordermatch) //Matches the symbol and //Matches the Order Type
{

flag = 1; //turn on the flag

data = sym;



for( jitem = 0; ( posdetails = StrExtract( data, jitem,',' )) != ""; jitem++ )
{
  
  if(StrFind(posdetails,"\""+stringinfo+"\""))
  {
   posdetails = StrExtract(posdetails,1,':');
   Nstordno = StrTrim(posdetails,"\"");
   ExitOrder(Tsym,Pcode,Nstordno);
  }
  

} //end of for loop
} //end of if loop


}//end of for loop


_TRACE(Tsym+" "+Pcode+" Order Cancelled");

}
Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in and Co-Creator of Algomojo (Algorithmic Trading Platform for DIY Traders)

Stoploss, Multi Target, and Trailing Stoploss Module – Amibroker…

After Intraday V2.0 Amibroker Module - a readymade drag and drop module to convert any trading strategy into the intraday trading system with alerts,...
Rajandran R
6 min read

Line Trading – Algomojo Automation Module for Amibroker Users

The line Trading Automation tool is designed for Manual traders who want to perform level-based trade execution faster and also bring some advanced trade...
Rajandran R
58 sec read

Plotting Day Wise Equity Curve using Amibroker

We know that Amibroker provides an equity curve for the whole strategy. Is there a way where we can build a simple Day Wise...
Rajandran R
1 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More