Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

flattrade api - fetch crude oil options ltp

Last updated on 5 days ago
K
KevinMember
Posted 5 days ago
import pandas as pd
import requests
from io import StringIO
import datetime
import urllib3

# Suppress SSL certificate verification warning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Step 1: Fetch the CSV from the URL
url = "https://flattrade.s3.ap-south-1.amazonaws.com/scripmaster/Commodity.csv"
response = requests.get(url, verify=False) # Added verify=False to bypass SSL verification

# Step 2: Read the CSV into a pandas DataFrame
csv_data = response.text
df = pd.read_csv(StringIO(csv_data))

# Clean any leading/trailing spaces in column names
df.columns = df.columns.str.strip()

# Step 3: Get this month in 3-letter format (e.g., "DEC" for December)
this_month = datetime.datetime.now().strftime('%b').upper()

# Step 4: Filter the data based on expiry month and the given strike price (e.g., 6000)
strike_price = 6000

# Ensure that the column exists before filtering
if 'Strike' in df.columns:
filtered_data = df[df['Strike'] == strike_price]
filtered_data = filtered_data[filtered_data['Expiry'].str.contains(this_month)]

# Step 5: Now extract individual values for the different options (CE and PE for CRUDEOIL and CRUDEOIL MINI)
# Extract values for CRUDEOIL (CE and PE)
crude_ce_token = filtered_data.loc[filtered_data['Optiontype'] == 'CE', 'Token'].iloc[0] if not filtered_data[filtered_data['Optiontype'] == 'CE'].empty else None
crude_ce_trade_symbol = filtered_data.loc[filtered_data['Optiontype'] == 'CE', 'Tradingsymbol'].iloc[0] if not filtered_data[filtered_data['Optiontype'] == 'CE'].empty else None
crude_pe_token = filtered_data.loc[filtered_data['Optiontype'] == 'PE', 'Token'].iloc[0] if not filtered_data[filtered_data['Optiontype'] == 'PE'].empty else None
crude_pe_trade_symbol = filtered_data.loc[filtered_data['Optiontype'] == 'PE', 'Tradingsymbol'].iloc[0] if not filtered_data[filtered_data['Optiontype'] == 'PE'].empty else None

# Extract values for CRUDEOIL MINI (CE and PE)
crude_mini_ce_token = filtered_data.loc[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'CE'), 'Token'].iloc[0] if not filtered_data[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'CE')].empty else None
crude_mini_ce_trade_symbol = filtered_data.loc[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'CE'), 'Tradingsymbol'].iloc[0] if not filtered_data[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'CE')].empty else None
crude_mini_pe_token = filtered_data.loc[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'PE'), 'Token'].iloc[0] if not filtered_data[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'PE')].empty else None
crude_mini_pe_trade_symbol = filtered_data.loc[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'PE'), 'Tradingsymbol'].iloc[0] if not filtered_data[(filtered_data['Symbol'] == 'CRUDEOILM') & (filtered_data['Optiontype'] == 'PE')].empty else None

# Print results for CRUDEOIL and CRUDEOIL MINI
print(f"CRUDECETOKEN: {crude_ce_token}")
print(f"CRUDECETRADESYMBOL: {crude_ce_trade_symbol}")
print(f"CRUDEPETOKEN: {crude_pe_token}")
print(f"CRUDEPETRADESYMBOL: {crude_pe_trade_symbol}")
print(f"CRUDEMINICETOKEN: {crude_mini_ce_token}")
print(f"CRUDEMINICETRADESYMBOL: {crude_mini_ce_trade_symbol}")
print(f"CRUDEMINIPETOKEN: {crude_mini_pe_token}")
print(f"CRUDEMINIPETRADESYMBOL: {crude_mini_pe_trade_symbol}")

# Fetch LTP for the tokens
def fetch_ltp(token):
# Replace with the actual endpoint and parameters for Flattrade API to fetch LTP
ltp_url = f"https://api.flattrade.com/ltp/{token}" # This is just an example URL
response = requests.get(ltp_url)
if response.status_code == 200:
data = response.json()
return data.get('ltp', None)
else:
print(f"Error fetching LTP for token {token}")
return None

# Fetch LTP for each token
if crude_ce_token:
ltp_crude_ce = fetch_ltp(crude_ce_token)
print(f"LTP for CRUDECETOKEN: {ltp_crude_ce}")

if crude_pe_token:
ltp_crude_pe = fetch_ltp(crude_pe_token)
print(f"LTP for CRUDEPETOKEN: {ltp_crude_pe}")

if crude_mini_ce_token:
ltp_crude_mini_ce = fetch_ltp(crude_mini_ce_token)
print(f"LTP for CRUDEMINICETOKEN: {ltp_crude_mini_ce}")

if crude_mini_pe_token:
ltp_crude_mini_pe = fetch_ltp(crude_mini_pe_token)
print(f"LTP for CRUDEMINIPETOKEN: {ltp_crude_mini_pe}")

else:
print("Column 'Strike' does not exist in the DataFrame!")
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 11
Newest Member: Jhilam