Quantcast
Channel: Data Science, Analytics and Big Data discussions - Latest topics
Viewing all articles
Browse latest Browse all 4448

How to apply a code for another 100 stocks ?

$
0
0

@j_s wrote:

i have got a code for one stock and would like to apply the code for another stock.
How to apply a code for another stock ?
Below is the code for 1 stock.

 #> A code chunk 
library(tseries)
IBM<-tail(get.hist.quote("IBM"),1000)
class(IBM)
#> convert to dataframe.
IBM <- data.frame(IBM)
library(tidyverse)
which_max_in_window = function(x, size){
out = vector(mode = "numeric", length = length(x))
win = seq(1, size)
out[win] = which.max(x[win])
for( i in seq(size + 1, length(x)) ){
first = i - size + 1
last = i
win = seq(first, last)
out[i] = which.max(x[win]) + first - 1
}
return(out)
}
IBM = IBM %>% mutate(Serial = seq(1, nrow(.)),
Max_Val_Serial_No = which_max_in_window(x = Close, size = 100))

IBM_max = tibble(x = IBM %>% select(Max_Val_Serial_No) %>% distinct %>% pull,
y = IBM %>% select(Close) %>% slice(x) %>% pull)

which_min_in_window = function(x, size){
out = vector(mode = "numeric", length = length(x))
win = seq(1, size)
out[win] = which.min(x[win])
for( i in seq(size + 1, length(x)) ){
first = i - size + 1
last = i
win = seq(first, last)
out[i] = which.min(x[win]) + first - 1
}
return(out)
}
IBM = IBM %>% mutate(Serial = seq(1, nrow(.)),
Min_Val_Serial_No = which_min_in_window(x = Close, size = 100))
IBM_min = tibble(x = IBM %>% select(Min_Val_Serial_No) %>% distinct %>% pull,
y = IBM %>% select(Close) %>% slice(x) %>% pull)

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4448

Trending Articles