Signal and Rule question in Quantstrat
On Fri, Jan 23, 2015 at 8:49 AM, Isak Engdahl <isak.engdahl at gmail.com> wrote:
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!
Help with what? You haven't stated any problem.
//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]]
_______________________________________________ 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.
Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com