株のシステムトレードをしよう - 1から始める株自動取引システムの作り方

株式をコンピュータに売買させる仕組みを少しずつ作っていきます。できあがってから公開ではなく、書いたら途中でも記事として即掲載して、後から固定ページにして体裁を整える方式で進めていきます。

作業経過:「株式分析チュートリアル」の冪等性のある実行環境の作成 その2

昨日の記事の続きをやっている。

how-to-make-stock-trading-system.dogwood008.com

まだ完成していないので途中だが、できたところまでを掲載する。(動作を確認しながら、試行錯誤中) vagrant up で仮想マシン起動、 vagrant ssh でSSHクライアントを通じてゲストのコンソールを開くことができる。 ~/J-Quants-Tutorial 以下に、チュートリアル用のレポジトリを展開する。

なお、同じ内容をgistにも載せておく。

gist.github.com

# Vagrantfile

# frozen_string_literal: true
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The '2' in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.

MEMORY_IN_GB = 4.freeze
DISK_IN_GB = 40.freeze
MACHINE_NAME = 'J-Quants-Tutorial'
CURRENT_PATH = Dir.pwd
HOME_PATH = '/home/vagrant'

Vagrant.configure('2') do |config|

  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = 'ubuntu/focal64'  # Ubuntu 20.04 LTS

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  config.vbguest.auto_update = false
  config.vm.box_check_update = false
  #config.vbguest.no_remote = true

  config.disksize.size = "#{DISK_IN_GB}GB"


  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing 'localhost:8080' will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # 開放したいポートがあればここで宣言
  ports = {
    # ssh: 22, デフォルトでホストの2222番ポートはゲストの22番ポートにフォワードされるので、不要
    jupyter: 8888,
  }
  ports.each do |_service, port|
    config.vm.network 'forwarded_port', guest: port, host: port
  end

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.define MACHINE_NAME do |server|
    server.vm.network 'private_network', ip: '192.168.33.10'
  end

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network 'public_network'

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder(
    Pathname.new(CURRENT_PATH).join('J-Quants-Tutorial').to_s,
      "#{HOME_PATH}/J-Quants-Tutorial",
      mount_options: ['uid=1000,gid=1000'])
  config.vm.synced_folder '.', '/vagrant', disabled: true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider 'virtualbox' do |vb|
    # Display the VirtualBox GUI when booting the machine
    # vb.gui = true

    # Customize the amount of memory on the VM:
    vb.memory = (1024 * MEMORY_IN_GB).to_s
    vb.name = MACHINE_NAME
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define 'atlas' do |push|
  #   push.app = 'YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME'
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  files = {
    home_dir: { '.' => %w[.bashrc .bash_profile] },
    handson_dir: { Pathname.new('./J-Quants-Tutorial').join('/handson').to_s =>
      %w[Dockerfile docker-compose.yml] },
  }

  files.each do |_name, parent_file_pair|
    parent_file_pair.each do |parent, files|
      files.each do |filename|
        config.vm.provision('file',
          source: "#{CURRENT_PATH}/#{filename}",
          destination: Pathname.new(parent).join(filename).to_s)
      end
    end
  end

  config.vm.provision 'shell', inline: <<-SHELL
    # Exit if already bootstrapped
    test -f /etc/bootstrapped && exit

    apt update && \
      apt install -y apt-transport-https ca-certificates curl software-properties-common && \
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
      apt-key fingerprint 0EBFCD88 && \
          add-apt-repository \
              'deb [arch=amd64] https://download.docker.com/linux/ubuntu \
              $(lsb_release -cs) \
              stable'

    apt update
    apt install -y docker-ce direnv libssl-dev git screen gcc g++ make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev build-essential libstdc++6

    groupadd docker
    gpasswd -a vagrant docker
    service docker restart
    curl -L https://github.com/docker/compose/releases/download/1.28.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose

    apt -y upgrade

    su - vagrant
    date > /etc/bootstrapped
  SHELL
end
# Dockerfile

FROM continuumio/anaconda3:2019.03
LABEL mainteiner="dogwood008"

ENV PYTHONPATH=/opt/ml/src

RUN \
    # https://japanexchangegroup.github.io/J-Quants-Tutorial/#_%E5%BF%85%E8%A6%81%E3%81%AA%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB
    # :shap用にg++とgccをインストールします
    apt update && \
      apt install -y --no-install-recommends g++ gcc \
      # 必要なライブラリをインストールします
      pip install shap==0.37.0 slicer==0.0.3 xgboost==1.3.0.post0

CMD ["jupyter", "notebook", "--ip 0.0.0.0", "--allow-root", "--no-browser", \
 "--no-mathjax", "--NotebookApp.disable_check_xsrf=True", \
 "--NotebookApp.token=''", "--NotebookApp.password=''", \
 "/notebook"]
# docker-compose.yml

version: '3.9'
services:
  app:
    build: .
    ports:
      - '8888:8888'
    volumes:
      # https://docs.docker.jp/docker-for-mac/osxfs-caching.html#consistentcacheddelegated
      # delegated: コンテナの表示が信頼できる(コンテナ上の更新がホスト上に反映するまで、遅延が発生するのを許容)
      # cached: ホストの表示が信頼できる(ホスト上の更新がコンテナ上に反映するまで、遅延が発生するのを許容)
      # データ配置先のディレクトリ
      - ./data_dir:/path/to:cached
      # 学習済みモデル提出用のディレクトリ (handson/Chapter02/archive)
      - ./Chapter02/archive:/opt/ml:delegated
      # カレントディレクトリを JupyterNotebook の保存先にする
      - .:/notebook:delegated
    environment:
      - PYTHONPATH=/opt/ml/src

(C) 2020 dogwood008 禁無断転載 不許複製 Reprinting, reproducing are prohibited.