An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120724/9bab85d5/attachment.pl>
quantstrat questions
3 messages · Bos, Roger, R. Michael Weylandt, Sven Duve
Quantstrat is under active development so you should probably ask on the R-SIG-Finance list where the developers hang out. Michael
On Tue, Jul 24, 2012 at 3:17 PM, Bos, Roger <roger.bos at rothschild.com> wrote:
Quantstrat useRs,
I have a number of questions about how to use quantstrat that I have accumulated since I have begun playing with it. First, can the orderqty be dynamic? All of the examples I have seen are based on placing an order for 100 shares when a rule is triggered. Is it possible to set it up to buy the maximum number of shares given the starting or current equity? Similar to that question, I think most examples buy 100 shares each time a buy is triggered? Is it possible to make the rules only buy at the first buy trigger? So the fund is either long 100% equity or in cash for a fixed starting equity.
Another idea I would like to try to implement is a slow trading strategy where you have to keep a position for at least x days after a buy. For example some small cap or international funds have a penalty for trading in and out too quickly. It would be nice to test how much such a restriction hurts you?
My final question is how to create a rule that uses more than one signal. Here I have an code sample below. In the sample I add three moving averages, ma50, ma160, and ma200:
#Adding indicators to a strategy
stratName <- add.indicator(strategy = stratName, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
stratName <- add.indicator(strategy = stratName, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=160),label= "ma160")
stratName <- add.indicator(strategy = stratName, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=200),label= "ma200")
#Adding signals to a strategy
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(columns=c("ma50","ma160"), relationship="gte"),label="ma50.gt.ma160")
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(column=c("ma50","ma160"),relationship="lt"),label="ma50.lt.ma160")
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(columns=c("ma50","ma200"), relationship="gte"),label="ma50.gt.ma200")
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")
How would I modify the rules below to enter when ma50 greater than both ma160 and ma200 and exit when ma50 is below both ma160 and ma200?
#Add rules to a strategy
stratName <- add.rule(strategy = stratName,name='ruleSignal', arguments =
list(sigcol="maFast.gt.maSlow",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter')
stratName <- add.rule(strategy = stratName,name='ruleSignal', arguments =
list(sigcol="maFast.lt.maSlow",sigval=TRUE, orderqty='all', ordertype='market', orderside='long'),type='exit')
Thanks to everyone who helped develop quantstrat and blotter,
Roger
***************************************************************
This message is for the named person's use only. It may\...{{dropped:15}}
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
You need to code a function which returns either a 1 for long and a 0 for flat based on any conditions like, so something like
MASig <- function(MA1, MA2, MA3){
k <- 0
if((MA1 > MA2) && (MA2 > MA3)){k <- 1}
return(k)
}
you then use this in your signalfunction in quantstrat.
dont nail me for the syntax, but something like this is what you looking for. i think...
But pls check with R-SIG finance again.
Am 24 Jul 2012 um 21:22 schrieb "R. Michael Weylandt" <michael.weylandt at gmail.com>:
Quantstrat is under active development so you should probably ask on the R-SIG-Finance list where the developers hang out. Michael On Tue, Jul 24, 2012 at 3:17 PM, Bos, Roger <roger.bos at rothschild.com> wrote:
Quantstrat useRs,
I have a number of questions about how to use quantstrat that I have accumulated since I have begun playing with it. First, can the orderqty be dynamic? All of the examples I have seen are based on placing an order for 100 shares when a rule is triggered. Is it possible to set it up to buy the maximum number of shares given the starting or current equity? Similar to that question, I think most examples buy 100 shares each time a buy is triggered? Is it possible to make the rules only buy at the first buy trigger? So the fund is either long 100% equity or in cash for a fixed starting equity.
Another idea I would like to try to implement is a slow trading strategy where you have to keep a position for at least x days after a buy. For example some small cap or international funds have a penalty for trading in and out too quickly. It would be nice to test how much such a restriction hurts you?
My final question is how to create a rule that uses more than one signal. Here I have an code sample below. In the sample I add three moving averages, ma50, ma160, and ma200:
#Adding indicators to a strategy
stratName <- add.indicator(strategy = stratName, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=50),label= "ma50" )
stratName <- add.indicator(strategy = stratName, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=160),label= "ma160")
stratName <- add.indicator(strategy = stratName, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), n=200),label= "ma200")
#Adding signals to a strategy
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(columns=c("ma50","ma160"), relationship="gte"),label="ma50.gt.ma160")
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(column=c("ma50","ma160"),relationship="lt"),label="ma50.lt.ma160")
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(columns=c("ma50","ma200"), relationship="gte"),label="ma50.gt.ma200")
stratName <- add.signal(strategy = stratName,name="sigCrossover",arguments =
list(column=c("ma50","ma200"),relationship="lt"),label="ma50.lt.ma200")
How would I modify the rules below to enter when ma50 greater than both ma160 and ma200 and exit when ma50 is below both ma160 and ma200?
#Add rules to a strategy
stratName <- add.rule(strategy = stratName,name='ruleSignal', arguments =
list(sigcol="maFast.gt.maSlow",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter')
stratName <- add.rule(strategy = stratName,name='ruleSignal', arguments =
list(sigcol="maFast.lt.maSlow",sigval=TRUE, orderqty='all', ordertype='market', orderside='long'),type='exit')
Thanks to everyone who helped develop quantstrat and blotter,
Roger
***************************************************************
This message is for the named person's use only. It may\...{{dropped:15}}
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.