Send Smart Options Execution Orders from Futures or Spot Signals in Amibroker using Algomojo Platform

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.

Supported Brokers

All Algomojo Supported Brokers

Supported Amibroker Version: 6.0 or above

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/FuturesOrders to be Placed
Buy SignalLong Call
Sell SignalExit Long Call
Short SignalLong Put
Cover SignalExit Long Put
Buy and Cover SignalLong Call
Exit Long Put
Short and Cover SignalLong Put
Exit Long Call

Strategy Parameter Settings for Option Short and Exit Strategy

Trading Signal in Spot/FuturesOrders to be Placed
Buy SignalShort Put
Sell SignalExit Short Put
Short SignalShort Call
Cover SignalExit Short Call
Buy and Cover SignalShort Put
Exit Short Call
Short and Cover SignalShort 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.


/*
Algomojo - Smart Spot/Futures to Options Trading Module with Split Order Controls
Created By : Rajandran R(Founder - Marketcalls / Co-Founder Algomojo )
Created on : 25 Jan 2023.
Website : www.marketcalls.in / www.algomojo.com
*/


_SECTION_BEGIN("Algomojo - Broker Controls");


// Send orders even if Amibroker is minimized or Chart is not active
RequestTimedRefresh(1, False); 

//Creating Input Controls for Setting Order Related Information

broker =Paramlist("Broker","an|ab|fs|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");
expiry = ParamStr("Expiry Date","25JAN23");

exchange = ParamList("Exchange","NFO|MCX",0); 
Symbol = ParamStr("Underlying Symbol","NIFTY-I.NFO");
iInterval= Param("Strike Interval",50,1,10000,1);
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));



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


EnableTextOutput(False);



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


SetForeign(Symbol);
spotC = Ref(Close,-1);
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 Strikes
if(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 Functions
function Placeorder(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" ); 
}

function ExitOrder(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 Option
				ExitOrder("SELL","PE"); 
				PlaceOrder("BUY","CE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Put and Exit Short Call Option
				ExitOrder("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
                
        
            }
            else if ((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 Option
				PlaceOrder("BUY","CE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Put
				PlaceOrder("SELL","PE",quantity);
					
				}
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				
				ExitOrder("SELL","CE"); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Put Option
				ExitOrder("BUY","PE"); 
				
				}
                
                StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				ExitOrder("SELL","CE"); 
				PlaceOrder("BUY","PE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Call and Exit Short Put Option
				ExitOrder("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
                
            }
            else if ((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 Put
				PlaceOrder("BUY","PE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Call
				PlaceOrder("SELL","CE",quantity);
							
				}
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				
				ExitOrder("SELL","PE"); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Call Option
				ExitOrder("BUY","CE"); 
				
				}
               
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (AlgoCover != True )
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue","");
            }
        }
        
      else if(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 Option
				PlaceOrder("BUY","CE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Put
				PlaceOrder("SELL","PE",quantity);
					
				}
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            
            
            else if (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 Option
				
				ExitOrder("SELL","CE"); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Put Option
				ExitOrder("BUY","PE"); 
				
				}
                StaticVarSet(static_name_+"sellAlgo",1);
                StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);
            }
            else if (AlgoSell != True )
            {  
                StaticVarSet(static_name_+"sellAlgo",0);
                StaticVarSetText(static_name_+"sellAlgo_barvalue","");
            
            }
            
        } 
        else if(EnableAlgo == "ShortOnly")
        {
            if (AlgoShort==True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime)
            {
				if(tradetype=="BUY")
				{
				//Long Put
				PlaceOrder("BUY","PE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Call
				PlaceOrder("SELL","CE",quantity);
							
				}
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				
				ExitOrder("SELL","PE"); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Call Option
				ExitOrder("BUY","CE"); 
				
				}
               
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (AlgoCover != True)
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue","");
            }
        } 
        
    }
    
  
_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 : Rajandran R(Founder - Marketcalls / Co-Founder Algomojo )
Created on : 06 Dec 2022.
Website : www.marketcalls.in / www.algomojo.com
*/


_SECTION_BEGIN("Algomojo - Broker Controls");


// Send orders even if Amibroker is minimized or Chart is not active
RequestTimedRefresh(1, False); 

//Creating Input Controls for Setting Order Related Information

broker =Paramlist("Broker","an|ab|fs|fy|gc|pt|sm|tc|up|zb|ze",0);
ver = ParamStr("API Version ","v1");
apikey = ParamStr("apikey","xxxxxxxxxxxxxxxxx"); //Enter your API key here
apisecret = ParamStr("secretkey","xxxxxxxxxxxxxxxxx"); //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");
expiry = ParamStr("Expiry Date","25JAN23"); //Set the Option Expiry based on the options symbol in Algomojo Terminal.

exchange = ParamList("Exchange","NFO|MCX",0); 
Symbol = ParamStr("Underlying Symbol","NIFTY-I.NFO"); //replace your datafeed symbol heere to set the underlying symbi=ol
iInterval= Param("Strike Interval",50,1,10000,1);
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));



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


EnableTextOutput(False);



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


