strike project django user selected strike file
import pandas as pd
from django.db import connection
from multistrikeoi.utlis import utils as u
from datetime import datetime,timedelta
today_string=None
yesterday_string=None
tomorrow_string=None
class UserSelectedPrice:
def combined_oi_calculation(
symbol,
instrument,
strikeprice,
option_type,
expiery,
today,
tomorrow,
):
print(today)
print(tomorrow)
engine = connection
df = pd.read_sql_query(
f"""
SELECT symbol, expiry_date, strike_price, created_at, ticker, instrument_name, option_type, open_interest FROM indiacharts.fno_prices fp
WHERE created_at > '{today}' AND created_at < '{tomorrow}'
AND symbol = '{symbol}' AND instrument_name = '{instrument}' AND strike_price = {strikeprice}
AND option_type = '{option_type}'
ORDER BY created_at ASC
""",
con=engine,
)
df["created_at_ist"] = df["created_at"].apply(u.convert_utc_to_ist)
df["created_at_ist"] = pd.to_datetime(df["created_at_ist"])
df["rounded_created_at_ist"] = df["created_at_ist"].apply(
u.round_to_nearest_5_minutes
)
# Group by rounded_created_at_ist and calculate the sum of open_interest
oi_sum_df = (
df.groupby("rounded_created_at_ist")["open_interest"].sum().reset_index()
)
df["rounded_created_at_ist"] = pd.to_datetime(df["rounded_created_at_ist"])
oi_sum_df["rounded_created_at_ist"] = pd.to_datetime(
oi_sum_df["rounded_created_at_ist"]
)
# Merge the DataFrames on 'rounded_created_at_ist'
merged_df = pd.merge(df, oi_sum_df, on="rounded_created_at_ist", how="left")
merged_df.rename(columns={"open_interest_y": "Combined OI"}, inplace=True)
merged_df["expiry_date"] = merged_df["expiry_date"].astype(str)
merged_df = merged_df[merged_df["expiry_date"] == expiery]
return merged_df
def get_historical_eod_data(
symbol, instrument, strikeprice, option_type, expiery, yesterday_string
):
engine = connection
print("yesterday_string=",yesterday_string)
df = pd.read_sql_query(
f"""select symbol,expiry_date,strike_price,created_at,ticker, instrument_name,option_type,open_interest from indiacharts.fno_price_eod fpe
WHERE created_at ='{yesterday_string}'
AND symbol = '{symbol}' AND instrument_name = '{instrument}' AND strike_price = {strikeprice}
AND option_type = '{option_type}'
ORDER BY created_at ASC
""",
con=engine,
)
# Calculate the sum of open_interest and add it as a new column
combined_oi_eod = df["open_interest"].sum()
df["Combined OI EOD"] = combined_oi_eod
return df
Comments
Post a Comment