[PYTHON] Read zipline brand data from csv file and perform back test

1 What about this article?

When reading brand data with zipline and performing backtesting, there is a method of creating a "bundle" on zipline and managing the brand data there, but there is also a method of reading brand data directly from the csv file and performing backtesting. In this article, I will write how to read stock data directly from a csv file and perform backtesting.

2 Contents

2-1 Installation of trading calendar

First, install the trading calendar for each exchange.

python


(python355) C:\Users\***\anaconda3>conda install -c quantopian trading-calendars

2-2 Installing Trading Calendar

Execute the code below. The csv files of brands n1570 and n7752 used when executing the code below Get it here

python


from zipline.api import order, record, symbol,set_benchmark
import pandas as pd
from datetime import datetime
import zipline
import pytz  #timezone settings https://narito.ninja/blog/detail/81/
from trading_calendars import get_calendar #Import the calendar of each exchange
from collections import OrderedDict

HDIR="xxxxxxxxxxxxxx" #csv file (brand data))Specify the directory to put
 
data=OrderedDict() #Give order to the ordered dictionary.
tickers=["n1570","n7752"] #Specify the csv file name corresponding to the brand to be read here.

#Read the stock price of a stock from a csv file
for ticker in tickers:
    DIR=HDIR + ticker +".csv" #Brand to read(csv file)To specify.
    data[ticker]= pd.read_csv(DIR, index_col=0,parse_dates=True) #Read the csv file.

panel=pd.Panel(data)  #Put the brand data in the 3D array panel.
panel.major_axis=panel.major_axis.tz_localize(pytz.utc) #Set the time to the UTC zone.(An error will occur if the UTC zone is not set for convenience.)

def initialize(contect):
    set_benchmark(symbol("n1570")) #Designate brand n1570 as a benchmark.

def handle_data(context,data):
    order(symbol("n1570"),1) #Buy one share every day at the close.
    record(N1570=data.current(symbol("n1570"),"price")) #Record the close value of issue n1570.

    
#Run backtest.(Buy one share of brand n1570 at the close every day.)
perf=zipline.run_algorithm(start=datetime(2020,1,4,0,0,0,0,pytz.utc),
                            end=datetime(2020,3,4,0,0,0,0,pytz.utc),
                            initialize=initialize,
                            capital_base=1000000, #Specify the asset at the start.
                            handle_data= handle_data,
                            data=panel,
                            trading_calendar=get_calendar('XTKS') #Read the Tokyo Stock Exchange calendar
                           )

perf.to_csv("********") #Write the backtest execution result to the csv file

print(perf.head())

★ Supplement ・ OrderedDict () # Give order to the ordered dictionary ・ ・ ・ [Click here for explanation](# https://note.nkmk.me/python-collections-ordereddict/) ・ Create a Panel 3D array ... Click here for explanation ・ Time zone setting pytz ・ ・ ・ Click here for explanation

The following is the execution result.

python


                           N1570  algo_volatility  algorithm_period_return  \
2020-01-06 06:00:00+00:00  21510              NaN                 0.000000   
2020-01-07 06:00:00+00:00  22200         0.000125                -0.000011   
2020-01-08 06:00:00+00:00  21530         0.006189                -0.000692   
2020-01-09 06:00:00+00:00  22480         0.017575                 0.001197   
2020-01-10 06:00:00+00:00  22730         0.015535                 0.001936   

                              alpha  benchmark_period_return  \
2020-01-06 06:00:00+00:00       NaN                -0.042724   
2020-01-07 06:00:00+00:00 -0.001598                -0.012016   
2020-01-08 06:00:00+00:00 -0.046605                -0.041834   
2020-01-09 06:00:00+00:00  0.071751                 0.000445   
2020-01-10 06:00:00+00:00  0.084250                 0.011571   

                           benchmark_volatility      beta  capital_used  \
2020-01-06 06:00:00+00:00                   NaN       NaN         0.000   
2020-01-07 06:00:00+00:00              0.839647 -0.000148    -22211.101   
2020-01-08 06:00:00+00:00              0.635930  0.003357    -21540.766   
2020-01-09 06:00:00+00:00              0.692524  0.017970    -22491.241   
2020-01-10 06:00:00+00:00              0.604182  0.018330    -22741.366   

                           ending_cash  ending_exposure  \
2020-01-06 06:00:00+00:00  1000000.000              0.0   
2020-01-07 06:00:00+00:00   977788.899          22200.0   
2020-01-08 06:00:00+00:00   956248.133          43060.0   
2020-01-09 06:00:00+00:00   933756.892          67440.0   
2020-01-10 06:00:00+00:00   911015.526          90920.0   

                                   ...            short_exposure  short_value  \
2020-01-06 06:00:00+00:00          ...                       0.0          0.0   
2020-01-07 06:00:00+00:00          ...                       0.0          0.0   
2020-01-08 06:00:00+00:00          ...                       0.0          0.0   
2020-01-09 06:00:00+00:00          ...                       0.0          0.0   
2020-01-10 06:00:00+00:00          ...                       0.0          0.0   

                           shorts_count    sortino  starting_cash  \
2020-01-06 06:00:00+00:00             0        NaN    1000000.000   
2020-01-07 06:00:00+00:00             0 -11.224972    1000000.000   
2020-01-08 06:00:00+00:00             0  -9.313364     977788.899   
2020-01-09 06:00:00+00:00             0  13.968067     956248.133   
2020-01-10 06:00:00+00:00             0  20.185869     933756.892   

                           starting_exposure  starting_value  trading_days  \
2020-01-06 06:00:00+00:00                0.0             0.0             1   
2020-01-07 06:00:00+00:00                0.0             0.0             2   
2020-01-08 06:00:00+00:00            22200.0         22200.0             3   
2020-01-09 06:00:00+00:00            43060.0         43060.0             4   
2020-01-10 06:00:00+00:00            67440.0         67440.0             5   

                                                                transactions  \
2020-01-06 06:00:00+00:00                                                 []   
2020-01-07 06:00:00+00:00  [{'amount': 1, 'sid': Equity(0 [N1570]), 'orde...   
2020-01-08 06:00:00+00:00  [{'amount': 1, 'sid': Equity(0 [N1570]), 'orde...   
2020-01-09 06:00:00+00:00  [{'amount': 1, 'sid': Equity(0 [N1570]), 'orde...   
2020-01-10 06:00:00+00:00  [{'amount': 1, 'sid': Equity(0 [N1570]), 'orde...   

                          treasury_period_return  
2020-01-06 06:00:00+00:00                    0.0  
2020-01-07 06:00:00+00:00                    0.0  
2020-01-08 06:00:00+00:00                    0.0  
2020-01-09 06:00:00+00:00                    0.0  
2020-01-10 06:00:00+00:00                    0.0  

Recommended Posts

Read zipline brand data from csv file and perform back test
Read and write csv file
[Python] How to read data from CIFAR-10 and CIFAR-100
Python --Read data from a numeric data file and find the multiple regression line.
Python-Read data from a numeric data file and calculate covariance
Read the csv file and display it in the browser
Read CSV file: pandas
Read Python csv file
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
[Python] Read the csv file and display the figure with matplotlib
Read and write a file
Write and read a file
Extract csv data and calculate
[Python] Read a csv file with a large data size using a generator
Read and use Python files from Python
Python CSV file reading and writing
Read and format a csv file mixed with comma tabs with Python pandas
Read CSV file with Python and convert it to DataFrame as it is