Skip to content
Browse files

Added quick and dirty real time quote function

  • Loading branch information...
1 parent 265bcb7 commit 8f7bf6bee3ecc11656ca74b6737fb04aca300016 B. W. Lewis committed
Showing with 94 additions and 5 deletions.
  1. +3 −2 R/charting.R
  2. +8 −1 R/controlCMD.R
  3. +39 −0 R/level1.R
  4. +7 −2 R/symbology.R
  5. +37 −0 man/iqQuote.Rd
View
5 R/charting.R
@@ -23,8 +23,8 @@ crapmod = function(x, title="", col=4, lwd=2, lty=1, xlim=NULL, ylim=NULL)
if(yat[length(yat)] > length(x))
yat = yat[-length(yat)]
abline(h=yax,lwd=1,col="#aabbff")
- if(length(mat)>0) {
-# Plot a whole year or more of data
+ if(length(mat)>1) {
+# Plot a whole month or more of data
M = format(index(x)[mat],"%h")
axis(side=1,at=mat,labels=FALSE)
abline(v=mat,lwd=1,col="#aabbff")
@@ -40,6 +40,7 @@ crapmod = function(x, title="", col=4, lwd=2, lty=1, xlim=NULL, ylim=NULL)
return(invisible())
}
D = unique(format(index(x)[at],"%h %d"))
+ D = D[!is.na(D)]
axis(side=1,at=at,labels=D)
abline(v=at,lwd=1,col="#aabbff")
if(length(D)<2) {
View
9 R/controlCMD.R
@@ -24,7 +24,14 @@ function(timeout=7)
function(port)
{
port <- port[[1]]
- .iqEnv$con[port][[1]] <- socketConnection(.iqEnv$host, .iqEnv$ports[port][[1]],open='a+b')
+# R Windows appears to suffer from a serious problem affecting non-blocking
+# connections and readBin with raw data, see:
+# http://www.mail-archive.com/r-devel@r-project.org/msg16420.html.
+# we force blocking connections on Windows systems.
+ if(Sys.info()[[1]] == "Windows")
+ .iqEnv$con[port][[1]] <- socketConnection(.iqEnv$host, .iqEnv$ports[port][[1]],open='a+b', blocking=TRUE)
+ else
+ .iqEnv$con[port][[1]] <- socketConnection(.iqEnv$host, .iqEnv$ports[port][[1]],open='a+b')
}
# Close a port
View
39 R/level1.R
@@ -0,0 +1,39 @@
+`iqQuote` <-
+ function(symbol, type="P", limit=10)
+{
+ cmd = paste("w",symbol,"\r\n",sep="")
+ retval <- NULL
+ tryCatch(
+ {
+ .iqConnect("level1")
+ con <- .iqEnv$con["level1"][[1]]
+ if(.iqBlock(con,write=TRUE)==FALSE) return(NULL)
+ cat(cmd, file=con)
+ retval <- .getQuote(type=type,sym=symbol,limit=limit)
+ },
+ error=function(e) {.iqClose("level1"); warning(e)})
+ retval
+}
+
+.getQuote <- function(type, sym, limit)
+{
+ con <- .iqEnv$con["level1"][[1]]
+ if(.iqBlock(con,write=FALSE)==FALSE) return(NULL)
+ j <- 1
+ x <- c()
+ while(j<limit) {
+ if(.iqBlock(con,write=FALSE)==FALSE) break
+ dat <- tryCatch(readLines(con, n=1), error=function(e) warning(e))
+ if(length(dat)>0) {
+ y <- strsplit(dat,",")[[1]]
+ if(length(y)>0)
+ if(y[1]==type && y[2]==sym) {
+ x <- y
+ break
+ }
+ }
+ j <- j + 1
+ }
+ .iqClose("level1")
+ return(x)
+}
View
9 R/symbology.R
@@ -4,8 +4,13 @@
con <- .iqEnv$con["historic"][[1]]
# Check for a numeric month and convert to IQfeed month codes
if(is.numeric(monthCodes)){
- monthCodes <- paste(LETTERS[c(monthCodes, 12 + monthCodes)],collapse="")
- near <- NULL
+ if(pc=="p")
+ monthCodes <- paste(LETTERS[12 + monthCodes],collapse="")
+ else if(pc=="c")
+ monthCodes <- paste(LETTERS[c(monthCodes)],collapse="")
+ else
+ monthCodes <- paste(LETTERS[c(monthCodes, 12 + monthCodes)],collapse="")
+ near <- ""
}
cmd <- paste("CEO",symbol, pc, monthCodes, near, "\r\n",sep=",")
if(.iqBlock(con,write=TRUE)==FALSE) return(NULL)
View
37 man/iqQuote.Rd
@@ -0,0 +1,37 @@
+\name{iqQuote}
+\alias{iqQuote}
+\title{
+Quick and dirty real time quote
+}
+\description{
+Retrieve a single real time quote from an IQFeed stream using a new
+ephemeral IQFeed Level1 data session.
+}
+\usage{
+iqQuote(symbol, type="P", limit=10)
+}
+\arguments{
+ \item{symbol}{
+An underlying equity symbol.
+}
+ \item{type}{
+A character indicating the quote type (the first field in the IQFeed quote
+format, usually "P" for price quote or "F" for fundamentals quote).
+}
+ \item{limit}{
+Limit feed session to the number of lines specified.
+}
+}
+\value{
+A character vector containing the requested IQFeed quote data, or NULL if
+no quote matching the input parameters was returned.
+}
+\author{
+B. W. Lewis
+}
+
+\examples{
+require('iqfeed')
+iqConf()
+iqQuote("MSFT")
+}

0 comments on commit 8f7bf6b

Please sign in to comment.
Something went wrong with that request. Please try again.