From 97826430b1937e4157c0c332f1b7748247044cb6 Mon Sep 17 00:00:00 2001 From: pierGit7 Date: Wed, 11 Jan 2023 00:38:56 +0100 Subject: [PATCH 1/6] Create EOD.R --- backend/script/EOD.R | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 backend/script/EOD.R diff --git a/backend/script/EOD.R b/backend/script/EOD.R new file mode 100644 index 00000000..f30f9242 --- /dev/null +++ b/backend/script/EOD.R @@ -0,0 +1,57 @@ +# This package is required for Accessing APIS (HTTP or HTTPS URLS from Web) +library(httr) +#This package exposes some additional functions to convert json/text to data frame +library(rlist) +#This package exposes some additional functions to convert json/text to data frame +library(jsonlite) +#This library is used to manipulate data +library(dplyr) +library(smooth) +library(ggplot2) +symbols= c('AAPL','ABBV','ABT','ACN','AGN','AIG','ALL','AMGN','AMZN','AXP','BA','BAC','BIIB','BK','BLK','BMY','C','CAT','CELG','CL','CMCSA','COF','COP','COST','CSCO','CVS','CVX','DD','DHR','DIS','DOW','DUK','EMC','EMR','EXC','F','FB','FDX','FOX','FOXA','GD','GE','GILD','GM','GOOG','GOOGL','GS','HAL','HD','HON','IBM','INTC','JNJ','JPM','KMI','KO','LLY','LMT','LOW','MA','MCD','MDLZ','MDT','MET','MMM','MO','MON','MRK','MS','MSFT','NEE','NKE','ORCL','OXY','PEP','PFE','PG','PM','PYPL','QCOM','RTN','SBUX','SLB','SO','SPG','T','TGT','TXN','UNH','UNP','UPS','USB','USD','UTX','V','VZ','WBA','WFC' + +) +dati_totali<-data.frame(1:14) +SMA_3<-data.frame(1:14) +SMA_7<-data.frame(1:14) +for(s in symbols){ + + r<- GET(paste('https://api.marketstack.com/v1/tickers/', s, '/eod', sep=''),add_headers(Name = 'access_key',Name='limit'),query=list(access_key='b7f513ecf8b91aa4dc345df69bf55a3d',limit=14)) + dati<-fromJSON(content(r,as="text")) + dati_totali[s]=rev(dati[["data"]][["eod"]][["adj_close"]]) + +SMA_3[s]<-sma(ts(dati_totali[s]),order = 3,silent = FALSE)$fitted +SMA_7[s]<-sma(ts(dati_totali[s]),order = 7,silent = FALSE)$fitted +} +list_buy_sell<-buy_sell(SMA_3,SMA_7,dati_totali) + + + +buy_sell <- function(SMA_Short, SMA_Long, dataset) { + d <- list() + + last <- 14 + for (s in symbols) { + if (SMA_Short[last,s] > SMA_Long[last,s]) { + if (SMA_Short[last-1,s] < SMA_Long[last-1,s]) { + # buy =1 + d[[s]]<- list(1,dataset[last,s]) + } else { + d[[s]] <- list(0, NA) + } + } else if (SMA_Short[last,s] < SMA_Long[last,s]) { + if (SMA_Short[last-1,s] > SMA_Long[last-1,s]) { + # sell =-1 + d[[s]] <- list(-1, dataset[last,s]) + } else { + d[[s]] <- list(0, NA) + } + } else { + d[[s]] <- list(0, NA) + } + } + return(d) +} + + + From ec920d024dd8cf6d907709a0b4621a4703c42b63 Mon Sep 17 00:00:00 2001 From: pierGit7 Date: Wed, 18 Jan 2023 11:15:47 +0100 Subject: [PATCH 2/6] Update EOD.R --- backend/script/EOD.R | 70 +++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/backend/script/EOD.R b/backend/script/EOD.R index f30f9242..d95bfc5e 100644 --- a/backend/script/EOD.R +++ b/backend/script/EOD.R @@ -1,3 +1,28 @@ +buy_sell <- function(SMA_Short, SMA_Long, dataset) { + d <- list() + last <- 14 + for (s in symbols) { + #checking if in the last three day the stock value increased compared to the + if (SMA_Short[last,s] > SMA_Long[last,s]) { + if (SMA_Short[last-1,s] < SMA_Long[last-1,s]) { + # buy =1 + d[[s]]<- list(1,dataset[last,s]) + } else { + d[[s]] <- list(0, NA) + } + } else if (SMA_Short[last,s] < SMA_Long[last,s]) { + if (SMA_Short[last-1,s] > SMA_Long[last-1,s]) { + # sell =-1 + d[[s]] <- list(-1, dataset[last,s]) + } else { + d[[s]] <- list(0, NA) + } + } else { + d[[s]] <- list(0, NA) + } + } + return(d) +} # This package is required for Accessing APIS (HTTP or HTTPS URLS from Web) library(httr) #This package exposes some additional functions to convert json/text to data frame @@ -6,52 +31,37 @@ library(rlist) library(jsonlite) #This library is used to manipulate data library(dplyr) +#this library is used to create SMA model library(smooth) -library(ggplot2) +#initializing stock label symbols= c('AAPL','ABBV','ABT','ACN','AGN','AIG','ALL','AMGN','AMZN','AXP','BA','BAC','BIIB','BK','BLK','BMY','C','CAT','CELG','CL','CMCSA','COF','COP','COST','CSCO','CVS','CVX','DD','DHR','DIS','DOW','DUK','EMC','EMR','EXC','F','FB','FDX','FOX','FOXA','GD','GE','GILD','GM','GOOG','GOOGL','GS','HAL','HD','HON','IBM','INTC','JNJ','JPM','KMI','KO','LLY','LMT','LOW','MA','MCD','MDLZ','MDT','MET','MMM','MO','MON','MRK','MS','MSFT','NEE','NKE','ORCL','OXY','PEP','PFE','PG','PM','PYPL','QCOM','RTN','SBUX','SLB','SO','SPG','T','TGT','TXN','UNH','UNP','UPS','USB','USD','UTX','V','VZ','WBA','WFC' ) + +#initializing three different data_frame dati_totali<-data.frame(1:14) SMA_3<-data.frame(1:14) SMA_7<-data.frame(1:14) + +#API request to get all the EOD data for each stock for(s in symbols){ + r<- GET(paste('https://api.marketstack.com/v1/tickers/', s, '/eod', sep=''),add_headers(Name = 'access_key',Name='limit'),query=list(access_key='b7f513ecf8b91aa4dc345df69bf55a3d',limit=14)) dati<-fromJSON(content(r,as="text")) dati_totali[s]=rev(dati[["data"]][["eod"]][["adj_close"]]) - -SMA_3[s]<-sma(ts(dati_totali[s]),order = 3,silent = FALSE)$fitted -SMA_7[s]<-sma(ts(dati_totali[s]),order = 7,silent = FALSE)$fitted + #sma model with two different time window for each stock + SMA_3[s]<-sma(ts(dati_totali[s]),order = 3,silent = FALSE)$fitted + SMA_7[s]<-sma(ts(dati_totali[s]),order = 7,silent = FALSE)$fitted } -list_buy_sell<-buy_sell(SMA_3,SMA_7,dati_totali) +#binding date information to each value +date<-rev(dati[["data"]][["eod"]][["date"]]) +SMA_3<-dplyr::bind_cols(date,SMA_3) +SMA_7<-dplyr::bind_cols(date,SMA_7) +list_buy_sell<-buy_sell(SMA_3,SMA_7,dati_totali) -buy_sell <- function(SMA_Short, SMA_Long, dataset) { - d <- list() - - last <- 14 - for (s in symbols) { - if (SMA_Short[last,s] > SMA_Long[last,s]) { - if (SMA_Short[last-1,s] < SMA_Long[last-1,s]) { - # buy =1 - d[[s]]<- list(1,dataset[last,s]) - } else { - d[[s]] <- list(0, NA) - } - } else if (SMA_Short[last,s] < SMA_Long[last,s]) { - if (SMA_Short[last-1,s] > SMA_Long[last-1,s]) { - # sell =-1 - d[[s]] <- list(-1, dataset[last,s]) - } else { - d[[s]] <- list(0, NA) - } - } else { - d[[s]] <- list(0, NA) - } - } - return(d) -} From 92c5d1566c1375bbd25a6919bccc01bb447d8f30 Mon Sep 17 00:00:00 2001 From: pierGit7 Date: Wed, 18 Jan 2023 11:29:17 +0100 Subject: [PATCH 3/6] Update EOD.R --- backend/script/EOD.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/script/EOD.R b/backend/script/EOD.R index d95bfc5e..f8a8c6f3 100644 --- a/backend/script/EOD.R +++ b/backend/script/EOD.R @@ -1,8 +1,9 @@ buy_sell <- function(SMA_Short, SMA_Long, dataset) { d <- list() last <- 14 + #if neither a positive nor negative trend is found, than the system does nothing= 0 for (s in symbols) { - #checking if in the last three day the stock value increased compared to the + #checking if there is a positive trend if (SMA_Short[last,s] > SMA_Long[last,s]) { if (SMA_Short[last-1,s] < SMA_Long[last-1,s]) { # buy =1 @@ -10,6 +11,7 @@ buy_sell <- function(SMA_Short, SMA_Long, dataset) { } else { d[[s]] <- list(0, NA) } + #checking if there is a negative trend } else if (SMA_Short[last,s] < SMA_Long[last,s]) { if (SMA_Short[last-1,s] > SMA_Long[last-1,s]) { # sell =-1 From a4225e460ce15a35544e78eeb2d89e76cf9efe6c Mon Sep 17 00:00:00 2001 From: pierGit7 Date: Wed, 18 Jan 2023 11:33:36 +0100 Subject: [PATCH 4/6] Update EOD.R --- backend/script/EOD.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/script/EOD.R b/backend/script/EOD.R index f8a8c6f3..11df43e1 100644 --- a/backend/script/EOD.R +++ b/backend/script/EOD.R @@ -35,10 +35,11 @@ library(jsonlite) library(dplyr) #this library is used to create SMA model library(smooth) + #initializing stock label symbols= c('AAPL','ABBV','ABT','ACN','AGN','AIG','ALL','AMGN','AMZN','AXP','BA','BAC','BIIB','BK','BLK','BMY','C','CAT','CELG','CL','CMCSA','COF','COP','COST','CSCO','CVS','CVX','DD','DHR','DIS','DOW','DUK','EMC','EMR','EXC','F','FB','FDX','FOX','FOXA','GD','GE','GILD','GM','GOOG','GOOGL','GS','HAL','HD','HON','IBM','INTC','JNJ','JPM','KMI','KO','LLY','LMT','LOW','MA','MCD','MDLZ','MDT','MET','MMM','MO','MON','MRK','MS','MSFT','NEE','NKE','ORCL','OXY','PEP','PFE','PG','PM','PYPL','QCOM','RTN','SBUX','SLB','SO','SPG','T','TGT','TXN','UNH','UNP','UPS','USB','USD','UTX','V','VZ','WBA','WFC' -) + ) #initializing three different data_frame dati_totali<-data.frame(1:14) @@ -50,17 +51,22 @@ for(s in symbols){ r<- GET(paste('https://api.marketstack.com/v1/tickers/', s, '/eod', sep=''),add_headers(Name = 'access_key',Name='limit'),query=list(access_key='b7f513ecf8b91aa4dc345df69bf55a3d',limit=14)) + + #convert JSON text in dataframe dati<-fromJSON(content(r,as="text")) dati_totali[s]=rev(dati[["data"]][["eod"]][["adj_close"]]) + #sma model with two different time window for each stock SMA_3[s]<-sma(ts(dati_totali[s]),order = 3,silent = FALSE)$fitted SMA_7[s]<-sma(ts(dati_totali[s]),order = 7,silent = FALSE)$fitted } + #binding date information to each value date<-rev(dati[["data"]][["eod"]][["date"]]) SMA_3<-dplyr::bind_cols(date,SMA_3) SMA_7<-dplyr::bind_cols(date,SMA_7) +#creating a list of buy_sel where each stock are checked list_buy_sell<-buy_sell(SMA_3,SMA_7,dati_totali) From 637469b5754d8a85af17213a063d426aab2fdcf0 Mon Sep 17 00:00:00 2001 From: pierGit7 Date: Sat, 4 Feb 2023 16:56:08 +0100 Subject: [PATCH 5/6] Update EOD.R --- backend/script/EOD.R | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/backend/script/EOD.R b/backend/script/EOD.R index 11df43e1..df1ae874 100644 --- a/backend/script/EOD.R +++ b/backend/script/EOD.R @@ -1,3 +1,16 @@ +# This package is required for Accessing APIS (HTTP or HTTPS URLS from Web) +library(httr) +#This package exposes some additional functions to convert json/text to data frame +library(rlist) +#This package exposes some additional functions to convert json/text to data frame +library(jsonlite) +#This library is used to manipulate data +library(dplyr) +#this library is used to create SMA model +library(smooth) + + + buy_sell <- function(SMA_Short, SMA_Long, dataset) { d <- list() last <- 14 @@ -25,16 +38,6 @@ buy_sell <- function(SMA_Short, SMA_Long, dataset) { } return(d) } -# This package is required for Accessing APIS (HTTP or HTTPS URLS from Web) -library(httr) -#This package exposes some additional functions to convert json/text to data frame -library(rlist) -#This package exposes some additional functions to convert json/text to data frame -library(jsonlite) -#This library is used to manipulate data -library(dplyr) -#this library is used to create SMA model -library(smooth) #initializing stock label symbols= c('AAPL','ABBV','ABT','ACN','AGN','AIG','ALL','AMGN','AMZN','AXP','BA','BAC','BIIB','BK','BLK','BMY','C','CAT','CELG','CL','CMCSA','COF','COP','COST','CSCO','CVS','CVX','DD','DHR','DIS','DOW','DUK','EMC','EMR','EXC','F','FB','FDX','FOX','FOXA','GD','GE','GILD','GM','GOOG','GOOGL','GS','HAL','HD','HON','IBM','INTC','JNJ','JPM','KMI','KO','LLY','LMT','LOW','MA','MCD','MDLZ','MDT','MET','MMM','MO','MON','MRK','MS','MSFT','NEE','NKE','ORCL','OXY','PEP','PFE','PG','PM','PYPL','QCOM','RTN','SBUX','SLB','SO','SPG','T','TGT','TXN','UNH','UNP','UPS','USB','USD','UTX','V','VZ','WBA','WFC' @@ -46,6 +49,9 @@ dati_totali<-data.frame(1:14) SMA_3<-data.frame(1:14) SMA_7<-data.frame(1:14) +#getting user's Stock +userStock<- GET('http://localhost/api/admin/stocks',add_headers(Authorization='Bearer TOKEN')) + #API request to get all the EOD data for each stock for(s in symbols){ @@ -57,6 +63,7 @@ for(s in symbols){ dati_totali[s]=rev(dati[["data"]][["eod"]][["adj_close"]]) #sma model with two different time window for each stock + #more information: "https://en.wikipedia.org/wiki/Moving_average" SMA_3[s]<-sma(ts(dati_totali[s]),order = 3,silent = FALSE)$fitted SMA_7[s]<-sma(ts(dati_totali[s]),order = 7,silent = FALSE)$fitted } From b4cf27dec74c72db054abc6f9d96612af52e2c59 Mon Sep 17 00:00:00 2001 From: pierGit7 Date: Wed, 8 Feb 2023 15:27:24 +0100 Subject: [PATCH 6/6] php call R --- backend/app/Http/Controllers/Stocks/StocksController.php | 7 +++++++ backend/script/EOD.R | 7 ++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/backend/app/Http/Controllers/Stocks/StocksController.php b/backend/app/Http/Controllers/Stocks/StocksController.php index 5aa5cdf6..fe4cb42d 100644 --- a/backend/app/Http/Controllers/Stocks/StocksController.php +++ b/backend/app/Http/Controllers/Stocks/StocksController.php @@ -15,4 +15,11 @@ public function __construct() { $this->middleware('auth:api'); } + + public function RscriptCall() + { + $reading = shell_exec('Rscript EOD.R $Token'); + } } + + diff --git a/backend/script/EOD.R b/backend/script/EOD.R index df1ae874..e258131c 100644 --- a/backend/script/EOD.R +++ b/backend/script/EOD.R @@ -50,7 +50,8 @@ SMA_3<-data.frame(1:14) SMA_7<-data.frame(1:14) #getting user's Stock -userStock<- GET('http://localhost/api/admin/stocks',add_headers(Authorization='Bearer TOKEN')) +Token <- commandArgs() +userStock<- GET('http://localhost/api/admin/stocks',add_headers(Authorization=paste("Bearer ",Token))) #API request to get all the EOD data for each stock for(s in symbols){ @@ -68,12 +69,12 @@ for(s in symbols){ SMA_7[s]<-sma(ts(dati_totali[s]),order = 7,silent = FALSE)$fitted } -#binding date information to each value +#binding date's information to each value date<-rev(dati[["data"]][["eod"]][["date"]]) SMA_3<-dplyr::bind_cols(date,SMA_3) SMA_7<-dplyr::bind_cols(date,SMA_7) -#creating a list of buy_sel where each stock are checked +#creating a list of buy_sell where each stock are checked list_buy_sell<-buy_sell(SMA_3,SMA_7,dati_totali)