Commit
·
19efd8e
1
Parent(s):
fcd4739
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import mplfinance as mpf
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import datetime
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@st.cache(show_spinner=False)
|
| 9 |
+
def get_graph(df, no_days):
|
| 10 |
+
plot_df = df.iloc[-no_days:]
|
| 11 |
+
plot_df = plot_df.set_index('Date')
|
| 12 |
+
filename = 'CandelPlot.png'
|
| 13 |
+
mpf.plot(plot_df,type='candle',mav=(21, 55), style="yahoo", figratio=(60,20), linecolor='#00ff00', savefig=filename)
|
| 14 |
+
|
| 15 |
+
def buy_sell_decision(st, lt, close, last):
|
| 16 |
+
# checking for null values
|
| 17 |
+
if st == 'None' or lt == 'None':
|
| 18 |
+
return 'Dont trade', close, last
|
| 19 |
+
|
| 20 |
+
if st >= lt:
|
| 21 |
+
if close >= st:
|
| 22 |
+
return 'Buy', last, None
|
| 23 |
+
|
| 24 |
+
elif close <= st:
|
| 25 |
+
return 'sell', None, last
|
| 26 |
+
|
| 27 |
+
if lt > st :
|
| 28 |
+
return 'Dont trade', close, last
|
| 29 |
+
|
| 30 |
+
def get_SMA_df(df):
|
| 31 |
+
moving_average_df = df
|
| 32 |
+
moving_average_df['St21'] = moving_average_df['Close'].rolling(window = 21, min_periods = 21).mean()
|
| 33 |
+
moving_average_df['Lt55'] = moving_average_df['Close'].rolling(window = 55, min_periods = 55).mean()
|
| 34 |
+
moving_average_df = moving_average_df.fillna('None')
|
| 35 |
+
|
| 36 |
+
# computing buy asd sell days
|
| 37 |
+
st_list = moving_average_df['St21'].to_list()
|
| 38 |
+
lt_list = moving_average_df['Lt55'].to_list()
|
| 39 |
+
close_price_list = moving_average_df['Close'].to_list()
|
| 40 |
+
last_price_list = moving_average_df['Last'].to_list()
|
| 41 |
+
|
| 42 |
+
buy_sell_list = []
|
| 43 |
+
buy_price_list = []
|
| 44 |
+
sell_price_list = []
|
| 45 |
+
|
| 46 |
+
for idx, value in enumerate(st_list):
|
| 47 |
+
dec_label, buy_price, sell_price = buy_sell_decision(st_list[idx], lt_list[idx], close_price_list[idx], last_price_list[idx])
|
| 48 |
+
|
| 49 |
+
buy_sell_list.append(dec_label)
|
| 50 |
+
buy_price_list.append(buy_price)
|
| 51 |
+
sell_price_list.append(sell_price)
|
| 52 |
+
|
| 53 |
+
moving_average_df['buy/sell'], moving_average_df['buy_price'], moving_average_df['sell_price'] = buy_sell_list, buy_price_list, sell_price_list
|
| 54 |
+
return moving_average_df
|
| 55 |
+
|
| 56 |
+
def get_action_dict(sma_df, date):
|
| 57 |
+
df = sma_df.set_index('Date')
|
| 58 |
+
try:
|
| 59 |
+
row = df.loc[datetime.datetime(date.year, date.month, date.day)]
|
| 60 |
+
action_dict = {}
|
| 61 |
+
action_dict['symbol'] = row['Symbol']
|
| 62 |
+
action_dict['action'] = row['buy/sell']
|
| 63 |
+
if action_dict['action'] == 'Buy':
|
| 64 |
+
price = row['buy_price']
|
| 65 |
+
else:
|
| 66 |
+
price = row['sell_price']
|
| 67 |
+
|
| 68 |
+
action_dict['price'] = price
|
| 69 |
+
except:
|
| 70 |
+
action_dict = None
|
| 71 |
+
return action_dict
|
| 72 |
+
|
| 73 |
+
def main():
|
| 74 |
+
st.set_page_config(
|
| 75 |
+
page_title="Project",
|
| 76 |
+
layout="wide",
|
| 77 |
+
initial_sidebar_state="expanded"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
st.write('Upload a CSV file')
|
| 81 |
+
|
| 82 |
+
col1, col2 = st.columns(2)
|
| 83 |
+
|
| 84 |
+
with col1:
|
| 85 |
+
spectra = st.file_uploader("upload file", type={"csv", "txt"})
|
| 86 |
+
if spectra is not None:
|
| 87 |
+
df = pd.read_csv(spectra)
|
| 88 |
+
df['Date'] = df['Date'].astype('datetime64[ns]')
|
| 89 |
+
sma_df =get_SMA_df(df)
|
| 90 |
+
|
| 91 |
+
plot_days = st.slider('Select No. of days to plot graph', 56, len(df), 100)
|
| 92 |
+
st.write("Plot graph for last", plot_days, 'days')
|
| 93 |
+
|
| 94 |
+
get_image = st.button('Get graph')
|
| 95 |
+
|
| 96 |
+
if spectra is not None:
|
| 97 |
+
with col2:
|
| 98 |
+
trade_date = st.date_input("Select Day for trading", max(df['Date']), min_value=min(df['Date']), max_value=max(df['Date']))
|
| 99 |
+
get_action = st.button('Check trade or No trade')
|
| 100 |
+
ref_flag = False
|
| 101 |
+
|
| 102 |
+
if get_action:
|
| 103 |
+
dict_action = get_action_dict(sma_df, trade_date)
|
| 104 |
+
if dict_action == None:
|
| 105 |
+
st.error("Something Went Wrong please try with different date", icon="🚨")
|
| 106 |
+
else:
|
| 107 |
+
symbol = dict_action['symbol']
|
| 108 |
+
action = dict_action['action']
|
| 109 |
+
price = dict_action['price']
|
| 110 |
+
st.success(f'''
|
| 111 |
+
* Symbol : {symbol}
|
| 112 |
+
* Action : **{action}**
|
| 113 |
+
* {action} Price : {price}
|
| 114 |
+
''')
|
| 115 |
+
ref_flag, get_image = True, True
|
| 116 |
+
|
| 117 |
+
if get_image:
|
| 118 |
+
get_graph(df, plot_days)
|
| 119 |
+
image = Image.open('CandelPlot.png')
|
| 120 |
+
st.image(image, use_column_width='auto', caption='Blue Line = 21-SMA, Orange Line = 55-SMA')
|
| 121 |
+
|
| 122 |
+
if __name__ == '__main__':
|
| 123 |
+
main()
|