import pandas as pd
import numpy as np
import [Link] as plt
from breeze_connect import BreezeConnect
import urllib
import zipfile
import io
import os
import threading
import time
import warnings
[Link]('ignore')
breeze = BreezeConnect(api_key=str(app_key))
breeze.generate_session(api_secret=str(secret_key),session_token=str(session_key))
#Atm Strike price
Quotes = breeze.get_quotes(stock_code="BSESEN",
exchange_code="BSE",
expiry_date="",
product_type="cash",
right="",
strike_price="0")
Ltp = Quotes["Success"][0]["ltp"]
ATM = round((Quotes["Success"][1]["ltp"])/100)*100
print(ATM)
# How to Find Strike price of particular Premium
#Call
df =breeze.get_option_chain_quotes(stock_code="BSESEN",
exchange_code="BFO",
product_type="options",
expiry_date="2025-02-04T06:00:00.000Z",
right="call")["Success"]
df = [Link](df)
#print(df)
strike_price_call = [Link][(df["ltp"] >= 70) & (df["ltp"] <= 100),
"strike_price"].astype(int).tolist()[0]
print(strike_price_call)
print(type(strike_price_call))
#Put
df =breeze.get_option_chain_quotes(stock_code="BSESEN",
exchange_code="BFO",
product_type="options",
expiry_date="2025-02-04T06:00:00.000Z",
right="put")["Success"]
df = [Link](df)
#print(df)
strike_price_put = [Link][(df["ltp"] >= 70) & (df["ltp"] <= 100),
"strike_price"].astype(int).tolist()[0]
print(strike_price_put)
print(type(strike_price_put))
#Find LTP through Get_Quotes
#Call
premium_call = breeze.get_quotes(stock_code="BSESEN",
exchange_code="BFO",
expiry_date="2025-02-04T06:00:00.000Z",
product_type="options",
right="call",
strike_price = str(strike_price_call))["Success"][0]["ltp"]
print(f"Premium_call_{strike_price_call} :{premium_call}")
#Put
premium_put = breeze.get_quotes(stock_code="BSESEN",
exchange_code="BFO",
expiry_date="2025-02-04T06:00:00.000Z",
product_type="options",
right="put",
strike_price = str(strike_price_put))["Success"][0]["ltp"]
print(f"Premium_put_{strike_price_put} :{premium_put}")
#Strangle Place Order
#Call
call_placeorder = breeze.place_order(stock_code="BSESEN",
exchange_code="BFO",
product="options",
action="buy",
order_type="market",
stoploss="",
quantity="20",
price="",
validity="day",
validity_date="2022-08-30T06:00:00.000Z",
disclosed_quantity="0",
expiry_date="2025-02-04T06:00:00.000Z",
right="call",
strike_price=str(strike_price_call))
print(call_placeorder)
call_orderID = call_placeorder["Success"]["order_id"]
print(f"call_orderID : {call_orderID}")
#Put
put_placeorder = breeze.place_order(stock_code="BSESEN",
exchange_code="BFO",
product="options",
action="buy",
order_type="market",
stoploss="",
quantity="20",
price="",
validity="day",
validity_date="2022-08-30T06:00:00.000Z",
disclosed_quantity="0",
expiry_date="2025-02-04T06:00:00.000Z",
right="put",
strike_price=str(strike_price_put))
print(put_placeorder)
put_orderID = put_placeorder["Success"]["order_id"]
print(f"put_orderID : {put_orderID}")
#Find the executed price using tradebook
#call
Call_executedprice = float(breeze.get_trade_detail(exchange_code="BFO",
order_id=call_orderID)["Success"][0]["execution_price"])
#put
Put_executedprice = float(breeze.get_trade_detail(exchange_code="BFO",
order_id=put_orderID)["Success"][0]["execution_price"])
print(f"Call_executedprice: {Call_executedprice}")
print(f"Put_executedprice: {Put_executedprice}")
#Find Stock Token using security master file
# Step 1: Download and Extract the Data
url = "[Link]
response = [Link](url, stream=True)
if response.status_code == 200:
print("Downloading ZIP file...")
# Open the ZIP file from the response content
zip_data = [Link]([Link]([Link]))
# Extract to a folder
extract_folder = "extracted_data"
zip_data.extractall(extract_folder)
print(f"Extracted files to: {extract_folder}")
else:
print(f"Failed to download ZIP file. HTTP Status: {response.status_code}")
exit()
# Step 2: Load the [Link] File into a DataFrame
txt_file_path = [Link](extract_folder, "[Link]")
if not [Link](txt_file_path):
print("[Link] not found in the extracted files.")
exit()
print("Loading data into DataFrame...")
df = pd.read_csv(txt_file_path, delimiter=',', engine='python')
# Step 3: Ensure Proper Data Types for Filtering
df['StrikePrice'] = pd.to_numeric(df['StrikePrice'], errors='coerce')
df['ExpiryDate'] = pd.to_datetime(df['ExpiryDate'], format='%d-%b-
%Y').[Link]('%d-%b-%Y')
# Step 4: Define the Function to Fetch Token
def get_token(df, instrument_name, short_name, expiry_date, strike_price, series,
option_type):
"""
Filters the FONSEScripMaster DataFrame based on the input criteria and returns
the Token.
"""
# Filter the DataFrame
filtered_df = df[
(df['InstrumentName'].[Link]() == instrument_name.strip()) &
(df['ShortName'].[Link]() == short_name.strip()) &
(df['ExpiryDate'].[Link]() == expiry_date.strip()) &
(df['StrikePrice'] == float(strike_price)) &
(df['Series'].[Link]() == [Link]()) &
(df['OptionType'].[Link]() == option_type.strip())
]
# Return the Token if a match is found
if not filtered_df.empty:
return filtered_df['Token'].iloc[0]
else:
return None
# Step 5: Input Parameters
instrument_name = "OPTIND"
short_name = "BSESEN"
expiry_date = "04-Feb-2025"
strike_price = str(strike_price_call)
series = "OPTION"
option_type = "CE"
# Step 6: Fetch the Token
token_call = get_token(df, instrument_name, short_name, expiry_date, strike_price,
series, option_type)
# Step 7: Output the Result
if token_call:
print(f"Token found: {token_call}")
else:
print("No matching Token found.")
instrument_name = "OPTIND"
short_name = "BSESEN"
expiry_date = "04-Feb-2025"
strike_price = str(strike_price_put)
series = "OPTION"
option_type = "PE"
# Step 6: Fetch the Token
token_put = get_token(df, instrument_name, short_name, expiry_date, strike_price,
series, option_type)
# Step 7: Output the Result
if token_put:
print(f"Token_put found: {token_put}")
else:
print("No matching Token_put found.")
#converting token into subscription model
call_token= "8.1!"+str(token_call)
put_token= "8.1!"+str(token_put)
print(f"call_token_websocket :{call_token}")
print(f"put_token_websocket :{put_token}")
# Websocket subscribtion and pnl calculation
# Connect to WebSocket
breeze.ws_connect()
# Global dictionary to track PnL values
pnl_tracker = {"pnl_call": None, "pnl_put": None}
lock = [Link]() # Ensure thread-safe operations
# Function to calculate Call PnL
def calculate_pnl_call(last):
pnl_call = last - Call_executedprice # Adjust premium logic if needed
with lock:
pnl_tracker["pnl_call"] = pnl_call
#print(f"✅ pnl_call calculated: {pnl_call}")
# Function to calculate Put PnL
def calculate_pnl_put(last):
pnl_put = last - Put_executedprice # Adjust premium logic if needed
with lock:
pnl_tracker["pnl_put"] = pnl_put
#print(f"✅ pnl_put calculated: {pnl_put}")
# Callback function to receive ticks
def on_ticks(ticks):
global pnl_tracker
symbol = [Link]("symbol")
last = [Link]("last")
if symbol == call_token: # Call option
[Link](target=calculate_pnl_call, args=(int(last),)).start()
if symbol == put_token: # Put option
[Link](target=calculate_pnl_put, args=(int(last),)).start()
# Wait for both threads to complete (simulate real-time processing)
[Link](2) # Small delay to ensure both threads complete
with lock:
if pnl_tracker["pnl_call"] is not None and pnl_tracker["pnl_put"] is not
None:
total_pnl = (pnl_tracker["pnl_call"] + pnl_tracker["pnl_put"])*20
print(f"✅ Combined total_pnl: {total_pnl}")
else:
print("⏳ Waiting for both PnL values to update...")
# Assign the callback function
breeze.on_ticks = on_ticks
breeze.subscribe_feeds(stock_token=[call_token,put_token])
Response:
{'message': "Stock ['8.1!839069', '8.1!839861'] subscribed successfully"}
⏳ Waiting for both PnL values to update...
✅ Combined total_pnl: -76.99999999999989
✅ Combined total_pnl: -76.99999999999989
✅ Combined total_pnl: -76.99999999999989
✅ Combined total_pnl: -76.99999999999989
✅ Combined total_pnl: -76.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -96.99999999999989
✅ Combined total_pnl: -116.99999999999989
✅ Combined total_pnl: -116.99999999999989
✅ Combined total_pnl: -116.99999999999989