I want to perform a simple and quick backtest in pandas by providing buy signals as DatetimeIndex to check against ohlc quotes DataFrame (adjusted close price) and am not sure if I am doing this right.
To be clear I want to calculate the cummulated returns of all swapping buy signals (and stock returns as well?) over the whole holding period. After that I want to compare several calculations via a simple sharpe function. Is this the right way to test a buy singal quick and easy in pandas?
Any help is very appreciated!
signals:
In [216]: signal
Out[216]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2000-08-21, ..., 2013-07-09]
Length: 21, Freq: None, Timezone: UTC
ohlc:
In [218]:
df.head()
Out[218]:
open high low close volume amount
Date
2000-01-14 00:00:00+00:00 6.64 6.64 6.06 6.08 74500 4.91
2000-01-17 00:00:00+00:00 6.30 6.54 6.25 6.40 45000 5.17
2000-01-18 00:00:00+00:00 7.56 8.75 7.51 8.75 250200 7.07
backtest:
analysis = pd.DataFrame(index=df.index)
#calculate returns of adjusted close price
analysis["returns"] = df['amount'].pct_change()
#set signal returns to quote returns where there is a signal DatetimeIndex and ffill
analysis["signal"] = nan
analysis["signal"][signal] = analysis["returns"][signal]
analysis["signal"] = analysis["signal"].fillna(method="ffill")
#calculation of signal returns
trade_rets = analysis["signal"].shift(1)*analysis["returns"]
expected result (values of buy_returns are not correct):
Out[2]:
returns buy_returns
Date
2000-08-21 00:00:00+00:00 -0.153226 -0.076613
2001-02-12 00:00:00+00:00 0.000000 0.000000
2002-10-29 00:00:00+00:00 0.246155 0.030769
2003-02-12 00:00:00+00:00 0.231884 0.014493
2003-03-12 00:00:00+00:00 1.548386 0.048387
My question really is how do I have to calculate a returns Series to represent the strength of a provided buy signal (True/ False Series or Datetimeindex) in pandas?