pandasdata analysis package, and aims to ensure that all users are able to rapidly backtest realistic strategies.
datetimemodule with the alias
dtm. This module is used in defining dates, times, and more.
pandasdata analysis module under the alias
numpynumerical computing module under the alias
matplotlib.pyplotlibrary for plotting under the alias
seabornunder the alias
sns. Set the default figure size for all plots within the notebook:
historymethod on that asset. This returns a pandas Series
(pd.Series)object, whose values are prices and whose index entries are the corresponding dates:
lenon a series (or dataframe)
pd.Seriesformat. The below cells check that
stock_historyis a series:
pd.DataFrame, is returned if there are multiple columns of time series data.
history_fieldson that object:
fieldscan be obtained by supplying the list to the
headon it. The analogue for the last rows is to use the
DatetimeIndexcan be easily sliced and utilised in data filtering. To filter a pandas series or dataframe based on the value of elements of its index the
locmethod can be used. In the case of a
DatetimeIndexthere are further possibilities:
pd.Seriesis unaltered but a new
headmethod is called on the resulting series so that only its first seven rows are returned:
locsyntax to filter columns:
plotmethod can be used on the result of filtering.
np.logto the ratio of prices to shifted prices. This is relying on the vectorised way in which a function is applied to each element of the series:
returnsseries is a 'nan'. No return can be computed for January 3rd 2000 as no prices prior to this date are available.
returnsto be the output from calling
dropnais to remove any row containing at least one nan.
howparameter enables a requirement of every element in a row being nan before that row is removed.
0, and to apply it along the dataframe's columns set
1. In this way the operation is performed along columns.
inplaceparameter can be used.
sort_values. A list of columns should then be provided to indicate the order of sorting preference.
standard deviationare easily obtained from a series or dataframe.
describemethod to retrieve a collection of common statistics from the numeric data in a series or a dataframe. In particular, the result’s index will include count, mean, std, min, max as well as 25, 50 and 75 percentiles.
describemethod is applied to object data, such as strings or timestamps, the resulting index will include
topis the most common value,
freqis the most common value’s frequency. Timestamps also include the
pd.DataFrameconstructor whose keys are strings and whose values are
1yields a series indexed by date with the relevant statistic computed over the list of ETFs on each day:
tip_returnsthis returns a series restricted to those index values where the condition was true:
locto access that particular slice and then set the new values:
historytime series and then using this signal as input to the