File size: 2,734 Bytes
cbd6be0
 
 
e0caec9
cbd6be0
e0caec9
 
 
cbd6be0
e0caec9
 
 
 
 
 
 
 
 
 
 
 
 
cbd6be0
e0caec9
 
 
 
cbd6be0
e0caec9
cbd6be0
e0caec9
 
cbd6be0
 
 
 
e0caec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objs as go

def fetch_data(ticker, start_date, end_date):
    data = yf.download(ticker, start=start_date, end=end_date)
    return data

def calculate_indicators(data):
    # Exponential Moving Averages for trend
    data['EMA50'] = data['Close'].ewm(span=50, adjust=False).mean()
    data['EMA200'] = data['Close'].ewm(span=200, adjust=False).mean()
    
    # Relative Strength Index for momentum
    delta = data['Close'].diff()
    gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
    rs = gain / loss
    data['RSI'] = 100 - (100 / (1 + rs))
    
    return data

def identify_signals(data):
    data['Buy Signal'] = (data['EMA50'] > data['EMA200']) & (data['RSI'] < 30)
    data['Sell Signal'] = (data['EMA50'] < data['EMA200']) & (data['RSI'] > 70)
    return data

def plot_data(data):
    fig = go.Figure()

    # Adding price trace
    fig.add_trace(go.Candlestick(x=data.index,
                                 open=data['Open'],
                                 high=data['High'],
                                 low=data['Low'],
                                 close=data['Close'], name='Candlestick'))

    # Adding EMAs
    fig.add_trace(go.Scatter(x=data.index, y=data['EMA50'], name='EMA 50', line=dict(color='blue', width=1.5)))
    fig.add_trace(go.Scatter(x=data.index, y=data['EMA200'], name='EMA 200', line=dict(color='red', width=1.5)))

    # Adding Buy and Sell signals
    buys = data[data['Buy Signal']]
    sells = data[data['Sell Signal']]
    fig.add_trace(go.Scatter(x=buys.index, y=buys['Close'], mode='markers', name='Buy Signal', marker=dict(symbol='triangle-up', size=12, color='green')))
    fig.add_trace(go.Scatter(x=sells.index, y=sells['Close'], mode='markers', name='Sell Signal', marker=dict(symbol='triangle-down', size=12, color='red')))

    # Layout updates
    fig.update_layout(title='Momentum Reversal Trading Strategy Visualization', xaxis_title='Date', yaxis_title='Price', template='plotly_dark')
    fig.update_xaxes(rangeslider_visible=True)
    
    return fig

def main():
    st.title("Momentum Reversal Trading Strategy Visualization")
    ticker = st.text_input("Enter the ticker symbol, e.g., 'AAPL'")
    start_date = st.date_input("Select the start date")
    end_date = st.date_input("Select the end date")
    
    if st.button("Analyze"):
        data = fetch_data(ticker, start_date, end_date)
        data = calculate_indicators(data)
        data = identify_signals(data)
        fig = plot_data(data)
        st.plotly_chart(fig, use_container_width=True)

if __name__ == "__main__":
    main()