Profit and loss using pandas
<p>I have a dataframe containing data like this:</p>

<pre><code>                       open   close  signal
date_time                                                          
2011-01-03 01:04:00  1.5560  1.5556    0.0
2011-01-03 01:05:00  1.5557  1.5556    0.0
2011-01-03 01:06:00  1.5557  1.5556    1.0
2011-01-03 01:07:00  1.5556  1.5545    1.0
2011-01-03 01:08:00  1.5546  1.5548    1.0
2011-01-03 01:09:00  1.5549  1.5547    0.0
2011-01-03 01:10:00  1.5548  1.5549    0.0
2011-01-03 01:11:00  1.5549  1.5551    1.0
2011-01-03 01:12:00  1.5550  1.5552    1.0
2011-01-03 01:13:00  1.5553  1.5553    0.0
2011-01-03 01:14:00  1.5552  1.5553    0.0
</code></pre>

<p>Which is a fairly standard way of representing a financial timeseries in Python. </p>

<p>Now, I want to use the signal column to trade. When signal == 1 then buy, when it gets back to 0 then sell. The signal is known at the end of the current minute, so when we say 'Buy', it really means 'buy at the open of the next minute'.</p>

<p>Let's say the initial value of our portfolio is 1.0. I want a timeseries that outputs:</p>

<pre><code>                       pnl
date_time                                                          
2011-01-03 01:04:00    1.0
2011-01-03 01:05:00    1.0
2011-01-03 01:06:00    1.0
2011-01-03 01:07:00    0.999292877 # Buy: pnl = (1.0 * 1.5545 / 1.5556)
2011-01-03 01:08:00    0.999485729 # Hold: pnl = (1.0 * 1.5548 / 1.5556)
2011-01-03 01:09:00    0.999421445 # Hold: pnl = (1.0 * 1.5547 / 1.5556)
2011-01-03 01:10:00    0.999485729 # Sell: pnl = (1.0 * 1.5548 / 1.5556)
2011-01-03 01:11:00    0.999485729 # Wait
2011-01-03 01:12:00    0.999614280 # Buy: pnl = (0.999485729 * 1.5552 / 1.5550)
2011-01-03 01:13:00    0.999678556 # Hold: pnl = (0.999485729 * 1.5553 / 1.5550)
2011-01-03 01:14:00    0.999614280 # Sell: pnl = (0.999485729 * 1.5552 / 1.5550)
2011-01-03 01:15:00    0.999614280 # Wait
</code></pre>

<p>Any idea how to do this with pandas <strong>without looping through the dataframe?</strong></p>