diff --git a/README.md b/README.md index 8a46c14..6b7d9b6 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# GoogleAppsScript \ No newline at end of file +# GoogleAppsScript + +### 1 Stock Watch + +Its a container bound app-script, which takes the watch stock list from the google spreadsheet its linked to and it finds the recent business news related to the watch list Stock, and performs sentimental analysis using Prediction API, and alerts if the stock may increase or decrease and also along with it records their Market high values to the spreadsheet. diff --git a/StockWatch/.clasp.json b/StockWatch/.clasp.json new file mode 100644 index 0000000..0e8ede7 --- /dev/null +++ b/StockWatch/.clasp.json @@ -0,0 +1 @@ +{"scriptId":"1haPCAarlTD_wMzX1-_nk8lbz22A2FR4zcGypBQr6N50JvcGjEg0dFHD5"} diff --git a/StockWatch/Code.js b/StockWatch/Code.js new file mode 100644 index 0000000..e0f1619 --- /dev/null +++ b/StockWatch/Code.js @@ -0,0 +1,121 @@ +//Opening the sheets using Id +var sheetID = "Insert Your Sheet ID"; +var sheet = SpreadsheetApp.openById(sheetID).getSheetByName("Sheet1"); + +//Global Variables +var rowList=[]; + +//driver function +function main(){ + + var endColumn=sheet.getLastColumn()-1; + var startColumn=1; + var startRow=2; + Logger.log(endColumn); + var watchList=sheet.getRange(startColumn,startRow,startColumn,endColumn).getValues(); + //Logger.log(test); + //var watchList=sheet.getRange(''+startColumn+":"+endColumn).getValues(); + Logger.log(watchList); + + for(var i = 0; i < watchList.length; i++) { + var stockList = watchList[i]; + //Logger.log(stockList); + for(var j = 0; j < stockList.length; j++) { + //Logger.log(stockList[j]); + var res=getStockQuote(stockList[j]); + } + } + + rowList.unshift(res); + //Logger.log(rowList); + + //Append the price to the Sheet + sheet.appendRow(rowList); +} + +function getStockQuote(stock){ + var gTime; + var gName; + var mainUrl="https://api.iextrading.com/1.0/stock/"; + var book="/book"; + var url=mainUrl+stock+book; + Logger.log(url); + var results=fetch(url); + //Logger.log(results); + for each( var result in results) { + // Logger.log(result); + var name=result.companyName; + var value=result.high; + var time=result.openTime; + rowList.push(value); + //sheet.appendRow(rowList); + gTime=time; + gName=name.split(" "); + //Logger.log(gName[0]); + break; + } + + //Check For recent news and its sentiment + sentimentCheck(gName[0]); + + return gTime; + +} +function sentimentCheck(stockName){ + + //News API Call + var mainUrl="https://newsapi.org/v2/top-headlines?q="; + var remain="&languag=en&category=business&sortBy=publishedAt&apiKey="; + var url=mainUrl+stockName+remain; + + //Fetch the news + var results=fetch(url); + Logger.log(results); + + if(results.totalResults ==0){ + Logger.log("No recent News"); + } + else{ + var news=results.articles[0].title; + //Logger.log(name); + var sense=queryHostedModel(news); + if (sense==="positive"){ + MailApp.sendEmail("abc.com", "StockWatch","News : "+ news+"\nCheck this Stock : "+stockName+"\nTrend : Its increasing"); + } + if(sense==="negative"){ + MailApp.sendEmail("abc.com", "StockWatch","News : "+ news+"\nCheck this Stock : "+stockName+"\nTrend : decreasing"); + } + } + +} + +function fetch(url) { + var response = UrlFetchApp.fetch(url); //makes api calls + var parsed_response = JSON.parse(response); + return parsed_response; +} + + + +function queryHostedModel(news) { + // When querying hosted models you must always use this + // specific project number. + var projectNumber = '414649711441'; + var hostedModelName = 'sample.sentiment'; + + // Query the hosted model with a positive statement. + var predictionString = news; + var prediction = Prediction.Hostedmodels.predict( + { + input: { + csvInstance: [predictionString] + } + }, + projectNumber, + hostedModelName); + // Logs Sentiment: positive. + Logger.log('Sentiment: ' + prediction.outputLabel); + return prediction.outputLabel; + + +} \ No newline at end of file diff --git a/StockWatch/README.md b/StockWatch/README.md new file mode 100644 index 0000000..88ab24e --- /dev/null +++ b/StockWatch/README.md @@ -0,0 +1,26 @@ +# Stack Watch +Its a container bound app-script, which takes the watch stock list from the google spreadsheet its linked to and it finds the recent business news related to the watch list Stock, and performs sentimental analysis using Prediction API, and alerts if the stock may increase or decrease and also along with it records their Market high values to the spreadsheet. + +### API's USED + + - News Api + - iexTrading + - Prediction API (GCP) + +### Sample Spreadsheet +![Example Spreadsheet ](https://image.ibb.co/fjCc7n/appscript.jpg) + - The Watch List Stock are entered in Row1 (To be entered by user) + - The timestamp is UNIX TimeStamp + - The values are in USD and its High Value +### Sample Mail +![Email Sample](https://image.ibb.co/eTLpnn/appscript2.jpg) + - News, the Stock and its predicted Trend will be mailed +### NOTE +Please replace the following: + - The sheetID in line 2 of code.gs + - The News API Key in line 68 of code.gs + - The e-Mail id in line 83 and 86 of code.gs + +If any issues found please be free to create an issue + +Follow me on [Github](https://github.com/mspawanranjith) diff --git a/StockWatch/appsscript.json b/StockWatch/appsscript.json new file mode 100644 index 0000000..5a9322a --- /dev/null +++ b/StockWatch/appsscript.json @@ -0,0 +1,11 @@ +{ + "timeZone": "Asia/Calcutta", + "dependencies": { + "enabledAdvancedServices": [{ + "userSymbol": "Prediction", + "serviceId": "prediction", + "version": "v1.6" + }] + }, + "exceptionLogging": "STACKDRIVER" +} \ No newline at end of file