1
8

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.

仮想通貨APIラッパー「ccxt」をインストールしてみた

Last updated at Posted at 2021-03-13

あらすじ

さあ大仮想通貨時代の到来です。自動トレードして人生をサボっていきましょう。と思ったら、その自動トレードシステムの構築をサボれるライブラリがあるらしいです。その名も ccxt

ccxtはたくさんの仮想通貨取引所のapiをまとめたラッパーライブラリです。Node.jsPythonPHPから利用することができます。取引所ごとに覚えるという手間をサボれるわけですね。ラッパー使うとサボれるので、ラッパーという単語が大好きです。

さっそく今回は、Python用にインストールするときの手順を紹介します。重要なのは**MSVC++**です。

conda で仮想環境作成

miniconda3 を利用して仮想環境を作成します。

conda create -n ccxt-env -y
conda activate ccxt-env
conda install python -y

1行目で「ccxt-env」という名前の仮想環境を作ります。
2行目でこの仮想環境に入ります。
3行目でこの中にPythonをインストールします。

CCXTをインストール

続けて下記のコマンドで「ccxt」をインストールしてみます。

pip install ccxt

すると、実行結果にエラーメッセージがあります。

実行結果
Collecting ccxt
  Downloading ccxt-1.42.93-py2.py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 3.3 MB/s
Requirement already satisfied: certifi>=2018.1.18 in c:\tools\miniconda3\envs\ccxt-env\lib\site-packages (from ccxt) (2020.12.5)
Collecting requests>=2.18.4
  Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 3.8 MB/s
Collecting aiodns<2.1,>=1.1.1
  Downloading aiodns-2.0.0-py2.py3-none-any.whl (4.8 kB)
Collecting cryptography>=2.6.1
  Downloading cryptography-3.4.6-cp36-abi3-win_amd64.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB ...
Collecting aiohttp<3.8,>=3.7.4
  Downloading aiohttp-3.7.4.post0-cp39-cp39-win_amd64.whl (634 kB)
     |████████████████████████████████| 634 kB ...
Collecting yarl==1.1.0
  Downloading yarl-1.1.0.tar.gz (156 kB)
     |████████████████████████████████| 156 kB ...
Requirement already satisfied: setuptools>=38.5.1 in c:\tools\miniconda3\envs\ccxt-env\lib\site-packages (from ccxt) (49.6.0.post20210108)
Collecting multidict>=4.0
  Downloading multidict-5.1.0-cp39-cp39-win_amd64.whl (48 kB)
     |████████████████████████████████| 48 kB 1.5 MB/s
Collecting idna>=2.0
  Downloading idna-3.1-py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 3.8 MB/s
Collecting pycares>=3.0.0
  Downloading pycares-3.1.1.tar.gz (233 kB)
     |████████████████████████████████| 233 kB 6.4 MB/s
Collecting chardet<5.0,>=2.0
  Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
     |████████████████████████████████| 178 kB 2.2 MB/s
Collecting typing-extensions>=3.6.5
  Downloading typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting async-timeout<4.0,>=3.0
  Downloading async_timeout-3.0.1-py3-none-any.whl (8.2 kB)
Collecting attrs>=17.3.0
  Downloading attrs-20.3.0-py2.py3-none-any.whl (49 kB)
     |████████████████████████████████| 49 kB 1.7 MB/s
Collecting cffi>=1.12
  Using cached cffi-1.14.5-cp39-cp39-win_amd64.whl (179 kB)
Collecting pycparser
  Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB)
Collecting idna>=2.0
  Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 4.1 MB/s
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.3-py2.py3-none-any.whl (137 kB)
     |████████████████████████████████| 137 kB 2.2 MB/s
