File size: 3,782 Bytes
5080203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
import numpy as np

st.sidebar.title('Bali Scalping Strategy Analyzer')
ticker = st.sidebar.text_input('Enter ticker symbol', 'AAPL')
start_date = st.sidebar.date_input('Start date', pd.to_datetime('2020-01-01'))
end_date = st.sidebar.date_input('End date', pd.to_datetime('2020-12-31'))
time_frame = st.sidebar.selectbox('Select time frame', ['60m', '1d'])
analyze_button = st.sidebar.button('Analyze')

st.title('Bali Scalping Strategy Visualization')
st.markdown("""
This app visualizes the 'Bali' scalping strategy with buy and sell signals.
Enter the stock ticker, select the date range and time frame, and click 'Analyze'.
""")

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

def calculate_lwma(data, period=48):
    weights = np.arange(1, period + 1)
    return data.rolling(window=period).apply(lambda prices: np.dot(prices, weights) / weights.sum(), raw=True)

def calculate_trend_envelopes(data, period=2):
    ma = data.rolling(window=period).mean()
    deviation = data.rolling(window=period).std()
    upper_band = ma + (0.02 * deviation)
    lower_band = ma - (0.02 * deviation)
    return upper_band, lower_band

def calculate_dss(data, period=10):
    stoch = ((data - data.rolling(window=period).min()) / 
             (data.rolling(window=period).max() - data.rolling(window=period).min())) * 100
    dss = stoch.rolling(window=period).mean()
    return dss

if analyze_button:
    data = fetch_data(ticker, start_date, end_date, time_frame)
    data['LWMA'] = calculate_lwma(data['Close'])
    upper_band, lower_band = calculate_trend_envelopes(data['Close'])
    data['DSS'] = calculate_dss(data['Close'])

    data['Buy'] = ((data['Close'] > data['LWMA']) & (data['Close'] > upper_band.shift()) & (data['DSS'] > 80))
    data['Sell'] = ((data['Close'] < data['LWMA']) & (data['Close'] < lower_band.shift()) & (data['DSS'] < 20))
    
    fig = go.Figure()
    fig.add_trace(go.Candlestick(x=data.index,
                                 open=data['Open'],
                                 high=data['High'],
                                 low=data['Low'],
                                 close=data['Close'],
                                 name="Candlestick"))
    fig.add_trace(go.Scatter(x=data.index, y=data['LWMA'], 
                             line=dict(color='red', width=1.5), name='LWMA'))
    fig.add_trace(go.Scatter(x=data.index, y=upper_band, 
                             line=dict(color='blue', width=0.7), name='Upper Band'))
    fig.add_trace(go.Scatter(x=data.index, y=lower_band, 
                             line=dict(color='orange', width=0.7), name='Lower Band'))
    buy_signals = data[data['Buy']]
    sell_signals = data[data['Sell']]
    fig.add_trace(go.Scatter(x=buy_signals.index, y=buy_signals['Close'], 
                             mode='markers', marker_symbol='triangle-up',
                             marker_line_color="green", marker_color="green",
                             marker_line_width=2, marker_size=10, name='Buy Signal'))
    fig.add_trace(go.Scatter(x=sell_signals.index, y=sell_signals['Close'],
                             mode='markers', marker_symbol='triangle-down',
                             marker_line_color="red", marker_color="red",
                             marker_line_width=2, marker_size=10, name='Sell Signal'))
    fig.update_layout(title='Bali Scalping Strategy Visualization',
                      xaxis_title='Date',
                      yaxis_title='Price',
                      xaxis_rangeslider_visible=False)
    st.plotly_chart(fig, use_container_width=True)