![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
このガイドでは、Coingecko APIを活用する暗号バックテストツールを構築します。これにより、さまざまな取引戦略を簡単にテストできます。
This guide will help you build a crypto backtesting tool using the CoinGecko API. With this tool, you can easily test various trading strategies, ranging from simple price-based approaches like buying the dip to more complex strategies utilizing technical analysis and indicators.
このガイドは、Coingecko APIを使用して暗号バックテストツールを構築するのに役立ちます。このツールを使用すると、DIPを購入するなどの簡単な価格ベースのアプローチから、テクニカル分析と指標を利用するより複雑な戦略まで、さまざまな取引戦略を簡単にテストできます。
As always, you'll find a link to the GitHub repository at the end of the article, allowing you to dive right in and start experimenting.
いつものように、記事の最後にGitHubリポジトリへのリンクがあり、すぐに飛び込み、実験を開始できます。
What is Crypto Backtesting?
暗号のバックテストとは何ですか?
In the world of trading, crypto backtesting refers to the process of evaluating a trading strategy using historical market data to assess how it would have performed in the past.
取引の世界では、Cryptoのバックテストとは、過去にどのように実行されるかを評価するために、過去の市場データを使用して取引戦略を評価するプロセスを指します。
This enables traders to gauge the profitability, risk, and overall effectiveness of a strategy before deploying it in live trading. By simulating trades based on past data, traders can refine their approach, identify potential weaknesses, and gain confidence in their strategy without risking real money.
これにより、トレーダーは、ライブ取引に展開する前に、戦略の収益性、リスク、および全体的な有効性を測定できます。過去のデータに基づいて取引をシミュレートすることにより、トレーダーはアプローチを改良し、潜在的な弱点を特定し、実際のお金を危険にさらすことなく戦略に対する自信を得ることができます。
Pre-requisites
前提条件
Before we start building our crypto backtesting tool, we'll need the following:
暗号バックテストツールの構築を開始する前に、次のことが必要です。
To obtain a CoinGecko API key, head over to the Developer’s Dashboard and click on +Add New Key in the top right corner. For detailed instructions on generating and setting up your key, refer to this guide.
Coingecko APIキーを取得するには、開発者のダッシュボードにアクセスして、右上隅に新しいキーを追加します。キーの生成とセットアップに関する詳細な手順については、このガイドを参照してください。
We'll be using the OHLC Chart within Time Range endpoint to fetch historical prices, which is available on the Analyst plan and above. For a free alternative, you may use this endpoint instead. The only difference is that, on the Demo endpoint, you cannot specify a time range.
Time Range Endpoint内のOHLCチャートを使用して、アナリストプランおよび上記で利用できる履歴価格を取得します。無料の代替品の場合、代わりにこのエンドポイントを使用できます。唯一の違いは、デモのエンドポイントで、時間範囲を指定できないことです。
Step 1. Set Up Your Environment
ステップ1。環境を設定します
To get started, create an empty directory, which will serve as the root of your project. Within the root directory, let's create a new virtual environment that will allow us to install our requirements locally, without making any changes to the global Python environment.
開始するには、プロジェクトのルートとして機能する空のディレクトリを作成します。ルートディレクトリ内で、グローバルなPython環境に変更を加えることなく、ローカルに要件をインストールできる新しい仮想環境を作成しましょう。
Let’s now configure our Python application. Run the following commands to create and activate your environment:
次に、Pythonアプリケーションを構成しましょう。次のコマンドを実行して、環境を作成およびアクティブにします。
If you're using VS Code, your IDE may also ask you if you’d like to use the local Python compiler – choose yes.
VSコードを使用している場合、IDEはローカルPythonコンパイラを使用するかどうかを尋ねることもできます。[はい]を選択してください。
Installing Requirements
要件のインストール
We’re now ready to install our project’s requirements. The easiest way to do this is by copying the file below to your root directory in a file called requirements.txt and then running pip install -r requirements.txt.
これで、プロジェクトの要件をインストールする準備が整いました。これを行う最も簡単な方法は、以下のファイルを要件と呼ばれるファイルのルートディレクトリにコピーし、PIPインストール-R要件を実行することです。
Installing Ta-Lib (Optional)
TA-LIBのインストール(オプション)
There is one more requirement that we need to install: ta-lib. This is a fantastic Python library for calculating indicator values from raw data. Unlike the requirements above, ta-lib requires us to use the release files and build the package ourselves, otherwise, it will error during installation.
インストールする必要があるもう1つの要件があります:ta-lib。これは、生データからインジケータ値を計算するための素晴らしいPythonライブラリです。上記の要件とは異なり、TA-LIBでは、リリースファイルを使用して自分でパッケージを構築する必要があります。そうしないと、インストール中にエラーが発生します。
Head over to the project’s release page and select a version that matches your OS, CPU architecture, and Python version. For instance, I’m running 64-bit Windows 11 with Python 3.11 and an x86 CPU architecture. The correct release for me was ta_lib-0.6.0-cp311-cp311-win_amd64.whl.
プロジェクトのリリースページにアクセスして、OS、CPUアーキテクチャ、Pythonバージョンに一致するバージョンを選択します。たとえば、Python 3.11とX86 CPUアーキテクチャで64ビットWindows 11を実行しています。私にとって正しいリリースはTA_LIB-0.6.0-CP311-CP311-WIN_AMD64.WHLでした。
To run this on a Macbook with Python 3.11 and an M1 chip or higher, you may use the following release: ta_lib-0.6.0-cp311-cp311-win_arm64.whl. Once you have downloaded the correct version for your machine, drop the file inside your project root. From your project root, install the package using the file you just downloaded.
これをPython 3.11およびM1チップ以上のMacBookで実行するには、次のリリースを使用できます:TA_LIB-0.6.0-CP311-CP311-WIN_ARM64.WHL。マシン用の正しいバージョンをダウンロードしたら、プロジェクトルート内にファイルをドロップします。プロジェクトルートから、ダウンロードしたばかりのファイルを使用してパッケージをインストールします。
For instance: pip install ta_lib-0.6.0-cp311-cp311-win_amd64.whl. This should take care of all the project requirements.
たとえば、PIPインストールTA_LIB-0.6.0-CP311-CP311-WIN_AMD64.WHL。これは、すべてのプロジェクト要件を処理する必要があります。
Create project scaffold
プロジェクトの足場を作成します
Inside your root directory, create the services and utils directories, alongside an empty .env file and an empty main.py file. It should look like so:
ルートディレクトリ内で、空の.envファイルと空のmain.pyファイルとともに、サービスとutilsディレクトリを作成します。そうするはずです:
Inside your .env file, define a variable called CG_API_KEY and assign your CoinGecko API key as its value. We’ll use this to securely load the key into our app, without hardcoding it in the project files.
.ENVファイル内で、CG_API_KEYと呼ばれる変数を定義し、Coingecko APIキーをその値として割り当てます。これを使用して、プロジェクトファイルにハードコードすることなく、キーをアプリに安全にロードします。
Step 2. Defining Utilities
ステップ2。ユーティリティの定義
Inside the utils directory that we defined, create a file called load_env.py. This will help us load our API key and define any additional configuration options that we may have.
定義したutilsディレクトリ内で、load_env.pyというファイルを作成します。これにより、APIキーをロードし、追加の追加の構成オプションを定義できます。
Note that in addition to our API Key, which we’re storing in cg_api_key, we’ve also defined some basic strategy settings such as a take_profit, a stop_loss, an order size, and a total_amount.
CG_API_KEYに保存しているAPIキーに加えて、Take_Profit、STOP_LOSS、注文サイズ、Total_Amountなどの基本的な戦略設定も定義していることに注意してください。
Feel free to adjust these settings to suit your needs, and play around with different settings during backtesting to find the best combination of stop loss and take profit for your strategy.
お客様のニーズに合わせてこれらの設定を調整し、バックテスト中にさまざまな設定で遊んで、ストップロスの最良の組み合わせを見つけ、戦略に利益を得てください。
The backtesting library can behave unpredictably if the input amount is much smaller than the asset's price. To avoid this, we've set the amount high enough to prevent issues.
入力量が資産の価格よりもはるかに少ない場合、バックテストライブラリは予測不可能に動作できます。これを避けるために、問題を防ぐのに十分な量を設定しました。
免責事項:info@kdj.com
提供される情報は取引に関するアドバイスではありません。 kdj.com は、この記事で提供される情報に基づいて行われた投資に対して一切の責任を負いません。暗号通貨は変動性が高いため、十分な調査を行った上で慎重に投資することを強くお勧めします。
このウェブサイトで使用されているコンテンツが著作権を侵害していると思われる場合は、直ちに当社 (info@kdj.com) までご連絡ください。速やかに削除させていただきます。