Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from datetime import timedelta
|
| 7 |
+
|
| 8 |
+
import hvplot as hv
|
| 9 |
+
import holoviews as hvs
|
| 10 |
+
import panel as pn
|
| 11 |
+
import hvplot.pandas
|
| 12 |
+
|
| 13 |
+
import yfinance as yf
|
| 14 |
+
|
| 15 |
+
from datasets import load_dataset
|
| 16 |
+
|
| 17 |
+
pn.extension('bokeh', template='bootstrap')
|
| 18 |
+
|
| 19 |
+
def _extract_raw_data(ticker):
|
| 20 |
+
df = yf.Ticker(ticker)
|
| 21 |
+
return df.history(period="6mo", interval="1d").reset_index()
|
| 22 |
+
|
| 23 |
+
def _transform_data(raw_data: pd.DataFrame):
|
| 24 |
+
data = raw_data[["Date", "Open", "High", "Low", "Close", "Volume"]].copy(deep=True).rename(columns={
|
| 25 |
+
"Date": "time",
|
| 26 |
+
"Open": "open",
|
| 27 |
+
"High": "high",
|
| 28 |
+
"Low": "low",
|
| 29 |
+
"Close": "close",
|
| 30 |
+
"Volume": "volume",
|
| 31 |
+
})
|
| 32 |
+
t_delta = timedelta(hours=1)
|
| 33 |
+
data['time_start'] = data.time - 9*t_delta # rectangles start
|
| 34 |
+
data['time_end'] = data.time + 9*t_delta # rectangles end
|
| 35 |
+
data['positive'] = ((data.close - data.open)>0).astype(int)
|
| 36 |
+
return data
|
| 37 |
+
|
| 38 |
+
def make_candle_stick(ticker):
|
| 39 |
+
raw_data = _extract_raw_data(ticker = ticker)
|
| 40 |
+
data = _transform_data(raw_data=raw_data)
|
| 41 |
+
_delta = np.median(np.diff(data.time))
|
| 42 |
+
candlestick = hvs.Segments(data, kdims=['time', 'low', 'time', 'high']) * hvs.Rectangles(data, kdims=['time_start','open', 'time_end', 'close'], vdims=['positive'])
|
| 43 |
+
candlestick = candlestick.redim.label(Low='Values')
|
| 44 |
+
candlechart = pn.Column(candlestick.opts(hvs.opts.Rectangles(color='positive', cmap=['red', 'green'], responsive=True), hvs.opts.Segments(color='black', height=400, responsive=True , show_grid=True)) ,
|
| 45 |
+
data.hvplot(x="time", y="volume", kind="line", responsive=True, height=200).opts( show_grid=True) )
|
| 46 |
+
# data.hvplot(y="volume", kind="bar", responsive=True, height=200) )
|
| 47 |
+
return candlechart
|
| 48 |
+
|
| 49 |
+
# Function to convert DataFrame to CSV
|
| 50 |
+
def get_csv(df):
|
| 51 |
+
sio = io.StringIO()
|
| 52 |
+
df.to_csv(sio, index=False)
|
| 53 |
+
sio.seek(0)
|
| 54 |
+
return sio
|
| 55 |
+
|
| 56 |
+
# Function to convert the 'Ticker' column to a comma-separated string in a text file
|
| 57 |
+
def get_text(df):
|
| 58 |
+
tickers = df['Ticker'].tolist()
|
| 59 |
+
tickers_str = ','.join(tickers)
|
| 60 |
+
sio = io.StringIO()
|
| 61 |
+
sio.write(tickers_str)
|
| 62 |
+
sio.seek(0)
|
| 63 |
+
return sio
|
| 64 |
+
|
| 65 |
+
dataset = load_dataset('AmirTrader/optioncharts.io', data_files='DFtickerTotal.csv')
|
| 66 |
+
df = dataset['train'].to_pandas()
|
| 67 |
+
|
| 68 |
+
#widget
|
| 69 |
+
ticker = pn.widgets.AutocompleteInput(name='Ticker', options=list(df.Ticker) , placeholder='Write Ticker here همین جا',value='ALL', restrict=False)
|
| 70 |
+
Industry = pn.widgets.CheckBoxGroup( name='Select Industry', value=list(set(df.Industry)), options=list(set(df.Industry)), inline=True)
|
| 71 |
+
Sector = pn.widgets.CheckBoxGroup( name='Select Sector', value=list(set(df.Sector)), options=list(set(df.Sector)), inline=False)
|
| 72 |
+
MarketCap = pn.widgets.FloatSlider(name='Market Capital (B$)', start=0, end=4000, step=1, value=1)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def get_DF(DF,ticker, Sector,MarketCap):
|
| 76 |
+
if ticker and ticker!="ALL":
|
| 77 |
+
table1 = pn.widgets.Tabulator(DF.query("Ticker == @ticker"), height=200, widths=200, show_index=False)
|
| 78 |
+
chart1 = make_candle_stick(ticker)
|
| 79 |
+
return pn.Column(table1,chart1)
|
| 80 |
+
else:
|
| 81 |
+
# return pn.widgets.Tabulator( DF.query(" Sector in @Sector & MarketCap>@MarketCap"), height=800, widths=200, show_index=False)
|
| 82 |
+
return pn.widgets.Tabulator( DF, height=800, widths=200, show_index=False)
|
| 83 |
+
|
| 84 |
+
pn.extension('tabulator')
|
| 85 |
+
bound_plot = pn.bind(get_DF, DF=df,ticker=ticker, Sector=Sector ,MarketCap=MarketCap)
|
| 86 |
+
|
| 87 |
+
pn.Column(pn.Row(pn.Column(ticker, MarketCap, Sector),bound_plot)).servable(title="Option Volatility View")
|
| 88 |
+
|