Freqtrade 是一個自由和開源的Python 編寫的加密貨幣交易機器人。 Freqtrade的目的是透過 Telegram 支援所有主要的交換和控制。 它包含回溯測試、繪圖和資金管理工具,以及機器學習的策略最佳化。Freqtrade並不是一個單一的Python庫,Freqtrade用Python編寫,是Python交易應用程式的應用例項。Freqtrade支援多種個主要的交易所,能讓您設定自定義交易機器人,並提供與Telegram的整合。
本文整理了在Windows 10的環境下,安裝freqtrade的流程、設置自己的freqtrade,啟動執行和監管等部分,如下:
事先準備帳號,包括交易帳號(本文用幣安)和Telegram 帳號和創建 telegram 的 bot (略)
(A)【安裝Windows 的 Docker Desktop】,然後啟動Docker
(B)【安裝freqtrade】
(C)【啟動freqtrade和監管】
------- 技術內容,可略讀。簡單做安裝、使用可直接跳至 下文(A)及(B)(C)處 -------
freqtrade 必備的環境:如下,(單擊讀取安裝指南) 【Windows上可以安Docker後,執行freqtrade的image即可】
- Python >= 3.6.x
- pip
- git
- virtualenv(virtual envirnoment虛擬環境)Virtualenv is a tool to create isolated Python environments. Virtualenv is a CLI tool that needs a Python interpreter to run.
python -m pip install --user virtualenv - TA-Lib
C. 安裝 freqtrade (包含輔助管理工具,如:Docker)安裝 Docker windows desktop然後(也可以)用Docker來安裝freqtrade,或在command line用手動。安裝 Docker 後,只需創建配置文件config.json,然後創建 Docker image。以freqtrade使用此 repo 中的 Dockerfile。
D.設置自己的freqtrade機器人,除了config.json檔中的帳號變數換成自己的帳號;另外最主要的是strategy的設定(要改python檔strategy.py)。
E. 運作及監管
---------------- 簡介相關的軟體工具 -----------------------
通常交易機器人(freqtrade) 會24/7 全天候上線運行,上傳交易機器人到虛擬服務器 (VPS)是運行交易機器人的最佳選擇,使用雲主機運行有很多好處,一個缺點是要付費。另一種可以考慮的作法是:若要將 freqtrade 放在到自己的 Windows 系統來運行中,強烈建議 Windows 用戶使用Docker,會更容易和更順暢也更安全。
什麼是Docker
屬於作業系統層面的虛擬化技術,對程序進行封裝隔離。由於隔離的程序獨立於Host和其它的隔離的程序,因此也稱其為容器。Docker 在容器的基礎上,進行了進一步的封裝,從檔案系統、網路互聯到程序隔離等等,極大的簡化了容器的建立和維護,使得 Docker 技術比虛擬機器技術更為輕便、快捷。Dokcer實際是Host主機的一個普通的程序,這也是Dokcer與傳統虛擬化技術的最大不同。
安裝 docker要安裝 使用 WSL2(Windows Subsystem for LinuxWindows 子系統 Linux 版第2版)來設定適用于 Windows 的 Docker Desktop 提供開發環境,藉由啟用 WSL 2 型引擎,您可以在同一部電腦上執行 Docker Desktop 中的 Linux 和 Windows 容器。(在 WSL 2 上開始使用 Docker 遠端容器) 。安裝 docker可參考docker(連結)文件和docker-compose(連結)官方文檔來安裝Docker,(可詳本文下方【安裝Docker】)。Docker Compose則是一個用於定義和運行多容器 Docker 應用程序的工具(Docker Compose 包含在在 Docker Desktop for Windows桌面的安裝中)。
Compose 專案是 Docker 官方的開源專案,負責實現對 Docker 容器叢集的快速編排。使用前面介紹的Dockerfile我們很容易定義一個單獨的應用容器。然而在日常開發工作中,經常會碰到需要多個容器相互配合來完成某項任務的情況。例如要實現一個 Web 專案,除了 Web 服務容器本身,往往還需要再加上後端的資料庫服務容器;再比如在分散式應用一般包含若干個服務,每個服務一般都會部署多個例項。如果每個服務都要手動啟停,那麼效率之低、維護量之大可想而知。這時候就需要一個工具能夠管理一組相關聯的的應用容器,這就是Docker Compose。
Compose有2個重要的概念:
專案(Project):由一組關聯的應用容器組成的一個完整業務單元,在 docker-compose.yml 檔案中定義。
服務(Service):一個應用的容器,實際上可以包括若干執行相同映象的容器例項。
Docker-Compose,只要寫一個 docker-compose.yml,把所有要使用 Docker Image 寫上去,另外也可以把 Container 之間的關係連結(link)起來,最後只要下 docker-compose up 指令,就可以把所有的 Docker Container 執行起來,這樣就可以很快速和方便的啟動多個 container。
了解Docker 架構
Docker使用client-server架構。Docker客戶端與Docker服務器對話,服務器主機端負責構建、運行和分發 Docker 容器的繁重工作。Docker 客戶端和主機端可以在同一系統上運行,也可以將 Docker 客戶端連接到遠程 Docker 主機程序。Docker 客戶端和主機程序使用 REST API,通過 UNIX 套接字或網絡接口進行通信。另一種是使用Docker Compose Docker做客戶端 ,Docker Compose Docker允許您使用由一組containers組成的應用程序。
(credit: Docker Docs 連結)

