Quant Algorithm: Dotnet C# Code to Calculate ATM (At the Money) Strike for Underlying Stocks/Index for Option Derivative Market.
Kamal K Chanchal
Experience in Building Trading Application (C++/C#) |Python| MYSQL
Let's Talk about ATM: At the money?
ATM In Simple Terms: At the money (ATM) are calls and puts whose strike price is at or very near to the current market price of the underlying security.
In the world of algorithms, we want to automate our Trading ideology to the System or let?Computer programs?handle it. In the?Options Derivative market, Strike Selection Plays an important Role, When?Volatility?increases. Derivatives options Classify Strikes based on?ATM,?ITM?(In the Money), and?OTM?(Out of Money).
Once we Calculate the ATM, we can calculate the other two ITM or ATMs by Comparision
Algorithm:
4. Logic: Calculate?Deviation?(From Spot price to Nearest Strike Interval) and?Adjust Base Strike using?Remainder or Mod?Function
Let's Create a POCO class (DataStructure)
?
internal class AtTheMoney //ATM?
{
? ? ?public string OptionSymbol { get; set; }
? ? ?public System.Double Interval { get; set; }
? ? ?public System.Double Spotprice { get; set; }
? ? ?public System.Double ATMStrike { get; set; }
?}
?
Now, Add to Custom List of Type "AtTheMoney" .
private List<AtTheMoney> ListGetATM = new List<AtTheMoney>();
Now Load all Requirements (data) to this List Except Property "ATMStrike". This will be the output,?what we are expecting!!
Below Code Structure to Calculate ATM Strike using?C# Dotnet, the output will be got Stored in the List itself.
? ? ? ? private void CalculateATMStrikePriceAtLive()
{
? ? /* Req :
? ? ?* Option Symbol StrikePrice Lists??
? ? ?* Option Stock Spot Price
? ? ?* RoundOf : Nifty 50 , BankNifty 100
? ? ?* Logic : Comparision = < > */
? ? try
? ? {
? ? ? ? foreach (var record in ListGetATM)
? ? ? ? {
? ? ? ? ? ? int tokenSpot = GetTokenOfSymbol(record.OptionSymbol);
? ? ? ? ? ? if (tokenSpot < 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? continue; // By Displaying "invalid trading Symbol"
? ? ? ? ? ? }
? ? ? ? ? ? double spotprice = 0.0;
? ? ? ? ? ? spotprice = LiveQuoteGlobal.mLtp[tokenSpot];
? ? ? ? ? ? // logic
? ? ? ? ? ? if (spotprice == 0.0)
? ? ? ? ? ? ? ? continue; //early return
? ? ? ? ? ? if (record.Interval == 0.0)
? ? ? ? ? ? ? ? continue;//early return
? ? ? ? ? ? double remainder = spotprice % record.Interval;
? ? ? ? ? ? double ATMStrike = 0.0;
? ? ? ? ? ? double adjustedstrike = spotprice - remainder; // TO Save Computational
? ? ? ? ? ? if (remainder > (record.Interval / 2)) // ROUNDOFF
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ATMStrike = adjustedstrike + record.Interval;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ATMStrike = adjustedstrike;
? ? ? ? ? ? }
? ? ? ? ? ? if (Math.Abs(spotprice - ATMStrike) <= record.Interval) //Validation
? ? ? ? ? ? {
? ? ? ? ? ? ? ? record.ATMStrike = ATMStrike; //ADD The Strike
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? catch (Exception ex)
? ? {
? ? ? ? Debug.WriteLine($" Error While Calculating ATM Strike : {ex.Message}");
? ? }
}
领英推荐
Validation
In the above code, If?the Spot Price?is 0 than an early return or passes to the Next iteration.
In the same When,?Interval?is set to 0, in this case, we have to return or pass to the next stocks/Index.
SPOT PRICE :
?int tokenSpot = GetTokenOfSymbol(record.OptionSymbol);
if (tokenSpot < 0)
{
? ? continue; // By Displaying "invalid trading Symbol"
}
double spotprice = 0.0;
spotprice = LiveQuoteGlobal.mLtp[tokenSpot];
// logic
if (spotprice == 0.0)
? ? continue; //early return
? ? ?
In My Coding Structure, the Above dotnet code helps to get Spot Price using?TokenOfSymbol?aligned with Broker API Structure.
Adjusted Strike and Mod Function
if (remainder > (record.Interval / 2)) // ROUNDOF
{
? ? ATMStrike = adjustedstrike + record.Interval;
}
else
{
? ? ATMStrike = adjustedstrike;
}
Our Logic is Developed using C# for?the Indian Stock market?and Indian Broker which provides Rela time Live Data and Historical Data Through?API.?Using the above code, we can easily calculate the ATM strike price.
I will be coming up with More Algorithms related to the Indian Stock market using Dotnet, C#, or Python.
Thanks for Reading.