1

I need my function to work on some period on the stock, in the example, AKER["2013-11-19/2018-11-19"], from oct of 2013 to oct of 2018. And then work again BUT this time with one year closer to the date that I set, like this AKER["2014-11-19/2018-11-19"]. And then again. And again.

That's what I got:

resistence_line_by_volume <- function(x) {
open_prices <- x[,1]
close_prices <- x[,4]
volume_amount <- x[,5]
average_open_and_close <- (open_prices + close_prices)/2
weighet_price_volume <- (average_open_and_close*volume_amount)/sum(volume_amount)
result <- sum(weighet_price_volume)
result
}

getSymbols("AKER")
[1] "AKER"

 resistence_line_by_volume(AKER["2013-11-19/2018-11-19")
[1] 3.353938

resistence_line_by_volume(AKER["2014-11-19/2018-11-19")
[1] 3.319899

resistence_line_by_volume(AKER["2015-11-19/2018-11-19")
[1] 3.290728

resistence_line_by_volume(AKER["2016-11-19/2018-11-19")
[1] 3.256264

resistence_line_by_volume(AKER["2017-11-19/2018-11-19")
[1] 3.191081

And that's what I need (some version of that):

resistence_line_by_volume(AKER["2013-11-19/2018-11-19")
[1] 3.353938
[2] 3.319899
[3] 3.290728
[4] 3.256264
[5] 3.191081

How do I repete all this functions with one year closer every time?

Artur Dutra
  • 515
  • 3
  • 18
  • From where is `function_price_research` function from? Is it from a package or you have defined it? Please include any non-base packages in the post. – Ronak Shah May 02 '19 at 02:52
  • I defined but I don't see how this could be relevant. Thanks for the feedback anyway – Artur Dutra May 02 '19 at 02:58
  • My main doubt is about repeating functions with decreased time for each repetition – Artur Dutra May 02 '19 at 03:00
  • I posted my function but it made more convoluted – Artur Dutra May 02 '19 at 03:04
  • 1
    The reason why I am asking you this is to reproduce the same thing which you are facing. So if we are to provide you an answer, we can be sure that it works 100%. Without reproducible data all we can do is guess what the issue is and provide you some suggestions which may or may not work. For example, when I run your attempt `function_price_research(stocks$APPL["2013-10-30/2018-10-30"])` it gives me `Error in function_price_research(stocks$APPL["2013-10-30/2018-10-30"]) : object 'stocks' not found` Now I don't know what `stocks` is. – Ronak Shah May 02 '19 at 03:07
  • Also read : [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Ronak Shah May 02 '19 at 03:14
  • I see the problem now! I'm new here and I understand now your point. Just made the updates and thanks for the tips related on how to make questions – Artur Dutra May 02 '19 at 03:20

1 Answers1

1

If there are limited number of dates, we can manually create a vector of dates

library(quantmod)
dates <- c("2013-11-19/2018-11-19","2014-11-19/2018-11-19","2015-11-19/2018-11-19",
           "2016-11-19/2018-11-19", "2017-11-19/2018-11-19")

and then use any looping technique to loop over dates (sapply, lapply, map, for loop etc.)

sapply(dates, function(x) resistence_line_by_volume(AKER[x]), USE.NAMES = FALSE)
#[1] 3.327881 3.294591 3.266057 3.232329 3.168454

Or we can also generate the dates programatically using seq

dates <- paste(seq(as.Date("2013-11-19"), length.out = 5, by = "year"),
               "2018-11-19", sep = "/")

sapply(dates, function(x) resistence_line_by_volume(AKER[x]), USE.NAMES = FALSE)
#[1] 3.327881 3.294591 3.266057 3.232329 3.168454
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213