[R-SIG-Finance] Return.portfolio() in PerformanceAnalytics and discrete returns
Thanks Joshua, in the meantime, I have taken a closer look at function Return.portfolio() in package PerformanceAnalytics and something seems quite odd. I hope I am not making a basic mistake here in my thinking, but it seems that this function does not handle discrete returns properly. Things start out fine with a weighted wealth index being constructed by geometrically linking discrete period returns: wealthindex.assets=cumprod(1+R[,colnames(weights)]) wealthindex.weighted = matrix(nrow=nrow(R),ncol=ncol(R[,colnames(weights)])) colnames(wealthindex.weighted)=colnames(wealthindex.assets) rownames(wealthindex.weighted)=as.character(index(wealthindex.assets)) # weight the results for (col in colnames(weights)){ wealthindex.weighted[,col]=weights[,col]*wealthindex.assets[,col] } wealthindex=apply(wealthindex.weighted,1,sum) But then after line 181 the function stops in its tracks, abandons its results so far except for the unweighted individual gross cumulative returns and instead of doing something along the lines of the following to decompose wealthindex into discrete portfolio period returns result - as-matrix(c(wealthindex[1]-1, wealthindex[2]/wealthindex[1]-1, wealthindex[3]/wealthindex[2]-1, ... ), ncol = 1) the returns are treated as if they were additive log returns # weighted cumulative returns weightedcumcont=t(apply (wealthindex.assets,1, function(x,weights){ as.vector((x-1)* weights)},weights=weights)) weightedreturns=diff(rbind(0,weightedcumcont)) # compound returns colnames(weightedreturns)=colnames(wealthindex.assets) if (!wealth.index){ result=as.matrix(apply(weightedreturns,1,sum),ncol=1) } else { wealthindex=matrix(cumprod(1 + as.matrix(apply(weightedreturns,1, sum), ncol = 1)),ncol=1) } - Original Message - From: Joshua Ulrich Sent: 06/20/12 09:52 PM To: susa...@mail.com Subject: Re: [R-SIG-Finance] issue with xts affectin Return.calculate in PerformanceAnalytics On Tue, Jun 19, 2012 at 1:43 PM, susa...@mail.com wrote: I came across the issue below trying to calculate simple returns of an xts object using Return.calculate() in PerformanceAnalytics. This returns a series of all zeros. The root of the problem, however, seems to be with xts for which division seems to be a challenge. What am I missing? Coercion to numeric is not really a reasonable solution. Return.calculate was fixed on R-Forge on April 4, 2012. Please see this SO question about installing from R-Forge: http://stackoverflow.com/q/11105131/271616 Division is only a challenge when you try to divide two objects that do not have any index values in common. Use lag() to align the index instead. p - to.monthly(as.xts(s.ts), OHLC = FALSE) head(aap) s1 s2 Dec 1999 1000.00 1000.00 Jan 2000 1021.27 959.85 Feb 2000 1017.30 962.06 Mar 2000 1022.99 1008.44 Apr 2000 1015.95 980.95 May 2000 1044.30 963.79 r - Return.calculate(aap, method = simple) head(r) s1 s2 Dec 1999 0 0 Jan 2000 0 0 Feb 2000 0 0 Mar 2000 0 0 Apr 2000 0 0 May 2000 0 0 lr - Return.calculate(aap, method = compound) head(lr) s1 s2 Dec 1999 NA NA Jan 2000 0.02104695084364 -0.04097825672856 Feb 2000 -0.00389489202608 0.00229979652957 Mar 2000 0.00557765293308 0.04708304254280 Apr 2000 -0.00690557640376 -0.02763837145911 May 2000 0.02752266915648 -0.01764806131103 p[2]/p[1]-1 Data: numeric(0) Index: NULL as.numeric(p[2])/as.numeric(p[1])-1 [1] 0.02127 -0.04015 Best, -- Joshua Ulrich | FOSS Trading: www.fosstrading.com ___ R-SIG-Finance@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.
Re: [R-SIG-Finance] PortfolioAnalytics
Hi I used SVN with Cygwin to checkout the project but host name could not be reached. Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 13:59, G See gsee...@gmail.com wrote: Precisely what do you mean by the SVN is not working? When you read the thread that Julien provided, did you follow this link that I provided in that thread? http://stackoverflow.com/questions/11105131/cannot-install-r-forge-package-using-install-packages R-Forge does not build packages for old versions of R. You have to build it yourself. If it really is an SVN problem, then R-sig-finance is not the best place to ask. HTH, Garrett On Thu, Jun 21, 2012 at 7:21 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: The SVN is not working on my side. Do you think I could find any Orr package zip for R 2.12? I am not very familiar with the building process . Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 12:04, julien cuisinier j_cuisin...@hotmail.com wrote: Please do some research before posting: http://r.789695.n4.nabble.com/Where-to-obtain-version-of-PortfolioAnalytics-package-installable-on-R-2-15-td4633724.html Same comment would be valid for Juan's post yesterday asking about the RExcel, half a minute googling would have told him how to extract a tar.gz file, even on Windows seems there have been some issues with the build as Garret mentioned, you might have to build it yourself. If you do please share with the list Rgds, Julien From: p.desma...@gmail.com Date: Thu, 21 Jun 2012 11:42:09 +0100 To: r-sig-finance@r-project.org Subject: [R-SIG-Finance] PortfolioAnalytics Hi I was looking for PortfolioAnalytics package zip but could not find it on the web. Can you please help on that Thank you ___ R-SIG-Finance@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. [[alternative HTML version deleted]] ___ R-SIG-Finance@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. ___ R-SIG-Finance@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.
Re: [R-SIG-Finance] PortfolioAnalytics
I suggest that when you take this up on a Cygwin SVN list you show *exactly* what commands you entered, and *exactly* what errors you saw. Or, you could just use TortoiseSVN. On Thu, Jun 21, 2012 at 8:19 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: Hi I used SVN with Cygwin to checkout the project but host name could not be reached. Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 13:59, G See gsee...@gmail.com wrote: Precisely what do you mean by the SVN is not working? When you read the thread that Julien provided, did you follow this link that I provided in that thread? http://stackoverflow.com/questions/11105131/cannot-install-r-forge-package-using-install-packages R-Forge does not build packages for old versions of R. You have to build it yourself. If it really is an SVN problem, then R-sig-finance is not the best place to ask. HTH, Garrett On Thu, Jun 21, 2012 at 7:21 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: The SVN is not working on my side. Do you think I could find any Orr package zip for R 2.12? I am not very familiar with the building process . Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 12:04, julien cuisinier j_cuisin...@hotmail.com wrote: Please do some research before posting: http://r.789695.n4.nabble.com/Where-to-obtain-version-of-PortfolioAnalytics-package-installable-on-R-2-15-td4633724.html Same comment would be valid for Juan's post yesterday asking about the RExcel, half a minute googling would have told him how to extract a tar.gz file, even on Windows seems there have been some issues with the build as Garret mentioned, you might have to build it yourself. If you do please share with the list Rgds, Julien From: p.desma...@gmail.com Date: Thu, 21 Jun 2012 11:42:09 +0100 To: r-sig-finance@r-project.org Subject: [R-SIG-Finance] PortfolioAnalytics Hi I was looking for PortfolioAnalytics package zip but could not find it on the web. Can you please help on that Thank you ___ R-SIG-Finance@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. [[alternative HTML version deleted]] ___ R-SIG-Finance@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. ___ R-SIG-Finance@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.
[R-SIG-Finance] expanding window forecasting by rugarch nd rmgarch
Dear alexios and other R users, I have the returns data on a firm named ret having 2767 observations total. I want the one day ahead rollingt forecasts starting from 1001th day and model is to be refit every 5 days. So my first 5 forecasts (i.e., for the 1001,1002,1003,1004 nd 1005th day) will be based on first 1000 observations, next 5 forecasts(i.e., for the 1006 to 1010'th day) will be based on first 1005 observations and then next 5 days forecast will be based on first 1010 observations and this process continues. I used following code spec= ugarchspec(variance.model= list(model = 'gjrGARCH') ,mean.model=list(armaOrder= c(0,0),include.mean= FALSE, archpow=2)) forecast-ugarchroll(spec=spec,data=mkt_ret,forecast.length=1767,refit.every=5, refit.window=recursive) mkt_fcst- as.data.frame(forecast) My questions are 1) I suppose the f_sigma in mkt_fcst dataframe is forecasted volatality but is this the true forecast which I need according to above description of my needed forecast 2) If this f_sigma is the right forecasted vol then why i got only the forecasts till 2765 days because next two days forecasts could be based on the data till 2765 observations. 3) Please suggest me how to formulate the right code for my needed forecast 4) I will do assymetric DCC also using second series on market index (mkt_ret) nd would need the DCC forecasts too in the same manner using rmgarch. -- View this message in context: http://r.789695.n4.nabble.com/expanding-window-forecasting-by-rugarch-nd-rmgarch-tp4634118.html Sent from the Rmetrics mailing list archive at Nabble.com. [[alternative HTML version deleted]] ___ R-SIG-Finance@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.
[R-SIG-Finance] expanding window forecasting by rugarch nd rmgarch
Dear alexios and other R users, I have the returns data on a firm named ret having 2767 observations total. I want the one day ahead rollingt forecasts starting from 1001th day and model is to be refit every 5 days. So my first 5 forecasts (i.e., for the 1001,1002,1003,1004 nd 1005th day) will be based on first 1000 observations, next 5 forecasts(i.e., for the 1006 to 1010'th day) will be based on first 1005 observations and then next 5 days forecast will be based on first 1010 observations and this process continues. I used following code spec= ugarchspec(variance.model= list(model = 'gjrGARCH') ,mean.model=list(armaOrder= c(0,0),include.mean= FALSE, archpow=2)) forecast-ugarchroll(spec=spec,data=mkt_ret,forecast.length=1767,refit.every=5, refit.window=recursive) mkt_fcst- as.data.frame(forecast) My questions are 1) I suppose the f_sigma in mkt_fcst dataframe is forecasted volatality but is this the true forecast which I need according to above description of my needed forecast 2) If this f_sigma is the right forecasted vol then why i got only the forecasts till 2765 days because next two days forecasts could be based on the data till 2765 observations. 3) Please suggest me how to formulate the right code for my needed forecast 4) I will do assymetric DCC also using second series on market index (mkt_ret) nd would need the DCC forecasts too in the same manner using rmgarch. -- View this message in context: http://r.789695.n4.nabble.com/expanding-window-forecasting-by-rugarch-nd-rmgarch-tp4634119.html Sent from the Rmetrics mailing list archive at Nabble.com. ___ R-SIG-Finance@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.
Re: [R-SIG-Finance] PortfolioAnalytics
G, I was just wondering if there is any alternative solution to the r-forge website. I must admit I am not too sure about how to build a package. I will code my own script to do the job. Thanks anyway Regards, Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 15:13, G See gsee...@gmail.com wrote: I suggest that when you take this up on a Cygwin SVN list you show *exactly* what commands you entered, and *exactly* what errors you saw. Or, you could just use TortoiseSVN. On Thu, Jun 21, 2012 at 8:19 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: Hi I used SVN with Cygwin to checkout the project but host name could not be reached. Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 13:59, G See gsee...@gmail.com wrote: Precisely what do you mean by the SVN is not working? When you read the thread that Julien provided, did you follow this link that I provided in that thread? http://stackoverflow.com/questions/11105131/cannot-install-r-forge-package-using-install-packages R-Forge does not build packages for old versions of R. You have to build it yourself. If it really is an SVN problem, then R-sig-finance is not the best place to ask. HTH, Garrett On Thu, Jun 21, 2012 at 7:21 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: The SVN is not working on my side. Do you think I could find any Orr package zip for R 2.12? I am not very familiar with the building process . Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 12:04, julien cuisinier j_cuisin...@hotmail.com wrote: Please do some research before posting: http://r.789695.n4.nabble.com/Where-to-obtain-version-of-PortfolioAnalytics-package-installable-on-R-2-15-td4633724.html Same comment would be valid for Juan's post yesterday asking about the RExcel, half a minute googling would have told him how to extract a tar.gz file, even on Windows seems there have been some issues with the build as Garret mentioned, you might have to build it yourself. If you do please share with the list Rgds, Julien From: p.desma...@gmail.com Date: Thu, 21 Jun 2012 11:42:09 +0100 To: r-sig-finance@r-project.org Subject: [R-SIG-Finance] PortfolioAnalytics Hi I was looking for PortfolioAnalytics package zip but could not find it on the web. Can you please help on that Thank you ___ R-SIG-Finance@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. [[alternative HTML version deleted]] ___ R-SIG-Finance@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. ___ R-SIG-Finance@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.
Re: [R-SIG-Finance] PortfolioAnalytics
Thank you very much G see I will dig into the subject once home and hopefully will be able to find a solution. regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 17:50, G See gsee...@gmail.com wrote: The alternative to the r-forge *website* is SVN. If you're going to try to source files instead of building the package, you're on your own. I guess you could browse through the repository here, https://r-forge.r-project.org/scm/viewvc.php/pkg/PortfolioAnalytics/?root=returnanalytics, and click the download link for files you're interested in. For example, suppose you want the charts.DE.R file. You can browse to it, and click the download link which will bring you here http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/PortfolioAnalytics/R/charts.DE.R?root=returnanalytics You can source that directly source(http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/PortfolioAnalytics/R/charts.DE.R?root=returnanalytics;) (You may have to replace https with http in the link) Gd luck with that, Garrett On Thu, Jun 21, 2012 at 11:25 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: G, I was just wondering if there is any alternative solution to the r-forge website. I must admit I am not too sure about how to build a package. I will code my own script to do the job. Thanks anyway Regards, Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 15:13, G See gsee...@gmail.com wrote: I suggest that when you take this up on a Cygwin SVN list you show *exactly* what commands you entered, and *exactly* what errors you saw. Or, you could just use TortoiseSVN. On Thu, Jun 21, 2012 at 8:19 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: Hi I used SVN with Cygwin to checkout the project but host name could not be reached. Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 13:59, G See gsee...@gmail.com wrote: Precisely what do you mean by the SVN is not working? When you read the thread that Julien provided, did you follow this link that I provided in that thread? http://stackoverflow.com/questions/11105131/cannot-install-r-forge-package-using-install-packages R-Forge does not build packages for old versions of R. You have to build it yourself. If it really is an SVN problem, then R-sig-finance is not the best place to ask. HTH, Garrett On Thu, Jun 21, 2012 at 7:21 AM, Pierre-Alexandr des Mazis p.desma...@gmail.com wrote: The SVN is not working on my side. Do you think I could find any Orr package zip for R 2.12? I am not very familiar with the building process . Regards Pierre-Alexandre des Mazis + 447 779 171 499 On 21 Jun 2012, at 12:04, julien cuisinier j_cuisin...@hotmail.com wrote: Please do some research before posting: http://r.789695.n4.nabble.com/Where-to-obtain-version-of-PortfolioAnalytics-package-installable-on-R-2-15-td4633724.html Same comment would be valid for Juan's post yesterday asking about the RExcel, half a minute googling would have told him how to extract a tar.gz file, even on Windows seems there have been some issues with the build as Garret mentioned, you might have to build it yourself. If you do please share with the list Rgds, Julien From: p.desma...@gmail.com Date: Thu, 21 Jun 2012 11:42:09 +0100 To: r-sig-finance@r-project.org Subject: [R-SIG-Finance] PortfolioAnalytics Hi I was looking for PortfolioAnalytics package zip but could not find it on the web. Can you please help on that Thank you ___ R-SIG-Finance@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. [[alternative HTML version deleted]] ___ R-SIG-Finance@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. ___ R-SIG-Finance@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.
Re: [R-SIG-Finance] expanding window forecasting by rugarch nd rmgarch
Please do not post more than once (this is your 3rd duplicate post in 24 hours). Your original email was received this morning and this is an open source software mailing list...someone MIGHT reply when they have the time and inclination. If you read the documentation on ugarchrolll (the 'Details' section) it explains how the number of periods to forecast are calculated (i.e. since 2767 is not divisible by 5 it takes the nearest number divisible by 5 less than 2767). If you search previous posts to the mailing list you will also find hints as to how to use the function and that it only produces forecast to the extend of available out of sample data. For more complex setups use ugarchfit+ugarchforecast based on your own loop/rolling scheme. -Alexios PS Do have the courtesy of signing your emails with your name. On 21/06/2012 17:11, naval wrote: Dear alexios and other R users, I have the returns data on a firm named ret having 2767 observations total. I want the one day ahead rollingt forecasts starting from 1001th day and model is to be refit every 5 days. So my first 5 forecasts (i.e., for the 1001,1002,1003,1004 nd 1005th day) will be based on first 1000 observations, next 5 forecasts(i.e., for the 1006 to 1010'th day) will be based on first 1005 observations and then next 5 days forecast will be based on first 1010 observations and this process continues. I used following code spec= ugarchspec(variance.model= list(model = 'gjrGARCH') ,mean.model=list(armaOrder= c(0,0),include.mean= FALSE, archpow=2)) forecast-ugarchroll(spec=spec,data=mkt_ret,forecast.length=1767,refit.every=5, refit.window=recursive) mkt_fcst- as.data.frame(forecast) My questions are 1) I suppose the f_sigma in mkt_fcst dataframe is forecasted volatality but is this the true forecast which I need according to above description of my needed forecast 2) If this f_sigma is the right forecasted vol then why i got only the forecasts till 2765 days because next two days forecasts could be based on the data till 2765 observations. 3) Please suggest me how to formulate the right code for my needed forecast 4) I will do assymetric DCC also using second series on market index (mkt_ret) nd would need the DCC forecasts too in the same manner using rmgarch. -- View this message in context: http://r.789695.n4.nabble.com/expanding-window-forecasting-by-rugarch-nd-rmgarch-tp4634119.html Sent from the Rmetrics mailing list archive at Nabble.com. ___ R-SIG-Finance@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. ___ R-SIG-Finance@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.
[R-SIG-Finance] a problem about using quantstrat: add.Indicators and applySignals in BBband
Dear R experts: I am a newcomer and learning R with quantstrat for strategy testing such as Bollinger band. (Most codes are from the sample of the demo in the package.) I encountered a problem here and hope you can help me out from the error below. My process is 1. Reading a data file, instead of using data from yahoo ##my own code tmp - read.table(AAPL.dat, sep = ,) #dat file is in below f - function(x) as.POSIXct(x, origin =1970-01-01) symbolData - read.zoo(tmp, index = 1, FUN = f) colnames(symbolData) - c(Open,High,Low,Close,Volume, Adjust,V8,V9) #here is one line of the symbolData symbolData Open HighLow Close Volume Adjust V8 V9 2011-06-16 14:45:00 326.57 326.80 326.56 326.65353 326.688 0 249 2. Pass it to add.Indicators, and applySignals dataSrc - list(HLC = quote(HLC(symbolData)), maType='SMA') stratBBands - add.indicator(strategy = stratBBands, name = BBands, arguments = dataSrc) #use my file instead of mktdata . ### modified by my own code out-try(applyStrategy(strategy=stratBBands , portfolios=port.st, mktData = symbolData, parameters=list(sd=SD,n=N) ) ) The problem is: out-try(applyStrategy(strategy=stratBBands , portfolios=port.st, mktData = symbolData, parameters=list(sd=SD,n=N) ) ) Error in if (inherits(sret$indicators, xts) nrow(mktdata) == nrow(sret$indicators)) { : argument is of length zero Another basic questions, how to set a breakpoint and trace line by line? Any new tool? Thanks Richard Main.R ###lib needed## library(quantstrat) library(quantmod) library(chron) symbols = c(AAPL)#,AAPL, FB timeFrame - 15 ###my own code tmp - read.table(AAPL.dat, sep = ,) f - function(x) as.POSIXct(x, origin =1970-01-01) symbolData - read.zoo(tmp, index = 1, FUN = f) colnames(symbolData) - c(Open,High,Low,Close,Volume, Adjust,V8,V9) #barChart(symbolData) initDate='2010-05-28' initEq=10 port.st-'backTest11' #use a string here for easier changing of parameters and re-trying # bollinger bands ### SD = 2 # how many standard deviations, traditionally 2 N = 20 # how many periods for the moving average, traditionally 20 stratBBands - strategy(bbands) #one indicator ###my own code dataSrc - list(HLC = quote(HLC(symbolData)), maType='SMA') stratBBands - add.indicator(strategy = stratBBands, name = BBands, arguments = dataSrc) #use my file instead of mktdata #add signals: stratBBands - add.signal(stratBBands,name=sigCrossover,arguments = list(columns=c(Close,up),relationship=gt),label=Cl.gt.UpperBand) stratBBands - add.signal(stratBBands,name=sigCrossover,arguments = list(columns=c(Close,dn),relationship=lt),label=Cl.lt.LowerBand) stratBBands - add.signal(stratBBands,name=sigCrossover,arguments = list(columns=c(High,Low,mavg),relationship=op),label=Cross.Mid) # lets add some rules stratBBands - add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol=Cl.gt.UpperBand,sigval=TRUE, orderqty=-0, ordertype='market', orderside=NULL, threshold=NULL),type='enter') stratBBands - add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol=Cl.lt.LowerBand,sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL),type='enter') stratBBands - add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol=Cross.Mid,sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit') end of bollinger bands ## ##initialize## currency(USD) #symbols = c(BIDU) for(symbol in symbols){ # establish trade-able instruments stock(symbol, currency=USD,multiplier=1) #getSymbols(symbol, from=initDate) #not using server data, use my own } initPortf(port.st, symbols=symbols, initDate=initDate) initAcct(port.st, portfolios=port.st, initDate=initDate) initOrders(portfolio=port.st, initDate=initDate) print(initialize done) Process the indicators and generate trades start_t-Sys.time() ### modified by my own code out-try(applyStrategy(strategy=stratBBands , portfolios=port.st, mktData = symbolData, parameters=list(sd=SD,n=N) ) ) end_t-Sys.time() print(Strategy Loop:) print(end_t-start_t) # look at the order book #print(getOrderBook(port.st)) start_t-Sys.time() updatePortf(Portfolio=port.st,Dates=paste('::',as.Date(Sys.time()),sep='')) end_t-Sys.time() print(trade blotter portfolio update:) print(end_t-start_t) # hack for new quantmod graphics, remove later themelist-chart_theme() themelist$col$up.col-'lightgreen' themelist$col$dn.col-'red' for(symbol in symbols){ dev.new() chart.Posn(Portfolio=port.st,Symbol=symbol,theme=themelist)