Hi Joshua,

Oh snap, awesome to have the author of the blog your reading at that
moment to reply to your message.

I updated the query to be 200 sessions, the function now prints, but
appears to get the dates all wrong.  (I actually was following along with
the post here:
http://blog.fosstrading.com/2011/03/how-to-backtest-strategy-in-r.html great
blog by the way.)

What I have now (simplified it a bit):

# Libs
library('RPostgreSQL') # http://code.google.com/p/rpostgresql/
library('quantmod')
library('TTR')

# Connect and get data
drv <- dbDriver('PostgreSQL')
db <- dbConnect(drv, host='localhost', user='postgres', dbname='technica',
password='password')
fr <- dbGetQuery(db, 'SELECT date as "Date", open as "Open", high as
"High", low as "Low", close as "Close", volume as "Volume", 0 as "Adjusted"
FROM stocks s INNER JOIN historical_prices hp ON s.id = hp.stock_id WHERE
s.symbol = \'SDL\' ORDER BY date DESC limit 200')

fr <- data.frame(fr[,-1], row.names=fr[,1])
fr <- xts(as.matrix(fr[,-1]), order.by=as.Date(fr[,1]), updated=Sys.time())

dvi <- DVI(Cl(fr))
print(dvi)
# Clean up.
dbDisconnect(db)
dbUnloadDriver(drv)

Then as you can see below the dates are all out of whack, My guess was it
was the date format that postgres returns (being mm/dd/yyyy) but no luck,
any ideas?

Thanks!
Max

              dvi.mag     dvi.str        dvi
1970-01-01          NA          NA         NA
(Repeated a few hundread times)
Then this:
1970-01-01 0.134920635 0.349206349 0.17777778
1970-01-01 0.079365079 0.369047619 0.13730159
1970-01-01 0.071428571 0.261904762 0.10952381
1970-01-01 0.067460317 0.281746032 0.11031746
1970-01-01 0.091269841 0.388888889 0.15079365
1970-01-01 0.123015873 0.289682540 0.15634921
1970-01-01 0.170634921 0.289682540 0.19444444
1970-01-01 0.075396825 0.289682540 0.11825397
1970-01-01 0.023809524 0.289682540 0.07698413
1970-01-01 0.023809524 0.492063492 0.11746032
1970-01-01 0.027777778 0.492063492 0.12063492
1970-01-01 0.039682540 0.492063492 0.13015873
1970-01-01 0.285714286 0.575396825 0.34365079
1970-01-01 0.531746032 0.575396825 0.54047619
1970-01-01 0.559523810 0.615079365 0.57063492
1970-01-01 0.587301587 0.650793651 0.60000000
1970-01-01 0.531746032 0.650793651 0.55555556
1970-01-01 0.313492063 0.619047619 0.37460317
1970-01-01 0.202380952 0.527777778 0.26746032
1970-01-01 0.198412698 0.452380952 0.24920635
1970-01-01 0.146825397 0.563492063 0.23015873
1970-01-01 0.198412698 0.555555556 0.26984127
1970-01-01 0.230158730 0.444444444 0.27301587
1970-01-01 0.309523810 0.456349206 0.33888889
1970-01-01 0.329365079 0.341269841 0.33174603
1970-01-01 0.182539683 0.119047619 0.16984127
1970-01-01 0.063492063 0.130952381 0.07698413
1970-01-01 0.095238095 0.373015873 0.15079365
1970-01-01 0.043650794 0.488095238 0.13253968
1970-01-01 0.027777778 0.353174603 0.09285714
1970-01-01 0.285714286 0.234126984 0.27539683
1970-01-01 0.599206349 0.234126984 0.52619048
1970-01-01 0.420634921 0.234126984 0.38333333
1970-01-01 0.396825397 0.142857143 0.34603175
1970-01-01 0.515873016 0.142857143 0.44126984
1970-01-01 0.333333333 0.242063492 0.31507937
1970-01-01 0.186507937 0.142857143 0.17777778
1970-01-01 0.234126984 0.007936508 0.18888889
1970-01-01 0.261904762 0.003968254 0.21031746
1970-01-01 0.194444444 0.003968254 0.15634921
1970-01-01 0.178571429 0.003968254 0.14365079
1970-01-01 0.226190476 0.007936508 0.18253968
1970-01-01 0.273809524 0.019841270 0.22301587
1970-01-01 0.329365079 0.011904762 0.26587302
1970-01-01 0.369047619 0.003968254 0.29603175
1970-01-01 0.380952381 0.003968254 0.30555556
1970-01-01 0.337301587 0.007936508 0.27142857
1970-01-01 0.242063492 0.023809524 0.19841270
1970-01-01 0.206349206 0.023809524 0.16984127
1970-01-01 0.186507937 0.023809524 0.15396825
1970-01-01 0.226190476 0.055555556 0.19206349
1970-01-01 0.182539683 0.055555556 0.15714286
1970-01-01 0.178571429 0.055555556 0.15396825
1970-01-01 0.190476190 0.099206349 0.17222222
1970-01-01 0.238095238 0.091269841 0.20873016
1970-01-01 0.178571429 0.087301587 0.16031746
1970-01-01 0.269841270 0.079365079 0.23174603
1970-01-01 0.218253968 0.079365079 0.19047619
1970-01-01 0.289682540 0.150793651 0.26190476
1970-01-01 0.234126984 0.277777778 0.24285714
1970-01-01 0.265873016 0.285714286 0.26984127
1970-01-01 0.309523810 0.448412698 0.33730159
1970-01-01 0.488095238 0.448412698 0.48015873
1970-01-01 0.333333333 0.297619048 0.32619048
1970-01-01 0.376984127 0.297619048 0.36111111
1970-01-01 0.416666667 0.452380952 0.42380952
1970-01-01 0.313492063 0.583333333 0.36746032
1970-01-01 0.202380952 0.583333333 0.27857143
1970-01-01 0.289682540 0.452380952 0.32222222
1970-01-01 0.361111111 0.452380952 0.37936508
1970-01-01 0.349206349 0.452380952 0.36984127
1970-01-01 0.309523810 0.297619048 0.30714286
1970-01-01 0.297619048 0.297619048 0.29761905
1970-01-01 0.503968254 0.297619048 0.46269841
1970-01-01 0.511904762 0.484126984 0.50634921
1970-01-01 0.757936508 0.642857143 0.73492063
1970-01-01 0.845238095 0.507936508 0.77777778
1970-01-01 0.853174603 0.515873016 0.78571429
1970-01-01 0.833333333 0.797619048 0.82619048
1970-01-01 0.781746032 0.797619048 0.78492063
1970-01-01 0.138888889 0.674603175 0.24603175
1970-01-01 0.063492063 0.678571429 0.18650794
1970-01-01 0.111111111 0.678571429 0.22460317
1970-01-01 0.095238095 0.662698413 0.20873016
1970-01-01 0.142857143 0.507936508 0.21587302
1970-01-01 0.634920635 0.329365079 0.57380952
1970-01-01 0.650793651 0.329365079 0.58650794
1970-01-01 0.519841270 0.353174603 0.48650794
1970-01-01 0.396825397 0.218253968 0.36111111
1970-01-01 0.384920635 0.218253968 0.35158730
1970-01-01 0.095238095 0.361111111 0.14841270
1970-01-01 0.051587302 0.194444444 0.08015873
1970-01-01 0.047619048 0.198412698 0.07777778
1970-01-01 0.107142857 0.551587302 0.19603175
1970-01-01 0.095238095 0.702380952 0.21666667
1970-01-01 0.154761905 0.515873016 0.22698413
1970-01-01 0.329365079 0.515873016 0.36666667
1970-01-01 0.420634921 0.515873016 0.43968254
1970-01-01 0.313492063 0.515873016 0.35396825
1970-01-01 0.440476190 0.726190476 0.49761905
1970-01-01 0.619047619 0.726190476 0.64047619
1970-01-01 0.599206349 0.710317460 0.62142857
1970-01-01 0.646825397 0.559523810 0.62936508
1970-01-01 0.686507937 0.353174603 0.61984127
1970-01-01 0.583333333 0.341269841 0.53492063
1970-01-01 0.285714286 0.313492063 0.29126984
1970-01-01 0.150793651 0.154761905 0.15158730
1970-01-01 0.138888889 0.154761905 0.14206349
1970-01-01 0.178571429 0.321428571 0.20714286
1970-01-01 0.019841270 0.154761905 0.04682540
1970-01-01 0.039682540 0.154761905 0.06269841
1970-01-01 0.170634921 0.317460317 0.20000000
1970-01-01 0.123015873 0.313492063 0.16111111
1970-01-01 0.051587302 0.313492063 0.10396825
1970-01-01 0.690476190 0.305555556 0.61349206
1970-01-01 0.817460317 0.492063492 0.75238095
1970-01-01 0.706349206 0.698412698 0.70476190
1970-01-01 0.837301587 0.496031746 0.76904762
1970-01-01 0.908730159 0.146825397 0.75634921
1970-01-01 0.678571429 0.146825397 0.57222222
1970-01-01 0.623015873 0.146825397 0.52777778
1970-01-01 0.619047619 0.071428571 0.50952381
1970-01-01 0.567460317 0.150793651 0.48412698
1970-01-01 0.611111111 0.150793651 0.51904762
1970-01-01 0.543650794 0.071428571 0.44920635
1970-01-01 0.420634921 0.051587302 0.34682540
1970-01-01 0.448412698 0.051587302 0.36904762
1970-01-01 0.341269841 0.075396825 0.28809524
1970-01-01 0.202380952 0.075396825 0.17698413
1970-01-01 0.226190476 0.083333333 0.19761905
1970-01-01 0.329365079 0.083333333 0.28015873
1970-01-01 0.297619048 0.174603175 0.27301587
1970-01-01 0.484126984 0.345238095 0.45634921
1970-01-01 0.638888889 0.365079365 0.58412698
1970-01-01 0.587301587 0.345238095 0.53888889
1970-01-01 0.702380952 0.543650794 0.67063492
1970-01-01 0.678571429 0.591269841 0.66111111
1970-01-01 0.396825397 0.599206349 0.43730159
1970-01-01 0.234126984 0.789682540 0.34523810
1970-01-01 0.301587302 0.626984127 0.36666667
1970-01-01 0.083333333 0.396825397 0.14603175
1970-01-01 0.111111111 0.210317460 0.13095238
1970-01-01 0.214285714 0.059523810 0.18333333
1970-01-01 0.317460317 0.063492063 0.26666667
1970-01-01 0.365079365 0.150793651 0.32222222
1970-01-01 0.571428571 0.067460317 0.47063492
1970-01-01 0.543650794 0.023809524 0.43968254
1970-01-01 0.492063492 0.023809524 0.39841270
1970-01-01 0.353174603 0.031746032 0.28888889
1970-01-01 0.369047619 0.087301587 0.31269841
1970-01-01 0.424603175 0.178571429 0.37539683
1970-01-01 0.571428571 0.162698413 0.48968254
1970-01-01 0.698412698 0.154761905 0.58968254
1970-01-01 0.813492063 0.071428571 0.66507937
1970-01-01 0.662698413 0.079365079 0.54603175
1970-01-01 0.531746032 0.293650794 0.48412698
1970-01-01 0.289682540 0.460317460 0.32380952
1970-01-01 0.119047619 0.460317460 0.18730159
1970-01-01 0.119047619 0.706349206 0.23650794
1970-01-01 0.202380952 0.686507937 0.29920635
1970-01-01 0.142857143 0.452380952 0.20476190
1970-01-01 0.166666667 0.424603175 0.21825397
1970-01-01 0.285714286 0.650793651 0.35873016
1970-01-01 0.186507937 0.841269841 0.31746032
1970-01-01 0.170634921 0.853174603 0.30714286
1970-01-01 0.460317460 0.857142857 0.53968254
1970-01-01 0.730158730 0.853174603 0.75476190
1970-01-01 0.761904762 0.662698413 0.74206349
1970-01-01 0.817460317 0.265873016 0.70714286
1970-01-01 0.805555556 0.162698413 0.67698413
1970-01-01 0.694444444 0.261904762 0.60793651
1970-01-01 0.583333333 0.666666667 0.60000000
1970-01-01 0.456349206 0.662698413 0.49761905
1970-01-01 0.646825397 0.694444444 0.65634921
1970-01-01 0.785714286 0.690476190 0.76666667
1970-01-01 0.638888889 0.261904762 0.56349206
1970-01-01 0.345238095 0.261904762 0.32857143
1970-01-01 0.178571429 0.432539683 0.22936508
1970-01-01 0.007936508 0.611111111 0.12857143
1970-01-01 0.007936508 0.936507937 0.19365079
1970-01-01 0.166666667 0.976190476 0.32857143
1970-01-01 0.650793651 0.940476190 0.70873016
1970-01-01 0.956349206 0.936507937 0.95238095
1970-01-01 1.000000000 0.952380952 0.99047619
1970-01-01 0.996031746 0.837301587 0.96428571
1970-01-01 0.920634921 0.825396825 0.90158730
1970-01-01 0.726190476 0.821428571 0.74523810
1970-01-01 0.210317460 0.817460317 0.33174603
1970-01-01 0.011904762 0.626984127 0.13492063
1970-01-01 0.015873016 0.420634921 0.09682540
1970-01-01 0.146825397 0.281746032 0.17380952
1970-01-01 0.250000000 0.178571429 0.23571429
1970-01-01 0.595238095 0.178571429 0.51190476
1970-01-01 0.968253968 0.281746032 0.83095238
1970-01-01 0.976190476 0.416666667 0.86428571
1970-01-01 0.940476190 0.662698413 0.88492063
1970-01-01 0.964285714 0.833333333 0.93809524
1970-01-01 0.908730159 0.829365079 0.89285714
1970-01-01 0.579365079 0.805555556 0.62460317
1970-01-01 0.313492063 0.599206349 0.37063492
1970-01-01 0.273809524 0.595238095 0.33809524
1970-01-01 0.079365079 0.789682540 0.22142857
1970-01-01 0.103174603 0.801587302 0.24285714
1970-01-01 0.384920635 0.638888889 0.43571429
1970-01-01 0.658730159 0.392857143 0.60555556
1970-01-01 0.734126984 0.273809524 0.64206349
1970-01-01 0.825396825 0.087301587 0.67777778
1970-01-01 0.821428571 0.087301587 0.67460317
1970-01-01 0.817460317 0.281746032 0.71031746
1970-01-01 0.591269841 0.408730159 0.55476190
1970-01-01 0.198412698 0.261904762 0.21111111
1970-01-01 0.103174603 0.261904762 0.13492063
1970-01-01 0.055555556 0.448412698 0.13412698
1970-01-01 0.007936508 0.448412698 0.09603175
1970-01-01 0.047619048 0.666666667 0.17142857
1970-01-01 0.781746032 0.948412698 0.81507937
1970-01-01 0.952380952 0.984126984 0.95873016
1970-01-01 0.932539683 0.940476190 0.93412698
1970-01-01 0.988095238 0.853174603 0.96111111
1970-01-01 0.988095238 0.849206349 0.96031746
1970-01-01 0.861111111 0.821428571 0.85317460
1970-01-01 0.630952381 0.658730159 0.63650794
1970-01-01 0.857142857 0.456349206 0.77698413
1970-01-01 0.821428571 0.686507937 0.79444444
1970-01-01 0.853174603 0.865079365 0.85555556
1970-01-01 0.892857143 0.738095238 0.86190476
1970-01-01 0.940476190 0.734126984 0.89920635
1970-01-01 0.896825397 0.888888889 0.89523810
1970-01-01 0.742063492 0.726190476 0.73888889
1970-01-01 0.087301587 0.488095238 0.16746032
1970-01-01 0.007936508 0.472222222 0.10079365
1970-01-01 0.003968254 0.698412698 0.14285714
1970-01-01 0.003968254 0.662698413 0.13571429
1970-01-01 0.011904762 0.456349206 0.10079365
1970-01-01 0.214285714 0.285714286 0.22857143
1970-01-01 0.619047619 0.281746032 0.55158730
1970-01-01 0.813492063 0.281746032 0.70714286
1970-01-01 0.873015873 0.190476190 0.73650794
1970-01-01 0.932539683 0.285714286 0.80317460
1970-01-01 0.984126984 0.460317460 0.87936508
1970-01-01 0.992063492 0.710317460 0.93571429
1970-01-01 1.000000000 0.900793651 0.98015873
1970-01-01 1.000000000 0.908730159 0.98174603
1970-01-01 0.996031746 0.904761905 0.97777778
1970-01-01 0.992063492 0.968253968 0.98730159
1970-01-01 0.988095238 0.968253968 0.98412698
1970-01-01 0.916666667 0.968253968 0.92698413
1970-01-01 0.726190476 1.000000000 0.78095238
1970-01-01 0.734126984 0.996031746 0.78650794


