[R-SIG-Finance] Blotter : pbm with P&L calculation

Brian G. Peterson brian at braverock.com
Thu Sep 9 17:20:49 CEST 2010

On 09/09/2010 08:21 AM, Frédéric LABORDERIE wrote:
> hello,
> I am trying to port a strategie from Tradestation to R, and I bump into
> what seems to me to be a problem (?).
> In the screenshot attached you can see that on June 21st I reverse a
> position from short to long (I buy 2 shares to cancel the short and be
> long) at 111.41, at the close.
> So I'm long and bought at 111.41
> On July 7th, I reverse again, selling at 106.11.
> I expect my transaction to be a loss of 106.11-111.41=-5.30 (Gross
> Transaction Realized PL)
> This seems to happen only when testing a Stop And Reverse system, if I
> test a long only or exit a position before taking the opposite, then it
> seems to work (but I could not complete the test because of a lack of
> computer memory)?
> So my question is : is this normal behavior ? am I using it incorrectly ?
> (I tried to look at the code but could not figure what was wrong, the
> data are SPY from Yahoo)
> Thank you for your help

It would be helpful if you provided a reproducible example, so that we 
can help you debug the problem.

I've just committed a new file to the project that should aid you (or 
anyone else) in creating reproducible examples.  The function is 
'extractTxns', and may be found here:


With the associated calls for setup of the portfolio, and downloading 
data from somewhere, this would even allow us to create a test, if 

I suspect that you are crossing directly through a zero position.  In 
this case you will likely encounter problems.  It is generally a good 
idea to close a position before initiating a new position on the other 
side of the market.  Many prime brokers in fact require this, or split 
transactions in the transaction log to create the same effect.

The coding necessary to detect this crossover of a zero position and 
separate the transaction P&L, from experience, is rather painful, and I 
don't know how to vectorize it for performance.

With a reproducible example I'd be happy to work through it, and if 
necessary, work on fixing any problem or providing a workaround.


    - Brian

Brian G. Peterson
Ph: 773-459-4973
IM: bgpbraverock

More information about the R-SIG-Finance mailing list