This tutorial provides instructions on how to utilize simple buy and sell trading signals in Spot/Future charts to place option orders (including ATM, ITM, and OTM options) on the Algomojo platform. Implementing this system can help traders mitigate risk, particularly in the event of unexpected market movements that may result in significant losses. Additionally, implementing ATM or ITM option buying strategies, as opposed to futures, may help to reduce the risk of extreme black swan events and significantly mitigate the potential for large gap-up or gap-down risks when carrying forward positions.
Features of the Options Execution Module
1)Simple Drag and Drop Module on top of any Amibroker trading strategy with proper buy,sell,short,cover defined variables. 2)Place Smart Option Orders to intelligent send orders by manipulating the current existing positions. 3)Place Larger Option Orders by Splitting Larger Orders into multiple small orders. 4)Option Strike calculation at Amibroker end (Trades can configure the Underlying symbol as Spot./Futures) based on their trading requirement) accordingly, options strikes will be calculated.
Amibroker Option Buying Settings for Placing Ordes in ATM Options (Long Only Options Strategy)
Trades can control the CE/PE Offset Parameters to place orders in various option strikes. To place orders in ATM options one has to set the Offset value as “0” as shown below
Amibroker Parameter Settings for Place Orders with ATM CE/PE
Amibroker Option Buying Settings for Placing Ordes in ITM Options (Long Only Options Strategy)
Strategy Parameter Settings for Option Long and Exit Strategy
Trading Signal in Spot/Futures
Orders to be Placed
Buy Signal
Long Call
Sell Signal
Exit Long Call
Short Signal
Long Put
Cover Signal
Exit Long Put
Buy and Cover Signal
Long Call Exit Long Put
Short and Cover Signal
Long Put Exit Long Call
Strategy Parameter Settings for Option Short and Exit Strategy
Trading Signal in Spot/Futures
Orders to be Placed
Buy Signal
Short Put
Sell Signal
Exit Short Put
Short Signal
Short Call
Cover Signal
Exit Short Call
Buy and Cover Signal
Short Put Exit Short Call
Short and Cover Signal
Short Call Exit Short Put
To Build this module we need 2 components
1)Trading System with Buy/Sell/Short/Cover Signals 2)Option Execution Module that needs to be drag and dropped over the Trading System
1)Building your Trading System
Apply the Trading System with your Buy/Sell/Short/Cover variables on the charts.
Here is the sample EMA Crossover Trading System with Buy,Sell,Short,Cover variables.
_SECTION_BEGIN("EMA Crossover Strategy");// Param controls for EMA values
p1 =Param("EMA1",10,1,200,1);
p2 =Param("EMA2",20,1,200,1);// Param controls for EMA line colors
col1 =ParamColor("EMA1 Color", colorRed);
col2 =ParamColor("EMA2 Color", colorBlue);// Calculate EMAs
EMA1 =EMA(C, p1);
EMA2 =EMA(C, p2);// Plot EMAsPlot(EMA1,"EMA1", col1, styleThick);Plot(EMA2,"EMA2", col2, styleThick);// Set position sizeSetPositionSize(100, spsShares);// Generate buy/sell signals
Buy =Cross(EMA1, EMA2);
Sell =Cross(EMA2, EMA1);// Generate short/cover signals
Short = Sell;
Cover = Buy;//Generate Signals only on close of the candle
Buy =Ref(Buy,-1);
Sell =Ref(Sell,-1);
Short =Ref(Short,-1);
Cover =Ref(Cover,-1);// Plot buy/sell signalsPlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen,0, L, Offset=-40);PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime,0,L, Offset=-50);PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite,0,L, Offset=-45);PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed,0, H, Offset=40);PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange,0,H, Offset=50);PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite,0,H, Offset=-45);// Plot candlestick chartPlot(C,"Price", colorWhite, styleCandle);_SECTION_END();
Note: Buy/Sell/Short/Cover parameters are mandatory without that execution will not happen properly
If in case your trading system supports only Buy and Sell and you don’t want to use short and cover variables, in that case, use the below-mentioned logic where initialize short and cover variables to zero.
Buy = your buy trading logic ;
Sell = your sell trading logic ;
short =0;
cover =0;
2)Main Option Execution Module
Copy the Main Execution Module to Amibroker\Formulas\Algomojo folder. Save the AFL under the name Algomojo Spot Or Future Signals to Option Orders.afl
Now Drag and Drop the Module on top of your Charting with Buy/Sell Trading System
/*
Algomojo - Smart Spot/Futures to Options Trading Module with Split Order Controls
Created By : Algomojo
Created on : 07 MAR 2023.
Website : www.algomojo.com
*/_SECTION_BEGIN("Algomojo - Broker Controls");// Send orders even if Amibroker is minimized or Chart is not activeRequestTimedRefresh(1, False);EnableTextOutput(False);//Creating Input Controls for Setting Order Related Information
broker =Paramlist("Broker","an|ab|fs|fp|fy|gc|pt|sm|tc|up|zb|ze",0);
ver =ParamStr("API Version ","v1");
apikey =ParamStr("apikey","xxxxxxxxxxxxxxxxxx");//Enter your API key here
apisecret =ParamStr("secretkey","xxxxxxxxxxxxxxxxxx");//Enter your API secret key here_SECTION_END();_SECTION_BEGIN("Algomojo - Order Controls");
strategy =ParamStr("Strategy Name","Test Strategy");
spot =Paramlist("Spot Symbol","NIFTY|BANKNIFTY|FINNIFTY");
expiry =ParamStr("Expiry Date","29MAR23");
exchange =ParamList("Exchange","NFO|MCX",0);
Symbol =ParamStr("Underlying Symbol","NIFTY-I.NFO");
iInterval=Param("Strike Interval",50,1,10000,1);
StrikeCalculation =Paramlist("Strike Calculation","PREVOPEN|PREVCLOSE|TODAYSOPEN",0);
LotSize =Param("Lot Size",50,1,10000,1);
offsetCE =Param("CE Offset",0,-40,40,1);
offsetPE =Param("PE Offset",0,-40,40,1);
pricetype =ParamList("Order Type","MARKET",0);
product =ParamList("Product","MIS|NRML",1);
tradetype =ParamList("Option Trade Type","BUY|SELL",0);
quantity =Param("quanity(Lot Size)",1,0,10000)*LotSize;
price =0;
disclosed_quantity =0;
trigger_price =0;
amo ="NO";
splitorder =ParamList("To Split Orders","NO|YES",0);
split_quantity =Param("Split Quantity",500,1,100000,1);
VoiceAlert =ParamList("Voice Alert","Disable|Enable",1);
Entrydelay =Param("Entry Delay",0,0,1,1);
Exitdelay =Param("Exit Delay",0,0,1,1);
EnableAlgo =ParamList("AlgoStatus","Disable|Enable|LongOnly|ShortOnly",0);
resp ="";//Static Variables for Order protection
static_name_ =Name()+GetChartID()+interval(2)+strategy;
static_name_algo =Name()+GetChartID()+interval(2)+strategy+"algostatus";
AlgoBuy =lastvalue(Ref(Buy,-Entrydelay));
AlgoSell =lastvalue(Ref(Sell,-Exitdelay));
AlgoShort =lastvalue(Ref(Short,-Entrydelay));
AlgoCover =lastvalue(Ref(Cover,-Exitdelay));//Plots DashboardGfxSelectFont("BOOK ANTIQUA",14,100);GfxSetBkMode(1);if(EnableAlgo !=""){
AlgoStatus = EnableAlgo;GfxSetTextColor(IIf(EnableAlgo =="Enable", colorGreen,IIf(EnableAlgo =="LongOnly",colorYellow,IIf(EnableAlgo =="ShortOnly",colorOrange,colorRed))));GfxTextOut("Algostatus : "+AlgoStatus ,20,40);StaticVarSet(static_name_algo,IIf(EnableAlgo =="Enable",1,IIf(EnableAlgo =="LongOnly",2,IIf(EnableAlgo =="ShortOnly",3,0))));//_TRACE("Algo Status : "+EnableAlgo);}//optionCEtype = WriteIf(offsetCE == 0, "ATM CE", WriteIf(offsetCE<0,"ITM"+abs(offsetCE)+" CE","OTM"+abs(offsetCE)+" CE"));//optionPEtype = WriteIf(offsetPE == 0, "ATM PE", WriteIf(offsetPE<0,"ITM"+abs(offsetPE)+" PE","OTM"+abs(offsetPE)+" PE"));if(StrikeCalculation=="PREVOPEN"){SetForeign(Symbol);
spotC =Ref(OPEN,-1);RestorePriceArrays();}if(StrikeCalculation=="PREVCLOSE"){SetForeign(Symbol);
spotC =Ref(Close,-1);RestorePriceArrays();}if(StrikeCalculation=="TODAYSOPEN"){SetForeign(Symbol);
spotC =TimeFrameGetPrice("O",inDaily);RestorePriceArrays();}//Maintain Array to Store ATM Strikes for each and every bar
strike =IIf(spotC % iInterval > iInterval/2, spotC -(spotC%iInterval)+ iInterval,
spotC -(spotC%iInterval));//Entry Strikes
strikeCE = strike +(offsetCE * iInterval);
strikePE = strike -(offsetPE * iInterval);
buycontinue =Flip(Buy,Sell);
shortcontinue =Flip(Short,Cover);
entrysym ="";
exitsym ="";//Exit Strikesif(tradetype=="BUY"){
ExitStrikeCE =ValueWhen(Ref(Buy,-Entrydelay),strikeCE);
ExitStrikePE =ValueWhen(Ref(Short,-Entrydelay),strikePE);if(broker=="tc" OR broker =="fs"){
entrysym =WriteIf(buycontinue OR sell,spot+expiry+"C"+strikeCE,WriteIf(shortcontinue OR cover,spot+expiry+"P"+strikePE,""));
exitsym =WriteIf(buycontinue OR sell,spot+expiry+"P"+ExitStrikePE,WriteIf(shortcontinue OR cover,spot+expiry+"C"+ExitStrikeCE,""));}else{
entrysym =WriteIf(buycontinue OR sell,spot+expiry+strikeCE+"CE",WriteIf(shortcontinue OR cover,spot+expiry+strikePE+"PE",""));
exitsym =WriteIf(buycontinue OR sell,spot+expiry+ExitStrikePE+"PE",WriteIf(shortcontinue OR cover,spot+expiry+ExitStrikeCE+"CE",""));}}if(tradetype=="SELL"){
ExitStrikeCE =ValueWhen(Ref(Short,-Entrydelay),strikeCE);
ExitStrikePE =ValueWhen(Ref(Buy,-Entrydelay),strikePE);if(broker=="tc" OR broker =="fs"){
entrysym =WriteIf(buycontinue OR sell,spot+expiry+"P"+strikePE,WriteIf(shortcontinue OR cover,spot+expiry+"C"+strikeCE,""));
exitsym =WriteIf(buycontinue OR sell,spot+expiry+"C"+ExitStrikeCE,WriteIf(shortcontinue OR cover,spot+expiry+"P"+ExitStrikePE,""));}else{
entrysym =WriteIf(buycontinue OR sell,spot+expiry+"P"+strikePE,WriteIf(shortcontinue OR cover,spot+expiry+"P"+strikeCE,""));
exitsym =WriteIf(buycontinue OR sell,spot+expiry+"C"+ExitStrikeCE,WriteIf(shortcontinue OR cover,spot+expiry+"C"+ExitStrikePE,""));}}printf("\n\n\nEntry Symbol : "+entrysym);printf("\nExit Symbol : "+exitsym);_SECTION_END();//Buy and Sell Order FunctionsfunctionPlaceorder(action,OptionType,orderqty){
algomojo=CreateObject("AMAMIBRIDGE.Main");if(broker=="tc" OR broker=="fs"){if(OptionType=="CE") OptType ="C";if(OptionType=="PE") OptType ="P";
tradingsymbol = spot+expiry+OptType+VarGetText("strike"+OptionType);}else{
tradingsymbol = spot+expiry+VarGetText("strike"+OptionType)+OptionType;}
api_data ="{ \"broker\": \""+broker+"\",
\"strategy\":\""+strategy+"\",
\"exchange\":\""+exchange+"\",
\"symbol\":\""+tradingsymbol+"\",
\"action\":\""+action+"\",
\"product\":\""+product+"\",
\"pricetype\":\""+pricetype+"\",
\"quantity\":\""+orderqty+"\",
\"price\":\""+price+"\",
\"disclosed_quantity\":\""+disclosed_quantity+"\",
\"trigger_price\":\""+trigger_price+"\",
\"amo\":\""+amo+"\",
\"splitorder\":\""+splitorder+"\",
\"split_quantity\":\""+split_quantity+"\"
}";_TRACE("API Request"+api_data);
resp=algomojo.AMDispatcher(apikey, apisecret,"PlaceOrder",api_data,"am",ver);_TRACE("API Response : "+resp);if(VoiceAlert =="Enable")Say("Order Placed");}functionExitOrder(action,OptionType){
algomojo=CreateObject("AMAMIBRIDGE.Main");if(broker=="tc" or broker=="fs"){if(OptionType=="CE") OptType ="C";if(OptionType=="PE") OptType ="P";
tradingsymbol = spot+expiry+OptType+VarGetText("ExitStrike"+OptionType);}else{
tradingsymbol = spot+expiry+VarGetText("ExitStrike"+OptionType)+OptionType;}
api_data ="{ \"broker\": \""+broker+"\",
\"strategy\":\""+strategy+"\",
\"exchange\":\""+exchange+"\",
\"symbol\":\""+tradingsymbol+"\",
\"action\":\""+action+"\",
\"product\":\""+product+"\",
\"pricetype\":\""+pricetype+"\",
\"quantity\":\""+"0"+"\",
\"price\":\""+price+"\",
\"position_size\":\""+"0"+"\",
\"disclosed_quantity\":\""+disclosed_quantity+"\",
\"trigger_price\":\""+trigger_price+"\",
\"amo\":\""+amo+"\",
\"splitorder\":\""+splitorder+"\",
\"split_quantity\":\""+split_quantity+"\"
}";_TRACE("Broker API Request"+api_data);
resp=algomojo.AMDispatcher(apikey, apisecret,"PlaceSmartOrder",api_data,"am",ver);_TRACE("API Response : "+resp);if(VoiceAlert =="Enable")Say("Order Placed");return resp;}if(EnableAlgo !="Disable"){
lasttime =StrFormat("%0.f",LastValue(BarIndex()));SetChartBkColor(colorDarkGrey);if(EnableAlgo =="Enable"){if(AlgoBuy==True AND AlgoCover == True AND StaticVarGet(static_name_+"buyCoverAlgo")==0 AND StaticVarGetText(static_name_+"buyCoverAlgo_barvalue")!= lasttime ){if(tradetype=="BUY"){//Long Call and Exit Long Put OptionExitOrder("SELL","PE");PlaceOrder("BUY","CE",quantity);}if(tradetype=="SELL"){//Short Put and Exit Short Call OptionExitOrder("BUY","CE");PlaceOrder("SELL","PE",quantity);}StaticVarSetText(static_name_+"buyCoverAlgo_barvalue",lasttime);StaticVarSet(static_name_+"buyCoverAlgo",1);//Algo Order was triggered, no more order on this bar}elseif((AlgoBuy != True OR AlgoCover != True)){StaticVarSet(static_name_+"buyCoverAlgo",0);StaticVarSetText(static_name_+"buyCoverAlgo_barvalue","");}if(AlgoBuy==True AND AlgoCover != True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Long Call and Exit Long Put OptionPlaceOrder("BUY","CE",quantity);}if(tradetype=="SELL"){//Short PutPlaceOrder("SELL","PE",quantity);}StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);StaticVarSet(static_name_+"buyAlgo",1);//Algo Order was triggered, no more order on this bar}elseif(AlgoBuy != True){StaticVarSet(static_name_+"buyAlgo",0);StaticVarSetText(static_name_+"buyAlgo_barvalue","");}if(AlgoSell==true AND AlgoShort != True AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Exit Long Call OptionExitOrder("SELL","CE");}if(tradetype=="SELL"){//Exit Short Put OptionExitOrder("BUY","PE");}StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);StaticVarSet(static_name_+"sellAlgo",1);//Algo Order was triggered, no more order on this bar}elseif(AlgoSell != True ){StaticVarSet(static_name_+"sellAlgo",0);StaticVarSetText(static_name_+"sellAlgo_barvalue","");}if(AlgoShort==True AND AlgoSell==True AND StaticVarGet(static_name_+"ShortSellAlgo")==0 AND StaticVarGetText(static_name_+"ShortSellAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Long Put and Exit Long Call OptionExitOrder("SELL","CE");PlaceOrder("BUY","PE",quantity);}if(tradetype=="SELL"){//Short Call and Exit Short Put OptionExitOrder("BUY","PE");PlaceOrder("SELL","CE",quantity);}StaticVarSetText(static_name_+"ShortsellAlgo_barvalue",lasttime);StaticVarSet(static_name_+"ShortSellAlgo",1);//Algo Order was triggered, no more order on this bar}elseif((AlgoShort != True OR AlgoSell != True)){StaticVarSet(static_name_+"ShortSellAlgo",0);StaticVarSetText(static_name_+"ShortsellAlgo_barvalue","");}if(AlgoShort==True AND AlgoSell != True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND StaticVarGetText(static_name_+"ShortAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Long PutPlaceOrder("BUY","PE",quantity);}if(tradetype=="SELL"){//Short CallPlaceOrder("SELL","CE",quantity);}StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime);StaticVarSet(static_name_+"ShortAlgo",1);//Algo Order was triggered, no more order on this bar}elseif(AlgoShort != True ){StaticVarSet(static_name_+"ShortAlgo",0);StaticVarSetText(static_name_+"ShortAlgo_barvalue","");}if(AlgoCover==true AND AlgoBuy != True AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Exit Long Put OptionExitOrder("SELL","PE");}if(tradetype=="SELL"){//Exit Short Call OptionExitOrder("BUY","CE");}StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime);StaticVarSet(static_name_+"CoverAlgo",1);//Algo Order was triggered, no more order on this bar}elseif(AlgoCover != True ){StaticVarSet(static_name_+"CoverAlgo",0);StaticVarSetText(static_name_+"CoverAlgo_barvalue","");}}elseif(EnableAlgo =="LongOnly"){if(AlgoBuy==True AND StaticVarGet(static_name_+"buyAlgo")==0 AND StaticVarGetText(static_name_+"buyAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Long Call and Exit Long Put OptionPlaceOrder("BUY","CE",quantity);}if(tradetype=="SELL"){//Short PutPlaceOrder("SELL","PE",quantity);}StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);StaticVarSet(static_name_+"buyAlgo",1);//Algo Order was triggered, no more order on this bar}elseif(AlgoBuy != True ){StaticVarSet(static_name_+"buyAlgo",0);StaticVarSetText(static_name_+"buyAlgo_barvalue","");}if(AlgoSell==True AND StaticVarGet(static_name_+"sellAlgo")==0 AND StaticVarGetText(static_name_+"sellAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Exit Long Call OptionExitOrder("SELL","CE");}if(tradetype=="SELL"){//Exit Short Put OptionExitOrder("BUY","PE");}StaticVarSet(static_name_+"sellAlgo",1);StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);}elseif(AlgoSell != True ){StaticVarSet(static_name_+"sellAlgo",0);StaticVarSetText(static_name_+"sellAlgo_barvalue","");}}elseif(EnableAlgo =="ShortOnly"){if(AlgoShort==True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND StaticVarGetText(static_name_+"ShortAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Long PutPlaceOrder("BUY","PE",quantity);}if(tradetype=="SELL"){//Short CallPlaceOrder("SELL","CE",quantity);}StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime);StaticVarSet(static_name_+"ShortAlgo",1);//Algo Order was triggered, no more order on this bar}elseif(AlgoShort != True ){StaticVarSet(static_name_+"ShortAlgo",0);StaticVarSetText(static_name_+"ShortAlgo_barvalue","");}if(AlgoCover==true AND StaticVarGet(static_name_+"CoverAlgo")==0 AND StaticVarGetText(static_name_+"CoverAlgo_barvalue")!= lasttime){if(tradetype=="BUY"){//Exit Long Put OptionExitOrder("SELL","PE");}if(tradetype=="SELL"){//Exit Short Call OptionExitOrder("BUY","CE");}StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime);StaticVarSet(static_name_+"CoverAlgo",1);//Algo Order was triggered, no more order on this bar}elseif(AlgoCover != True){StaticVarSet(static_name_+"CoverAlgo",0);StaticVarSetText(static_name_+"CoverAlgo_barvalue","");}}}_SECTION_END();
3)Ensure Log Window is open to capture the Trace Logs
4)Enter the Spot Symbol, Expiry Date, Strike Interval, Offset, and Trade Delay
Note : 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
7)Bingo Now you can Send ATM Option Orders from Amibroker by connecting any of your Buy/Sell Trading System. To Send ITM/OTM Option Orders to adjust the offset parameters from the Properties section
2 thoughts on “Send Smart Options Execution Orders from Futures or Spot Signals in Amibroker using Algomojo Platform”
Bhuvaneswaran K
I am getting error message as Error 42, Include file doesn’t exists . Ass per the instructions given I have copied the Header Include Option Execution Module to include folder, but still I get error. Please guide me to clear the issue
I am getting error message as Error 42, Include file doesn’t exists . Ass per the instructions given I have copied the Header Include Option Execution Module to include folder, but still I get error. Please guide me to clear the issue
Can this be done using MT4 EA?