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)

Code Snippet: ATM/ITM/OTM Call and Put calculation from Spot Prices: Amibroker AFL

1 min read

Here is a simple prototype code with a display dashboard for the System traders who want to calculate ATM Call or ATM Put from the underlying spot price/future price for options backtesting or automated execution purpose.

It is a simple straightforward calculation derived from underlying spot or futures data. Hence, Amibroker data subscription is required to compute ATM CE or ATM PE strikes. However, when it comes to backtesting one need an array of ATM CE & ATM PE strike price and hence static arrays are used to create & store an array of ATM CE & ATM PE symbols which can be further extended to the backtest or automate option trading ideas.

ATM/ITM/OTM Calculation – Amibroker AFL Code

//Coded by Rajandran R - Marketcalls
//website - www.marketcals.in
//Version 1.0.0.1
//Coded Date : 04/07/2021

//Symbol Format - NIFTY29JUL2115700CE     Spot+Expiry+StrikePrice+OptionType(CE/PE)

_SECTION_BEGIN("ATM/ITM/OTM Caclulation");

EnableTextOutput(False);

spot = Paramlist("Spot Symbol","NIFTY|BANKNIFTY");
expiry = ParamStr("Expiry Date","29JUL21");

offsetCE = Param("CE Offset",-1,-40,40,1);
offsetPE = Param("PE Offset",-1,-40,40,1);

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(spot == "NIFTY")
{
Symbol = "NIFTY 50.NSE_IDX";
iInterval= 50;
}

if(spot == "BANKNIFTY")
{
Symbol = "NIFTY BANK.NSE_IDX";
iInterval= 100;
}

SetForeign(Symbol);
spotC = Close;
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));
strikeCE = strike + (offsetCE * iInterval);
strikePE = strike - (offsetPE * iInterval);

//Derives the Symbol Format from the Strike Price
symCE = spot+expiry+strikeCE+"CE";
symPE = spot+expiry+strikePE+"PE";

printf("\nTrading CE Symbol :"+symCE);
printf("\nTrading PE Symbol :"+symPE);
printf("\n");
//Datafeed Symbol Format
SymbolCE = symCE+".NFO";
SymbolPE = symPE+".NFO";

printf("\nDatafeed CE Symbol :"+SymbolCE);
printf("\nDatafeed PE Symbol :"+SymbolPE);

//Call Options LTP
SetForeign(SymbolCE);
CEclose = Close;
RestorePriceArrays();

//Put Options LTP
SetForeign(SymbolPE);
PEclose = Close;
RestorePriceArrays();

//Dasboard

GfxSelectFont( "BOOK ANTIQUA", 12, 100 );
GfxSetBkMode( 1 ); 
GfxSetTextColor ( colorWhite ); 
GfxSelectSolidBrush( colorDarkGrey );

pxHeight = Status( "pxchartheight" ) ; 
xx = Status( "pxchartwidth"); 
x = 5; 
x2 = 340; 
y = pxHeight; 

GfxSelectPen( colorLightBlue, 1); // border color 

GfxRoundRect( x, y - 155, x2, y-30 , 7, 7 ) ; 
GfxTextOut( optionCEtype+" :" +symCE,13,y-130);
GfxTextOut("CE Premium - "+CEclose,13,y-110);
GfxTextOut( optionPEtype+" :" +symPE,13,y-90);
GfxTextOut("PE Premium - "+PEclose,13,y-70);


_SECTION_END();


_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

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)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

9 Replies to “Code Snippet: ATM/ITM/OTM Call and Put calculation from Spot…”

  1. Sorry, but absolutely rotten way to write the code. Bad choice of variable name also. Interval implies time. better name would be StrikeGap

    ///////////////////// SCALAR VALUE CREATION
    paramStrikeGap = Param(“Strike Interval”,50,1,5000,1);

    rootStrike = int( ( LastValue(Close) / paramStrikeGap ) ) * paramStrikeGap;

    callStrike = rootStrike – paramStrikeGap;
    putStrike = rootStrike + paramStrikeGap;

    //…. All code necesssary for display
    ///////////////////// VECTOR VALUE CREATION
    paramStrikeGap = Param(“Strike Interval”,50,1,5000,1);

    rootStrike = int( Close / paramStrikeGap ) * paramStrikeGap;

    callStrike = rootStrike – paramStrikeGap;
    putStrike = rootStrike + paramStrikeGap;

    //…. All code necesssary for display

    1. What we provided is a code snippet to built an array of ATM symbols (strings) for option backtesting purposes.
      Not just to display ATM CE or ATM PE strikes which one can do with simple math.

  2. Storing StaticVarSetText inside the loop slow down the whole process i have expanded this code to calculate OTM and ITM and its very slow, instead of storing StaticVarSetText inside the loop i simply call convert it to NumtoStr to print the output its 1000 times faster than above code, example if you calculate ATM,OTM,ITM in above manner it takes minimum 8 to 9 sec to execute where as numtostr way deliver the same result in 0.14 sec.

  3. //Derives the Symbol Format from the Strike Price

    Sir when I am trying to do this, making array of option symbol it is taking last entry in the symbol and making string, not array of string.

    I am using the code in explore to see all strangle price at various times

  4. Thanks, After calculation of ATM Strike,how to add the Option strike script in Amiboker watchlist dynamically so it gets dynamically pulled from Truedata.

  5. Hello Sir,
    Thanks for your code. I applied it in Amibroker. Everything works fine. only issue which i faced is the Premiums of CE & PE Strike is not correct. Means instead of showing strike premium, it is showing the Nifty/Nifty Future Price (Whichever is selected on chart). The Strike Symbols showing ok

    1. Probably you need to set the Spot symbols as provided in the code according to your data vendor. I used GDFL and so their symbol format for Nifty and Bank Nifty in the code.

Leave a Reply

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