[R-SIG-Finance] quantstrat chain rule type
Ilya Kipnis
ilya.kipnis at gmail.com
Fri Aug 22 22:46:25 CEST 2014
Also, just to answer the question with a demo I know for sure works,
here's one with the take profit order working as intended.
require(quantstrat)
require(PerformanceAnalytics)
initDate="1990-01-01"
from="2003-01-01"
to=as.character(Sys.Date())
options(width=70)
options("getSymbols.warning4.0"=FALSE)
rm(list=ls(.blotter), envir=.blotter)
currency('USD')
Sys.setenv(TZ="UTC")
getSymbols("SPY", from=from, to=to)
stock(symbols, currency="USD", multiplier=1)
strategy.st <- portfolio.st <- account.st <- "chainExample"
rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)
add.indicator(strategy.st, name="RSI",
arguments=list(price=quote(Cl(mktdata)), n=2),
label="rsi")
add.signal(strategy.st, name="sigThreshold",
arguments=list(column="rsi", threshold=10,
relationship="lt", cross=TRUE),
label="entryLong")
add.signal(strategy.st, name="sigThreshold",
arguments=list(column="rsi", threshold=70,
relationship="gt", cross=TRUE),
label="exitLong")
add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="entryLong", sigval=TRUE, ordertype="market",
orderside="long", replace=FALSE,
prefer="Open", orderqty=2,
orderset='orders'),
type="enter", path.dep=TRUE, label="longEntry")
add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="entryLong", sigval=TRUE, ordertype="limit",
orderside="long", replace=FALSE,
prefer="Open", orderqty=-1,
threshold=1, tmult=FALSE, orderset='orders'),
type='chain', path.dep=TRUE, parent="longEntry", label="TPlong")
add.rule(strategy.st, name="ruleSignal",
arguments=list(sigcol="exitLong", sigval=TRUE, ordertype="market",
orderside="long", replace=FALSE,
prefer="Open", orderqty="all",
orderset='orders'),
type='exit', path.dep=TRUE, label="usualExit")
#apply strategy
t1 <- Sys.time()
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
t2 <- Sys.time()
print(t2-t1)
#set up analytics
updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)
chart.Posn(portfolio.st, "SPY")
zoom_Chart("2005")
On Fri, Aug 22, 2014 at 4:08 PM, stergios marinopoulos
<stergenator at gmail.com> wrote:
> 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]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
More information about the R-SIG-Finance
mailing list