前提・実現したいこと
Google Apps Scriptでbitflyerの自動売買botを作っているのですがエラーを吐いてしまい、先に進めません。
見直してみてもどこが悪いのかわからないので皆さんの力をお借りしたいです。
発生している問題・エラーメッセージ
SyntaxError: Unexpected identifier(行 182、ファイル「tradingview_bf.gs」)
該当のソースコード
tradingview_bf.gs
1//========================================================= 2// TradingViewのアラートに入力するメッセージ 3// 買い... BUY 4// 売り... SELL 5//========================================================= 6 7//========================================================= 8// ↓入力部分↓ 9//========================================================= 10// API Key等を設定 11var key = 'API Key'; 12var secret = 'API Secret'; 13 14// 注文量(FXBTC) 15ORDER_SIZE = 0.01; 16 17// Discord Webhook URL 18DISCORD_WEBHOOK_URL = "Discord Webhook URL"; 19 20//========================================================= 21// ↓コード本体↓ 22//========================================================= 23var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); 24var sheet = spreadsheet.getActiveSheet(); 25 26function doPost(p){ 27var order_side = p.postData.contents; 28sheet.getRange("A1").setValue(order_side); 29var ucs = order_side.toUpperCase(); 30 31 32try{ 33 // bitFlyerに注文 34 bitflyer(ucs); 35 36 // Discord投稿 37 var message = "@here "+ucs+" FXBTCJPY "+ORDER_SIZE+"₿FX" 38 discord(message); 39 40 // A1を空欄に戻し、各種情報を取得するためのフラグを立てる 41 sheet.getRange("A1").setValue(""); 42 sheet.getRange("A2").setValue("TRUE"); 43}catch(e){ 44 Logger.log(e); 45 46 // A1を空欄に戻し、各種情報を取得するためのフラグを立てる 47 sheet.getRange("A1").setValue(""); 48 sheet.getRange("A2").setValue("TRUE"); 49 50 // Discord投稿 51 var message = "@here 注文エラーが発生しました" 52 discord(message); 53} 54} 55 56// 注文後に各種情報を取得 57function get_info(){ 58var pub = sheet.getRange("A2").getValue(); 59Logger.log(pub); 60if(pub==true){ 61 var bal = getBalance(); 62 var pos = getPositions(); 63 var message = "@here \n預入証拠金: "+bal[0]+"\n必要証拠金: "+bal[1]+"\n評価損益: "+bal[2]+"\n評価証拠金: "+bal[3]+"\n証拠金維持率: "+bal[4] +"\n--------------------"+ "\n合計建玉: "+pos[0]+ "\n平均約定価格: "+pos[1]+ "\n売買: "+pos[2]; 64 discord(message); 65 sheet.getRange("A2").setValue(""); 66} 67} 68 69//Discordの処理 70function discord(message) { 71var discordUrl = DISCORD_WEBHOOK_URL; 72var payload = JSON.stringify({content: message}); 73var params = { 74 headers: { 75 'Content-Type': 'application/json' 76 }, 77 method: "POST", 78 payload: payload, 79 muteHttpExceptions: true 80}; 81try { 82 var response = UrlFetchApp.fetch(discordUrl, params); 83 Logger.log(response.getContentText()); 84} 85catch(e){ 86 Logger.log(e); 87} 88} 89 90// bitFlyer注文 91function bitflyer(ucs) { 92var timestamp = Date.now().toString(); 93var method = 'POST'; 94var path = '/v1/me/sendchildorder'; 95var body = JSON.stringify({ 96 product_code: "FX_BTC_JPY", 97 child_order_type: "MARKET", 98 side: ucs, 99 size: ORDER_SIZE 100}); 101var text = timestamp + method + path + body; 102var signature = Utilities.computeHmacSha256Signature(text, secret); 103var sign = signature.reduce(function(str,chr){ 104 chr = (chr < 0 ? chr + 256 : chr).toString(16); 105 return str + (chr.length==1?'0':'') + chr; 106},''); 107var url = 'https://api.bitflyer.com' + path; 108var options = { 109 method: method, 110 payload: body, 111 headers: { 112 'ACCESS-KEY': key, 113 'ACCESS-TIMESTAMP': timestamp, 114 'ACCESS-SIGN': sign, 115 'Content-Type': 'application/json' 116 } 117}; 118var response = UrlFetchApp.fetch(url, options); 119if( response != null ){ 120 var obj = JSON.parse(response.getContentText()); 121 Logger.log("child_order_acceptance_id(APIの受付ID): "+ obj.child_order_acceptance_id); 122} 123} 124 125// 証拠金や損益関係の情報を取得 126function getBalance(){ 127var timestamp = Date.now().toString(); 128var method = 'GET'; 129var path = '/v1/me/getcollateral'; 130var text = timestamp + method + path; 131var signature = Utilities.computeHmacSha256Signature(text, secret); 132var sign = signature.reduce(function(str,chr){ 133 chr = (chr < 0 ? chr + 256 : chr).toString(16); 134 return str + (chr.length==1?'0':'') + chr; 135},''); 136var url = 'https://api.bitflyer.com' + path; 137var options = { 138 method: method, 139 headers: { 140 'ACCESS-KEY': key, 141 'ACCESS-TIMESTAMP': timestamp, 142 'ACCESS-SIGN': sign, 143 'Content-Type': 'application/json' 144 } 145}; 146var strRespons = UrlFetchApp.fetch(url, options); 147if( strRespons == null ){ return false; } 148var json = JSON.parse(strRespons.getContentText()); 149var open_collateral = parseInt(json.collateral, 10) + parseInt(json.open_position_pnl, 10); 150var keep_rate_par = parseInt(json.keep_rate * 100, 10); 151return [json.collateral, json.require_collateral, json.open_position_pnl, open_collateral, keep_rate_par] 152} 153 154// 建玉に関する情報を取得 155function getPositions(){ 156var timestamp = Date.now().toString(); 157var method = 'GET'; 158var path = '/v1/me/getpositions?product_code=FX_BTC_JPY'; 159var text = timestamp + method + path; 160var signature = Utilities.computeHmacSha256Signature(text, secret); 161var sign = signature.reduce(function(str,chr){ 162 chr = (chr < 0 ? chr + 256 : chr).toString(16); 163 return str + (chr.length==1?'0':'') + chr; 164},''); 165var url = 'https://api.bitflyer.com' + path; 166var options = { 167 method: method, 168 headers: { 169 'ACCESS-KEY': key, 170 'ACCESS-TIMESTAMP': timestamp, 171 'ACCESS-SIGN': sign, 172 'Content-Type': 'application/json' 173 } 174}; 175var strRespons = UrlFetchApp.fetch(url, options); 176if( strRespons == null ){ return false; } 177var json = JSON.parse(strRespons.getContentText()); 178Logger.log(json); 179var size_sum = 0.0; 180var ave_pri = 0; 181var last_side; 182for each(var obj in json){ 183 size_sum += obj.size; 184 ave_pri += obj.price * obj.size; 185 last_side = obj.side; 186} 187var ave_con = ave_pri/size_sum; 188return [size_sum, ave_con, last_side] 189}
試したこと
182行目を改行して183行目にずらしたが、次のエラーメッセージで183行目がおかしいといわれた。
182行目のfor文の中身の;を全部消したが、同じエラーメッセージ。
補足情報(FW/ツールのバージョンなど)
for each(var obj in json){
size_sum += obj.size;
ave_pri += obj.price * obj.size;
last_side = obj.side;
}
この文のfor文が記述してある行が182行目になります。
ここにより詳細な情報を記載してください。