AmirTrader's picture
Upload 2 files
c83b2fb verified
raw
history blame
7.86 kB
# streamlit
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import os
import time
from dotenv import load_dotenv
from datetime import datetime
from utils import upload_to_hf_dataset, download_from_hf_dataset, load_hf_dataset
# Get current date and time
# current_datetime = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
current_datetime = datetime.now().strftime("%Y-%m-%d")
# Load environment variables from .env file
load_dotenv()
# Get the name of the HuggingFace dataset for TradingView to read from
dataset_name_TradingView_input = os.getenv("dataset_name_TradingView_input")
# Get the name of the HuggingFace dataset for YfOptions to export
dataset_name_YfOptions_output = os.getenv("dataset_name_YfOptions_output")
# Get the Hugging Face API token from the environment; either set in .env file or in the environment directly in GitHub
HF_TOKEN_YfOptions = os.getenv("HF_TOKEN_YfOptions")
# Set page configuration
st.set_page_config(
page_title="Option Data Screener App",
page_icon="📊",
layout="wide"
)
@st.cache_data
def get_TD_DF(current_datetime):
# Load lastest TradingView DataSet from HuggingFace Dataset which is always america.csv
# download_from_hf_dataset("america.csv", "AmirTrader/TradingViewData", HF_TOKEN_YfOptions)
DF = load_hf_dataset("america.csv", HF_TOKEN_YfOptions, dataset_name_TradingView_input)
# get ticker list by filtering only above 1 billion dollar company
# DF = pd.read_csv(f'america_2024-03-01.csv')
tickerlst = list(DF.query("`Market Capitalization`>10e9").Ticker)
# tickerlist = ['INDO', 'TSLA', 'AAPL', 'MSFT', 'GOOGL', 'AMZN', 'NFLX', 'META', 'NVDA', 'AMD', 'INTC', 'IBM', 'CSCO', 'ORCL', 'QCOM', 'TXN', 'AVGO', 'ADBE', 'CRM', 'NFLX', 'PYPL', 'SNAP']
return DF, tickerlst
@st.cache_data
def get_options_DF(current_datetime):
DF = load_hf_dataset("optionchain.csv", HF_TOKEN_YfOptions, dataset_name_YfOptions_output)
return DF
@st.cache_data
def convert_df(df):
return df.to_csv().encode('utf-8')
@st.cache_data
def get_options_merge(current_datetime):
DF, tickerlst = get_TD_DF(current_datetime)
DF_options_origin = get_options_DF(current_datetime)
DF_options_origin['Volume_OpenInterest_Ratio'] = DF_options_origin['volume'] / DF_options_origin['openInterest']
# Extract ticker from contractSymbol and merge dataframes
DF_options_origin['Ticker'] = DF_options_origin['contractSymbol'].str.extract(r'([A-Z]+)')
TD_interestedColumns = ['Ticker', 'Market Capitalization', 'Relative Volume']
DF_options_merged = pd.merge(DF_options_origin, DF[TD_interestedColumns], on='Ticker', how='left')
# Pivot the DataFrame to separate 'Call' and 'Put' for volume
volume_pivot = DF_options_merged.groupby(['Ticker', 'Type'])['volume'].sum().unstack()
volume_pivot.columns = ['Call_Volume', 'Put_Volume']
# Pivot the DataFrame to separate 'Call' and 'Put' for openInterest
openInterest_pivot = DF_options_merged.groupby(['Ticker', 'Type'])['openInterest'].sum().unstack()
openInterest_pivot.columns = ['Call_openInterest', 'Put_openInterest']
# Merge the volume and open interest DataFrames
merged_df = volume_pivot.merge(openInterest_pivot, left_index=True, right_index=True)
# Calculate Put/Call Volume Ratio
merged_df['Put_Call_Volume_Ratio'] = merged_df['Put_Volume'] / merged_df['Call_Volume'] #.replace(0, pd.NA)
# Calculate Put/Call Open Interest Ratio
merged_df['Put_Call_OI_Ratio'] = merged_df['Put_openInterest'] / merged_df['Call_openInterest'] #.replace(0, pd.NA)
DFtotal = pd.merge(DF_options_merged, merged_df, left_on='Ticker', right_index=True, how='left')
return DFtotal, tickerlst
DF_options, tickerlst = get_options_merge(current_datetime)
# Title
st.title("📊 Options Data Dashboard")
st.write(f'Number of avialable tickers: {len(tickerlst)}')
st.write(f'Number of options records: {len(DF_options)}')
# Display options data
st.header("Options Data")
# Sidebar
st.sidebar.header("Controls")
st.sidebar.markdown("### Filter Options Data")
# Add volume and open interest filters in sidebar
min_volume = st.sidebar.number_input("Minimum Volume", min_value=0, value=100)
min_open_interest = st.sidebar.number_input("Minimum Open Interest", min_value=0, value=100)
min_vol_oi_ratio = st.sidebar.number_input("Minimum Volume/Open Interest Ratio", min_value=0.0, value=0.5, step=0.1)
st.sidebar.markdown("---") # Add a horizontal line as a visual separator
st.sidebar.markdown("### Filter Stock Data")
min_relative_volume = st.sidebar.number_input("Minimum Relative Volume", min_value=0.0, value=1.5, step=0.1)
min_put_call_volume = st.sidebar.number_input("Minimum Put/Call Volume Ratio", min_value=0.0, value=0.0, step=0.1)
min_put_call_oi = st.sidebar.number_input("Minimum Put/Call OI Ratio", min_value=0.0, value=0.0, step=0.1)
# Filter the dataframe
filtered_df = DF_options[
(DF_options['volume'] >= min_volume) &
(DF_options['openInterest'] >= min_open_interest) &
(DF_options['Relative Volume'] >= min_relative_volume) &
(DF_options['Put_Call_Volume_Ratio'] >= min_put_call_volume) &
(DF_options['Put_Call_OI_Ratio'] >= min_put_call_oi) &
(DF_options['Volume_OpenInterest_Ratio'] >= min_vol_oi_ratio)
]
st.write(f"Filtered records: {len(filtered_df)} rows")
interestedColumns = ['contractSymbol' , 'volume', 'openInterest', 'impliedVolatility', 'Volume_OpenInterest_Ratio' , 'Relative Volume' , 'Put_Call_Volume_Ratio' , 'Put_Call_OI_Ratio' , ]
selected_columns = st.multiselect(
"Select columns to display",
options=filtered_df.columns.tolist(),
default=interestedColumns
)
if selected_columns:
st.dataframe(filtered_df[selected_columns])
# Download button for the DataFrame
csv = convert_df(filtered_df)
st.download_button(
label="Download Options Data as CSV",
data=csv,
file_name=f'options_data_{current_datetime}.csv',
mime='text/csv',
)
st.write(f"Filtered Tickers: {filtered_df['Ticker'].unique()} ")
st.sidebar.markdown("---") # Add a horizontal line as a visual separator
st.sidebar.header("Advanced")
# **Daily Change in Open Interest**
# Monitoring the increase or decrease in Open Interest compared to the previous day can indicate the inflow (rising OI) or outflow (declining OI) of capital. This factor helps assess the strength of the current trend.
st.sidebar.button("Daily Change in Open Interest ")
# contractSymbol
# lastTradeDate
# strike
# lastPrice
# bid
# ask
# change
# percentChange
# volume
# openInterest
# impliedVolatility
# inTheMoney
# contractSize
# currency
# Type
# expirationDate
# daysleft
# mark
# pricepercent
# pricepercentstrike
# interinsicvalue
# interinsicvalue%
# timevalue
# timevalue%
# breakevenprice
# # Create sample data
# np.random.seed(42)
# data = pd.DataFrame({
# 'x': np.random.randn(100),
# 'y': np.random.randn(100),
# 'category': np.random.choice(['A', 'B', 'C'], 100)
# })
# # Create two columns
# col1, col2 = st.columns(2)
# # First column - Scatter plot
# with col1:
# st.subheader("Scatter Plot")
# fig = px.scatter(data, x='x', y='y', color='category')
# st.plotly_chart(fig, use_container_width=True)
# # Second column - Bar chart
# with col2:
# st.subheader("Bar Chart")
# category_counts = data['category'].value_counts()
# fig = px.bar(x=category_counts.index, y=category_counts.values)
# st.plotly_chart(fig, use_container_width=True)
# # Add a checkbox
# if st.checkbox("Show raw data"):
# st.dataframe(data)
# # Add a download button
# @st.cache_data
# def convert_df(df):
# return df.to_csv().encode('utf-8')
# csv = convert_df(data)
# st.download_button(
# label="Download data as CSV",
# data=csv,
# file_name='sample_data.csv',
# mime='text/csv',
# )