Docker components and commands
Image
image: 是一個唯讀的模板,用來建立 container,用物件化的思維: image可以想成是 class;container是instance。您可以創建自己的image,也可以只使用其他人創建並在registry表中發布的image。要構建您自己的鏡像,您需要使用簡單的語法創建一個Dockerfile ,用於定義創建和運行鏡像所需的步驟。Dockerfile 中的每條指令都會在映像中創建一個層。當您更改 Dockerfile 並重建映像時,僅重建那些已更改的層。與其他虛擬化技術相比,這是使映像如此輕量、小巧和快速的部分原因。
Container
container是Image的可運行實例。Container 是透過 Docker image 執行起來的 Process,同一個 Docker image 可以啟動多個 Docker Container;container是一個獨立、隔離的空間,裡面包括了我們執行一個應用程式所需要的元件。可以透過 Docker API 或 CLI 創建、啟動、停止、移動或刪除container。也可以將container連接到一個或多個網絡,將存儲附加到它,甚至可以根據其當前狀態創建新映像。container由其映像以及您在創建或啟動它時提供給它的任何配置選項定義。當container被移除時,任何未存儲在持久存儲中的狀態更改都會消失。
Registry: 這其實就很像 github,裡面存放了很多 images,公開的像是 Docker Hub,你也可以建立自己私人的 registry。
----- 【Windows上可以安Docker後,執行freqtrade的image即可】 ----
(A)【安裝Windows 的 Docker Desktop】,然後啟動Docker
等待片刻,當鯨魚圖示靜止時, Docker便已成功啟動。之後你可以打開 PowerShell 來使用 Docker。使用介面可安裝Microsoft 的 Windows Terminal 终端介面 (安裝 Windows 終端機)。當您開啟 Windows 終端機時,它會以PowerShell命令列作為 [開啟] 索引標籤中的預設設定檔來啟動。

