Write some code to analyze stock purchases in Russia

1. Data acquisition

The data acquisition range is from January 1, 2022 to February 25, 2022, and the acquired data is gold, silver, oil, bank, and natural gas:

# 导入模块
import numpy as np
import pandas as pd
import yfinance as yf

# GC=F黄金,SI=F白银,ROSN.ME俄罗斯石油,SBER.ME俄罗斯银行,天然气
tickerSymbols = ['GC=F', 'SI=F', 'ROSN.ME', 'SBER.ME','NG=F']

# 获取这些代码的数据
MSFT = yf.Ticker(tickerSymbols[0])
TSLA = yf.Ticker(tickerSymbols[1])
AAPL = yf.Ticker(tickerSymbols[2])
AMZN = yf.Ticker(tickerSymbols[3])
GOOG = yf.Ticker(tickerSymbols[4])

# 获取代码的历史价格
MSFT_df = MSFT.history(period='1d', start='2022-1-1', end='2022-2-25')
TSLA_df = TSLA.history(period='1d', start='2022-1-1', end='2022-1-25')
AAPL_df = AAPL.history(period='1d', start='2022-1-1', end='2022-1-25')
AMZN_df = AMZN.history(period='1d', start='2022-1-1', end='2022-1-25')
GOOG_df = GOOG.history(period='1d', start='2022-1-1', end='2022-1-25')

# 比如天然气
GOOG_df.head()

as follows:
insert image description here

2. Combined data

# 将每只股票的收盘列保存到新变量中
MSFT = MSFT_df['Close']
TSLA = TSLA_df['Close']
AAPL = AAPL_df['Close']
AMZN = AMZN_df['Close']
GOOG = GOOG_df['Close']

# Concatenate all stocks close columns into one data frame
stocks_df = pd.concat([MSFT, TSLA, AAPL, AMZN, GOOG], axis='columns', join='inner')

# Rename the data frame columns with their corresponding tickers symbols
stocks_df.columns = ['gold', 'silver', 'oil', 'bank', 'gas']

# Visualize the new data frame
stocks_df.head()

as follows:
insert image description here

3. Plot the daily percentage change of the stock

# 接下来,让我们计算股票每日百分比变化并绘制它们以直观地分析它们在过去一个多月中的变化行为。
# 获取每日百分比变化
stocks_df = stocks_df.pct_change().dropna()

# 可视化新数据框
stocks_df.head()

# 绘制每日百分比变化
stocks_df.plot(figsize=(20, 10), title="Daily Returns");

As follows:
insert image description here
From the picture, you can also see which one changes greatly and which one is stable) See it with your own eyes

Fourth, the box plot

The above picture is really not good-looking, so it is more intuitive to draw a boxplot:

# 箱线图
# 计算累积回报
cumulative_returns = (1 + stocks_df).cumprod()

# 绘制累积回报
cumulative_returns.plot(figsize=(20, 10), title="Cumulative Returns");

# 箱线图直观地显示风险
stocks_df.plot.box(figsize=(20, 10), title="Portfolio Risk");

Cumulative Return Plot:
insert image description here
Box Plot:
insert image description here

Natural gas is obviously the most profitable; the wider the box and the longer the whiskers, the more volatile the stock is. Oil is the most stable, natural gas is more volatile

5. Calculate the monthly Sharpe ratio

#计算月化夏普比率
sharpe_ratios = (stocks_df.mean() * 30) / (stocks_df.std() * np.sqrt(30))
sharpe_ratios = sharpe_ratios.sort_values(ascending=False)
sharpe_ratios

as follows:
insert image description here

#将夏普比率可视化为条形图
sharpe_ratios.plot(figsize=(20, 10), kind="bar", title="Sharpe Ratios");

as follows:
insert image description here

6. Conclusion

According to the above results, gold is recommended to buy, followed by silver, natural gas, Russian banks and oil are not recommended to buy. Note: The above analysis data is the analysis chart from January 1, 2022 to February 5, 2022. You are welcome to add qun:428335755 to communicate with me, no guarantee of profit, only for reference and learning.
Full file: Github

Guess you like

Origin blog.csdn.net/weixin_46211269/article/details/123141587