Spaces:
Running
Running
File size: 7,859 Bytes
c83b2fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | # 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',
# )
|