Screenshot of Windows Terminal Settings default profile
(B)【安裝freqtrade】
接這在PS(就是power shell,上圖的命列列中) 輸入執行下列指令:
(Freqtrade 在 Dockerhub 上提供了一個官方的 Docker 鏡像,以及一個可供使用的docker -compose 文件、下載官方 FreqTrade docker 鏡像,創建一個新目錄並將docker-compose (.yml)文件檔放在此目錄中。)
mkdir ft_userdata
cd ft_userdata/
# Download the docker-compose file from the repository
curl https://raw.githubusercontent.com/freqtrade/freqtrade/stable/docker-compose.yml -o docker-compose.yml
# Pull the freqtrade image
docker-compose pull
# Create user directory structure
docker-compose run --rm freqtrade create-userdir --userdir user_data
# Create configuration - Requires answering interactive questions
docker-compose run --rm freqtrade new-config --config user_data/config.json
上面的指令創建了一個名為 的新目錄ft_userdata,下載最新的 compose 文件並拉取 freqtrade image檔案。代碼中的最後 2 個步驟使用user_data以及選擇預設的配置創建目錄。
【輸入config.json自訂設置】(Telegram bot 和 Chat id,請參考這裡 <取得 Chat ID> <建立Telegram Bot 機器人>)
然後,從Widows Docker dshboard中察看,如下:
編輯Freqtrade的配置
從Freqtrade的GitHub中獲取複製json檔。將其粘貼到文字編輯器,並將檔保存為config.json。接著打開config.json,找到這兩行:
•“max_open_trades”:10,
• “stake_amount”:"unlimited",
(也可以依據需要設定為“stake_amount”: 100)
這二行是機器人設置將可於交易的數量。因此在此示例中,它進行了10個交易。你可以根據需要上下調整這兩個數字,需確保有足夠的資產來執行機器人交易。可以使用上述配置在user_data/config.json(在目錄中)隨時編輯配置。還可以通過編輯文件的命令部分來更改策略和命令docker-compose.yml。
添加自定義策略
- 該配置現在可用作
user_data/config.json - 將自定義策略複製到目錄
user_data/strategies/ - 將策略的類(class)名稱添加到
docker-compose.yml文件中。注意:是class名稱,而不是python策略檔案的名稱。記得查看python內容將class名稱拷貝到docker-compose.yml檔案中。
(C)【啟動freqtrade和監管機器人】¶
您可以使用docker-compose ps. 這應該將服務freqtrade列為running。
日誌紀錄到:user_data/logs/freqtrade.log。用命令docker-compose logs -f ,便可以查看日誌
然後,就可以下指令 docker-compose up -d ,啟動機器人。
到TG中察看結果,如下圖:
(D)使用 docker-compose 更新 freqtrade¶
更新時,使用時freqtradedocker-compose下 2 個命令,拉取最新的鏡像,然後使用剛剛拉取的版本重新啟動容器:
# Download the latest image
docker-compose pull
# Restart the image
docker-compose up -d
------------以下為參考資源 -------------
【參考】在Docker運行freqtrade -> https://www.freqtrade.io/en/stable/docker_quickstart/
接著登錄到Binance帳戶,進入到並按API設置按鈕。為你的新API命名為“加密幣交易機器人”或類似的東西,以便日後一件便知道知道該金鑰的用途內容。然後,Binance將引導你完成設置的一系列步驟,過程中記得,要同時勾選「讀取資訊」和「啟用交易」,並取消「啟用取款」選項。將該API金要設定成只做交易用途,而無法用於出金。並將API鑰匙,複製並保存在安全的地方。如果丟失金鑰,因為你將永遠不會再看到金鑰,只能再次重做此流程,取得新的API金鑰。
現在返回config.json檔並更改以下內容:
• 名稱需要從“bittrex”改為“binance”(如果是要在幣安使用機器人交易)
• 將“your_exchange_key”替換為來自Binance的API金鑰
• 用來自Binance的金鑰替換“your_exchange_secret”
接著設定pair_whitelist和pair_blacklist。pair_whitelist名單是機器人所監看的貨幣數目;建議使用10。至於pair_blacklist,則只需要有BNB / BTC(因為機器人與Binance混淆使用BNB來折扣費用)。
用Telegram來與freqtrade互動
現在設定檔的下一個重要部分是Telegram部分。這將允許你從機器人獲取更新並發送命令,如停止/啟動等。
首先在Telegram上尋找@BotFather,使用/ start,/ newbot,並按照其說明創建新的Telegram機器人。複製Telegram機器人的HTTP API權杖。並貼製替換“your_telegram_token”一欄。
下一條訊息指令@get_id_bot with / start,取得聊天ID,並將聊天ID複製、粘貼至替換“your_telegram_chat_id”一欄。這摩一來,你的Telegram機器人只會回復你的聊天ID,而忽略其他ID的訊息。
如果一切正常,你應該在Telegram上收到你的機器人已啟動的消息。現在,它將開始掃描選定的貨幣以進行移動,並在適當時進行買賣。
Telegram RPC commands
Telegram is not mandatory. However, this is a great way to control your bot.
/start: Starts the trader./stop: Stops the trader./stopbuy: Stop entering new trades./status <trade_id>|[table]: Lists all or specific open trades./profit [<n>]: Lists cumulative profit from all finished trades, over the last n days./forcesell <trade_id>|all: Instantly sells the given trade (Ignoringminimum_roi)./performance: Show performance of each finished trade grouped by pair/balance: Show account balance per currency./daily <n>: Shows profit or loss per day, over the last n days./help: Show help message./version: Show version.
使用Telegram,創建設定Telegram bot,More details and the full command list on 參考連結:https://www.freqtrade.io/en/latest/telegram-usage/
【自己設定Telegram上的按鈕】Create a custom keyboard (command shortcut buttons)¶
Telegram allows us to create a custom keyboard with buttons for commands. The default custom keyboard looks like this.
[
["/daily", "/profit", "/balance"], # row 1, 3 commands
["/status", "/status table", "/performance"], # row 2, 3 commands
["/count", "/start", "/stop", "/help"] # row 3, 4 commands
]
Usage¶
You can create your own keyboard in config.json:
"telegram": {
"enabled": true,
"token": "your_telegram_token",
"chat_id": "your_telegram_chat_id",
"keyboard": [
["/daily", "/stats", "/balance", "/profit"],
["/status table", "/performance"],
["/reload_config", "/count", "/logs"]
]
},
Supported Commands
Only the following commands are allowed. Command arguments are not supported!
/start, /stop, /status, /status table, /trades, /profit, /performance, /daily, /stats, /count, /locks, /balance, /stopbuy, /reload_config, /show_config, /logs, /whitelist, /blacklist, /edge, /help, /version
Telegram commands¶
Per default, the Telegram bot shows predefined commands. Some commands are only available by sending them to the bot. The table below list the official commands. You can ask at any moment for help with /help.
| Command | Description |
|---|---|
/start |
Starts the trader |
/stop |
Stops the trader |
/stopbuy |
Stops the trader from opening new trades. Gracefully closes open trades according to their rules. |
/reload_config |
Reloads the configuration file |
/show_config |
Shows part of the current configuration with relevant settings to operation |
/logs [limit] |
Show last log messages. |
/status |
Lists all open trades |
/status <trade_id> |
Lists one or more specific trade. Separate multiple <trade_id style="box-sizing: inherit; margin-top: 0px; margin-bottom: 0px;">with a blank space. |
/status table |
List all open trades in a table format. Pending buy orders are marked with an asterisk () Pending sell orders are marked with a double asterisk (*) |
/trades [limit] |
List all recently closed trades in a table format. |
/delete <trade_id> |
Delete a specific trade from the Database. Tries to close open orders. Requires manual handling of this trade on the exchange. |
/count |
Displays number of trades used and available |
/locks |
Show currently locked pairs. |
/unlock <pair or lock_id> |
Remove the lock for this pair (or for this lock id). |
/profit [<n>] |
Display a summary of your profit/loss from close trades and some stats about your performance, over the last n days (all trades by default) |
/forcesell <trade_id> |
Instantly sells the given trade (Ignoring minimum_roi). |
/forcesell all |
Instantly sells all open trades (Ignoring minimum_roi). |
/forcebuy <pair> [rate] |
Instantly buys the given pair. Rate is optional and only applies to limit orders. (forcebuy_enable must be set to True) |
/performance |
Show performance of each finished trade grouped by pair |
/balance |
Show account balance per currency |
/daily <n> |
Shows profit or loss per day, over the last n days (n defaults to 7) |
/weekly <n> |
Shows profit or loss per week, over the last n weeks (n defaults to 8) |
/monthly <n> |
Shows profit or loss per month, over the last n months (n defaults to 6) |
/stats |
Shows Wins / losses by Sell reason as well as Avg. holding durations for buys and sells |
/whitelist |
Show the current whitelist |
/blacklist [pair] |
Show the current blacklist, or adds a pair to the blacklist. |
/edge |
Show validated pairs by Edge if it is enabled. |
/help |
Show help message |
/version |
Show version |
【安裝Docker】Windows 上建議使用Docker(Install Docker Desktop on Windows)
Download Docker Desktop for Windows
在 Windows 上安裝 Docker Desktop
雙擊Docker Desktop Installer.exe運行安裝程序。
如果您尚未下載安裝程序 ( Docker Desktop Installer.exe),您可以從 Docker Hub獲取它。它通常會下載到您的Downloads文件夾中,或者您可以從 Web 瀏覽器底部的最近下載欄中運行它。
出現提示時,確保在配置頁面上選擇啟用 Hyper-V Windows 功能或為 WSL 2 安裝所需的 Windows 組件選項。
按照安裝嚮導上的說明授權安裝程序並繼續安裝。安裝成功後,單擊關閉以完成安裝過程。
如果您的管理員帳戶與您的用戶帳戶不同,則必須將用戶添加到docker-users組。以管理員身份運行計算機管理並導航到本地用戶和組>組> docker-users。右鍵單擊以將用戶添加到組。註銷並重新登錄以使更改生效。
Docker for windows 10 安裝初體驗 (連結)
如果您需要管理多個進程,您也許可以考慮在安裝一個簡易的流程管理器,如:Monit 或 Supervisor。協助你容器進行更複雜的任務。如果您的應用程序停止(如果它乾淨退出或崩潰),而不是通過 Docker 獲取該信息,您將不得不從您的進程管理器獲取它。
【參考】
https://www.freqtrade.io/en/0.18.1/installation/ (安裝freqtrade)
https://www.freqtrade.io/en/stable/bot-basics/
https://github.com/freqtrade/freqtrade
https://medium.datadriveninvestor.com/automated-trading-bots-bca341fc7d6e (自動交易機器人 freqtrade )
WSL2 需不需要 Windows Docker Desktop
【相關指令】
透過 docker 下載,安裝來自 github 存儲庫中需要的檔案文件。如下:
Clone the git repository
git clone https://github.com/freqtrade/freqtrade.git
Windows with docker
git clone --config core.autocrlf=input https://github.com/freqtrade/freqtrade.git
Copy config.json.example to config.json
cd freqtrade
cp -n config.json.example config.json
Create your database file
touch tradesv3.sqlite ```` Dry-Run ```bash touch tradesv3.dryrun.sqlite
Build your own Docker image
docker build -t freqtrade -f Dockerfile.technical
docker build -f Dockerfile.develop -t freqtrade-dev
Verify the Docker image
docker images
Run the Docker image
docker run --rm -v `pwd`/config.json:/freqtrade/config.json -it freqtrade
【Freqtrade 教學影片(英文)】
【資源檔案】
Strategy repository
How to test a strategy?
Let assume you have selected the strategy strategy001.py:
Simple backtesting
freqtrade backtesting --strategy Strategy001
Refresh your test data
freqtrade download-data --days 100
Note: Generally, it's recommended to use static backtest data (from a defined period of time) for comparable results.
Please check out the official backtesting documentation for more information.





