Financial Derivatives: Introduction and Implementation in Python
Financial Derivatives are financial instruments whose value is derived from an underlying asset such as a stock, commodity, or currency. They’re used to hedge against price fluctuations and speculations. It is a contract whose value depends on an underlying asset.
Derivatives are traded on an exchange or over the counter (OTC). Over-the-counter is trading securities via a broker network instead of an exchange. The price of derivatives is determined by using complex mathematical models like the Black-Scholes model for options pricing. These models take into account the price of the underlying asset, the time remaining until the contract expiry, and the volatility of the underlying asset.
Types of Derivatives:
Options : An agreement between two parties to buy or sell an asset at a pre-determined future date for a specific price.
American Options v/s European Options :
American Options can be exercised at any time between the date of purchase and the expiration date.
European Options can only be exercised at the end of their lives on their expiration date.
The names have nothing to do with geography.
Futures : An agreement between two parties for the purchase and delivery of an asset at an agreed-upon price at a future date. Futures contracts are commonly used to hedge against price fluctuations in commodities and financial instruments.
Forwards : These are agreements to buy or sell an underlying asset at a certain price at a future date. Forward contracts are similar to futures, but they are traded over the counter.
Swaps : A contract through which two parties exchange cash flows from two different financial instruments.
Now we’ll be using the library nsepython in order to get the historical data of various derivatives etc.
!pip install nsepython
from nsepython import *
print(indices)
# Open Interest Data
oi_data, ltp, crontime = oi_chain_builder("RELIANCE","latest","full")
print(oi_data)
print(ltp)
print(crontime)
#It will print the price of BankNIFTY Futures of the current expiry.
print(nse_quote_ltp("RELIANCE","latest","Fut"))
print(nse_quote_ltp("RELIANCE","latest","PE",2300))
"""
inp = 0 means current expiry. It is the default.
inp = 1 means next expiry and so on.
"""
payload=nse_optionchain_scrapper('BANKNIFTY')
print(nse_optionchain_ltp(payload,43000,"CE",0,"sell"))
#PCR
payload=nse_optionchain_scrapper('BANKNIFTY')
print(pcr(payload,1))
# the date of the expiry and number of days to expiry (DTE)
payload=nse_optionchain_scrapper('BANKNIFTY')
currentExpiry,dte=nse_expirydetails(payload,1)
print(currentExpiry)
print(dte)
# fii/dii data
print(nse_fiidii())
# Top gainers and loosers of the day
print(nse_get_top_gainers())
print(nse_get_top_losers())
We’ll explore more such interesting topics in the coming articles, stay tuned!!
Thank you for reading this article :)
Code is avaliable on my git profile: @Pratham2012
Don’t forget to follow and share it with your Investor friends.