This tutorial provides you a basic framework for sending multi-legged options module and how to exit the multi-legged option module using a simple button trading based example. However, this module can be extended to trade any sort of neutral trading strategy using Amibroker.
This button trading module also comes with providing priority to execute the Long Option Orders followed by Option Selling Orders in order to effectively manage the Margin as per the new margin framework for Derivatives.
Supported Brokers : Aliceblue, Tradejini, Zebu
Supported Amibroker Version : Amibroker Version 6.22 onwards
Supported Algomojo API Version : 1.0
In case if you are new to the option execution module then kindly kickstart with the basic tutorial on Sending Option Orders from Amibroker using Algomojo Platform
In case if you are looking for sending single-leg option orders from Amibroker based on Signals from Futures/Spot then kickstart with the basic tutorial on Send Option Orders from Futures or Spot Signals in Amibroker using Algomojo Platform
To Build Button Trading module we need 2 Major components
1)Header Option Execution Module that needs to be placed inside the Amibroker\Formulas\Include folder
2)Main Options Entry/Exit Button Execution Module that needs to be drag and dropped over the Blank Charts
1)Header Multi Legged Options Execution Module
Copy the Header Multi Leg Execution Module to Amibroker\Formulas\include folder. Save the AFL under the name algomojomultioptions.afl
//////////////////////////////////////////////
//Multi Broker Amibroker Option Execution Module
//Coded by Algomojo
//Date : 30/12/2020
//////////////////////////////////////////////
//Use this code only for Single Legged Long Only Options and Exiting Long Only Options
_SECTION_BEGIN("Algomojo Multi legged Options");
uid = ParamStr("Client ID","TS2499");
user_apikey = ParamStr("user_apikey","86cbef19e7e61ccee91e497690d5814e"); //Enter your API key here
api_secret = ParamStr("api_secret","4a94db82ea4fa140afaa2f039efffd18"); //Enter your API secret key here
s_prdt_ali = "BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML";
prctyp = ParamList("prctyp","MKT|L|SL|SL-M",0);
Pcode = ParamList("Pcode","NRML|CO|MIS",2);
Price = ParamList("Price","0");
TrigPrice = ParamList("TrigPrice","0");
stgy_name = ParamStr("Strategy Name", "Options");
broker = ParamStr("Broker","ab"); //Broker Short Code - ab - aliceblue, tj - tradejini, zb - zebu, en - enrich
ver = ParamStr("API Version","1.0");
fpath = ParamStr("Symbol Filepath", "C:\\Program Files (x86)\\AmiBroker\\Formulas\\Algomojo\\");
timer = 1; //3 seconds for providing delay after placing order
RequestTimedRefresh(1,False);
function getnestorderno(response)
{
NOrdNo = "";
if(StrFind(response,"NOrdNo")) //Matches the orderstatus
{
NOrdNo = StrTrim( response, "{\"NOrdNo\":" );
NOrdNo = StrTrim( NOrdNo, "\",\"stat\":\"Ok\"}" );
}
return NOrdNo;
}
function getorderhistory(orderno)
{
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data = "{\"uid\":\""+uid+"\",\"NOrdNo\":\""+orderno+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"OrderHistory",api_data,broker,ver);
return resp;
}
function getsymbol(orderno)
{
ordresp = getorderhistory(orderno);
data = "";
Trsym="";
for( item = 0; ( sym = StrExtract( ordresp, item,'{' )) != ""; item++ )
{
sym = StrTrim(sym," "); //Trim Whitespaces
if(StrFind(sym,"complete") OR StrFind(sym,"rejected")) //Matches the orderstatus
{
flag = 1; //turn on the flag
data = sym;
for( jitem = 0; ( ohistory = StrExtract( data, jitem,',' )) != ""; jitem++ )
{
if(Strfind(ohistory,"Trsym"))
{
Trsym = StrExtract(ohistory,1,':');
Trsym = StrTrim(Trsym,"\"");
}
}
}
}
return Trsym;
}
function getorderstatus(orderno)
{
orderstatus="";
ordresp = getorderhistory(orderno);
data = "";
for( item = 0; ( sym = StrExtract( ordresp, item,'{' )) != ""; item++ )
{
sym = StrTrim(sym," "); //Trim Whitespaces
if(StrFind(sym,"complete") OR StrFind(sym,"rejected")) //Matches the orderstatus
{
flag = 1; //turn on the flag
data = sym;
for( jitem = 0; ( ohistory = StrExtract( data, jitem,',' )) != ""; jitem++ )
{
if(Strfind(ohistory,"Status"))
{
orderstatus = StrExtract(ohistory,1,':');
orderstatus = StrTrim(orderstatus,"\"");
}
}
}
}
return orderstatus;
}//end function
function gettoken(symbol)
{
stoken="";
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data = "{\"s\":\""+symbol+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"fetchsymbol",api_data,broker,ver);
for( item = 0; ( sym = StrExtract( resp, item,'{' )) != ""; item++ )
{
sym = StrTrim(sym," "); //Trim Whitespaces
data = sym;
for( jitem = 0; ( ofetch = StrExtract( data, jitem,',' )) != ""; jitem++ )
{
if(Strfind(ofetch,"symbol_token"))
{
stoken = StrExtract(ofetch,1,':');
stoken = StrTrim(stoken,"\"");
}
}
}
return stoken;
}
function writetofile(filepath,ordno,symbol,ostatus)
{
result =0;
if(ostatus=="complete" OR ostatus=="rejected")
{
fh = fopen( filepath, "w"); //Filepath of csv file with symbols
if(fh)
{
fputs(ordno + ",", fh);
fputs(symbol + ",", fh);
fputs(ostatus+",", fh);
fclose(fh);
result =1;
}
}
return result;
}
function placeoptionorder(spot_sym,expiry_dt,strike_int,qty,Ttranstype,opt,off,leg)
{
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data = "{\"strg_name\":\""+stgy_name+"\",\"spot_sym\":\""+spot_sym+"\",\"expiry_dt\":\""+expiry_dt+"\",\"opt_type\":\""+opt+"\",\"Ttranstype\":\""+Ttranstype+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+qty+"\",\"Price\":\""+Price+"\",\"TrigPrice\":\""+TrigPrice+"\",\"Pcode\":\""+Pcode+"\",\"strike_int\":\""+strike_int+"\",\"offset\":\""+off+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"PlaceFOOptionsOrder",api_data,broker,ver);
_TRACE("\n"+api_data);
//Get Nest Order Number
nestorderno = getnestorderno(resp);
_TRACE("\nNest Order No : "+nestorderno);
tradetime=GetPerformanceCounter()/1000;
while ((GetPerformanceCounter()/1000 - tradetime) < timer)
{
//Get Trading Symbol
Tsym = getsymbol(nestorderno);
}
_TRACE("\nTrading Symbol : "+Tsym);
//Get Order Status
orderstatus = getorderstatus(nestorderno);
_TRACE("\nOrder Status : "+orderstatus);
//Get Token Number
//token = gettoken(Tsym);
//_TRACE("\nSymbol Token : "+token);
if(opt=="CE")
{
path = fpath+leg+"AlgomojoCE.csv";
}
if(opt=="PE")
{
path = fpath+leg+"AlgomojoPE.csv";
}
writestatus = writetofile(path,nestorderno,Tsym,orderstatus);
_TRACEF(WriteIf(writestatus,"\nWriting to File - Success","\nWriting to File - Failure"));
//resp = resp+"\nNest Order No : "+nestorderno+"\nTrading Symbol : "+Tsym+"\nOrder Status : "+orderstatus+"\nSymbol Token : "+token;
return Tsym;
}
function getquantity(Tsym)
{
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data ="{\"uid\":\""+uid+"\",\"actid\":\""+uid+"\",\"type\":\""+"NET"+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"PositionBook",api_data,broker,ver);
//Initialization
flag = 0;
possym = "";
posNetqty =0;
for( item = 0; ( sym = StrExtract( resp, item,'{' )) != ""; item++ )
{
sym = StrTrim(sym," ");
Tsym = StrTrim(Tsym," ");
if(Strfind(sym,Tsym) AND StrFind(sym,Pcode)) //Matches the symbol and //Matches the Order Type
{
flag = 1; //turn on the flag
data = sym;
_TRACE(" Position Book : " +data);
for( jitem = 0; ( posdetails = StrExtract( data, jitem,',' )) != ""; jitem++ )
{
if(Strfind(posdetails,"Netqty"))
{
posdetails = StrExtract(posdetails,1,':');
posNetqty = StrToNum(StrTrim(posdetails,"\""));
_TRACE("\nNetQty : "+posNetqty);
}
} //end of for loop
}
}//end of for loop
if(flag==0)
{
_TRACE("\nTrading Symbol Not Found");
}
return posNetqty;
}
function squareoffoptions(opt,Ttranstype,leg)
{
ttype="";
if(Ttranstype=="B")
{
ttype = "S";
}
if(Ttranstype=="S")
{
ttype = "B";
}
ordno = "";
Symbol = "";
ostatus ="";
if(opt=="CE")
{
fpath = fpath+leg+"AlgomojoCE.csv";
}
if(opt=="PE")
{
fpath = fpath+leg+"AlgomojoPE.csv";
}
fh = fopen(fpath, "r");
if(fh)
{
read = fgets(fh);
ordno = StrTrim(StrExtract(read,0)," ");
Symbol = StrTrim(StrExtract(read,1)," ");
ostatus = StrTrim(StrExtract(read,2)," ");
fclose(fh);
}
exitqty = getquantity(Symbol);
_TRACE("Net Quantity in the OrderBook for the Symbol : "+Symbol+" is : "+exitqty);
if(ostatus=="complete" AND exitqty!=0)
{
algomojo=CreateObject("AMAMIBRIDGE.Main");
api_data = "{\"strg_name\":\""+stgy_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Symbol+"\",\"exch\":\""+"NFO"+"\",\"Ttranstype\":\""+ttype+"\",\"Ret\":\""+"DAY"+"\",\"prctyp\":\""+prctyp+"\",\"qty\":\""+exitqty+"\",\"discqty\":\""+"0"+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+"0"+"\",\"TrigPrice\":\""+"0"+"\",\"Pcode\":\""+Pcode+"\",\"AMO\":\""+"NO"+"\"}";
resp=algomojo.AMDispatcher(user_apikey, api_secret,"PlaceOrder",api_data,broker,ver);
//Get Nest Order Number
nestorderno = getnestorderno(resp);
_TRACE("\nNest Order No : "+nestorderno);
tradetime=GetPerformanceCounter()/1000;
while ((GetPerformanceCounter()/1000 - tradetime) < timer)
{
//Get Trading Symbol
Tsym = getsymbol(nestorderno);
}
_TRACE("\nTrading Symbol : "+Tsym);
//Get Order Status
orderstatus = getorderstatus(nestorderno);
_TRACE("\nOrder Status : "+orderstatus);
}
else
{
resp = "Not Squared Off. Either No Open Position Exist or Prev Signal Status Not in Completed State";
}
return resp;
}
_SECTION_END();
2)Main Options Enty/Exit Button Execution Module
Copy the Main Execution Module to Amibroker\Formulas\Algomojo Platform. Save the AFL under the name Algomojo Button Trading Multi Legged Options.afl
Now drag and Drop the Module on top of your Charting with Buy/Sell Trading System
/*
Created By : Algomojo
Created on : 01 Apr 2020.
Website : www.algomojo.com
*/
#include < algomojomultioptions.afl >
_SECTION_BEGIN("Button Trading - Multi Broker - Algomojo");
//Notes
//This afl code works only on Amibroker 6.22 and higher version
//Requires Algomojo Trading account
//Replace the API Key and API Secrety key with yours
Version(6.22);
RequestTimedRefresh(1, False); // Send orders even if Amibroker is minimized or Chart is not active
spot_sym = ParamStr("spot_sym","NIFTY"); //Enter the symbol name here
strike_int = ParamStr("strike_int","50");
lotsize = Param("Symbol Lot Size",75,1,50000);
leg1 = ParamToggle("1st Leg","Disable|Enable");
leg1expiry_dt = ParamStr("Leg 1 expiry_dt", "28JAN21");
leg1qty = Param("Leg 1 Lot Size",1)*lotsize;
leg1Ttranstype = ParamList("Leg 1 Transaction Type","B|S");
leg1opt_type = ParamList("Leg 1 Option Type","CE|PE",0);
leg1offset = ParamStr("Leg 1 Offset","0");
leg2 = ParamToggle("2ng Leg","Disable|Enable");
leg2expiry_dt = ParamStr("Leg 2 expiry_dt", "28JAN21");
leg2qty = Param("Leg 2 Lot Size",1)*lotsize;
leg2Ttranstype = ParamList("Leg 2 Transaction Type","B|S");
leg2opt_type = ParamList("Leg 2 Option Type","CE|PE",0);
leg2offset = ParamStr("Leg 2 Offset","0");
leg3 = ParamToggle("3rd Leg","Disable|Enable");
leg3expiry_dt = ParamStr("Leg 3 expiry_dt", "28JAN21");
leg3qty = Param("Leg 3 Lot Size",1)*lotsize;
leg3Ttranstype = ParamList("Leg 3 Transaction Type","B|S");
leg3opt_type = ParamList("Leg 3 Option Type","CE|PE",0);
leg3offset = ParamStr("Leg 3 Offset","0");
leg4 = ParamToggle("4th Leg","Disable|Enable");
leg4expiry_dt = ParamStr("Leg 4 expiry_dt", "28JAN21");
leg4qty = Param("Leg 4 Lot Size",1)*lotsize;
leg4Ttranstype = ParamList("Leg 4 Transaction Type","B|S");
leg4opt_type = ParamList("Leg 4 Option Type","CE|PE",0);
leg4offset = ParamStr("Leg 4 Offset","0");
tradedelay = Param("Trade Delay",0);
EnableAlgo = ParamList("Algo Mode","Disable|Enable",0); // Algo Mode
static_name_ = Name()+GetChartID()+interval(2)+stgy_name;
static_name_algo = Name()+GetChartID()+interval(2)+stgy_name+"algostatus";
//StaticVarSet(static_name_algo, -1);
GfxSelectFont( "BOOK ANTIQUA", 14, 100 );
GfxSetBkMode( 1 );
if(EnableAlgo == "Enable")
{
AlgoStatus = "Algo Enabled";
GfxSetTextColor( colorGreen );
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40);
if(Nz(StaticVarGet(static_name_algo),0)!=1)
{
_TRACE("Algo Status : Enabled");
StaticVarSet(static_name_algo, 1);
}
}
if(EnableAlgo == "Disable")
{
AlgoStatus = "Algo Disabled";
GfxSetTextColor( colorRed );
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40);
if(Nz(StaticVarGet(static_name_algo),0)!=0)
{
_TRACE("Algo Status : Disabled");
StaticVarSet(static_name_algo, 0);
}
}
if(EnableAlgo == "LongOnly")
{
AlgoStatus = "Long Only";
GfxSetTextColor( colorYellow );
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40);
if(Nz(StaticVarGet(static_name_algo),0)!=2)
{
_TRACE("Algo Status : Long Only");
StaticVarSet(static_name_algo, 2);
}
}
if(EnableAlgo == "ShortOnly")
{
AlgoStatus = "Short Only";
GfxSetTextColor( colorYellow );
GfxTextOut( "Algostatus : "+AlgoStatus , 20, 40);
if(Nz(StaticVarGet(static_name_algo),0)!=3)
{
_TRACE("Algo Status : Short Only");
StaticVarSet(static_name_algo, 3);
}
}
resp = "";
function Buyorder()
{
//Managing the efficient Margin according to new margin regulations. Giving Priority for Long Options first followed by option short positions
if(leg1 AND leg1Ttranstype=="B")
{
leg1response = placeoptionorder(spot_sym,leg1expiry_dt,strike_int,leg1qty,leg1Ttranstype,leg1opt_type,leg1offset,1);
_TRACE("Leg 1 Buy Order Placed Successfully");
}
if(leg2 AND leg2Ttranstype=="B")
{
leg2response = placeoptionorder(spot_sym,leg2expiry_dt,strike_int,leg2qty,leg2Ttranstype,leg2opt_type,leg2offset,2);
_TRACE("Leg 2 Buy Order Placed Successfully");
}
if(leg3 AND leg3Ttranstype=="B")
{
leg3response = placeoptionorder(spot_sym,leg3expiry_dt,strike_int,leg3qty,leg3Ttranstype,leg3opt_type,leg3offset,3);
_TRACE("Leg 3 Buy Order Placed Successfully");
}
if(leg4 AND leg4Ttranstype=="B")
{
leg4response = placeoptionorder(spot_sym,leg4expiry_dt,strike_int,leg4qty,leg4Ttranstype,leg4opt_type,leg4offset,4);
_TRACE("Leg 4 Buy Order Placed Successfully");
}
if(leg1 AND leg1Ttranstype=="S")
{
leg1response = placeoptionorder(spot_sym,leg1expiry_dt,strike_int,leg1qty,leg1Ttranstype,leg1opt_type,leg1offset,1);
_TRACE("Leg 1 Sell Order Placed Successfully");
}
if(leg2 AND leg2Ttranstype=="S")
{
leg2response = placeoptionorder(spot_sym,leg2expiry_dt,strike_int,leg2qty,leg2Ttranstype,leg2opt_type,leg2offset,2);
_TRACE("Leg 2 Sell Order Placed Successfully");
}
if(leg3 AND leg3Ttranstype=="S")
{
leg3response = placeoptionorder(spot_sym,leg3expiry_dt,strike_int,leg3qty,leg3Ttranstype,leg3opt_type,leg3offset,3);
_TRACE("Leg 3 Sell Order Placed Successfully");
}
if(leg4 AND leg4Ttranstype=="S")
{
leg4response = placeoptionorder(spot_sym,leg4expiry_dt,strike_int,leg4qty,leg4Ttranstype,leg4opt_type,leg4offset,4);
_TRACE("Leg 4 Sell Order Placed Successfully");
}
Say( "Order Placed" );
}
function Sellorder()
{
if(leg1)
{
sqoff1status = squareoffoptions(leg1opt_type,leg1Ttranstype,1);
_TRACE("Leg 1 Exit Response :"+sqoff1status);
}
if(leg2)
{
sqoff2status = squareoffoptions(leg2opt_type,leg2Ttranstype,2);
_TRACE("Leg 2 Exit Response :"+sqoff2status);
}
if(leg3)
{
sqoff3status = squareoffoptions(leg3opt_type,leg3Ttranstype,3);
_TRACE("Leg 3 Exit Response :"+sqoff3status);
}
if(leg4)
{
sqoff4status = squareoffoptions(leg4opt_type,leg4Ttranstype,4);
_TRACE("Leg 4 Exit Response :"+sqoff4status);
}
_TRACE("Order Placed Successfully");
Say( "Order Placed" );
}
function GuiButtonTrigger( ButtonName, x, y, width, Height ) {
/// @link http://forum.amibroker.com/t/guibuttons-for-everyone/1716/4
/// by beaver & fxshrat
/// version 1.1
global IDset;
local id, event, clickeven;
if( typeof( IDset ) == "undefined" ) IDset = 0;
//_TRACEF( "IDset before: %g", IDset );
GuiButton( ButtonName, ++IDset, x, y, width, height, 7 );
//_TRACEF( "IDset after: %g", IDset );
result = 0;
id = GuiGetEvent( 0, 0 );// receiving button id
event = GuiGetEvent( 0, 1 );// receiving notifyflag
clickevent = event == 1;
BuyClicked = id == 1 && clickevent;
SellClicked = id == 2 && clickevent;
if( BuyClicked AND StaticVarGet(Name()+GetChartID()+"buyAlgo")==0 )
{
BuyOrder();
result = 1;
StaticVarSet(Name()+GetChartID()+"buyAlgo",1);
}
else
{
StaticVarSet(Name()+GetChartID()+"buyAlgo",0);
}
if( SellClicked AND StaticVarGet(Name()+GetChartID()+"sellAlgo")==0 )
{
SellOrder();
result = -1;
StaticVarSet(Name()+GetChartID()+"sellAlgo",1);
}
else
{
StaticVarSet(Name()+GetChartID()+"sellAlgo",0);
}
return result;
}
BuyTrigger = GuiButtonTrigger( "Enter Options", 0, 100, 200, 30 );
SellTrigger = GuiButtonTrigger( "Exit Options", 200, 100, 200, 30 );
GuiSetColors( 1, 3, 2, colorRed, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow );
Title = "Trigger: " + WriteIf(BuyTrigger==1,"Buy Triggered",WriteIf(BuyTrigger==-1,"Sell Triggered","0"));
SetChartOptions(0 , chartShowArrows | chartShowDates);
Plot(Close,"Candle", colorDefault, styleCandle);
_SECTION_END();
3)Now right-click over the charts and set the Client ID, user_apikey, api_secretkey,broker and set the required quantity
4)Ensure the Symbol File Path Exists. This is the path where the executed symbols CE and PE Options will get saved in a CSV file for later squareoff.
5)Enable/Disable Multiple Leg Options, Set the Symbol, Lot Size, Strike Interval (Difference between two immediate strike price), Set the Expiry Date, Lot Size and Offset for Multiple Legs as shown below
Ensure while entering the Option Expiry Format. Login to Algomojo Terminal and check out the weekly and monthly option format and enter the expiry date accordingly.
For Aliceblue account holders Monthly Option Symbol Format: NIFTY21JAN14500CE
Hence the Expiry Date needs to be entered as 21JAN
For Aliceblue Account holders weekly Option Symbol Format: NIFTY2111414500CE
Hence the Expiry Date needs to be entered as 21114
For Tradejini and Zebu Account Holders Monthly Option Symbol Format: NIFTY28JAN2114500CE
Hence the Expiry Date needs to be entered as 28JAN21
For Tradejini and Zebu Account Holders Monthly Option Symbol Format: NIFTY14JAN2114500CE
Hence the Expiry Date needs to be entered as 14JAN21
6)Ensure Log Window is open to capture the Trace Logs
7)Enable to Algo Mode : Enable State
7)Bingo Now on the click of Enter Options you will be able to send Multi legged options as per your settings and you can also see that long option orders take priority in execution compared to short legged options to effectively manage the margin.
8)on the Press of Exit Options button automatically all the multi-legged option orders get squared off. Square off happens only if the position exists in the position book. If no position is found one will get the following log notification
Not Squared Off. Either No Open Position Exist or Prev Signal Status Not in Completed State
This module can be extended further to place multi legged option orders autmatically from Amibroker by doing customization according to traders requirement.
If in case you need a customized multi-legged options requirement you can send your requirement to support@algomojo.com