[R-SIG-Finance] quantstrat chain rule type
stergios marinopoulos
stergenator at gmail.com
Fri Aug 22 22:08:04 CEST 2014
Hi, I am having trouble getting a simple profit taking order to work via a
chaining rule. In the contrived example below, I force a long entry of 2
shares. Then I expect 1 share to be sold after a 1 point move (i.e. the
"profit taker"), and then I expect the final share to be sold when price
falls below the SMA50.
While I do not see the profit taker order get executed, the price falling
below the SMA50 works as expected for all remaining shares.
I would appreciate it if someone could point out what I am doing wrong with
the chaining rule.
Thank you,
--
sm
Stergios Marinopoulos
library(quantstrat)
# Boiler Plate
tickerSymbol = "GE"
strategyStr = 'TwoUnits'
GE = getSymbols(tickerSymbol, from = "2012-01-01", to = "2012-11-15",
auto.assign = FALSE)
GE$SMA50 = SMA(Cl(GE), n = 50)
GE$CrossBack = Cl(GE) - GE$SMA50
GE = GE["2012-07-15/"]
magicGoLongDay = "2012-07-23"
currency("USD")
stock(tickerSymbol, currency="USD", multiplier=1)
rm.strat(strategyStr)
initDate = index(GE[1]) - 1
initPortf(name=strategyStr, symbols=tickerSymbol, initDate=initDate,
currency="USD")
initAcct(name=strategyStr, portfolios=strategyStr, initDate=initDate,
initEq=1e4, currency="USD")
initOrders(portfolio=strategyStr, initDate=initDate)
strategy(strategyStr, store=TRUE)
zeros = xts(rep(0,nrow(GE)), order.by=index(GE))
chartSeries(GE, TA="addTA(GE$SMA50, on=1, col=6);addTA(GE$CrossBack,
col=6);addTA(zeros, on=2, col=7);")
# The indicator function. Force a TRUE value on 6/5/2012
myIndicator <- function(n=2)
{
indicator = xts(x=rep(0, nrow(GE)), order.by=index(GE) )
names(indicator) = "indValue"
indicator[magicGoLongDay, "indValue"] = 1
return( indicator[, "indValue"] )
}
add.indicator(strategy=strategyStr, name="myIndicator",
arguments=list(n=2), label="indLabel")
add.signal(
strategy=strategyStr,
name="sigThreshold",
arguments=list(
column = "CrossBack",
relationship = "lt",
threshold = 0,
cross = TRUE
),
label="sig.price.lt.sma50"
)
add.signal(
strategy=strategyStr,
name="sigThreshold",
arguments=list(
column = "indValue.indLabel",
relationship = "eq",
threshold = 1,
cross = TRUE
),
label="goLong"
)
# Exit remaining when price crosses below on SMA50
add.rule(strategy=strategyStr, name='ruleSignal',
arguments = list(
sigcol = "sig.price.lt.sma50",
sigval = TRUE,
replace = FALSE,
orderside = 'long',
ordertype = 'market',
orderqty = 'all',
prefer = 'Open'
),
type = 'exit',
label = 'ExitPriceLTSMA50'
)
# Exit 1 unit as an initial profit target
add.rule(strategy=strategyStr, name='ruleSignal',
arguments = list(
sigcol = 'goLong',
sigval = TRUE,
replace = FALSE,
orderside = 'long',
ordertype = 'limit',
# ruletype = 'exit', # Is this order ambiguous?
tmult = FALSE,
threshold = 1.00,
orderqty = 1,
prefer = 'Open'
),
type = 'chain',
parent = 'EnterLong',
label = 'TakeProfit'
)
# Go long 2 shares when we have long signal
add.rule(strategy=strategyStr, name = 'ruleSignal',
arguments = list(
sigcol = 'goLong',
sigval = TRUE,
orderside = 'long' ,
ordertype = 'market',
orderqty = 2,
prefer = 'Open'
),
type = 'enter',
label = 'EnterLong'
)
out = applyStrategy(strategy=strategyStr, portfolios=strategyStr,
verbose=TRUE, debug=TRUE)
# Calculate P&L and resulting equity with blotter
dateRange=paste(
as.character(index(first(GE))-1),
'::',
as.character(index(last(GE))+1),
sep='')
updatePortf(strategyStr, Dates = dateRange)
updateAcct(strategyStr, Dates = dateRange)
updateEndEq(strategyStr, Dates = dateRange)
obook = getOrderBook(portfolio=strategyStr)
transactions = getTxns(Portfolio=strategyStr, Symbol=tickerSymbol)
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list