-3

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?

trbck
  • 5,187
  • 6
  • 26
  • 29
  • Can you provide sample input/output? – exp1orer Jul 25 '14 at 16:59
  • @exp1orer I edited the question. Hope this clarifies it. – trbck Jul 26 '14 at 17:10
  • I'm afraid I still don't understand what you are looking for. Here's what I understand--if you fill in the gaps I can try to help you. You start with a dataframe `df` (aka `ohlc`) with TimeSeries Index and columns for `open`, `close`, `high` and `low` price for each day as well as the trade `volume` and `amount` (what is `amount`?). You also have a TimeSeries Index `signal` but I don't know what that is. You do some manipulations to get a dataframe `analysis` and a Series `trade_rets`. For output you want 2 series -- `returns` and `buy_returns`, which have the same (TimeSeries) index. – exp1orer Jul 27 '14 at 03:06
  • 1
    You ask a very "big" question. Have done similar work and you will have to brush up your pandas skills and ask more specific questions. Also consider looking at packages like http://zipline.readthedocs.org/en/latest/ which uses python and pandas but already has some functionality baked in. – Joop Jul 31 '14 at 08:39

1 Answers1

2

You don't have enough information to run a backtest. Your "strategy" currently just has True or False. When it's True, how much do you want to buy? If it's True twice in a row, does that mean buy-and-hold or buy at both times? Does False mean liquidate or not to buy?

You need to:

  1. Translate your signal into a "quantity held at t"
  2. Then check the what the result of holding that quantity is
  3. Don't forget to benchmark

When doing (2), which is I think the main point of your question here, don't focus on speed, just make an intuitive iterative simulator that iterates through time, does what you say, and has a new overall value. For the size of data you're looking at, any speedup from being more complicated in pandas will be less than a blink of an eye.

metaperture
  • 2,393
  • 1
  • 18
  • 19