Olá, Visitante. Por favor entre ou registe-se se ainda não for membro.

Entrar com nome de utilizador, password e duração da sessão
 

Autor Tópico: Google programar...estratégia simples  (Lida 1820 vezes)

JoaoAP

  • Ordem dos Especialistas
  • Hero Member
  • *****
  • Mensagens: 4778
    • Ver Perfil
Google programar...estratégia simples
« em: 2013-09-14 01:26:31 »
Uma coisa simples e quem souber javascript ... pelos vistos não é dificil...
Não testei...
Kudos for sanzprophet

Mas para quem quiser começar a fazer sistemas... tem de ver coisas simples ... e free .. e esta é.
Ninjatrader também é free.

-------------------------
Citar
id you want to have a strategy on the cloud that monitors the market and updates you on new Buy/Sell signals (as well as number of shares, etc)  by email. Did you want to run it on best of breed "always ON" servers with free and accurate data?
How much would that set you back?

Well, Nada! Courtesy of Google.

This post will guide you through coding a simple Tactical Asset Allocation on Google's Docs.
You need:
1. A Google account.
2. Google Docs.

The system is similar to Faber's TAA model using 5 Etfs.: SPY,TLT,VNQ,EEM,DBC
We buy or sell at the beginning of the month ONLY.
If Close > 200-moving Average then we buy the ETF.
If Close < 200-moving Average then we sell the ETF.

Pseudo Code:
If TodayIsNewMonth AND CloseETF>MA(200) Then Buy
If TodayIsNewMonth AND CloseETF<MA(200) Then Sell

Agora... via google... eu não sabia que o Google permitia isto...
Citar
Let's get started. Go to Google Docs and create a new SpreadSheet. Call it TAA_5.
Once the spreadsheet is open in your browser, go up to the menu and select Tools-->Script Editor...
This should open a new script Editor. Select "SpreadSheet" as your project.
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.
Citar
So here's the function to store each ETF's info:

Código: [Seleccione]
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;
}

Citar
How do we retrieve data from Google?
Código: [Seleccione]
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;
}
Citar
We need to calculate the Simple Moving Average from the Data to check if close>sma(200).
Código: [Seleccione]
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);
}
Citar
How do we know it's the beginning of the month?
Código: [Seleccione]
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);
}
Citar
So let's start calculating and storing info for each ETF. Tickerlist would be "SPY,EEM,...etc".
Código: [Seleccione]
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);
}
Citar
So let's show all this info on the Spreadsheet.
Código: [Seleccione]
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);

}
Citar
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.

Se alguém não conseguir, eu depois coloco aqui o meu código. Todo Junto.

Esqueci-me... a cereja em cima do bolo...
MAS NÃO ABUSEM ... os servidores do Google ainda são free...

Citar
Last is a function to email ourselves the results. We should schedule this to run everyday (Menu-->Resources-->Current project's Triggers).
Please do not abuse Google's Servers by having it run too often. They are kind enough to provide this kind of functionality free of charge.
Código: [Seleccione]
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);

}
« Última modificação: 2013-09-14 01:42:47 por JoaoAP »

Lucky Luke

  • Ordem dos Especialistas
  • Sr. Member
  • *****
  • Mensagens: 493
    • Ver Perfil
Re:Google programar...estratégia simples
« Responder #1 em: 2013-12-11 11:46:11 »
isto não é um sistema mas um setup  ;)