[Blotter-commits] r1511 - in pkg/quantstrat: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Sep 29 22:58:46 CEST 2013
Author: braverock
Date: 2013-09-29 22:58:46 +0200 (Sun, 29 Sep 2013)
New Revision: 1511
Modified:
pkg/quantstrat/DESCRIPTION
pkg/quantstrat/NAMESPACE
pkg/quantstrat/R/orders.R
pkg/quantstrat/R/ruleOrderProc.R
pkg/quantstrat/man/getOrders.Rd
Log:
- fix time in force
- minor doc updates
- fix indents in ruleOrderProc.R
Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION 2013-09-26 16:18:11 UTC (rev 1510)
+++ pkg/quantstrat/DESCRIPTION 2013-09-29 20:58:46 UTC (rev 1511)
@@ -1,7 +1,7 @@
Package: quantstrat
Type: Package
Title: Quantitative Strategy Model Framework
-Version: 0.7.9
+Version: 0.7.10
Date: $Date$
Author: Peter Carl, Brian G. Peterson, Joshua Ulrich, Jan Humme
Depends:
Modified: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE 2013-09-26 16:18:11 UTC (rev 1510)
+++ pkg/quantstrat/NAMESPACE 2013-09-29 20:58:46 UTC (rev 1511)
@@ -13,12 +13,14 @@
export(applySignals)
export(applyStrategy)
export(applyStrategy.rebalancing)
+export(applyStrategy.rebalancing.training)
export(chart.forward)
export(chart.forward.training)
export(delete.paramset)
export(enable.rule)
export(get.orderbook)
export(get.strategy)
+export(getOrderBook)
export(getOrders)
export(getParameterTable)
export(getPosLimit)
Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R 2013-09-26 16:18:11 UTC (rev 1510)
+++ pkg/quantstrat/R/orders.R 2013-09-29 20:58:46 UTC (rev 1511)
@@ -25,7 +25,8 @@
#' get.orderbook
#' getOrderbook
#' @rdname getOrderBook
-#' @export
+#' @export get.orderbook
+#' @export getOrderBook
get.orderbook <- getOrderBook <- function(portfolio, envir=.strategy) #should symbol subsets be supported too? probably not.
{
if(!grepl("order_book",portfolio)) orders<-try(get(paste("order_book",portfolio,sep='.'),envir=envir),silent=TRUE)
@@ -110,7 +111,7 @@
#' @seealso addOrder
#' @concept order book
#' @export
-getOrders <- function(portfolio,symbol,status="open",timespan=NULL,ordertype=NULL, side=NULL, qtysign=NULL, orderset=NULL, which.i=FALSE, time.in.force=NULL)
+getOrders <- function(portfolio,symbol,status="open",timespan=NULL,ordertype=NULL, side=NULL, qtysign=NULL, orderset=NULL, which.i=FALSE)
{
#if(is.null(timespan)) stop("timespan must be an xts style timestring")
# get order book
@@ -134,7 +135,6 @@
(if(!is.null(ordertype)) ordersubset[,"Order.Type"]==ordertype else TRUE) &
(if(!is.null(side)) ordersubset[,"Order.Side"]==side else TRUE) &
(if(!is.null(orderset)) ordersubset[,"Order.Set"]==orderset else TRUE) &
- (if(!is.null(time.in.force)) strptime(ordersubset[,'Time.In.Force'], format='%Y-%m-%d %H:%M:%S')<time.in.force else TRUE) &
(if(!is.null(qtysign)) sign(as.numeric(ordersubset[,"Order.Qty"]))==qtysign else TRUE)
)
@@ -370,7 +370,7 @@
if(is.numeric(time.in.force))
time.in.force <- timestamp + time.in.force
- time.in.force <- format(time.in.force, "%Y-%m-%d %H:%M:%S")
+ time.in.force <- format(time.in.force, "%Y-%m-%d %H:%M:%0S")
}
}
Modified: pkg/quantstrat/R/ruleOrderProc.R
===================================================================
--- pkg/quantstrat/R/ruleOrderProc.R 2013-09-26 16:18:11 UTC (rev 1510)
+++ pkg/quantstrat/R/ruleOrderProc.R 2013-09-29 20:58:46 UTC (rev 1511)
@@ -51,380 +51,382 @@
#' @export
ruleOrderProc <- function(portfolio, symbol, mktdata, timestamp=NULL, ordertype=NULL, ..., slippageFUN=NULL)
{
- if(is.null(timestamp)) return()
+ if(is.null(timestamp)) return()
+
+ orderbook <- getOrderBook(portfolio)
+ ordersubset <- orderbook[[portfolio]][[symbol]]
+
+ ### retrieve open orders
+ OpenOrders.i<-getOrders(portfolio=portfolio, symbol=symbol, status="open", timespan=timespan, ordertype=ordertype, which.i=TRUE)
- orderbook <- getOrderBook(portfolio)
- ordersubset <- orderbook[[portfolio]][[symbol]]
+ #extract time in force for open orders
+ tif <- ordersubset[OpenOrders.i, 'Time.In.Force']
+ if(any(!tif=='')){
+ if (class(index(ordersubset))=='Date') tif <- as.Date(tif)
+ else tif <- strptime(tif, format='%Y-%m-%d %H:%M:%0S')
+ #check which ones should be expired
+ ExpiredOrders.i<-which(tif<index(ordersubset[OpenOrders.i]))
+ #mark them expired
+ ordersubset[OpenOrders.i[ExpiredOrders.i], "Order.Status"] = 'expired'
+ ordersubset[OpenOrders.i[ExpiredOrders.i], "Order.StatusTime"]<-ordersubset[OpenOrders.i[ExpiredOrders.i], "Time.In.Force"]
+ }
+
+ if(hasArg(prefer)) prefer=match.call(expand.dots=TRUE)$prefer
+ else prefer = NULL
+
+ # check for open orders
+ if (!(length(OpenOrders.i)>=1)){
+ return(NULL)
+ } else {
- ### first check time-in-force for open orders, and flag as 'expired' where appropriate
-# ExpiredOrders.i <- getOrders(portfolio=portfolio, symbol=symbol, status="open", time.in.force=timestamp, which.i=TRUE)
-# if(!is.null(ExpiredOrders.i))
-# {
-# ordersubset[ExpiredOrders.i, "Order.Status"] = 'expired'
-# ordersubset[ExpiredOrders.i, "Order.StatusTime"]<-ordersubset[ExpiredOrders.i, "Time.In.Force"]
-#
-# orderbook[[portfolio]][[symbol]] <- ordersubset
-# assign(paste("order_book",portfolio,sep='.'),orderbook,envir=.strategy)
-# }
-
- ### now retrieve open orders
- ### TODO calculate timespan here?
- OpenOrders.i<-getOrders(portfolio=portfolio, symbol=symbol, status="open", timespan=timespan, ordertype=ordertype, which.i=TRUE)
-
- if(hasArg(prefer)) prefer=match.call(expand.dots=TRUE)$prefer
- else prefer = NULL
-
- # check for open orders
- if (!(length(OpenOrders.i)>=1)){
- return(NULL)
- } else {
-
- mktdataTimestamp <- mktdata[timestamp]
- # only keep the last observation per time stamp
- if( NROW(mktdataTimestamp) > 1 ) mktdataTimestamp <- last(mktdataTimestamp)
- isOHLCmktdata <- is.OHLC(mktdata)
- isBBOmktdata <- is.BBO(mktdata)
- for (ii in OpenOrders.i )
+ mktdataTimestamp <- mktdata[timestamp]
+ # only keep the last observation per time stamp
+ if( NROW(mktdataTimestamp) > 1 ) mktdataTimestamp <- last(mktdataTimestamp)
+ isOHLCmktdata <- is.OHLC(mktdata)
+ isBBOmktdata <- is.BBO(mktdata)
+
+ for (ii in OpenOrders.i )
+ {
+ if(ordersubset[ii, "Order.Status"] != "open") # need to check this bc side effects may have changed order.status in this loop
+ next()
+
+ txnprice=NULL
+
+ txnfees=ordersubset[ii,"Txn.Fees"]
+
+ orderPrefer=ordersubset[ii, "Prefer"]
+ if(!orderPrefer=="") prefer=orderPrefer
+
+ orderPrice <- as.numeric(ordersubset[ii,"Order.Price"])
+
+ orderQty <- ordersubset[ii,"Order.Qty"]
+ if(orderQty %in% c('all','trigger'))
+ {
+ # this has to be an exit or risk order, so:
+ orderQty=-1*getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)
+ orderside<-ordersubset[ii, "Order.Side"]
+ if(((orderQty>0 && orderside=='long') || (orderQty<0 && orderside=='short')))
{
- if(ordersubset[ii, "Order.Status"] != "open") # need to check this bc sideeffects may have changed order.status in this loop
- next()
-
- txnprice=NULL
-
- txnfees=ordersubset[ii,"Txn.Fees"]
-
- orderPrefer=ordersubset[ii, "Prefer"]
- if(!orderPrefer=="") prefer=orderPrefer
-
- orderPrice <- as.numeric(ordersubset[ii,"Order.Price"])
-
- orderQty <- ordersubset[ii,"Order.Qty"]
- if(orderQty %in% c('all','trigger'))
- {
- # this has to be an exit or risk order, so:
- orderQty=-1*getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)
- orderside<-ordersubset[ii, "Order.Side"]
- if(((orderQty>0 && orderside=='long') || (orderQty<0 && orderside=='short')))
- {
- # this condition may occur if (for example) a signal triggers an 'increase LONG pos' and 'close all SHORT pos' simultaneously
- # hence this is legal condition, and we must 0 the orderQty to reject the order
-
- orderQty = 0
- }
- }
- orderQty<-as.numeric(orderQty)
-
- orderThreshold <- as.numeric(ordersubset[ii,"Order.Threshold"])
- # mktdataTimestamp <- mktdata[timestamp]
- #FIXME Should we only keep the last observation per time stamp?
- #if( NROW(mktdataTimestamp) > 1 ) mktdataTimestamp <- last(mktdataTimestamp)
-
- orderType <- ordersubset[ii,"Order.Type"]
-
- if(hasArg(allowMagicalThinking)) allowMagicalThinking=match.call(expand.dots=TRUE)$allowMagicalThinking
- else allowMagicalThinking = FALSE
-
- freq = periodicity(mktdata)
- #switch on frequency
- switch(orderType,
- market = {
- switch(freq$scale,
- yearly = ,
- quarterly = ,
- monthly = {
- txntime=as.character(index(ordersubset[ii,])) # transacts on this bar, e.g. in the intraday cross, or leading into the end of month, quarter, etc.
- # txntime=as.character(timestamp) # use this if you wanted to transact on the close of the next bar
- # txnprice=as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1])
- txnprice = orderPrice
- },
- daily = {
- if(isTRUE(allowMagicalThinking)){
- txntime=as.character(index(ordersubset[ii,])) # transacts on this bar, e.g. in the intraday cross, or leading into the end of month, quarter, etc.
- #txnprice=as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1])
- txnprice = orderPrice
- } else {
- txntime = timestamp
- txnprice = as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1]) #filled at now-prevailing 'price'
- }
- }, #end daily
- {
- txntime = timestamp
- if (isBBOmktdata) {
- #An ordertype of market will *almost* trump pricemethod here. orderPrice was determined using pricemethod.
- #but, for buy orders you'll be filled at either orderPrice or the current mkt ask -- whichever is worse.
- #and, for sell orders you'll be filled at either orderPrice or the current mkt bid -- whichever is worse.
- if(orderQty > 0){ # positive quantity 'buy'
- #fill at max(orderPrice,newMktAsk price)
- txnprice = max(orderPrice, as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]))
- } else { # negative quantity 'sell'
- txnprice = min(orderPrice, as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])) #presumes unique timestamp
- }
- #e.g. if pricemethod was opside, it sent a buy order at mktAsk. fill at greater of that ask, and current ask
- } else txnprice = as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1]) #filled at 'price'
- }) # end switch on frequency
- },
- limit= ,
- stoplimit =,
- iceberg = {
- if (!isBBOmktdata) {
- if( orderType == 'iceberg'){
- stop("iceberg orders only supported for BBO data")
- }
- # check to see if price moved through the limit
- if((orderQty > 0 && orderType != 'stoplimit') || (orderQty < 0 && (orderType=='stoplimit'))) {
- # buy limit, or sell stoplimit
- if( (has.Lo(mktdata) && orderPrice > as.numeric(Lo(mktdataTimestamp))) ||
- (!has.Lo(mktdata) && orderPrice > as.numeric(getPrice(mktdataTimestamp, prefer=prefer))))
- {
- txnprice = min(orderPrice, Hi(mktdataTimestamp))
- txntime = timestamp
- } else next() # price did not move through my order, should go to next order
- } else if((orderQty < 0 && orderType != 'stoplimit') || (orderQty > 0 && (orderType=='stoplimit'))) {
- # sell limit or buy stoplimit
- if ( (has.Hi(mktdata) && orderPrice < as.numeric(Hi(mktdataTimestamp))) ||
- (!has.Hi(mktdata) && orderPrice < as.numeric(getPrice(mktdataTimestamp,prefer=prefer))) )
- {
- txnprice = max(orderPrice, Lo(mktdataTimestamp))
- txntime = timestamp
- } else next() # price did not move through my order, should go to next order
- } else {
- warning('ignoring order with quantity of zero')
- next()
- }
- } else if(isBBOmktdata){
- # check side/qty
- if(orderQty > 0){ # positive quantity 'buy'
- if (orderType == 'stoplimit') {
- if(orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1])){
- # mktprice moved above our stop buy price
- txnprice = orderPrice #assume we got filled at our stop price
- #txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]) #presumes unique timestamps
- txntime = timestamp
- } else next()
- } else {
- if(orderPrice >= as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1])){
- # price we're willing to pay is higher than the offer price, so execute at the prevailing price
- #txnprice = orderPrice
- txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]) #presumes unique timestamps
- txntime = timestamp
- } else next()
- }
- } else { # negative quantity 'sell'
- if (orderType == 'stoplimit') {
- if(orderPrice >= as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])){
- # mktprice moved below our stop sell price
- txnprice = orderPrice #assumption is that we're filled at our stop price
- #txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1]) #presumes unique timestamp
- txntime = timestamp
- } else next()
- } else {
- if(orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])){
- # we're willing to sell at a better price than the bid, so execute at the prevailing price
- # txnprice = orderPrice
- txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1]) #presumes unique timestamp
- txntime = timestamp
- } else next()
- }
- }
-
-
- if( orderType == 'iceberg'){
- #we've transacted, so the old order was closed, put in a new one
- neworder<-addOrder(portfolio=portfolio,
+ # this condition may occur if (for example) a signal triggers an 'increase LONG pos' and 'close all SHORT pos' simultaneously
+ # hence this is legal condition, and we must 0 the orderQty to reject the order
+
+ orderQty = 0
+ }
+ }
+ orderQty<-as.numeric(orderQty)
+
+ orderThreshold <- as.numeric(ordersubset[ii,"Order.Threshold"])
+ # mktdataTimestamp <- mktdata[timestamp]
+ #FIXME Should we only keep the last observation per time stamp?
+ #if( NROW(mktdataTimestamp) > 1 ) mktdataTimestamp <- last(mktdataTimestamp)
+
+ orderType <- ordersubset[ii,"Order.Type"]
+
+ if(hasArg(allowMagicalThinking)) allowMagicalThinking=match.call(expand.dots=TRUE)$allowMagicalThinking
+ else allowMagicalThinking = FALSE
+
+ freq = periodicity(mktdata)
+ #switch on frequency
+ switch(orderType,
+ market = {
+ switch(freq$scale,
+ yearly = ,
+ quarterly = ,
+ monthly = {
+ txntime=as.character(index(ordersubset[ii,])) # transacts on this bar, e.g. in the intraday cross, or leading into the end of month, quarter, etc.
+ # txntime=as.character(timestamp) # use this if you wanted to transact on the close of the next bar
+ # txnprice=as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1])
+ txnprice = orderPrice
+ },
+ daily = {
+ if(isTRUE(allowMagicalThinking)){
+ txntime=as.character(index(ordersubset[ii,])) # transacts on this bar, e.g. in the intraday cross, or leading into the end of month, quarter, etc.
+ #txnprice=as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1])
+ txnprice = orderPrice
+ } else {
+ txntime = timestamp
+ txnprice = as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1]) #filled at now-prevailing 'price'
+ }
+ }, #end daily
+ {
+ txntime = timestamp
+ if (isBBOmktdata) {
+ #An ordertype of market will *almost* trump pricemethod here. orderPrice was determined using pricemethod.
+ #but, for buy orders you'll be filled at either orderPrice or the current mkt ask -- whichever is worse.
+ #and, for sell orders you'll be filled at either orderPrice or the current mkt bid -- whichever is worse.
+ if(orderQty > 0){ # positive quantity 'buy'
+ #fill at max(orderPrice,newMktAsk price)
+ txnprice = max(orderPrice, as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]))
+ } else { # negative quantity 'sell'
+ txnprice = min(orderPrice, as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])) #presumes unique timestamp
+ }
+ #e.g. if pricemethod was opside, it sent a buy order at mktAsk. fill at greater of that ask, and current ask
+ } else txnprice = as.numeric(getPrice(mktdataTimestamp, prefer=prefer)[,1]) #filled at 'price'
+ }
+ ) # end switch on frequency
+ },
+ limit= ,
+ stoplimit =,
+ iceberg = {
+ if (!isBBOmktdata) {
+ if( orderType == 'iceberg'){
+ stop("iceberg orders only supported for BBO data")
+ }
+ # check to see if price moved through the limit
+ if((orderQty > 0 && orderType != 'stoplimit') || (orderQty < 0 && (orderType=='stoplimit'))) {
+ # buy limit, or sell stoplimit
+ if( (has.Lo(mktdata) && orderPrice > as.numeric(Lo(mktdataTimestamp))) ||
+ (!has.Lo(mktdata) && orderPrice > as.numeric(getPrice(mktdataTimestamp, prefer=prefer))))
+ {
+ txnprice = min(orderPrice, Hi(mktdataTimestamp))
+ txntime = timestamp
+ } else next() # price did not move through my order, should go to next order
+ } else if((orderQty < 0 && orderType != 'stoplimit') || (orderQty > 0 && (orderType=='stoplimit'))) {
+ # sell limit or buy stoplimit
+ if ( (has.Hi(mktdata) && orderPrice < as.numeric(Hi(mktdataTimestamp))) ||
+ (!has.Hi(mktdata) && orderPrice < as.numeric(getPrice(mktdataTimestamp,prefer=prefer))) )
+ {
+ txnprice = max(orderPrice, Lo(mktdataTimestamp))
+ txntime = timestamp
+ } else next() # price did not move through my order, should go to next order
+ } else {
+ warning('ignoring order with quantity of zero')
+ next()
+ }
+ } else if(isBBOmktdata){
+ # check side/qty
+ if(orderQty > 0){ # positive quantity 'buy'
+ if (orderType == 'stoplimit') {
+ if(orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1])){
+ # mktprice moved above our stop buy price
+ txnprice = orderPrice #assume we got filled at our stop price
+ #txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]) #presumes unique timestamps
+ txntime = timestamp
+ } else next()
+ } else {
+ if(orderPrice >= as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1])){
+ # price we're willing to pay is higher than the offer price, so execute at the prevailing price
+ #txnprice = orderPrice
+ txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='ask')[,1]) #presumes unique timestamps
+ txntime = timestamp
+ } else next()
+ }
+ } else { # negative quantity 'sell'
+ if (orderType == 'stoplimit') {
+ if(orderPrice >= as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])){
+ # mktprice moved below our stop sell price
+ txnprice = orderPrice #assumption is that we're filled at our stop price
+ #txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1]) #presumes unique timestamp
+ txntime = timestamp
+ } else next()
+ } else {
+ if(orderPrice <= as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1])){
+ # we're willing to sell at a better price than the bid, so execute at the prevailing price
+ # txnprice = orderPrice
+ txnprice = as.numeric(getPrice(mktdataTimestamp,prefer='bid')[,1]) #presumes unique timestamp
+ txntime = timestamp
+ } else next()
+ }
+ }
+
+
+ if( orderType == 'iceberg'){
+ #we've transacted, so the old order was closed, put in a new one
+ neworder<-addOrder(portfolio=portfolio,
+ symbol=symbol,
+ timestamp=timestamp,
+ qty=orderQty,
+ price=as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1]),
+ ordertype=orderType,
+ side=ordersubset[ii,"Order.Side"],
+ threshold=orderThreshold,
+ status="open",
+ replace=FALSE, return=TRUE,
+ ,...=..., TxnFees=txnfees)
+
+ ordersubset<-rbind(ordersubset, neworder)
+
+ ordersubset[ii,"Order.Status"]<-'replaced'
+ ordersubset[ii,"Order.StatusTime"]<-format(timestamp, "%Y-%m-%d %H:%M:%S")
+ next()
+ }
+ }
+ },
+ stoptrailing = {
+ if(isBBOmktdata)
+ {
+
+ order.side <- ordersubset[ii, "Order.Side"]
+ mvstop <- FALSE
+ absOrderThreshold <- abs(orderThreshold)
+ # +++++++++ stop order price - buy
+ # ~~~~~~~~~ market price
+ # --------- stop order price - sell
+ if(orderQty > 0) { # positive quantity 'buy'
+ prefer <- 'ask'
+ mktPrice <- as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1])
+ # check to see if price moved through the limit
+ if(mktPrice >= orderPrice) { # buy when price >= stop
+ txnprice <- mktPrice
+ txntime <- timestamp
+ }
+ # move stop if price < stop - thresh
+ else {
+ mvstop <- orderPrice - absOrderThreshold > mktPrice
+ new.order.price <- min(orderPrice, mktPrice + absOrderThreshold)
+ #new.order.price <- mktPrice + absOrderThreshold
+ }
+ } else { # negative quantity 'sell'
+ prefer <- 'bid'
+ mktPrice <- as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1])
+ # check to see if price moved through the limit
+ if(mktPrice <= orderPrice) { # sell when price <= stop
+ txnprice <- mktPrice
+ txntime <- timestamp
+ }
+ # move stop if price > stop + thresh
+ else {
+ mvstop <- orderPrice + absOrderThreshold < mktPrice
+ new.order.price <- max(orderPrice, mktPrice - absOrderThreshold)
+ #new.order.price <- mktPrice - absOrderThreshold
+ }
+ }
+ if( isTRUE(mvstop) ){
+ # if ordersubset[ii, "Order.Qty"] was character, we must recover it
+ new.order.qty <- ordersubset[ii, "Order.Qty"]
+ if(!is.na(suppressWarnings(as.numeric(new.order.qty))))
+ new.order.qty <- as.numeric(new.order.qty)
+
+ neworder<-addOrder(portfolio=portfolio,
+ symbol=symbol,
+ timestamp=timestamp,
+ qty=new.order.qty,
+ price=new.order.price-orderThreshold,
+ ordertype=orderType,
+ prefer=prefer,
+ side=order.side,
+ threshold=orderThreshold,
+ status="open",
+ replace=FALSE, return=TRUE,
+ orderset=ordersubset[ii,"Order.Set"],
+ label=ordersubset[ii,"Rule"],
+ ,...=..., TxnFees=txnfees)
+
+ ordersubset<-rbind(ordersubset, neworder)
+
+ ordersubset[ii,"Order.Status"]<-'replaced'
+ ordersubset[ii,"Order.StatusTime"]<-format(timestamp, "%Y-%m-%d %H:%M:%S")
+ next()
+ }
+ # else next
+ }
+ else if(isOHLCmktdata)
+ {
+ # check to see if price moved through the limit
+
+ order.side <- ordersubset[ii, "Order.Side"]
+
+ if(order.side == 'long' && as.numeric(Lo(mktdataTimestamp)) < orderPrice
+ || order.side == 'short' && as.numeric(Hi(mktdataTimestamp)) > orderPrice)
+ {
+ txnprice <- orderPrice
+ txntime <- timestamp
+ }
+ else
+ {
+ # do we need to change the trailing stop?
+
+ order.threshold <- as.numeric(ordersubset[ii, "Order.Threshold"])
+
+ if(order.side == 'long')
+ new.order.price <- max(orderPrice, as.numeric(Hi(mktdataTimestamp)) + order.threshold)
+ if(order.side == 'short')
+ new.order.price <- min(orderPrice, as.numeric(Lo(mktdataTimestamp)) + order.threshold)
+
+ if(new.order.price != orderPrice)
+ {
+ # adjust trailing stop
+
+ order.qty <- ordersubset[ii, "Order.Qty"] # if orderQty='all' we must recover it
+
+ neworder<-addOrder(portfolio=portfolio,
symbol=symbol,
timestamp=timestamp,
- qty=orderQty,
- price=as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1]),
+ qty=order.qty,
+ price=new.order.price - order.threshold,
ordertype=orderType,
- side=ordersubset[ii,"Order.Side"],
- threshold=orderThreshold,
+ side=order.side,
+ threshold=order.threshold,
status="open",
replace=FALSE, return=TRUE,
+ orderset=ordersubset[ii,"Order.Set"],
+ label=ordersubset[ii,"Rule"],
,...=..., TxnFees=txnfees)
-
- ordersubset<-rbind(ordersubset, neworder)
-
- ordersubset[ii,"Order.Status"]<-'replaced'
- ordersubset[ii,"Order.StatusTime"]<-format(timestamp, "%Y-%m-%d %H:%M:%S")
- next()
- }
- }
- },
- stoptrailing = {
- if(isBBOmktdata)
- {
-
- order.side <- ordersubset[ii, "Order.Side"]
- mvstop <- FALSE
- absOrderThreshold <- abs(orderThreshold)
- # +++++++++ stop order price - buy
- # ~~~~~~~~~ market price
- # --------- stop order price - sell
- if(orderQty > 0) { # positive quantity 'buy'
- prefer <- 'ask'
- mktPrice <- as.numeric(getPrice(mktdataTimestamp,prefer=prefer)[,1])
- # check to see if price moved through the limit
- if(mktPrice >= orderPrice) { # buy when price >= stop
- txnprice <- mktPrice
- txntime <- timestamp
- }
- # move stop if price < stop - thresh
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/blotter -r 1511
More information about the Blotter-commits
mailing list