[R-SIG-Finance] Test for new event and save data in new data.frame
R. Michael Weylandt
michael.weylandt at gmail.com
Fri Oct 5 00:32:24 CEST 2012
On Thu, Oct 4, 2012 at 7:11 PM, Mark Knecht <markknecht at gmail.com> wrote:
> On Thu, Oct 4, 2012 at 7:49 AM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>> On Thu, Oct 4, 2012 at 12:12 AM, Mark Knecht <markknecht at gmail.com> wrote:
> <SNIP>
>>> TradeData = data.frame(cbind(t1,t2,t3,t4))
>>
>> This is a terrible awful and altogether wretched idiom. Instead just
>> do data.frame(t1, t2, t3, t4). cbind()-ing completely invalidates the
>> entire point of a data.frame() by coercing all its inputs to a common
>> class. [Don't take it personally: we've just been working to stamp it
>> out every where we see it on the main R-help list for a few months
>> now]
>>
>
> Hi Michael,
>
> Not in the least insulted or anything like that. I'm just trying to
> learn. Thanks for alluding to reasons it's not preferred. I'll try to
> keep that in mind.
>
>>
>> Best advice: don't use a data.frame() for this sort of data. Instead,
>> use a proper time series object, like xts or zoo. These also provide a
>> lag function for you:
>>
>> library(xts)
>>
>> PriceData <- xts(cbind(BarNum = 1:20, Price =
>> c(5,6,7,8,1,2,3,4,7,2,3,4,5,7,8,9,1,2,1,1), MP =
>> c(0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,1,1,0,0,0)), Sys.Date() + 1:20)
>>
>> print(PriceData)
>>
>> # Now we can add on the lag column:
>>
>> PriceData <- cbind(PriceData, LagMP = lag(PriceData[,3], 3))
>>
>> with(PriceData, (MP == 1) & (MP.1== 0)) # Gives you the rows you want
>> to manipulate
>>
>> Cheers,
>> Michael
>
> OK, that all works pretty well including the with(PriceData, ... part.
> Thanks. One question though - Is the 'LagMP' inside the cbind supposed
> to give me the name LagMP in the result?
Oops -- I would have assumed it did. Nevermind there...
> The columns are named MP.1 &
> MP.2 so I renamed it explicitly like this:
>
> colnames(PriceData) = c(colnames(PriceData[,1:3]),"LagMP","LongEntry")
>
> Maybe there's a cbind option to get the name in but I couldn't find it.
>
>
>> library(xts)
>>
>> PriceData <- xts(cbind(BarNum = 1:20,
> + Price = c(5,6,7,8,1,2,3,4,7,2,3,4,5,7,8,9,1,2,1,1),
> + MP = c(0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,1,1,0,0,0)),
> + Sys.Date() + 1:20)
>>
>> # Now we can add on the lag column:
>> PriceData <- cbind(PriceData, LagMP = lag(PriceData[,3], 1))
>> PriceData <- cbind(PriceData, LongEntry = with(PriceData,(MP == 1) & (MP.1 == 0)))
>>
>> print(PriceData[1:5,])
> BarNum Price MP MP.1 MP.2
> 2012-10-05 1 5 0 NA 0
> 2012-10-06 2 6 0 0 0
> 2012-10-07 3 7 1 0 1
> 2012-10-08 4 8 1 1 0
> 2012-10-09 5 1 1 1 0
>>
>> colnames(PriceData) = c(colnames(PriceData[,1:3]),"LagMP","LongEntry")
>>
>> print(PriceData[1:5,])
> BarNum Price MP LagMP LongEntry
> 2012-10-05 1 5 0 NA 0
> 2012-10-06 2 6 0 0 0
> 2012-10-07 3 7 1 0 1
> 2012-10-08 4 8 1 1 0
> 2012-10-09 5 1 1 1 0
>
>
>
> Next step is, I think, to use the LongEntry value to create a new
> data.frame, or maybe a matrix instead, to hold the actual trades as
> they progress.
No data.frame()s! They're slow so use them only if absolutely needed!
>I think that's done with some form of apply? Reading my
> 'Data Manipulation in R' book but haven't found an example of anything
> similar yet.
There are a few ways to do it, but I'd do something like
# Set rows <- the rows you want (possibly with the with() construction above)
do.call(cbind, lapply(rows, function(r) PriceData[seq(r, r+4), ]))
It's a little hard to parse, but basically:
lapply() goes over the elements of rows one by one and selects
PriceData[r:(r+4), ] from them: then it constructs a list from them
and passes that as the argument to cbind which should stick the
columns together.
I didn't test that so it might need some fiddling, but it's a start.
Cheers,
M
>
> Thanks,
> Mark
More information about the R-SIG-Finance
mailing list