In the last class, the shopkeeper introduced the method of obtaining ** data, which is to obtain the latest data through the API interface of Tushare. With the underlying data, we can do some simple analysis.
Today, we will introduce a particularly simple and actionable strategy - simple regular investment, that is, on the first trading day of each month, one lot (100 shares) will be sold at the opening price on the last trading day of each year.
Conventionally, the first time you use it, you need to import the relevant Python library first.
importtushare as ts
importpandas as pd
importnumpy as np
According to what was taught in the previous lesson, the transaction data of Ping An Bank from 2013 to 2023 was extracted from Tushare at one time.
Select the rise and fall information of Ping An Bank since 2013.
pro = ts.pro api() to initialize the interface.
df_pingan = pro.daily(ts_code='000001.sz',start_date='20130101',end_date='20231201')
To further process the data, the format of "trade date" in the original data is text format, and you need to process it in the datetime format supported by Python and set "trade date" to index.
df_pingan['trade_date'] = pd.to_datetime(df_pingan['trade_date'],format='%y%m%d') to convert text data into a standard time series
df_pingan.set_index('trade_date', inplace=true) sets the trading time to index
df_pingan.head()
Extract the data on the first trading day of each month from the data of ten years, here you can use the resample command of pandas dataframe to facilitate the grouping of data, similar to the groupby grouping usage, but resample is more suitable for time series data, which can be quickly grouped and filtered by date.
For example, resample('m') is the last day of the calendar of each month, and resample('a') is the last day of the year. After the groups are divided, statistical analysis can be carried out, for example, the first and last methods are used to count the first and last data, and there are also conventional statistical methods such as mean and sum.
df_buy = df_pingan.resample('m').first() selects the data of the first trading day of each month as a reference
df_buy.tail()
It is important to note that the index of the series shown here does not correspond to the data, e.g. the opening price ("open") of 2023-12-31$65 is the data as of December 1, 2023. The sequence index here corresponds to the data of the first trading day, which should be understood as the name of the group, which has no practical meaning.
Once you've grouped each month and extracted the data for the first day, it's easy to move on to the next step. You only need to add up the opening of each month, and then multiply by the number of 100 shares to get all the costs, which cost 175757 yuan.
Money spent
cost = df_buy['open'].sum() 100
cost Similarly, the next step is to select the last trading day of the year and use the opening price as the transaction**.
df_sell = df_pingan.resample('a').last()[1] selects the last trading day data as a selling reference every year
df_sell.tail()
It should be noted here that 2023 is not over yet, and it is not yet the time to sell the whole year.
Therefore, only the profit from selling ** from 2013 to 2022 is calculated.
The gain is 164064 yuan.
Sell ** to make money
revenue = df_sell['open'].sum() 1200
revenue
The value of *** in 2023 is also calculated here, and the value of *** held in 2023 is 11,592 yuan based on the latest period, that is, the *** price on December 1, 2023.
Calculate 2023 earnings
last_money = df_pingan['close'][0]*1200
last_money
Add the income from selling ** to the holding value in 2023, and then subtract the cost of ***, and the return of the regular investment is -101 yuan.
It means that I have lost 101 yuan in Ping An Bank for ten years, not counting the inflation of this decade, as well as transaction fees and taxes, and I should actually lose more.
You must know that 10 years ago, the share price of Ping An Bank was more than 15 yuan, and now it is only worth 9 yuan 6, a full third of the decline. In the case of one-third of the stock price, it can basically guarantee breakeven, which shows that simple regular investment also has certain benefits.
But in general, a simple regular investment strategy is still difficult to ensure a good return, and it is better to put money in the bank to earn interest.