3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

昨今の為替予測は難しいが、過去の為替ならひょいっと取得できます

Last updated at Posted at 2023-04-24

ドル建保険の闇からの脱却

時は遡ること数年前、ドル建の保険に加入しました。
当時は比較的為替市場は安定していたため、生活を圧迫することもなく平穏に暮らせていました。
しかし、昨今の為替市場の不安定さは目に余るものがあり、支払金額上昇に伴い生活は困窮しながらもなんとか支払いを継続している状況にあります。

そんなとき、ただ単に口座から保険料が引かれていくのを見ているだけでいいのだろうかと・・・ふと思うようになりました。
「自身がどれだけをしているのか、をしているのか」や「どれだけ為替が変動しているか」などを知ることができれば、"なんとなく高くなった"を明確に(意識)することができ、自身の生活スタイルの変革に繋げられるのではないかと思い立ち、開発に取り組みました。

開発

利用技術

・Railway
 PaaSサービスを提供。
 利用方法は以下を参照。
 Railwayのはじめ方
・Node-RED
 ハードウェアデバイス。APIおよびオンラインサービスを接続するためのツール。
 Node-REDの利用イメージは以下を参照。
 超初心者向けのNode-RED基本のき
 JavaScriptの記述方法は以下を参照。
 JavaScript入門
 functionノードにおける変数の有効範囲は以下を参照。
 Node-RED で遊んでみる第三歩: function ノードにおける変数の有効範囲
・LINE Messaging API
 LINE公式アカウントの作成方法、Messaging APIの設定方法は以下を参照。
 Line Messaging APIで簡単Line Bot作成(超初心者向け) - Qiita
 ※記載のAWSサービスは本記事では使用していないものになります。
・exchangeratesapi
 為替情報を取得できるAPI。
 APIキー取得は以下を参照。
 Exchange Rates Data API

システム構成図

Node-RED フロー図

開発ソース

