Tsingtao Brewery stock data analysis

Requirements: data analysis of stocks

specific requirement:

  • Use the tushare package to get the historical market data of a stock
  • Output all dates when the stock closed more than 3% higher than the open
  • Output all dates when the stock's opening price fell by more than 2% from the previous day's closing price
  • If I start from January 1, 2012, and buy 1 lot of stocks on the first trading day of each month, and sell all stocks on the last trading day of each year, what is my profit from today’s position?

This article will use the tushare financial data interface package

pip install tushare

Import of third-party libraries

import tushare as ts
import pandas as pd
from pandas import DataFrame,Series
import numpy as np

Obtain the historical market data of Tsingtao Brewery stock, code is the stock code in the form of a character string

pro = ts.pro_api('token接口值')
df = pro.daily(ts_code='600600.SH',start_date='2022-02-08')
df.head(5)

The interface value can be obtained from the official website of tushare, and can be found on the personal homepage after login

insert image description here
Save the obtained data locally to facilitate subsequent data processing

df.to_csv('./qingdaopijiu.csv')

data preprocessing

Convert the trade_date column into a time type, delete the columns with little meaning, and check whether there are null values.

df['trade_date'] = pd.to_datetime(df['trade_date'],format='%Y%m%d')
print(df.isnull().head())
df.drop(labels='Unnamed: 0',axis=1,inplace=True)
#将trace_date列变为行索引
df.set_index('trade_date',inplace=True)
df.sort_index(inplace=True)
print(df.index)

Output all the dates when the stock closed more than 3% higher than the open, pseudocode: (close - open) / open > 0.03

A1=(df['open']- df['close']) / df['open'] > 0.03

If a bool value is generated during the analysis, the boolean value is immediately converted into a row index. This is a trick for data analysis. If the bool value is used as the row index of df, the row data corresponding to true can be taken out, and the data corresponding to false can be ignored.
Output all the dates when the stock's opening price fell more than 2% from the previous day's closing price, pseudo code: (opening gain - previous day's closing price) / previous day's closing price < -0.02

A2 = (df['open'] - df['close'].shift(1)) / df['close'].shift(1) < -0.02

small case

If I start from January 1, 2012, buy 1 lot of stocks on the first trading day of each month, and sell all stocks on the last trading day of each year, what is my profit to today's position?

  • need
    • The time node is from 2012-2022
    • One lot: 100 stocks
    • purchase
      • Buy on the first trading day of each month, you need to buy 12 lots throughout the year
    • Sell
      • A full year needs to sell 12 lots
    • The unit price for buying and selling stocks
      • use opening price
new_df = df['2022-01-01':'2023-03']
new_df

Buying stocks: Find the row data corresponding to the first trading day of each month, the row data of the first trading day of each month

df_monthly = new_df.resample('M').first()
df_monthly

The total amount spent on buying stocks

cost = df_monthly['open'].sum()*100
cost

The total amount of stocks sold
#Special situation: The stocks bought in 2020 cannot be sold

df_yearly = new_df.resample('A').last()

#The money earned from selling stocks

resv = df_yearly['open'].sum()*1200

#The remaining stocks in the final hand need to estimate their value and calculate it into the total income

last_monry = 200*new_df['close'][-1]

# Calculate the total return

resv + last_monry-cost

After using the tushare package to obtain stock data, formulate a double-average strategy

df.set_index('trade_date',inplace=True)

need:

  • Calculate the 5-day moving average and 60-day moving average of the stock's historical data
  • what is moving average
    • For each trading day, the moving average of the previous N days can be calculated, and then these moving averages can be connected to form a line, which is called the N-day moving average, generally 5, 10, 30, 60, 120, 240 days as indicators
      • The reference index for 5-day and 10-day short-term operations is called the daily moving average index
      • The 30-day and 60-day moving averages are medium-term moving average indicators, called quarterly moving average indicators
      • 120 days and 240 days are long-term moving average indicators, annual average indicators
  • Calculation method of moving average: MA=(c1+c2+c3+...+cn)/N c: closing price of a certain day N: moving average cycle
    Calculate three-day moving average and five-day moving average
ma5 = df['close'].rolling(5).mean()
ma30 = df['close'].rolling(30).mean()
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(ma5[60:90])
plt.plot(ma30[60:90])
#蓝色ma5为短期均线,黄色ma30为长期均线

insert image description here

  • Analyze and output all golden cross dates and dead cross dates
    • The golden cross and dead cross in stock analysis technology can be simply explained as:
      • Analyze the two lines in the indicator, one is the indicator line for a short period of time, and the other is the indicator line for a longer period of time
      • If the short-term indicator line turns upwards and crosses the longer-term indicator line, it becomes a golden cross
      • If the short-term indicator line turns downward and crosses the longer-term indicator line, it is called a dead fork
      • Under normal circumstances, after a golden cross, the trend is to buy, and after a dead cross, the trend is to sell
        insert image description here
death_ex = s1 & s2.shift(1)#判定死叉条件
df.loc[death_ex]#死叉对应的行数据
death_date = df.loc[death_ex].index
gold_ex = -(s1 | s2.shift(1))#金叉对应的行数据
gold_date = df.loc[gold_ex].index
gold_date
  • If I start from January 1, 2012, with an initial capital of 100,000, buy as much as possible for golden crosses, and sell all dead crosses, what is the profit of my stock trading until today?
    • The unit price for buying and selling stocks is the opening price
    • When to Buy or Sell Stocks
    • Ultimately the remaining shares were not sold
      • There will be! If the last day is a golden cross, buy the stock. Estimate the value of the remaining stock, calculated into the total return
        • The unit price of the remaining shares is measured by the last day's closing price
first_money = 100000#本金,不变
money = first_money #可变的,买股票的钱和卖股票的钱都从该变量进行
hold = 0 #持有股票的数量(股数:100股=1手)
for i in range(0,len(s)):#i表示s的隐式索引
    if i == 1:#金叉时间
        #基于100000的本金尽可能多的买入股票
        #获取股票的单价(金叉时间对应的行数据中的开盘价)
        time = s.index[i]#金叉时间
        p = df.loc[time]['open']#股票的单价,当天开盘价
        hand_count = money // (p*100)#使用100000最多买入多少手
        hold = hand_count*100#买了多少支
        money -= (hold*p)#将买股票的钱减去
        #print(hold)
    else:
        #将买入的股票卖出去
        #找出卖出股票的单价
        death_time = s.index[i]#死叉时间
        p = df.loc[death_time]['open']
        money += p*hold#卖出转的钱
        hold = 0
        #print(money)
#print(money)
#如何判断最后一天是金叉还是死叉呢
#如果是金叉,则需要将收益算到总收益中
last_money = hold * df['close'][:1]#剩余股票价值
money + last_money - first_money

Guess you like

Origin blog.csdn.net/weixin_44052130/article/details/130457498