[R-SIG-Finance] Help required in getting SMA triggered entry with quantstrat add.rule
golam sakline
golam.sakline at gmail.com
Mon Aug 1 12:07:46 CEST 2016
Hi,
In Quantstrat, is it possible to use add.rule that can pick the crossover
between SMA and the price as entry rather than the bar close price? Is
there a way I can add buy-stop and sell-stop order types using limits that
look into the SMA figures.
Also why does it seem like that the trade registration date is one day
late? How can we fix this to register as per the day of the signal?-- See
the mktdata and getTxns output.
Many thanks
Golam
##################################################################################
if(exists('.strategy')) rm.strat(qs.strategy)
if(!exists('.blotter')) .blotter <- new.env()
if(!exists('.strategy')) .strategy <- new.env()
qs.strategy <- "AD26"
initPortf(qs.strategy,'XLE', initDate=initDate)
initAcct(qs.strategy,portfolios=qs.strategy, initDate=initDate, initEq=initEq)
initOrders(portfolio=qs.strategy,initDate=initDate)
strategy(qs.strategy,store=TRUE)
ls(.blotter)
ls(.strategy)
strat <-getStrategy(qs.strategy)
summary(strat)
add.indicator(strategy = qs.strategy, name = "SMA",
arguments = list(x = quote(Cl(mktdata)), n=21), label="RT")
summary(getStrategy(qs.strategy))
#
add.signal(qs.strategy, name="sigCrossover",
arguments=list(columns=c("Close", "SMA"), relationship="gte"),
label="Buy")
add.signal(qs.strategy, name="sigCrossover",
arguments=list(columns=c("Close", "SMA"), relationship="lte"),
label="Sell")
#
summary(getStrategy(qs.strategy))
#
add.rule(qs.strategy, name='ruleSignal',
arguments = list(sigcol="Buy", sigval=TRUE, orderqty=5000,
ordertype='market', orderside='long'),
type='enter')
add.rule(qs.strategy, name='ruleSignal',
arguments = list(sigcol="Sell", sigval=TRUE, orderqty="all",
ordertype='market', orderside='long'),
type='exit')
add.rule(qs.strategy, name='ruleSignal',
arguments = list(sigcol="Sell", sigval=TRUE, orderqty=-5000,
ordertype='market', orderside='short'),
type='enter')
add.rule(qs.strategy, name='ruleSignal',
arguments = list(sigcol="Buy", sigval=TRUE, orderqty="all",
ordertype='market', orderside='short'),
type='exit')
summary(getStrategy(qs.strategy))
applyStrategy(strategy=qs.strategy , portfolios=qs.strategy)
getTxns(Portfolio=qs.strategy, Symbol="XLE")
head(mktdata["2016"], 30)
XLE.Open XLE.High XLE.Low XLE.Close XLE.Volume
XLE.Adjusted SMA.RT Buy Sell
2016-01-22 55.33 56.12 54.45 55.74 38622500
54.98281 57.36524 NA NA
2016-01-25 54.68 56.08 53.10 53.14 29120000
52.41813 57.06048 NA NA
2016-01-26 54.10 55.22 53.40 55.15 25354900
54.40083 56.72810 NA NA
2016-01-27 54.77 56.42 54.20 54.89 36494500
54.14436 56.41000 NA NA
2016-01-28 56.97 57.19 55.43 *56.56* 38171900
55.79167 *56.22476* 1 NA
2016-01-29 56.66 58.27 56.49 *58.21* 31498200
57.41926 56.09810 NA NA
2016-02-01 57.24 57.67 56.39 57.24 27685100
56.46243 55.96429 NA NA
2016-02-02 55.84 56.10 55.05 55.33 25231200
54.57838 55.72667 NA 1
2016-02-03 56.13 57.35 54.11 57.30 36440700
56.52162 55.58381 1 NA
2016-02-04 57.42 58.50 57.00 57.27 37056100
56.49203 55.42857 NA NA
2016-02-05 56.50 56.74 55.47 55.94 30241000
55.18009 55.32095 NA NA
2016-02-08 54.83 56.00 54.00 55.65 34806700
54.89403 55.26714 NA NA
2016-02-09 54.82 55.49 53.44 54.31 37411300
53.57224 55.18429 NA 1
2016-02-10 54.15 55.32 53.51 54.09 30215800
53.35522 55.14810 NA NA
getTxns(Portfolio=qs.strategy, Symbol="XLE")...
10 2015-12-03 2016-01-29 -5000 -5000 2 -325550
34500.010 0.000 66700.005 0.1059745338
11 2016-02-03 2016-02-04 -5000 -5000 2 -286500
149.995 0.000 149.995 0.0005235428
12 2016-02-10 2016-02-16 -5000 -5000 2 -270450
-8700.010 -8700.010 1149.995 -0.0321686449
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list