Quant Algorithm: Dotnet C# Code to Calculate ATM (At the Money) Strike for Underlying Stocks/Index for Option Derivative Market.

Quant Algorithm: Dotnet C# Code to Calculate ATM (At the Money) Strike for Underlying Stocks/Index for Option Derivative Market.

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:

  1. We Need Underlying Stocks/ Index;
  2. The spot price of Underlying Stocks/ Index
  3. Interval (For eg:?Nifty has 50, BANKNIFTY has 100)

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)

No alt text provided for this image
POCO class for AtTheMoney
?
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.

No alt text provided for this image
Output for the above code


I will be coming up with More Algorithms related to the Indian Stock market using Dotnet, C#, or Python.



Thanks for Reading.

要查看或添加评论,请登录

Kamal K Chanchal的更多文章

社区洞察

其他会员也浏览了