市值: $3.3681T 1.190%
成交额(24h): $82.0486B 24.680%
  • 市值: $3.3681T 1.190%
  • 成交额(24h): $82.0486B 24.680%
  • 恐惧与贪婪指数:
  • 市值: $3.3681T 1.190%
加密货币
话题
百科
资讯
加密话题
视频
热门新闻
加密货币
话题
百科
资讯
加密话题
视频
bitcoin
bitcoin

$109408.092997 USD

1.15%

ethereum
ethereum

$2576.759001 USD

2.43%

tether
tether

$1.000278 USD

0.00%

xrp
xrp

$2.276102 USD

2.70%

bnb
bnb

$662.328194 USD

1.09%

solana
solana

$152.320048 USD

3.54%

usd-coin
usd-coin

$1.000060 USD

0.00%

tron
tron

$0.288181 USD

1.62%

dogecoin
dogecoin

$0.173414 USD

5.90%

cardano
cardano

$0.590629 USD

3.17%

hyperliquid
hyperliquid

$39.996344 USD

1.59%

sui
sui

$2.935392 USD

1.32%

bitcoin-cash
bitcoin-cash

$499.091118 USD

2.48%

chainlink
chainlink

$13.620152 USD

3.25%

unus-sed-leo
unus-sed-leo

$9.048157 USD

0.09%

加密货币新闻

加密与Python进行回测:逐步指南

2025/02/21 16:06

在本指南中,我们将构建一个由Coingecko API的加密进行回测工具,这使得可以轻松测试各种交易策略

加密与Python进行回测:逐步指南

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.

在交易世界中,加密进行回测是指使用历史市场数据评估交易策略的过程,以评估其过去的表现。

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.

我们将在时间范围端点内使用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.

我们现在准备安装项目的要求。最简单的方法是将下面的文件复制到名为unignts.txt的文件中,然后运行pip install -r unigess.txt。

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.

我们还有一个需要安装的要求: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.

转到项目的发布页面,然后选择与您的操作系统,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.6.0-CP311-CP311-WIN_ARM64.WHL。为计算机下载了正确的版本后,将文件放在项目根中。从您的项目root中,使用您刚下载的文件安装软件包。

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:

在您的根目录内部,创建服务和UTILS目录,以及一个空的.env文件和一个空的main.py文件。看起来应该如此:

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),我们将及时删除。

2025年07月07日 发表的其他文章