An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110906/1fbff5b5/attachment.pl>
Parameters setting for multiple indicators with the same argument names
4 messages · Brian G. Peterson, Igor Chernukhin
On Tue, 2011-09-06 at 15:40 +0200, igor_vilcek_external at tatrabanka.sk wrote:
Hi, I've encountered a problem in external parameters setting, in case I want to set parameters for more indicators which posses same argument names (e.g. setting length of multiple different Moving Averages - same name parameter 'n'). I can demonstrate this issue using quantstrat demo maCross (strategy uses "short" and "long" moving average, and my aim is to set length of both averages through 'parameters' argument in applyStrategy):
<...>
Using this input, strategy uses only "50day" Moving Average, for both MA50 and MA200 in 'mktdata'. Is there any way how to implement length of both different moving averages (or other indicators e.g. ROC, BBands,... with same arguments) into 'parameters'?
You have two options, a simple one, and a more complex one. The simple one is for you to generate a brute force set of parameters and loop over them, setting them as variables in your strategy using quote(). see ?expand.grid Or, more flexible but also more complicated, see the two parameterTest demos in the quantstrat package. You can tell the parameter subsystem which slots (indicators) you are applying parameters to, and apply a different distribution to each. Regards, - Brian
Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
2 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110909/7553073f/attachment.pl>
On Fri, 2011-09-09 at 09:35 +0200, igor_vilcek_external at tatrabanka.sk wrote:
My problem is, however, kinda different. E.g. in my example above, I need to modify (as simply as possible) length of both moving averages, externally, best through 'parameters' argument: applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st, parameters=list(n=50, n=200)) The problem is, I can't set n=50 for first MA and n=200 for second MA, because both MA indicators use same name argument 'n'. Thus, setting parameters=list(n=50, n=200) causes the system to use only first 'n=50' ignoring second 'n=200'. Is there any simple way how to set parameters like 'n1=50, n2=200'? I.e. to differentiate between these two with same names?
I wrote:
You have two options, a simple one, and a more complex one.
The answer won't change because you ask the question again. I understand your question exactly, two variables named 'n'. got it. The answer below is still exactly correct. I'll add a bit more detail:
The simple one is for you to generate a brute force set of parameters and loop over them, setting them as variables in your strategy using quote(). see ?expand.grid
Original: stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" ) stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200") New: stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=quote(shortMA)),label= "ma50" ) stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=quote(longMA)),label= "ma200") I'll leave as an exercise for the reader how to use expand.grid and loop over applyStrategy with two different values for shortMA and longMA.
Or, more flexible but also more complicated, see the two parameterTest demos in the quantstrat package. You can tell the parameter subsystem which slots (indicators) you are applying parameters to, and apply a different distribution to each.
The second option remains the more flexible and harder (for you) to code method.
Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock