[R-SIG-Finance] Blotter : pbm with P&L calculation
Frédéric LABORDERIE
flaborderie at yahoo.fr
Thu Sep 9 17:34:52 CEST 2010
Hello Brian,
Thank you for your very prompt answer.
I will look at your function, and use it in the future.
But you have answered my question already, as indeed I am going straight
from long to short or vice versa (my broker, IB, allows it).
I will split the order as you said, first close the current position,
then enter the opposite one. It is working fine, I already tried.
Thank you also for all the effort you and others put into blotter and
associated packages, it is very powerful / time saving, and I'm learning
a lot from reading the code (I'm an R newbie).
Brian G. Peterson wrote:
> 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:
>
> https://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/blotter/R/extractTests.R?root=blotter
>
>
> With the associated calls for setup of the portfolio, and downloading
> data from somewhere, this would even allow us to create a test, if
> warranted.
>
> 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.
>
> Regards,
>
> - Brian
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: flaborderie.vcf
Type: text/x-vcard
Size: 217 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20100909/976cdeef/attachment.vcf>
More information about the R-SIG-Finance
mailing list