[R-SIG-Finance] Signal and Rule question in Quantstrat

Isak Engdahl isak.engdahl at gmail.com
Fri Jan 23 15:49:15 CET 2015


Dear members,

I have a questions how to set up the signals and rules correct for this
stategy in Quantstrat.

Price must be above SMA=250 and below the 20 line in the Slow Stochastic
indicator.

The trigger for a buy signal is when the signal line of the stochastic
indicator close above the 20 line.

Enter trade
PRICE > SMA=250
Slow Stochastic < 20 and crossing above the 20 line

Exit trade
PRICE < SMA=250
Slow Stoch > 80 and closes below the 80 line

The rules and examples can be found here:
http://stockcharts.com/public/1107832

Here is the code I have come up with now. Please help!

//Isak

#parameters
nFastK <- 5
nSlowD <- 1
nSMA <- 250
buyThresh <- 20
sellThresh <- 80

######################################################################################
#indicator
add.indicator(strategy.st, name="stoch",
              arguments=list(HLC=quote(HLC(mktdata)), nFastK=nFastK,
nSlowD=nSlowD),
              label="stoch")

add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)), n=nSMA),
              label="sma")
###############################################################################
#signals
add.signal(strategy.st, name="sigComparison",
           arguments=list(columns=c("Close", "sma"), relationship="gt"),
           label="filter")

add.signal(strategy.st, name="sigThreshold",
           arguments=list(column="stoch", threshold=buyThresh,
                          relationship="lt", cross=FALSE),
           label="stochLtThresh")

add.signal(strategy.st, name="sigAND",
           arguments=list(columns=c("filter", "stochLtThresh"), cross=TRUE),
           label="longEntry")
add.signal(strategy.st, name="sigThreshold",
           arguments=list(column="stoch", threshold=sellThresh,
                          relationship="gt", cross=TRUE),
           label="longExit")
###############################################################################
#rules

#Long entry rule
add.rule(strategy.st, name="ruleSignal",
         arguments=list(sigcol="longEntry",
                        sigval=TRUE,
                        replace=FALSE,
                        orderside="long",
                        ordertype="market",
                        orderqty=100,
                        osFUN="osFixedDollar",
                        orderset="ocolong"),
         type="enter",label="LE")

#Long exit rule
add.rule(strategy.st, name="ruleSignal",
         arguments=list(sigcol="longExit",
                        sigval=TRUE,
                        replace=FALSE,
                        orderside="long",
                        ordertype="market",
                        orderqty="all",
                        orderset="ocolong"),
         type="exit",label="LX")

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list