QSTK Tutorial 9

From Quantwiki
Jump to: navigation, search


In this tutorial we look at the event profiler available in the quant software tool kit. It allows a developer to describe market events, then observe, statistically, how those events affect future equity prices. The event profiler scans over historical data for specified event and then calculates the impact of that event on the equity prices in the past and the future over a certain lookback period. In the following example we look at how to describe a event and how to create a study plot describing it.

The code for the tutorial can be found in "Examples/EventProfiler/tutorial.py"

Creating an Event Matrix

An Event Matrix is a Pandas DataFrame. For each equity for each date, we indicate whether an event occurred or not. A NaN indicates no event for that equity/date. A 1 indicates that an event occurred. Here's an example:

(d1) nan nan 1 nan nan 1
(d2) nan 1 nan nan nan nan
(d3) 1 nan 1 nan 1 nan
(d4) nan 1 nan 1 nan nan

To create an event matrix we start by reading the data for the specified time duration as mentioned in the tutorial 1. Then we calculate normalized returns for the equity data.

df_events = copy.deepcopy(df_close)
df_events = df_events * np.NAN

Now we assess for each day whether an event occurred. We start by creating a np.NAN matrix of similar size for marking the events. Then we fill in each cell according to whether an event occurred on that date for that symbol. In this example, we calculate the returns of the equities relative to the market and declare an event if the symbol went down 3% or more and the market went up 2% or more

for s_sym in ls_symbols: # for each symbol
    for i in range(1, len(ldt_timestamps)): # for each day
        # Calculating the returns for this timestamp
        f_symprice_today = df_close[s_sym].ix[ldt_timestamps[i]]
        f_symprice_yest = df_close[s_sym].ix[ldt_timestamps[i - 1]]
        f_marketprice_today = ts_market.ix[ldt_timestamps[i]]
        f_marketprice_yest = ts_market.ix[ldt_timestamps[i - 1]]
        f_symreturn_today = (f_symprice_today / f_symprice_yest) - 1
        f_marketreturn_today = (f_marketprice_today / f_marketprice_yest) - 1

        # Event is found if the symbol is down more then 3% while the
        # market is up more then 2%
        if f_symreturn_today <= -0.03 and f_marketreturn_today >= 0.02:
             df_events[s_sym].ix[ldt_timestamps[i]] = 1

Here we are trying to plot a event :

  • Event definition: (daily_return($SPX) >= 0.02) and (daily_return(equity) < -0.03)
  • Event in English: The S&P 500 index gains more than 2% and the equity drops more then 3%.

Then we scan across all entries to see if a state satisfies the criteria of the event, if the event is detected then we mark it as 1. In the end all the states are either 1 or NAN.

Plotting the event study

df_events = find_events(ls_symbols, d_data)
print "Creating Study"
ep.eventprofiler(df_events, d_data, i_lookback=20, i_lookforward=20,
                s_filename='MyEventStudy.pdf', b_market_neutral=True, b_errorbars=True,
How stocks perform after a market event.

The above code shows how to call the event profiler once you have created the event matrix using the findEvents function. This code plots the event profiler output in the file MyEventStudy.pdf.

Personal tools