Building wheels for collected packages: yarl, pycares
  Building wheel for yarl (setup.py) ... done
  Created wheel for yarl: filename=yarl-1.1.0-py3-none-any.whl size=82046 sha256=b1bc542bc0977addfc726260535a42e01aa46858250b612106503c0af95699b3
  Stored in directory: c:\users\shortarrow\appdata\local\pip\cache\wheels\ae\a8\19\e66dfb6563e74625d382ec9bc692a841a4e84970f142b96d20
  Building wheel for pycares (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'C:\tools\miniconda3\envs\ccxt-env\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\shortarrow\\AppData\\Local\\Temp\\pip-install-8y0p_z89\\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\\setup.py'"'"'; __file__='"'"'C:\\Users\\shortarrow\\AppData\\Local\\Temp\\pip-install-8y0p_z89\\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\shortarrow\AppData\Local\Temp\pip-wheel-6frqz6_r'
       cwd: C:\Users\shortarrow\AppData\Local\Temp\pip-install-8y0p_z89\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\
  Complete output (17 lines):
  running bdist_wheel
  running build
  creating build
  creating build\lib.win-amd64-3.9
  creating build\lib.win-amd64-3.9\pycares
  copying src\pycares\errno.py -> build\lib.win-amd64-3.9\pycares
  copying src\pycares\utils.py -> build\lib.win-amd64-3.9\pycares
  copying src\pycares\_version.py -> build\lib.win-amd64-3.9\pycares
  copying src\pycares\__init__.py -> build\lib.win-amd64-3.9\pycares
  copying src\pycares\__main__.py -> build\lib.win-amd64-3.9\pycares
  running build_ext
  generating cffi module 'build\\temp.win-amd64-3.9\\Release\\_cares.c'
  creating build\temp.win-amd64-3.9
  creating build\temp.win-amd64-3.9\Release
  building '_cares' extension
  error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
  ----------------------------------------
  ERROR: Failed building wheel for pycares
  Running setup.py clean for pycares
Successfully built yarl
Failed to build pycares
Installing collected packages: pycparser, multidict, idna, cffi, yarl, urllib3, typing-extensions, pycares, chardet, attrs, async-timeout, requests, cryptography, aiohttp, aiodns, ccxt
    Running setup.py install for pycares ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:\tools\miniconda3\envs\ccxt-env\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\shortarrow\\AppData\\Local\\Temp\\pip-install-8y0p_z89\\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\\setup.py'"'"'; __file__='"'"'C:\\Users\\shortarrow\\AppData\\Local\\Temp\\pip-install-8y0p_z89\\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\shortarrow\AppData\Local\Temp\pip-record-jjg8pulo\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\tools\miniconda3\envs\ccxt-env\Include\pycares'
         cwd: C:\Users\shortarrow\AppData\Local\Temp\pip-install-8y0p_z89\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\
    Complete output (17 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.9
    creating build\lib.win-amd64-3.9\pycares
    copying src\pycares\errno.py -> build\lib.win-amd64-3.9\pycares
    copying src\pycares\utils.py -> build\lib.win-amd64-3.9\pycares
    copying src\pycares\_version.py -> build\lib.win-amd64-3.9\pycares
    copying src\pycares\__init__.py -> build\lib.win-amd64-3.9\pycares
    copying src\pycares\__main__.py -> build\lib.win-amd64-3.9\pycares
    running build_ext
    generating cffi module 'build\\temp.win-amd64-3.9\\Release\\_cares.c'
    creating build\temp.win-amd64-3.9
    creating build\temp.win-amd64-3.9\Release
    building '_cares' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\tools\miniconda3\envs\ccxt-env\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\shortarrow\\AppData\\Local\\Temp\\pip-install-8y0p_z89\\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\\setup.py'"'"'; __file__='"'"'C:\\Users\\shortarrow\\AppData\\Local\\Temp\\pip-install-8y0p_z89\\pycares_ddd367eca14248eb93dccf8c5a3fcfcc\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\shortarrow\AppData\Local\Temp\pip-record-jjg8pulo\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\tools\miniconda3\envs\ccxt-env\Include\pycares' Check the logs for full command output.

よく見ると、下記のような部分があります。

 error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/

そこで、MSVC++14が要ることが分かります。
ccxtが依存しているライブラリには、インストール時にMSVC++でビルドする必要のあるものが含まれるようです。

MSVC++のインストール

ここにあるらしいですね。→https://visualstudio.microsoft.com/ja/downloads/

そこで、URLを開いてみると、vsとvscode、vs for mac しかないです。
私の環境では以前にvsをインストール済なので、更新作業になります。
インストールしていない人は新規になります。(どうやら、MSVC++単体インストールも可能です。後述します。)

image.png
ここから、vsの好きなエディションのインストーラーをダウンロードして実行します。
image.png
まずは更新作業をします。(またはインストール)
image.png
どうでもいいけどダークモード好きです。目に優しい。
image.png
更新したのにまだ更新があります。もうめんどくさいので後回しにします。
image.png
ここで、Microsoft Build Tools の変更ボタンを押します。
image.png
C++ Build Tools にチェックが入っているか確認します。入ってなければ入れて変更ボタンを押します。
image.png
変更完了です。
image.png
まだ更新があるみたいですが、無視します。めんどくさいので。
image.png
ここで、vsのインストーラーは閉じておきます。もう使いません。さようなら。

CCXTのインストール再挑戦

先ほど開いていた、Anaconda Prompt を開きなおします。
開きなおすのは、MSVC++のPathを反映させるためです。

先ほど作った仮想環境に入ることをお忘れなく。

conda activate ccxt-env

出来たら、インストールコマンドを実行します。

pip install ccxt
実行結果
Collecting ccxt
  Using cached ccxt-1.42.93-py2.py3-none-any.whl (2.1 MB)
Requirement already satisfied: setuptools>=38.5.1 in c:\tools\miniconda3\lib\site-packages (from ccxt) (46.1.3.post20200325)
Requirement already satisfied: requests>=2.18.4 in c:\tools\miniconda3\lib\site-packages (from ccxt) (2.23.0)
Collecting aiohttp<3.8,>=3.7.4; python_version >= "3.5.2"
  Downloading aiohttp-3.7.4.post0-cp37-cp37m-win_amd64.whl (630 kB)
     |████████████████████████████████| 630 kB 2.2 MB/s
Collecting yarl==1.1.0; python_version >= "3.5.2"
  Using cached yarl-1.1.0.tar.gz (156 kB)
Requirement already satisfied: certifi>=2018.1.18 in c:\tools\miniconda3\lib\site-packages (from ccxt) (2020.4.5.1)
Collecting aiodns<2.1,>=1.1.1; python_version >= "3.5.2"
  Using cached aiodns-2.0.0-py2.py3-none-any.whl (4.8 kB)
Requirement already satisfied: cryptography>=2.6.1 in c:\tools\miniconda3\lib\site-packages (from ccxt) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\tools\miniconda3\lib\site-packages (from requests>=2.18.4->ccxt) (1.25.9)
Requirement already satisfied: chardet<4,>=3.0.2 in c:\tools\miniconda3\lib\site-packages (from requests>=2.18.4->ccxt) (3.0.4)
Requirement already satisfied: idna<3,>=2.5 in c:\tools\miniconda3\lib\site-packages (from requests>=2.18.4->ccxt) (2.9)
Collecting attrs>=17.3.0
  Using cached attrs-20.3.0-py2.py3-none-any.whl (49 kB)
Collecting async-timeout<4.0,>=3.0
  Using cached async_timeout-3.0.1-py3-none-any.whl (8.2 kB)
Collecting multidict<7.0,>=4.5
  Downloading multidict-5.1.0-cp37-cp37m-win_amd64.whl (48 kB)
     |████████████████████████████████| 48 kB 1.4 MB/s
Collecting typing-extensions>=3.6.5
  Using cached typing_extensions-3.7.4.3-py3-none-any.whl (22 kB)
Collecting pycares>=3.0.0
  Downloading pycares-3.1.1-cp37-cp37m-win_amd64.whl (70 kB)
     |████████████████████████████████| 70 kB 2.2 MB/s
Requirement already satisfied: cffi!=1.11.3,>=1.8 in c:\tools\miniconda3\lib\site-packages (from cryptography>=2.6.1->ccxt) (1.14.0)
Requirement already satisfied: six>=1.4.1 in c:\tools\miniconda3\lib\site-packages (from cryptography>=2.6.1->ccxt) (1.14.0)
Requirement already satisfied: pycparser in c:\tools\miniconda3\lib\site-packages (from cffi!=1.11.3,>=1.8->cryptography>=2.6.1->ccxt) (2.20)
Building wheels for collected packages: yarl
  Building wheel for yarl (setup.py) ... done
  Created wheel for yarl: filename=yarl-1.1.0-cp37-cp37m-win_amd64.whl size=121310 sha256=9a761fcd5d4e0d722fd147fe032ed738bae81142261523364c1f0909617354d3
  Stored in directory: c:\users\shortarrow\appdata\local\pip\cache\wheels\d5\e1\3d\448a768a8baff7feee1e5babfeadfa073fb01c6121fa4adc92
Successfully built yarl
Installing collected packages: attrs, multidict, yarl, async-timeout, typing-extensions, aiohttp, pycares, aiodns, ccxt
Successfully installed aiodns-2.0.0 aiohttp-3.7.4.post0 async-timeout-3.0.1 attrs-20.3.0 ccxt-1.42.93 multidict-5.1.0 pycares-3.1.1 typing-extensions-3.7.4.3 yarl-1.1.0

エラーもなく正常にインストールできたようです。

インストール完了の確認

本当にインストールできたのでしょうか?
先ほどのAnaconda Prompt で、「pipでインストールしたパッケージの一覧」を表示するコマンドを実行します。

pip list
実行結果
Package                Version
---------------------- -------------------
aiodns                 2.0.0
aiohttp                3.7.4.post0
async-timeout          3.0.1
attrs                  20.3.0
brotlipy               0.7.0
ccxt                   1.42.93
certifi                2020.4.5.1
cffi                   1.14.0
chardet                3.0.4
conda                  4.8.3
conda-package-handling 1.6.0
cryptography           2.8
idna                   2.9
menuinst               1.4.16
multidict              5.1.0
pip                    20.0.2
pycares                3.1.1
pycosat                0.6.3
pycparser              2.20
pyOpenSSL              19.1.0
PySocks                1.7.1
pywin32                227
requests               2.23.0
ruamel-yaml            0.15.80
setuptools             46.1.3.post20200325
six                    1.14.0
tqdm                   4.45.0
typing-extensions      3.7.4.3
urllib3                1.25.9
vboxapi                1.0
wheel                  0.34.2
win-inet-pton          1.1.0
wincertstore           0.2
yarl                   1.1.0

この実行結果から、先ほどインストールしたccxtを探します。

見つかりました。ccxtの最新バージョンは1.42.93なようですね。

ccxt                   1.42.93

インポートできるか確認

vscodeでインテリセンスが効くか試しました。

image.png

これで、大丈夫!

MSVC++を単体インストールしたい

このページの下の方に、

image.png
image.png

Visual Studio 2019 のツールという部分があります。

これを広げると、Build Tools for Visual Studio 2019 があるので、ダウンロードします。
image.png

この先はVSをインストール済みの場合と同様です。

まとめ

さてここからbotによる資金運用の準備をすすめていきます。

質問などありましたら、ぜひコメントください!
いいねされたらPHP版・nodejs版の記事も書くかも!

Excelsior!

1
8
1

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
1
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?