Heiko,<div><br></div><div>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.</div>
<div><br></div><div>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?</div><div><br></div><div>-Garrett</div><div><br></div><div>
<br></div><div><div>#Heiko's test script, modified by Garrett</div><div><br></div><div>require(quantstrat)</div><div>library(timeDate)</div><div><br></div><div># Clean up</div><div>rm(list=ls(all=TRUE, pos=.blotter), pos=.blotter)</div>
<div>rm(list=ls(all=TRUE, pos=.instrument), pos=.instrument)</div><div>rm(list=ls(all=TRUE, pos=.strategy), pos=.strategy)</div><div><br></div><div># Custom indicator, returns last bar's Close price</div><div>LastPrice <- function(price) {</div>
<div> temp <- Cl(price)</div><div> colnames(temp) <- "LastPrice"</div><div> temp</div><div>}</div><div><br></div><div># Set initial values</div><div>initDate <- '1999-01-04 00:00'</div>
<div>initEq <- 10000</div><div><br></div><div># Set up instruments</div><div>currency("USD")</div><div>symbols = "EURUSD"</div><div>stock("EURUSD","USD")</div><div><br></div><div>
# Load data</div><div>#setwd('~/Downloads')</div><div>EURUSD <- read.csv("EURUSD240.csv", stringsAsFactors=F, header=F)</div><div>EURUSD_datetimes <- timeDate(paste(EURUSD[,1], EURUSD[,2]), format = "%Y.%m.%d %H:%M")</div>
<div>EURUSD <- xts(EURUSD[3:7], EURUSD_datetimes)</div><div><br></div><div>#getSymbols(symbols, src="csv")</div><div>for(symbol in symbols) {</div><div> x <- get(symbol)</div><div> x <- to.hourly(x, indexAt='firstof', drop.time=FALSE)</div>
<div> indexFormat(x) <- '%Y-%m-%d %H:%M:%S'</div><div> colnames(x) <- gsub("x",symbol,colnames(x))</div><div> assign(symbol,x)</div><div>}</div><div><br></div><div><a href="http://portfolio.st">portfolio.st</a> <- 'FXportf'</div>
<div><a href="http://account.st">account.st</a> <- 'FXacc'</div><div><br></div><div># Initialize portfolio and account</div><div>initPortf(name=<a href="http://portfolio.st">portfolio.st</a>, symbols=symbols, initDate=initDate)</div>
<div>initAcct(name=<a href="http://account.st">account.st</a>, portfolios=<a href="http://portfolio.st">portfolio.st</a>, initDate=initDate, initEq=initEq)</div><div>initOrders(portfolio=<a href="http://portfolio.st">portfolio.st</a>, initDate=initDate)</div>
<div><br></div><div># Initialize a strategy object</div><div>strat <- strategy("StoplossTest")</div><div><br></div><div># Signal: TRUE if last bar's Close > 1.41</div><div>strat <- add.signal(strategy = strat, name="sigThreshold",arguments = list(threshold=1.41, column="LastPrice",relationship="gt", cross=FALSE),label="LastCloseThrExc")</div>
<div><br></div><div># Add an indicator</div><div>strat <- add.indicator(strategy = strat, name = "LastPrice", arguments = list(price=quote(mktdata)), label= "LastPrice")</div><div><br></div><div>#Rule: Buy</div>
<div>strat <- add.rule(strategy = strat, name='ruleSignal', arguments = list(sigcol="LastCloseThrExc", sigval=TRUE, orderqty=1000, ordertype='market', orderside='long'), type='enter')</div>
<div><br></div><div># Rule: Add stop loss</div><div>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')</div>
<div><br></div><div># Process the indicators and generate trades</div><div>out <- applyStrategy(strategy=strat, portfolios=<a href="http://portfolio.st">portfolio.st</a>)</div><div><br></div><div>updatePortf(Portfolio=<a href="http://portfolio.st">portfolio.st</a>, Symbols=symbols)</div>
<div><br></div><div># graphics</div><div>for(symbol in symbols){</div><div> dev.new()</div><div> chart.Posn(Portfolio=<a href="http://portfolio.st">portfolio.st</a>, Symbol=symbol, theme=themelist)</div><div>}</div>
<div><br></div><div># Save results for inspection</div><div>ob <- getOrderBook("FXportf")</div><div>write.zoo(mktdata, quote=FALSE, file = "mktdata.csv")</div><div>write.zoo(getTxns("FXportf", "EURUSD"), quote=FALSE, file = "txns.csv")</div>
<div>write.zoo(ob$FXportf$EURUSD, quote=FALSE, file = "orderbook.csv")</div></div><div><br></div>