SetForeign(Symbol);
spotC = Ref(Close,-1);
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 Strikes
if(tradetype=="BUY")
{
ExitStrikeCE = ValueWhen(Ref(Buy,-Entrydelay),strikeCE); 
ExitStrikePE = ValueWhen(Ref(Short,-Entrydelay),strikePE);

if(broker=="tc")
{
entrysym = WriteIf(buycontinue,spot+expiry+"C"+strikeCE,WriteIf(shortcontinue,spot+expiry+"P"+strikePE,""));
exitsym = WriteIf(buycontinue,spot+expiry+"P"+ExitStrikePE,WriteIf(shortcontinue,spot+expiry+"C"+ExitStrikeCE,""));
}
else
{
entrysym = WriteIf(buycontinue,spot+expiry+"C"+strikeCE,WriteIf(shortcontinue,spot+expiry+"P"+strikePE,""));
exitsym = WriteIf(buycontinue,spot+expiry+"P"+ExitStrikePE,WriteIf(shortcontinue,spot+expiry+"C"+ExitStrikeCE,""));
}


}
if(tradetype=="SELL")
{
ExitStrikeCE = ValueWhen(Ref(Short,-Entrydelay),strikeCE); 
ExitStrikePE = ValueWhen(Ref(Buy,-Entrydelay),strikePE);
if(broker=="tc")
{
entrysym = WriteIf(buycontinue,spot+expiry+"P"+strikePE,WriteIf(shortcontinue,spot+expiry+"C"+strikeCE,""));
exitsym = WriteIf(buycontinue,spot+expiry+"C"+ExitStrikeCE,WriteIf(shortcontinue,spot+expiry+"P"+ExitStrikePE,""));
}
else
{
entrysym = WriteIf(buycontinue,spot+expiry+"P"+strikePE,WriteIf(shortcontinue,spot+expiry+"P"+strikeCE,""));
exitsym = WriteIf(buycontinue,spot+expiry+"C"+ExitStrikeCE,WriteIf(shortcontinue,spot+expiry+"C"+ExitStrikePE,""));
}
}

printf("\n\n\nEntry Symbol : "+entrysym);
printf("\nExit Symbol : "+exitsym);


_SECTION_END();



//Buy and Sell Order Functions
function Placeorder(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" ); 
}

function ExitOrder(action,OptionType,orderqty,position_size)
{


 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\":\""+position_size+"\",            
            \"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 Option
				ExitOrder("SELL","PE",quantity,0); 
				PlaceOrder("BUY","CE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Put and Exit Short Call Option
				ExitOrder("BUY","CE",quantity,0); 
				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
                
        
            }
            else if ((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 Option
				PlaceOrder("BUY","CE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Put
				PlaceOrder("SELL","PE",quantity);
					
				}
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				
				ExitOrder("SELL","CE",quantity,0); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Put Option
				ExitOrder("BUY","PE",quantity,0); 
				
				}
                
                StaticVarSetText(static_name_+"sellAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"sellAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				ExitOrder("SELL","CE",quantity,0); 
				PlaceOrder("BUY","PE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Call and Exit Short Put Option
				ExitOrder("BUY","PE",quantity,0);
				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
                
            }
            else if ((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 Put
				PlaceOrder("BUY","PE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Call
				PlaceOrder("SELL","CE",quantity);
							
				}
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				
				ExitOrder("SELL","PE",quantity,0); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Call Option
				ExitOrder("BUY","CE",quantity,0); 
				
				}
               
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (AlgoCover != True )
            {   
                StaticVarSet(static_name_+"CoverAlgo",0);
                StaticVarSetText(static_name_+"CoverAlgo_barvalue","");
            }
        }
        
      else if(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 Option
				PlaceOrder("BUY","CE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Put
				PlaceOrder("SELL","PE",quantity);
					
				}
                StaticVarSetText(static_name_+"buyAlgo_barvalue",lasttime);
                StaticVarSet(static_name_+"buyAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            
            
            else if (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 Option
				
				ExitOrder("SELL","CE",quantity,0); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Put Option
				ExitOrder("BUY","PE",quantity,0); 
				
				}
                StaticVarSet(static_name_+"sellAlgo",0);
                StaticVarSetText(static_name_+"sellAlgo_barvalue","");
            }
            else if (AlgoSell != True )
            {  
                StaticVarSet(static_name_+"sellAlgo",0);
                StaticVarSetText(static_name_+"sellAlgo_barvalue","");
            
            }
            
        } 
        else if(EnableAlgo == "ShortOnly")
        {
            if (AlgoShort==True AND StaticVarGet(static_name_+"ShortAlgo")==0 AND StaticVarGetText(static_name_+"ShortAlgo_barvalue") != lasttime)
            {
				if(tradetype=="BUY")
				{
				//Long Put
				PlaceOrder("BUY","PE",quantity);
				
				
				}
				
				if(tradetype=="SELL")
				{
				//Short Call
				PlaceOrder("SELL","CE",quantity);
							
				}
                StaticVarSetText(static_name_+"ShortAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"ShortAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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 Option
				
				ExitOrder("SELL","PE",quantity); 
				
				}
				
				if(tradetype=="SELL")
				{
				//Exit Short Call Option
				ExitOrder("BUY","CE",quantity); 
				
				}
               
                StaticVarSetText(static_name_+"CoverAlgo_barvalue",lasttime); 
                StaticVarSet(static_name_+"CoverAlgo",1); //Algo Order was triggered, no more order on this bar
                
            }
            else if (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”

  1. 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

Leave a Comment

Your email address will not be published. Required fields are marked *