![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
在本指南中,我們將構建一個由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.
在交易世界中,加密進行回測是指使用歷史市場數據評估交易策略的過程,以評估其過去的表現。
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),我們將及時刪除。
-
- 恆星(XLM)隨著價格鞏固看漲的旗幟,閃爍混合信號
- 2025-02-22 12:40:24
- XLM的技術設置,鏈指標和市場情緒表明,Altcoin可能會爆出牛市,並集會到0.51美元。
-
-
- 密碼蓋:阿根廷總統哈維爾·米利(Javier Milei
- 2025-02-22 12:40:24
- 阿根廷布宜諾斯艾利斯 - 阿根廷總統賈維爾·米利(Javier Milei)正面臨著腐敗的調查
-
- 揭示激增:製造商(MKR)令牌令人驚訝的上升
- 2025-02-22 12:40:24
- 儘管加密舞台上有震顫,但最近幾天震驚的攀登使製造商(MKR)代幣猛增了44%。
-
-
-
- 現在購買的前5個模因硬幣,可以在2025年提供大量回報
- 2025-02-22 12:30:25
- 模因硬幣以在所有加密貨幣中獲得一些最大的收益而聞名,但是找到新興的代幣並不總是那麼容易。
-
- 佛羅里達婦女涉嫌從前男友那裡偷走了80萬美元的“特朗普”加密貨幣
- 2025-02-22 12:30:25
- 安東尼·布拉沃(Anthony Bravo
-
- XRP設置為市場動態中的飛漲?發現周圍丘腦的嗡嗡聲!
- 2025-02-22 12:30:25
- 當寒意的微風在加密景觀上滾動時,XRP的前景突然被陽光普照。特朗普總統的低聲浪潮的耳語