0
0

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.

GitHub Actionsで株価データを毎日自動取得!DuckDBとGoogle Drive連携でハマった点の備忘録

0
Posted at

GitHub Actionsで株価データを毎日自動取得!DuckDBとGoogle Drive連携でハマった点の備忘録 📈💾☁️

はじめに

株価データの分析基盤を作る際、「毎日データを自動で取得して、いい感じに保存したい!」と思うことはよくありますよね。今回は、GitHub Actions を使って日経225のデータを yfinance で取得し、DuckDB に保存、Google Drive に同期する仕組みを構築しました。

当初はバックテストの最適化処理もGitHub Actionsで実行していましたが、処理が重すぎたため「データ取得とDB保存・同期のみ」に機能を絞ることに。その過程で、認証やライブラリインストールなど、いくつかハマった点があったので、備忘録としてまとめます。

やりたかったこと

  • 毎日定時(日本時間午前9時)に処理を自動実行したい。
  • 日経225の複数銘柄の株価データ(1分足〜60分足)を取得したい。
  • 取得したデータをローカルDB(DuckDB)に追記・更新したい。
  • 更新したDBファイルをGoogle Driveにアップロードして保存したい。
  • GitHub Actionsの実行環境を使う(無料枠を活用したい)。
  • 重いバックテスト処理(micmacV7.py)は実行したくない。

使った主な技術

  • GitHub Actions: CI/CD、自動化実行プラットフォーム
  • Python: メインの処理言語
  • yfinance: 株価データ取得ライブラリ
  • DuckDB: ファイルベースの高速な分析用データベース
  • PyDrive2: Google Drive API操作用Pythonライブラリ
  • Google Drive API: Google Drive操作用API
  • TA-Lib: テクニカル分析ライブラリ(今回はインストールで苦労)

ハマりポイントとその解決策

ここからが本題。いくつかのエラーに遭遇しました 😵‍💫


1. TA-Libがインストールできない! (fatal error: ta-lib/ta_defs.h)

requirements.txt に TA-Lib を書いて pip install しようとすると、GitHub Actions (Ubuntu環境) で fatal error: ta-lib/ta_defs.h: No such file or directory というコンパイルエラーが発生しました。

  • 原因: Pythonの TA-Lib は、C言語で書かれた本体ライブラリが必要です。実行環境にその本体がないため、ヘッダーファイルが見つからずエラーになっていました。
  • 試行錯誤: 最初は apt-get install libta-lib-dev を試しましたが、パッケージが存在せず E: Unable to locate package libta-lib-dev エラーに。
  • 解決策: Qiitaの記事 を参考に、C言語のソースコードから手動でビルド・インストールする手順を daily-run.yml に追加しました。
daily-run.yml
      # 3. ライブラリのインストール(必須)
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip

          # TA-Libのインストール (C言語本体を手動でビルド)
          sudo apt-get update
          # ビルドに必要なツールをインストール
          sudo apt-get install -y build-essential wget

          # ソースコードをダウンロード
          wget [http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz](http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz)

          # 解凍
          tar -xzf ta-lib-0.4.0-src.tar.gz

          # ビルドとインストール
          cd ta-lib/
          ./configure --prefix=/usr
          make
          sudo make install

          # requirements.txt (TA-Libを含む) をインストール
          cd ..
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?