JSON
[{"id":"acfd90dbe4bd3027","type":"tab","label":"フロー 8","disabled":false,"info":"","env":[]},{"id":"26da9099bcddb1f2","type":"Webhook","z":"acfd90dbe4bd3027","name":"","url":"/webhook","x":100,"y":80,"wires":[["460d74ae8b383b40"]]},{"id":"344eabeed3638bbe","type":"http request","z":"acfd90dbe4bd3027","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":210,"y":200,"wires":[["74057fd75ac48741","6c3cb941de31be40"]]},{"id":"460d74ae8b383b40","type":"function","z":"acfd90dbe4bd3027","name":"function 4","func":"var yyyymmdd = msg.payload;\nvar preyyyy = 0;\nvar premm = 0;\nvar predd = 0;\nvar money = 0;\nvar preyyyymmdd = \"\"\nvar query = \"\"\nvar prequery = \"\"\nvar apikey = \"***APIKey***\"\n\nmoney = Number(msg.payload.substr(9));\n\npreyyyy = Number(yyyymmdd.substring(4,0));\npremm = Number(yyyymmdd.substring(6, 4));\npredd = Number(yyyymmdd.substring(8, 6));\n\nif(premm === 1){\n    premm = 12;\n    preyyyy = preyyyy - 1;\n} else{\n    premm = premm - 1;\n    if (predd === 31){\n        if (premm === 4 || premm === 6 || premm === 9 || premm === 11){\n            predd = 30;\n        }\n    }\n    if(predd === 29 || predd === 30 || predd === 31){\n        if(premm === 2){\n            predd = 28;\n        }\n    }\n}\n\nyyyymmdd = yyyymmdd.substring(4, 0) + \"-\" + yyyymmdd.substring(6, 4) + \"-\" + yyyymmdd.substring(8, 6);\n\nif(premm >= 1 && premm <10){\n    preyyyymmdd = preyyyy + \"-0\" + premm;\n}else{\n    preyyyymmdd = preyyyy + \"-\" + premm;\n}\n\nif (predd >= 1 && predd < 10) {\n    preyyyymmdd = preyyyymmdd + \"-0\" + predd;\n} else {\n    preyyyymmdd = preyyyymmdd + \"-\" + predd;\n}\n\nquery = \"https://api.apilayer.com/exchangerates_data/\" + yyyymmdd + \"?apikey=\" + apikey + \"&symbols=JPY&base=USD\"\nprequery = \"https://api.apilayer.com/exchangerates_data/\" + preyyyymmdd + \"?apikey=\" + apikey + \"&symbols=JPY&base=USD\"\n\nmsg.temp = {\n    ymd: yyyymmdd,\n    preymd: preyyyymmdd,\n    okane: money,\n    q: query,\n    preq: prequery\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":80,"wires":[["5f6399ebd58495cb","0ed90f2ccf40a746"]]},{"id":"5f6399ebd58495cb","type":"change","z":"acfd90dbe4bd3027","name":"","rules":[{"t":"set","p":"url","pt":"msg","to":"temp.q","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":80,"wires":[["344eabeed3638bbe","bceb98a0c2f09487"]]},{"id":"bceb98a0c2f09487","type":"debug","z":"acfd90dbe4bd3027","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":40,"wires":[]},{"id":"74057fd75ac48741","type":"debug","z":"acfd90dbe4bd3027","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":400,"y":160,"wires":[]},{"id":"6c3cb941de31be40","type":"function","z":"acfd90dbe4bd3027","name":"function 6","func":"var rate = 0;\nvar enntenn = 0;\n\nrate = msg.payload.rates.JPY;\nenntenn = rate * msg.temp.okane;\n\nmsg.ans1 = {\n    ansenntenn: enntenn,\n    r: rate,\n    m: msg.temp.okane\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":200,"wires":[["4e339ad4bc42221b","faf5d490aa3bf06f"]]},{"id":"4e339ad4bc42221b","type":"debug","z":"acfd90dbe4bd3027","name":"debug 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"ans1","targetType":"msg","statusVal":"","statusType":"auto","x":580,"y":160,"wires":[]},{"id":"0ed90f2ccf40a746","type":"debug","z":"acfd90dbe4bd3027","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"temp","targetType":"msg","statusVal":"","statusType":"auto","x":480,"y":40,"wires":[]},{"id":"faf5d490aa3bf06f","type":"change","z":"acfd90dbe4bd3027","name":"","rules":[{"t":"set","p":"url","pt":"msg","to":"temp.preq","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":200,"wires":[["b3e5415d1a016397","8f6cea9f2d1a8766"]]},{"id":"b3e5415d1a016397","type":"debug","z":"acfd90dbe4bd3027","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"url","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":160,"wires":[]},{"id":"8f6cea9f2d1a8766","type":"http request","z":"acfd90dbe4bd3027","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":210,"y":300,"wires":[["7febde5bc23c5c4c"]]},{"id":"7febde5bc23c5c4c","type":"function","z":"acfd90dbe4bd3027","name":"function 7","func":"var prerate = 0;\nvar preenntenn = 0;\n\nprerate = msg.payload.rates.JPY;\npreenntenn = prerate * msg.temp.okane;\n\nmsg.ans2 = {\n    preansenntenn: preenntenn,\n    prer: prerate,\n    m: msg.temp.okane\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":300,"wires":[["b05e91cb7fbd39e5","177eca94c6c34025"]]},{"id":"b05e91cb7fbd39e5","type":"debug","z":"acfd90dbe4bd3027","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"ans2","targetType":"msg","statusVal":"","statusType":"auto","x":580,"y":260,"wires":[]},{"id":"177eca94c6c34025","type":"function","z":"acfd90dbe4bd3027","name":"function 8","func":"var strmsg = \"\"\nvar diff = 0;\n\nstrmsg = \"【基準日レート(USD)】\\r\\n \" + msg.ans1.r + \"\\r\\n\"\nstrmsg = strmsg + \"【円転金額】\\r\\n \" + msg.ans1.ansenntenn + \"\\r\\n\"\nstrmsg = strmsg + \"【1ヶ月前のレート(USD)】\\r\\n \" + msg.ans2.prer + \"\\r\\n\"\n\ndiff = msg.ans1.ansenntenn - msg.ans2.preansenntenn;\n\nif(diff >= 0){\n    strmsg = strmsg + \"先月より \" + diff + \" 円支払いが多くなります!\"\n}else{\n    diff = diff * -1 ;\n    strmsg = strmsg + \"先月より \" + diff + \" 円支払いが少なくなります!\"\n}\n\nmsg.payload = strmsg;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":400,"wires":[["1e98a2203814b6f4","59d8a42f3729350b"]]},{"id":"1e98a2203814b6f4","type":"debug","z":"acfd90dbe4bd3027","name":"debug 13","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":400,"y":360,"wires":[]},{"id":"59d8a42f3729350b","type":"ReplyMessage","z":"acfd90dbe4bd3027","name":"","replyMessage":"","credentials":{},"x":420,"y":400,"wires":[]}]

『USD⇒JPY』 LINE Bot実行

実行画面

取得データからわかること

毎月350ドルの支払いだった場合、為替レートは約30円も円安となり、支払金額は約1万円も多くなっていることに気付かされる結果となりました。

基準日 為替レート(米ドル/円) 支払金額(円貨)
2021/01/07 103.795032 36328.2612 円
2023/01/07 132.08504 46229.7639 円

直近月を基準に年単位でも確認してみましょう。

基準月 為替レート(米ドル/円) 支払金額(円貨)
2021/05 108.6 35838
2021/06 109.52 36141.6
2021/07 111.03 36639.9
2021/08 110.23 36375.9
2021/09 109.71 36204.3
2021/10 111.08 36656.4
2021/11 113.39 37418.7
2021/12 112.79 37220.7
2022/01 115.58 38141.4
2022/02 115.2 38016
2022/03 114.82 37890.6
2022/04 122.56 40444.8
2022/05 130.55 43081.5
2022/06 130.83 43173.9
2022/07 135.25 44632.5
2022/08 134.98 44543.4
2022/09 140.23 46275.9
2022/10 145.38 47975.4
2022/11 146.67 48401.1
2022/12 134.32 44325.6
2023/01 132.09 43589.7
2023/02 131.13 43272.9
2023/03 135.85 44830.5
2023/04 132.18 43619.4

直近2年間の年間差額は約9万円となり、家計を圧迫する大きな要因になっていることがわかりました。

集計期間 合計金額(円貨)
2021/05~2022/04 446988.3
2022/05~2023/04 537721.8
差額 90733.5

為替レートの推移を見てわかるように、2022年4月付近からの円高傾向は以前の水準からは離れたところにあります。

生活スタイルの変革

さて、家計がどれだけ圧迫されているかがわかったところで、どう生活スタイルを変革させるか!
以下2点取り組んでいこうと思います。
 ・スマートフォンアプリの課金をしない
 ・外食頻度を減らす

皆さまの応援をお待ちしております。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?