Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# GoogleAppsScript
# 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.
1 change: 1 addition & 0 deletions StockWatch/.clasp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"scriptId":"1haPCAarlTD_wMzX1-_nk8lbz22A2FR4zcGypBQr6N50JvcGjEg0dFHD5"}
121 changes: 121 additions & 0 deletions StockWatch/Code.js
Original file line number Diff line number Diff line change
@@ -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=<Insert Your API KEY>";
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;


}
26 changes: 26 additions & 0 deletions StockWatch/README.md
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions StockWatch/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"timeZone": "Asia/Calcutta",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Prediction",
"serviceId": "prediction",
"version": "v1.6"
}]
},
"exceptionLogging": "STACKDRIVER"
}