Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vhsu/a8dfb1564045afb389620018b9c67951 to your computer and use it in GitHub Desktop.
Save vhsu/a8dfb1564045afb389620018b9c67951 to your computer and use it in GitHub Desktop.
Twilio Free Sms Stock alerts with Google Spreadsheet
/**********************************************************************************************************************
//Check out the tutorial here : https://medium.com/@20cent.hsu/free-sms-stock-alerts-with-twilio-and-google-spreadsheets-1aa5dec7ef4c
//Realtime price quote in GOOGLEFINANCE can be delayed by up to 20 minutes.
//Author : Vincent Hsu (Suisseo)
//Twitter : @suisseo
**********************************************************************************************************************/
//These are mandatory parameters you will find in your twilio Dashboard
var ACCOUNT_SID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var AUTH_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
//the twilio number must be sms an sms enabled number
var twilioNumber = "+XXXXXXXXX";
//this is the mobile number where the alerts should be sent, if your using a twilio trial account your number must be validated
var receivingNumber = "+XXXXXXXXX";
//this parameter lets you define by how many percents the limit should be readjusted here 2% from last quote
var limitpercentchange = 0.02;
//Set this to true if you want your low/high value to be automatically set when you hit a high.
//For example lets say you hit your high value at 100 and your low value was 70, if this is set to true
//the new low value will be set to 98 ( if limitpercentchange = 0.02)
var setTrailingHL = false;
function main() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var sms = "";
for (var i = 1; i < data.length; i++) {
//Low Limit
if (typeof data[i][2] == 'number'&&data[i][1] <= data[i][2] && data[i][4] != "1") {
sms = "- " + data[i][0] + ' : ' + data[i][1] + "\n";
sheet.getRange(i + 1, 3).setValue(round(data[i][1]-(data[i][1]*limitpercentchange),2));
setTrailingHL&&sheet.getRange(i + 1, 4).setValue(round(data[i][1]+(data[i][1]*limitpercentchange),2));
}
//High limit
if (typeof data[i][3] == 'number'&&data[i][1] >= data[i][3] && data[i][4] != "1") {
sms += "+ " + data[i][0] + ' : ' + data[i][1] + "\n";
sheet.getRange(i + 1, 4).setValue(round(data[i][1]+(data[i][1]*limitpercentchange),2));
setTrailingHL&&sheet.getRange(i + 1, 3).setValue(round(data[i][1]-(data[i][1]*limitpercentchange),2));
}
}
if (sms.length > 0) {
sendsms(sms);
}
}
function sendsms(sms) {
var messages_url = "https://api.twilio.com/2010-04-01/Accounts/" + ACCOUNT_SID + "/Messages.json";
var payload = {
"To": receivingNumber,
"Body": sms,
"From": twilioNumber
};
var options = {
"method": "post",
"payload": payload
};
options.headers = {
"Authorization": "Basic " + Utilities.base64Encode(ACCOUNT_SID + ":" + AUTH_TOKEN+ " ")
};
UrlFetchApp.fetch(messages_url, options);
}
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment