File size: 15,392 Bytes
68361fe 412fe5d 2672a77 68361fe d4a54d8 7e94170 2254e31 68361fe 9531975 68361fe 9531975 68361fe d636118 7e94170 9531975 7e94170 9531975 68361fe 9531975 7e94170 68361fe 2254e31 7e94170 2254e31 7e94170 2254e31 7e94170 2254e31 7e94170 2254e31 69c1129 2254e31 567b0a8 2d2132b 9ae6397 7cd88ce 2d2132b 567b0a8 e2c4e9d da48dbc 2254e31 7e94170 2254e31 7e94170 2254e31 a1219fd 2254e31 68361fe 2dad25f 7e94170 2dad25f 68361fe 7e94170 68361fe 7e94170 68361fe 7e94170 68361fe 7e94170 68361fe 69c1129 68361fe 2dad25f 68361fe 2dad25f 567b0a8 2d2132b 7cd88ce 2d2132b 567b0a8 |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# app.py
import ast
from datetime import datetime
import streamlit as st
import plotly.graph_objects as go
import pandas as pd
from utils.helper import *
# english
def main_algo_trader():
# Front-end Design
st.set_page_config(layout="wide")
st.write("# Welcome to Algorithmic Trading! A Quick Implementation👋")
with st.sidebar:
with st.expander("Expand/Collapse"):
st.markdown(
r"""
The following app is a simple demonstration of the growth stock strategy. For simplicity, we assume our research team hand over a pool of stocks. Amongst the pool of stocks, we can do the following:
- use `yfinance` library to download stock data live (for the sake of speed, please start with time frame "1mo");
- every period (time frame is a tuning parameter), we balance our portfolio (equal weight) by holding the top n stocks (n can be top quintile/quartile of stocks);
"""
)
# Main inputs
tickers = st.text_input(
"Enter tickers (comma-separated):",
"MSFT, AAPL, NVDA, GOOG, AMZN, META, LLY, AVGO, TSLA, JPM, V, WMT, UNH, MA, PG, HD, JNJ, ORCL, MRK, COST, ABBV, BAC, CRM, AMD, NFLX, ACN, ADBE, DIS, TMO, WFC, MCD, CSCO, ABT, QCOM, INTC, INTU, IBM, AMAT, CMCSA, AXP, PFE, NOW, AMGN, MU",
)
start_date = st.sidebar.date_input("Start date", pd.to_datetime("2001-01-01"))
end_date = st.sidebar.date_input(
"End date", pd.to_datetime(datetime.now().strftime("%Y-%m-%d"))
)
time_frame = st.sidebar.selectbox(
"Select Time Frame:",
[
"1mo",
"3mo",
],
)
top_n = st.sidebar.number_input("Top n stocks", min_value=1, value=3)
height_of_graph = st.sidebar.number_input(
"Height of the plot", min_value=500, value=750
)
# Process inputs
tickers_list = [ticker.strip() for ticker in tickers.split(",")]
# Run analysis on button click
if st.button("Run Analysis"):
with st.spinner("Downloading data and calculating returns..."):
stock_data = download_stock_data(
tickers_list,
start_date.strftime("%Y-%m-%d"),
end_date.strftime("%Y-%m-%d"),
w=time_frame,
)
returns_data = create_portfolio_and_calculate_returns(stock_data, top_n)
benchmark_sharpe_ratio = (
returns_data["benchmark"].mean() / returns_data["benchmark"].std()
)
portfolio_sharpe_ratio = (
returns_data["portfolio_returns"].mean()
/ returns_data["portfolio_returns"].std()
)
# Data for plotting
df = returns_data[
["rolling_benchmark", "rolling_portfolio_returns", "portfolio_history"]
]
df.index = pd.to_datetime(df.index, unit="ms")
# Create download file
@st.cache_data
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode("utf-8")
csv = convert_df(df)
# Create plot
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df.index,
y=df["rolling_benchmark"],
mode="lines",
name="Rolling Benchmark",
)
)
fig.add_trace(
go.Scatter(
x=df.index,
y=df["rolling_portfolio_returns"],
mode="lines",
name="Rolling Portfolio Returns",
)
)
for date, stocks in df["portfolio_history"].items():
fig.add_shape(
type="line",
x0=date,
y0=0,
x1=date,
y1=0,
line=dict(color="RoyalBlue", width=1),
)
fig.add_annotation(
x=date,
y=0.5,
text=str(stocks),
showarrow=False,
yshift=10,
textangle=-90,
font=dict(size=15), # You can adjust the size as needed
)
# Calculate means and standard deviations
benchmark_mean = returns_data["benchmark"].mean()
benchmark_std = returns_data["benchmark"].std()
portfolio_mean = returns_data["portfolio_returns"].mean()
portfolio_std = returns_data["portfolio_returns"].std()
# Update title text with additional information
if time_frame == "1mo":
some_n_based_on_time_frame = 12
in_a_year = 1000 * (1 + portfolio_mean) ** (some_n_based_on_time_frame)
in_50_years = 1000 * (1 + portfolio_mean) ** (
some_n_based_on_time_frame * 50
)
else:
some_n_based_on_time_frame = 4
in_a_year = 1000 * (1 + portfolio_mean) ** (some_n_based_on_time_frame)
in_50_years = 1000 * (1 + portfolio_mean) ** (
some_n_based_on_time_frame * 50
)
title_text = (
f"Performance:<br>"
f"Benchmark Sharpe Ratio = {benchmark_sharpe_ratio:.3f}, "
f"Portfolio Sharpe Ratio = {portfolio_sharpe_ratio:.3f}, "
f"based on time frame: {time_frame}<br>"
f"Benchmark => Mean: {benchmark_mean:.4f}, Std: {benchmark_std:.4f}; "
f"Portfolio => Mean: {portfolio_mean:.4f}, Std: {portfolio_std:.4f}<br>"
f"---<br>"
f"This may or may not be a small number, let's check under the following cases 1) 12-mon, and 2) 50-year returns on $1,000 USD: <br>"
f"$1,000*(1+{portfolio_mean:.4f})^({some_n_based_on_time_frame})={in_a_year}, <br>"
f"$1,000*(1+{portfolio_mean:.4f})^({some_n_based_on_time_frame}*50)={in_50_years}."
)
curr_max_num = max(
df.rolling_benchmark.max(), df.rolling_portfolio_returns.max()
)
fig.update_layout(
title=title_text,
xaxis_title="Date",
yaxis_title="Value",
yaxis=dict(range=[0, curr_max_num * 1.1]),
legend=dict(
orientation="h", x=0.5, y=-0.4, xanchor="center", yanchor="bottom"
),
height=height_of_graph,
)
st.plotly_chart(fig, use_container_width=True)
# Post-analysis
col1, col2 = st.columns(2)
with col1:
# Checkpoint: ask user whether they want portfolio weights
if csv:
try:
recent_selected_stocks = df["portfolio_history"][-1]
recent_selected_stocks = ", ".join(recent_selected_stocks)
st.success(
f"The algorithm suggests to hold the following stocks for the current month (equally weighted): {recent_selected_stocks}"
)
except:
st.warning(
"Oops! No data found due during API calls. Please refresh the screen and rerun the simulation."
)
with col2:
# Download
st.download_button(
label="Download data as CSV",
data=csv,
file_name=f"history_{end_date}.csv",
mime="text/csv",
)
# chinese
def main_algo_trader_chinese():
# Front-end Design
st.set_page_config(layout="wide")
st.write("# 欢迎来到算法交易!一个快速模拟平台👋")
with st.sidebar:
with st.expander("Expand/Collapse"):
st.markdown(
r"""
以下应用程序是成长股策略的简单演示。为简单起见,我们假设我们的研究团队交给了一些股票。在这些股票池中,我们可以做到以下几点:
- 使用 `yfinance` 库实时下载股票数据(为了速度,请从时间框架 "1mo" 开始);
- 每个周期(时间框架是一个调整参数),我们通过持有前 n 只股票(n 可以是股票的前五分之一/四分之一)来平衡我们的投资组合(等权)。
"""
)
# Main inputs
tickers = st.text_input(
"使用英文键入输入股票代码(以逗号分隔):",
"MSFT, AAPL, NVDA, GOOG, AMZN, META, LLY, AVGO, TSLA, JPM, V, WMT, UNH, MA, PG, HD, JNJ, ORCL, MRK, COST, ABBV, BAC, CRM, AMD, NFLX, ACN, ADBE, DIS, TMO, WFC, MCD, CSCO, ABT, QCOM, INTC, INTU, IBM, AMAT, CMCSA, AXP, PFE, NOW, AMGN, MU",
)
start_date = st.sidebar.date_input("开始日期", pd.to_datetime("2001-01-01"))
end_date = st.sidebar.date_input(
"结束日期", pd.to_datetime(datetime.now().strftime("%Y-%m-%d"))
)
time_frame = st.sidebar.selectbox(
"选择时间框架:",
[
"1mo",
"3mo",
],
)
top_n = st.sidebar.number_input("选择前 n 支股票", min_value=1, value=3)
height_of_graph = st.sidebar.number_input("图像高度", min_value=500, value=750)
# Process inputs
tickers_list = [ticker.strip() for ticker in tickers.split(",")]
# Run analysis on button click
if st.button("运行分析"):
with st.spinner("下载数据并计算回报..."):
stock_data = download_stock_data(
tickers_list,
start_date.strftime("%Y-%m-%d"),
end_date.strftime("%Y-%m-%d"),
w=time_frame,
)
returns_data = create_portfolio_and_calculate_returns(stock_data, top_n)
benchmark_sharpe_ratio = (
returns_data["benchmark"].mean() / returns_data["benchmark"].std()
)
portfolio_sharpe_ratio = (
returns_data["portfolio_returns"].mean()
/ returns_data["portfolio_returns"].std()
)
# Data for plotting
df = returns_data[
["rolling_benchmark", "rolling_portfolio_returns", "portfolio_history"]
]
df.index = pd.to_datetime(df.index, unit="ms")
# Create download file
@st.cache_data
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode("utf-8")
csv = convert_df(df)
# Create plot
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df.index,
y=df["rolling_benchmark"],
mode="lines",
name="Rolling Benchmark",
)
)
fig.add_trace(
go.Scatter(
x=df.index,
y=df["rolling_portfolio_returns"],
mode="lines",
name="Rolling Portfolio Returns",
)
)
for date, stocks in df["portfolio_history"].items():
fig.add_shape(
type="line",
x0=date,
y0=0,
x1=date,
y1=0,
line=dict(color="RoyalBlue", width=1),
)
fig.add_annotation(
x=date,
y=0.5,
text=str(stocks),
showarrow=False,
yshift=10,
textangle=-90,
font=dict(size=15), # You can adjust the size as needed
)
# Calculate means and standard deviations
benchmark_mean = returns_data["benchmark"].mean()
benchmark_std = returns_data["benchmark"].std()
portfolio_mean = returns_data["portfolio_returns"].mean()
portfolio_std = returns_data["portfolio_returns"].std()
# Update title text with additional information
if time_frame == "1mo":
some_n_based_on_time_frame = 12
in_a_year = 1000 * (1 + portfolio_mean) ** (some_n_based_on_time_frame)
in_50_years = 1000 * (1 + portfolio_mean) ** (
some_n_based_on_time_frame * 50
)
else:
some_n_based_on_time_frame = 4
in_a_year = 1000 * (1 + portfolio_mean) ** (some_n_based_on_time_frame)
in_50_years = 1000 * (1 + portfolio_mean) ** (
some_n_based_on_time_frame * 50
)
title_text = (
f"业绩:<br>"
f"标杆回报风险比 = {benchmark_sharpe_ratio:.3f}, "
f"投资组合回报风险比 = {portfolio_sharpe_ratio:.3f}, "
f"交易窗口: {time_frame}<br>"
f"标杆 => Mean: {benchmark_mean:.4f}, Std: {benchmark_std:.4f}; "
f"投资组合 => Mean: {portfolio_mean:.4f}, Std: {portfolio_std:.4f}<br>"
f"---<br>"
f"这个数字如何理解,我们以1000块钱计算以下情况1)12个月、2)50年的业绩: <br>"
f"$1,000*(1+{portfolio_mean:.4f})^({some_n_based_on_time_frame})={in_a_year}, <br>"
f"$1,000*(1+{portfolio_mean:.4f})^({some_n_based_on_time_frame}*50)={in_50_years}."
)
curr_max_num = max(
df.rolling_benchmark.max(), df.rolling_portfolio_returns.max()
)
fig.update_layout(
title=title_text,
xaxis_title="Date",
yaxis_title="Value",
yaxis=dict(range=[0, curr_max_num * 1.1]),
legend=dict(
orientation="h", x=0.5, y=-0.4, xanchor="center", yanchor="bottom"
),
height=height_of_graph,
)
st.plotly_chart(fig, use_container_width=True)
# Post-analysis
col1, col2 = st.columns(2)
with col1:
# Checkpoint: ask user whether they want portfolio weights
if csv:
try:
recent_selected_stocks = df["portfolio_history"][-1]
recent_selected_stocks = ", ".join(recent_selected_stocks)
st.success(f"算法建议在本月持有以下股票(均仓位): {recent_selected_stocks}")
except:
st.warning("网络信号可能刚才没有收集到数据,请刷新网络然后重新跑以上算法。")
with col2:
# Download
st.download_button(
label="下载数据为CSV格式",
data=csv,
file_name=f"history_{end_date}.csv",
mime="text/csv",
)
|