On Tue, May 22, 2012 at 6:26 PM, Joshua Ulrich <josh.m.ulr...@gmail.com>wrote:

> On Tue, May 22, 2012 at 2:15 AM, R-type Studios <rtypestud...@gmail.com>
> wrote:
> > Hi Everyone,
> >
> > I'm currently using the latest build of R and R-Studio server (both are
> > amazing products)
> >
> > I'm still very new to this but I came across this issue:
> >
> > I'm trying to do a select from postgres and put the data into and xts
> > object like so:
> >
> > # Libs
> > library('RPostgreSQL') # http://code.google.com/p/rpostgresql/
> > library('quantmod')
> > library('TTR')
> > library('xts') #
> > http://cran.r-project.org/web/packages/xts/vignettes/xts.pdf
> >
> > # Connect and get data
> > drv <- dbDriver('PostgreSQL')
> > db <- dbConnect(drv, host='localhost', user='postgres',
> dbname='technica',
> > password='password')
> > fr <- dbGetQuery(db, 'SELECT date as "Date", open as "Open", high as
> > "High", low as "Low", close as "Close", volume as "Volume", random() as
> > "Adjusted" FROM stocks s INNER JOIN historical_prices hp ON s.id =
> > hp.stock_id WHERE s.symbol = \'SDL\' ORDER BY date DESC limit 50')
> >
> > # copied from the mysql code in the quantmod source
> > fr <- data.frame(fr[,-1],row.names=fr[,1])
> > fr <- xts(as.matrix(fr[,-1]), order.by=as.Date(fr[,1],
> > origin='1970-01-01'), src='technica', updated=Sys.time())
> > colnames(fr) <- paste('SDL',
> > c('Open','High','Low','Close','Volume','Adjusted'), sep='.')
> >
> > dvi <- DVI(Cl(fr))
> > print(dvi)
> >
> > When the code is executed I receive the error for the line dvi <-
> > DVI(Cl(fr)): "Error in runSum(x, n) : Invalid 'n'"
> >
> The defaults for DVI's magnitude and stretch arguments require there
> be at least 100 observations.  My guess is that your 'fr' object
> doesn't have the required 100 observations.  If it doesn't, then using
> the default value of n = 252 may also be an issue (giving misleading
> results, if not an error).
>
> > But if I do this (fetch the data from yahoo):
> >
> > getSymbols("SDL.AX")
> > dvi <- DVI(Cl(SDL.AX))
> > print(dvi)
> >
> > All seems to work fine, but I cant see any difference when I print the
> two
> > data sets out, except the data set size.
> >
> > Any ideas what I'm doing wrong? Id really like to be able to import from
> my
> > postgres database.
> >
> > Also i would be happy to write a postgres routine and submit it to
> quantmod
> > if i get this working
> >
> > Thanks!
> > Max
> >
>
> Best,
> --
> Joshua Ulrich  |  FOSS Trading: www.fosstrading.com
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to