How to perform a simple signal backtest in python pandas <p>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.</p> <p>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?</p> <p>Any help is very appreciated!</p> <p>signals:</p> <pre><code>In [216]: signal Out[216]: &lt;class 'pandas.tseries.index.DatetimeIndex'&gt; [2000-08-21, ..., 2013-07-09] Length: 21, Freq: None, Timezone: UTC </code></pre> <p>ohlc:</p> <pre><code>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 </code></pre> <p>backtest:</p> <pre><code>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"] </code></pre> <p>expected result (values of buy_returns are not correct):</p> <pre><code>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 </code></pre> <p>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? </p>