Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
st.sidebar.title('Bali Scalping Strategy Analyzer')
|
8 |
+
ticker = st.sidebar.text_input('Enter ticker symbol', 'AAPL')
|
9 |
+
start_date = st.sidebar.date_input('Start date', pd.to_datetime('2020-01-01'))
|
10 |
+
end_date = st.sidebar.date_input('End date', pd.to_datetime('2020-12-31'))
|
11 |
+
time_frame = st.sidebar.selectbox('Select time frame', ['60m', '1d'])
|
12 |
+
analyze_button = st.sidebar.button('Analyze')
|
13 |
+
|
14 |
+
st.title('Bali Scalping Strategy Visualization')
|
15 |
+
st.markdown("""
|
16 |
+
This app visualizes the 'Bali' scalping strategy with buy and sell signals.
|
17 |
+
Enter the stock ticker, select the date range and time frame, and click 'Analyze'.
|
18 |
+
""")
|
19 |
+
|
20 |
+
def fetch_data(ticker, start_date, end_date, interval):
|
21 |
+
data = yf.download(ticker, start=start_date, end=end_date, interval=interval)
|
22 |
+
return data
|
23 |
+
|
24 |
+
def calculate_lwma(data, period=48):
|
25 |
+
weights = np.arange(1, period + 1)
|
26 |
+
return data.rolling(window=period).apply(lambda prices: np.dot(prices, weights) / weights.sum(), raw=True)
|
27 |
+
|
28 |
+
def calculate_trend_envelopes(data, period=2):
|
29 |
+
ma = data.rolling(window=period).mean()
|
30 |
+
deviation = data.rolling(window=period).std()
|
31 |
+
upper_band = ma + (0.02 * deviation)
|
32 |
+
lower_band = ma - (0.02 * deviation)
|
33 |
+
return upper_band, lower_band
|
34 |
+
|
35 |
+
def calculate_dss(data, period=10):
|
36 |
+
stoch = ((data - data.rolling(window=period).min()) /
|
37 |
+
(data.rolling(window=period).max() - data.rolling(window=period).min())) * 100
|
38 |
+
dss = stoch.rolling(window=period).mean()
|
39 |
+
return dss
|
40 |
+
|
41 |
+
if analyze_button:
|
42 |
+
data = fetch_data(ticker, start_date, end_date, time_frame)
|
43 |
+
data['LWMA'] = calculate_lwma(data['Close'])
|
44 |
+
upper_band, lower_band = calculate_trend_envelopes(data['Close'])
|
45 |
+
data['DSS'] = calculate_dss(data['Close'])
|
46 |
+
|
47 |
+
data['Buy'] = ((data['Close'] > data['LWMA']) & (data['Close'] > upper_band.shift()) & (data['DSS'] > 80))
|
48 |
+
data['Sell'] = ((data['Close'] < data['LWMA']) & (data['Close'] < lower_band.shift()) & (data['DSS'] < 20))
|
49 |
+
|
50 |
+
fig = go.Figure()
|
51 |
+
fig.add_trace(go.Candlestick(x=data.index,
|
52 |
+
open=data['Open'],
|
53 |
+
high=data['High'],
|
54 |
+
low=data['Low'],
|
55 |
+
close=data['Close'],
|
56 |
+
name="Candlestick"))
|
57 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['LWMA'],
|
58 |
+
line=dict(color='red', width=1.5), name='LWMA'))
|
59 |
+
fig.add_trace(go.Scatter(x=data.index, y=upper_band,
|
60 |
+
line=dict(color='blue', width=0.7), name='Upper Band'))
|
61 |
+
fig.add_trace(go.Scatter(x=data.index, y=lower_band,
|
62 |
+
line=dict(color='orange', width=0.7), name='Lower Band'))
|
63 |
+
buy_signals = data[data['Buy']]
|
64 |
+
sell_signals = data[data['Sell']]
|
65 |
+
fig.add_trace(go.Scatter(x=buy_signals.index, y=buy_signals['Close'],
|
66 |
+
mode='markers', marker_symbol='triangle-up',
|
67 |
+
marker_line_color="green", marker_color="green",
|
68 |
+
marker_line_width=2, marker_size=10, name='Buy Signal'))
|
69 |
+
fig.add_trace(go.Scatter(x=sell_signals.index, y=sell_signals['Close'],
|
70 |
+
mode='markers', marker_symbol='triangle-down',
|
71 |
+
marker_line_color="red", marker_color="red",
|
72 |
+
marker_line_width=2, marker_size=10, name='Sell Signal'))
|
73 |
+
fig.update_layout(title='Bali Scalping Strategy Visualization',
|
74 |
+
xaxis_title='Date',
|
75 |
+
yaxis_title='Price',
|
76 |
+
xaxis_rangeslider_visible=False)
|
77 |
+
st.plotly_chart(fig, use_container_width=True)
|
78 |
+
|