Use Python to obtain historical stock data to facilitate quantitative analysis and strategy backtesting

Obtain stock historical technical data using Python

This article describes how to use Python to obtain stock historical technical data for quantitative analysis or strategy backtesting.

We use data provided by Mairui Data , which includes technical indicators . Below are two custom functions , one for converting JSON to DataFrame format and the other for getting the data.

The first custom function is a function that handles JSON, which converts JSON to DataFrame format for better data processing and analysis.

The second custom function is a function to obtain data, which can obtain the corresponding historical technical data based on the input stock code . This function can be modified as needed to meet different data acquisition needs.

By using these two custom functions, we can easily obtain historical technical data and conduct quantitative analysis or strategy backtesting. This will help us better understand market trends and formulate more effective investment strategies.

There are two places that need to be modified. One is to fill in your own license, and the other is to enter the code to obtain the stock when calling the custom function. These modifications will allow the code to adapt to different situations and needs.

code

import requests
import pandas as pd

licence = '替换成你的licence'

def json_to_df(url):
    """
    将json格式转换为DataFrame格式
    """
    response = requests.get(url)
    data = response.json()
    df = pd.DataFrame(data)
    return df

def get_stock_data(code):
    """
    通过股票代码获取历史数据,包括分时交易、kdj、macd、ma和boll
    """
    url_fs = f'http://api.mairui.club/hszbl/fsjy/{code}/dq/{licence}'
    url_kdj = f'http://api.mairui.club/hszbl/kdj/{code}/dq/{licence}'
    url_macd = f'http://api.mairui.club/hszbl/macd/{code}/dq/{licence}'
    url_ma = f'http://api.mairui.club/hszbl/ma/{code}/dq/{licence}'
    url_boll = f'http://api.mairui.club/hszbl/boll/{code}/dq/{licence}'
    
    # 获取数据并转换为DataFrame格式
    df_fs = json_to_df(url_fs)
    df_kdj = json_to_df(url_kdj)
    df_macd = json_to_df(url_macd)
    df_ma = json_to_df(url_ma)
    df_boll = json_to_df(url_boll)
    """
    倒序,即当前日期放在前面
    df_fs = json_to_df(url_fs).iloc[::-1].reset_index(drop=True)
    df_kdj = json_to_df(url_kdj).iloc[::-1].reset_index(drop=True)
    df_macd = json_to_df(url_macd).iloc[::-1].reset_index(drop=True)
    df_ma = json_to_df(url_ma).iloc[::-1].reset_index(drop=True)
    df_boll = json_to_df(url_boll).iloc[::-1].reset_index(drop=True)
    """
    # 将数据合并到一个数据框中
    df = pd.concat([df_fs, df_kdj, df_macd, df_ma, df_boll], axis=1)
    # 返回df
    return df

# 填入代码和将数据保存的名称,将数据保存为csv文件
get_stock_data('000001').to_csv('数据.csv', index=False, encoding='utf-8-sig')

Code explanation

This code mainly obtains stock data from the specified API, then merges the data of time-sharing transactions, KDJ, MACD, MA and BOLL into a DataFrame, and saves the results as a CSV file.

First, a json_to_df function is defined to convert the JSON format data returned by the API into DataFrame format. Then, a get_stock_data function is defined, which obtains historical data by stock symbol. In this function, the json_to_df function is used to obtain the data of time-sharing transactions, KDJ, MACD, MA and BOLL, and pd.concat is used to merge them into a DataFrame. Finally, use to_csv to save the results as a CSV file.

It is worth noting that the license variable in this code is the license number used to access the API and needs to be replaced with a valid license number for normal operation.

Guess you like

Origin blog.csdn.net/Everly_/article/details/133138331