import numpy as np import streamlit as st from plotly.subplots import make_subplots from streamlit_plotly_events import plotly_events import plotly.graph_objects as go import calculator import data_retriever import trends import ui import bidder def run(): col1, col2 = st.columns(2) with col1: exchange_names = data_retriever.get_exchange_code_names() exchanges_selectbox = st.selectbox( 'Exchange:', exchange_names, index=exchange_names.index('US exchanges (NYSE, Nasdaq)') ) exchange_name = exchanges_selectbox exchange_index = exchange_names.index(exchange_name) exchange = data_retriever.get_exchange_codes()[exchange_index] symbols = data_retriever.get_symbols(exchange) symbols_selectbox = st.selectbox( 'Stock:', symbols, index=symbols.index('AAPL') ) symbol = symbols_selectbox with col2: # max time period st.text_input('No. of years look-back:', value=1, key="years_back") years_back = int(st.session_state.years_back) weeks_back = years_back * 12 * 4 symbol_prices = data_retriever.get_current_stock_data(symbol, weeks_back) if not any(symbol_prices): return dates = symbol_prices.index.format() # back test st.text_input('No. of days back-test:', value=0, key="backtest_period") n_days_back = int(st.session_state.backtest_period) end_date = data_retriever.n_days_before(data_retriever.today(), n_days_back) symbol_prices_backtest = symbol_prices[symbol_prices.index <= end_date] backtest_dates = symbol_prices_backtest.index.format() # symbol candlestick graph candleFigure = make_subplots(rows=1, cols=1) ui.create_candlestick(candleFigure, dates, symbol_prices, symbol, 'Price') # plot all candleFigure.update_layout(title="Symbol Ticker", xaxis_title='Date', yaxis_title="Price per Share", template='plotly_dark') # use this to add markers on other graphs for click points on this graph selected_points = [] # selected_points = plotly_events(candleFigure, click_event=True, hover_event=False, select_event=False, key='3', override_height=800) st.plotly_chart(candleFigure, use_container_width=True) if st.checkbox('Trends'): period = st.slider(label='period', min_value=7, max_value=140, value=14, step=7) indicatorFigure = make_subplots(rows=1, cols=1) ui.create_line(indicatorFigure, dates, symbol_prices['Close'], "Close price", color="red") # # indicators # indicator_datasets = indicators.create_indicators(dates, symbol_prices) # ui.create_indicators(fig1, indicator_datasets) # trend lines # sigma_multiplier = st.slider(label='min trend deviation', min_value=1.0, max_value=10.0, value=3.0, step=0.1) # sigma_dates, sigma_values = trends.trend_line(symbol_prices_backtest.index, np.array(symbol_prices_backtest['Close']), min_trend_size=21, sigma_multiplier=sigma_multiplier) # ui.create_markers(candleFigure, sigma_dates, sigma_values, f"{sigma_multiplier} Deviations from regression line", 'Deviations') # vwap vwap = trends.vwap(symbol_prices_backtest) ui.create_line(candleFigure, backtest_dates, vwap, "Volume Weighted Average Price (VWAP)", "VWAP") # bollinger bands bollinger_dates, bollinger_low, bollinger_high = trends.bollinger_bands(backtest_dates, symbol_prices_backtest) ui.create_fill_area(candleFigure, bollinger_dates, bollinger_low, bollinger_high, "Bollinger Bands") # trend trend = trends.linear_regression_trend(symbol_prices['Close'][:], period=period) ui.create_line(indicatorFigure, dates[period:], trend, f"Trend: {period} day", color='blue') # trend deviation area price_deviation_from_trend = [price - trend_val for price, trend_val in zip(symbol_prices['Close'][period:], trend)] deviation_price_offset = [price + dev for price, dev in zip(symbol_prices['Close'][period:], price_deviation_from_trend)] ui.create_fill_area(indicatorFigure, dates[period:], trend, deviation_price_offset, f"Trend Deviation: {period} day trend", color="rgba(100,0,100,0.3)") # sma line deltas_sma = trends.sma(symbol_prices['Close'], period) ui.create_line(indicatorFigure, dates[period:], deltas_sma, f"SMA {period} day", color='green') # bids init_val = 1000.0 buy_and_hold_val = bidder.buy_and_hold(init_val, symbol_prices['Close']) st.write(f"BUY AND HOLD: {buy_and_hold_val}") bid_dates, bid_vals = trends.bids(dates, symbol_prices, period) strat_val = bidder.buy_rule(init_val, bid_dates, bid_vals, dates, symbol_prices['Close']) ui.create_markers(indicatorFigure, bid_dates, bid_vals, "Bids", "Price") st.write(f"STRAT: {strat_val}") indicatorFigure.update_layout(title="Indicators", xaxis_title='Date', yaxis_title="Price", template='plotly_dark') st.plotly_chart(indicatorFigure, use_container_width=True) # doxy st.markdown( f"Trend deviation: indicates the extent by which the price is deviating from its current trajectory. " f"The trajectory is the linear regression of the price over {period} days.") st.markdown('''