Strategies on The Cloud: TAA on Google Docs

Lets start coding. Google Docs scripting uses a version of JavaScript which seems fairly easy for non programmers.
It would be nice to create an object that holds all the ETF information. So here's the function to store each ETF's info:
function tickerobj(symbol,close,action,posscore,sma){ //tickerobj("SPY",154,1,20,150) this.symbol=symbol; this.close=close; this.action=action; this.posscore=posscore; this.sma=sma; }
function myGetHistoricalStockInfo(symbol,days){ var start=new Date(); var finish=new Date(); start.setDate(finish.getDate()-days); finish.setDate(finish.getDate()); var data = FinanceApp.getHistoricalStockInfo(symbol, start, finish, 1 ); if(data==undefined) return(0); else return data; }
function SMA(data,period){ //data is a FinanceApp.getHistoricalStockInfo object array var end=data.stockInfo.length-1; var close=[]; var sum=0;var count=0; for (var i=end;i>end-period;i--) { sum += data.stockInfo[i].close; count++; } return (sum/period); }
function NewMonth(){ var now= new Date(); var yesterday= new Date(); yesterday.setDate(now.getDate()-1); if(now.getMonth()!=yesterday.getMonth()) return (1); else return (0); }
function CreateInstr(tickerlist,SMAperiod){ var symbolarray=[]; //split tickerlist by comma (",") symbolarray= tickerlist.split(","); //how many symbols in the list var idx=symbolarray.length; var close=[];var smat=[];var posscore=[];var action=[];//var symbol=[]; var Instrument=[]; var idx1; var data=[]; //Go through each symbol. I.e. SPY...then EEM... for (var i=0;i<idx;i++) { //get hist data data=myGetHistoricalStockInfo(symbolarray[i],SMAperiod*2); //get index of the last data point idx1=data.stockInfo.length-1; //get the last closing price close[i]=data.stockInfo[idx1].close; //get the moving average smat[i] = SMA(data, SMAperiod); //not used in this example posscore[i]=0; //action - If close>mov. average, 1(buy) otherwise -1(sell) action[i]=(close[i]>smat[i])?1:-1 ; //Now store all the infor in the object Instrument[i]=new tickerobj(symbolarray[i],close[i],action[i],posscore[i],smat[i]); } return(Instrument); }
So let's show all this info on the Spreadsheet.
function ShowTAAOnSpreadsheet(){ var sheet = SpreadsheetApp.getActiveSheet(); var date=new Date(); //You may use your own ticker list and period var Instrument=CreateInstr("TLT,SPY,EEM,VNQ,DBC",200); //Set the Name headers for each column sheet.getRange(3,1).setValue("Symbol"); sheet.getRange(3,2).setValue("Close"); sheet.getRange(3,3).setValue("SMA"); sheet.getRange(3,4).setValue("Action"); var idx=Instrument.length; for (var i=0;i<idx;i++) { sheet.getRange(5+i,1).setValue(Instrument[i].symbol); sheet.getRange(5+i,2).setValue(Instrument[i].close); sheet.getRange(5+i,3).setValue(Instrument[i].sma); sheet.getRange(5+i,4).setValue(Instrument[i].action); } sheet.getRange(1,1).setValue("Last Update"); sheet.getRange(1,2).setValue(date); }
Now you can go on the top Menu and select a function to run. Select "ShowTAAOnSpreadsheet". Press the "paly" button to run.
Go to the spreadsheet, see if it updated.
function EmailPositions(){ //if not new month do not calculate or email anything, just exit with 0. if (NewMonth()==0) return (0); var email = Session.getActiveUser().getEmail(); var Instrument=CreateInstr("TLT,SPY,EEM,VNQ,DBC",200); var idx=Instrument.length; var txt=""; for (var i=0;i<idx;i++) { txt=txt+Instrument[i].symbol+","+" Action: "+Instrument[i].action+"n"; } MailApp.sendEmail(email, "TAA_5_FromGoogleDocs", txt); }
1 |
function ShowTAAOnSpreadsheet() |

