[R-SIG-Finance] Quantstrat stoplimit orders not triggered

G See gsee000 at gmail.com
Fri Jul 22 05:23:26 CEST 2011


Heiko,

The problem here is that you're ending up with more than one column that has
"Close" in the name.  When it tries to addOrder it gets confused because
there is more than one "Close" price.  I'll try to get that patched this
weekend.

For now, I modified your script so that it works by changing your LastClose
function to LastPrice.  Does that do what it's supposed to do?

-Garrett


#Heiko's test script, modified by Garrett

require(quantstrat)
library(timeDate)

# Clean up
rm(list=ls(all=TRUE, pos=.blotter), pos=.blotter)
rm(list=ls(all=TRUE, pos=.instrument), pos=.instrument)
rm(list=ls(all=TRUE, pos=.strategy), pos=.strategy)

# Custom indicator, returns last bar's Close price
LastPrice <- function(price) {
       temp <-  Cl(price)
       colnames(temp) <- "LastPrice"
       temp
}

# Set initial values
initDate <- '1999-01-04 00:00'
initEq <- 10000

# Set up instruments
currency("USD")
symbols = "EURUSD"
stock("EURUSD","USD")

# Load data
#setwd('~/Downloads')
EURUSD <- read.csv("EURUSD240.csv", stringsAsFactors=F, header=F)
EURUSD_datetimes <- timeDate(paste(EURUSD[,1], EURUSD[,2]), format =
"%Y.%m.%d %H:%M")
EURUSD <- xts(EURUSD[3:7], EURUSD_datetimes)

#getSymbols(symbols, src="csv")
for(symbol in symbols) {
   x <- get(symbol)
   x <- to.hourly(x, indexAt='firstof', drop.time=FALSE)
   indexFormat(x) <- '%Y-%m-%d %H:%M:%S'
   colnames(x) <- gsub("x",symbol,colnames(x))
   assign(symbol,x)
}

portfolio.st <- 'FXportf'
account.st <- 'FXacc'

# Initialize portfolio and account
initPortf(name=portfolio.st, symbols=symbols, initDate=initDate)
initAcct(name=account.st, portfolios=portfolio.st, initDate=initDate,
initEq=initEq)
initOrders(portfolio=portfolio.st, initDate=initDate)

# Initialize a strategy object
strat <- strategy("StoplossTest")

# Signal: TRUE if last bar's Close > 1.41
strat <- add.signal(strategy = strat, name="sigThreshold",arguments =
list(threshold=1.41, column="LastPrice",relationship="gt",
cross=FALSE),label="LastCloseThrExc")

# Add an indicator
strat <- add.indicator(strategy = strat, name = "LastPrice", arguments =
list(price=quote(mktdata)), label= "LastPrice")

#Rule: Buy
strat <- add.rule(strategy = strat, name='ruleSignal', arguments =
list(sigcol="LastCloseThrExc", sigval=TRUE, orderqty=1000,
ordertype='market', orderside='long'), type='enter')

# Rule: Add stop loss
strat <- add.rule(strategy = strat, name='ruleSignal', arguments =
list(sigcol="LastCloseThrExc", sigval=TRUE, orderqty=-1000,
ordertype='stoplimit', orderside='long', threshold=-0.02, tmult=FALSE,
replace=FALSE), type='risk')

# Process the indicators and generate trades
out <- applyStrategy(strategy=strat, portfolios=portfolio.st)

updatePortf(Portfolio=portfolio.st, Symbols=symbols)

# graphics
for(symbol in symbols){
       dev.new()
       chart.Posn(Portfolio=portfolio.st, Symbol=symbol, theme=themelist)
}

# Save results for inspection
ob <- getOrderBook("FXportf")
write.zoo(mktdata, quote=FALSE, file = "mktdata.csv")
write.zoo(getTxns("FXportf", "EURUSD"), quote=FALSE, file = "txns.csv")
write.zoo(ob$FXportf$EURUSD, quote=FALSE, file = "orderbook.csv")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110721/c93e95a7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Stoploss_Test-02.R
Type: text/x-r
Size: 2728 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110721/c93e95a7/attachment.bin>


More information about the R-SIG-Finance mailing list