R: Best way to preserve plot parameters between plots?

Discussion in 'Automated Trading' started by lolatency, Mar 15, 2009.

  1. I'm trying to overlay plots of the price action between the bid and the ask on intraday. I have a piece of data that's normalized to micro-returns [i.e., the bounce back from the bid and the ask during the day.]

    How do i get the plot to keep the same x and y dimensions from the previous plot automatically, without precalculating an xlim and ylim variable?

    Here's plot 1, from running this:

    > plot( log( rets$V4[2:l] / rets$V4[1:(l-1)] )[1:100], type="l" )

    <img src="http://media.chartvibe.com/etimg/retv4_1.jpg">

    Here's plot 2:

    > plot( log( rets$V5[2:l] / rets$V5[1:(l-1)] )[1:100], type="l", col="blue" )

    <img src="http://media.chartvibe.com/etimg/retv5_1.jpg">


    Now, if I try something like this:

    plot( log( rets$V4[2:l] / rets$V4[1:(l-1)] )[1:100], type="l" )
    plotparams <- par( no.readonly = TRUE )
    plotparams$new=T
    par( plotparams )
    plot( log( rets$V5[2:l] / rets$V5[1:(l-1)] )[1:100], type="l", col="blue" )

    I'll get something like this:

    <img src="http://media.chartvibe.com/etimg/combined.jpg">

    But what I want, is this (even though I manually compute this stuff), is something that gets created by this code:

    # slice off 1:100 for the ET post
    l <- length( rets$V4 )
    logret4 <- log( rets$V4[2: l] / rets$V4[1:(l-1)] )[1:100]
    logret5 <- log( rets$V5[2: l] / rets$V5[1:(l-1)] )[1:100]
    ydm <- c( min( logret4, logret5 ), max( logret4, logret5 ) )
    xdm <- c( 0, 100 )
    plot( logret4 , type="l", col="red", ylab="y", xlab="x", ylim=ydm, xlim=xdm )
    par( new=T )
    plot( logret5, type="l", col="blue", ylab="y", xlab="x", ylim=ydm, xlim=xdm )

    <img src="http://media.chartvibe.com/etimg/combined_smooth.jpg">

    How do I get R to cleanly carry over the underlying plot parameters and have plot() actually work with it? This drives me nuts every time, but I end up re-coding it every time. It's driving me bat5hi7 crazy. There should be a slick way to overlay the plot and preserve the dimensions as is.
     
  2. Ivanovich, please fix the federal reserve and my R problem.
     
  3. Table is huge, man. Two files of intraday data from two dow components ~ 80MB. I'm just plotting a tiny slice to let me zoom in on local areas where the math looks odd. The data is semi-worthless for real analysis because a lot of the retail data-feeds strip out the exchange time-stamps and lop off the accuracy at 1s.

    Oh, here are the two slices -- doubt it will help.

    > logret4
    [1] 0.000000e+00 0.000000e+00 5.018821e-04 0.000000e+00 5.016303e-04 5.013788e-04 0.000000e+00 0.000000e+00 0.000000e+00
    [10] 5.011275e-04 5.008765e-04 -5.008765e-04 0.000000e+00 0.000000e+00 5.008765e-04 0.000000e+00 0.000000e+00 0.000000e+00
    [19] 5.006258e-04 1.501389e-04 -1.501389e-04 0.000000e+00 -1.001502e-03 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
    [28] 0.000000e+00 0.000000e+00 -5.011275e-04 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
    [37] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 -5.013788e-04 0.000000e+00
    [46] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 -5.016303e-04 0.000000e+00 0.000000e+00 0.000000e+00
    [55] 0.000000e+00 -5.018821e-04 0.000000e+00 0.000000e+00 5.018821e-04 5.016303e-04 0.000000e+00 0.000000e+00 0.000000e+00
    [64] 5.013788e-04 0.000000e+00 -5.013788e-04 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 5.013788e-04
    [73] -5.013788e-04 0.000000e+00 -1.003512e-03 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 -5.021341e-04
    [82] 0.000000e+00 5.021341e-04 0.000000e+00 0.000000e+00 0.000000e+00 5.019954e-05 3.513263e-04 1.003563e-04 -5.018821e-04
    [91] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 5.018821e-04 -5.018821e-04 5.018821e-04
    [100] 0.000000e+00
    > logret5
    [1] 0.0000000000 0.0006495616 -0.0006495616 -0.0006499838 0.0006499838 0.0006495616 0.0000000000 0.0000000000 0.0006491399
    [10] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0006487188 0.0006482982 0.0000000000
    [19] 0.0006478782 -0.0006478782 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
    [28] 0.0000000000 -0.0006482982 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000
    [37] 0.0000000000 0.0000000000 0.0006482982 0.0000000000 -0.0006482982 0.0000000000 0.0000000000 0.0000000000 0.0000000000
    [46] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 -0.0006487188 0.0000000000 0.0000000000 -0.0006491399 0.0000000000
    [55] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 -0.0006495616 0.0000000000 0.0005196830 0.0000000000
    [64] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0000000000 -0.0001948495 0.0003247280 0.0000000000 0.0000000000
    [73] 0.0000000000 0.0000000000 -0.0006495616 0.0000000000 -0.0006499838 0.0000000000 0.0000000000 0.0000000000 -0.0006504065
    [82] 0.0000000000 0.0000000000 0.0006504065 0.0000000000 0.0000000000 0.0000000000 0.0000000000 -0.0006504065 0.0000000000
    [91] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0006504065 -0.0004552401 -0.0001951664 0.0000000000 0.0000000000
    [100] 0.0000000000
     
  4. try this:
    set one of the variables you want to
    vector b.
    The other to vector a.

    i.e. b<-1st set of data
    a<-2nd set

    then try
    require(graphics)
    ts.plot(ts(b),ts(a),col=c("red","blue"))
     
  5. comparison of your 1st method vs. the 2nd I showed:

    require(graphics)
    ts.plot(ts(b),ts(a),col=c("red","blue"))

    [​IMG]
    P.S. next time attach or print the file with the sample vectors as complete columns. Easier to process.
     
  6. Thanks!!