透過接收試算表 Google APP script, LINE Notify 回報 (股票)
(完成樣式)
一、登錄LINE Notify服務
https://notify-bot.line.me/zh_TW/
接著,點擊「登錄服務」,輸入LINE Notify服務的基本資料,由於本文的Python爬蟲是跑在本機上,所以「服務網址」及「Callback URL」皆為http://127.0.0.1,如下範例:
完成後請至電子郵件認證
點擊「前往服務一覽」,可以看到LINE Notify配發了一個Client ID(帳號)給我們
接下來就可以向LINE Notify取得權杖(token),只要程式碼中帶有LINE Notify所給的權杖(token),LINE Notify就會為你提供傳送訊息的服務。
二、發行LINE Notify權杖(Token)
主要是透過notify來進行通知
點選個人頁面>發行權杖
這邊的「權杖名稱」,會出現在訊息的開頭,之後才接著自訂的訊息
選取自己1:1
選取自己1:1
請把權杖碼複製下來
三、登錄Google試算表服務
配置範例如下
id = 股票編碼  ※切記股票ETF編碼有00開頭 此欄位設計上必須保留
getprice = 購買價錢
shares = 購入張數
接著點選右上方"共用"設定
權限設定為「知道連結的人都可以檢視」
請複製連結網址,格式如下
紅色的部分,是試算表對應的【Sheet id】,我們等等寫程式碼會需要
https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxxx/edit?usp=sharing
完成後點選工具>指令碼編輯器
之後把以下程式碼貼上至APP Script
roundDown = function( num, decimal ) { return Math.floor( ( num + Number.EPSILON ) * Math.pow( 10, decimal ) ) / Math.pow( 10, decimal ); }
function ETF() {
  var id = '試算表ID'; // 前一段取得的 Sheet id
  var spreadsheet = SpreadsheetApp.openById(id);
  var sheet = spreadsheet.getSheets()[0]; // 要第幾個sheet? 0 就是第一個
  var data = sheet.getDataRange().getValues(); // 取得的資料
  var now = Utilities.formatDate(new Date(), "GMT+8", "yyyy-MM-dd");
  var price = new Array();  //目前價格
  var feh = new Array(); //手續費
  var fee = new Array();  //買進費用
  var fee1 = new Array(); //賣出費用
  var all_total = 0;    //總價
  var total = now + " 結案報告\n";
  var etfid = ['0050','0051','0052','0053','0054','0055','0056','0057','0061','008201','006203','006205','006204','006206','006207','006208','00631L','00632R','00633L','00634R','00636','00635U','00637L','00638R','00639','00642U','00640L','00641R','00645','00643','00646','00647L','00648R','00650L','00651R','00655L','00656R','00652','00653L','00654R','00657','00660','00661','00662','00663L','00664R','00665L','00666R','00625K','00643K','00675L','00676R','00673R','00674R','00668','00669R','00677U','00678','00680L','00681R','00670L','00671R','00682U','00683L','00684R','00685L','00686R','00690','00688L','00689R','00693U','00692','00700','00703','00709','00701','00702','00710B','00711B','00712','00706L','00707R','00708L','00713','00636K','00657K','00668K','00714','00715L','00717','00730','00728','00731','00732','00733','00738U','00735','00736','00737','00742','00739','00743','00752','00753L','00757','00763U','00762','00770','00775B','00774B','00783','00830','00771','00851','00774C','00852L','00850','00861','00865B','00875','00876','00878','00881','00882','00886','00887','00888'];
  var dataExport = {};
  for(var i = 1; i < data.length; i++) {
    if (data[i][0] ==='00888'){//00888是上櫃  連結不同
    var url = 'https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=otc_'+data[i][0]+'.tw';
    }else{
    var url = 'https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_'+ data[i][0] +'.tw';
    }
    Utilities.sleep(3000);
    try {
      // 先檢查回應碼是否為 200
      if(UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getResponseCode() === 200) {
        // 是 200 執行
        var text = UrlFetchApp.fetch(url); 
        json = JSON.parse(text); 
        //低於20手續費//不太確定是否現在才有手續費低於20以20算
        feh[i] = roundDown(data[i][1] * data[i][2] * 0.001425,0);
        if (feh[i] <= 20){
          fee[i] = data[i][1] * data[i][2] + 20;
        }else{
          fee[i] = data[i][1] * data[i][2] + roundDown(data[i][1] * data[i][2] * 0.001425,0);
        }
        if (etfid.indexOf(data[i][0]) > -1 ){
          fee1[i] =json["msgArray"][0]["z"] * data[i][2] - roundDown(json["msgArray"][0]["z"] * data[i][2] * 0.001425,0) - roundDown(json["msgArray"][0]["z"] * data[i][2] * 0.001,0);
        }else{
          fee1[i] =json["msgArray"][0]["z"] * data[i][2] - roundDown(json["msgArray"][0]["z"] * data[i][2] * 0.001425,0) - roundDown(json["msgArray"][0]["z"] * data[i][2] * 0.003,0);
        }
        price[i] = json["msgArray"][0]["z"];
      }
    }
    catch (e) {//失敗5秒後重新
       i  = i - 1;
       Utilities.sleep(5000);
    }
  }
  for(var i = 1; i < data.length; i++) {
    if (fee1[i]-fee[i]>0){
        total= total+data[i][0] +" 目前價格 " + price[i] + "元 ! (賺"+ roundDown((fee1[i]-fee[i]), 2 ) +")\n ";
    }else{
        total= total+data[i][0] +" 目前價格 " + price[i] + "元 ! (賠"+ roundDown((fee1[i]-fee[i]), 2 ) +")\n ";
    }
    all_total = all_total + roundDown((fee1[i]-fee[i]), 2 );
  }
  UrlFetchApp.fetch('https://notify-api.line.me/api/notify', {
      'headers': {
         'Authorization': 'Bearer ' + 'LINE取得的權杖',
      },
      'method': 'post',
       'payload': {
           'message':total + '\n 淨利:' + all_total
      }
  });
}







 
 
留言
張貼留言