myTestFreqAI/dryrun.sh
2025-12-02 12:50:12 +08:00

233 lines
7.0 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 检查 .env 文件
if [ ! -f ".env" ]; then
echo "⚠️ 本地缺少 .env 文件,请创建并配置。示例内容如下:"
echo ""
echo "STRATEGY_NAME=FreqaiPrimer"
echo "CONFIG_FILE=basic.json"
echo ""
exit 1
fi
# 加载 .env 文件中的变量
if [ -f .env ]; then
set -a
source .env
set +a
fi
# 设置默认值
STRATEGY_NAME=${STRATEGY_NAME:-FreqaiPrimer}
CONFIG_FILE=${CONFIG_FILE:-basic.json}
# Function to extract the value of a parameter
get_param_value() {
local param="$1"
shift
local args=("$@")
local i=0
while [ $i -lt ${#args[@]} ]; do
case "${args[$i]}" in
$param=*)
echo "${args[$i]#*=}"
return 0
;;
$param)
# Check if the next argument exists and does not start with a dash
if [ $((i + 1)) -lt ${#args[@]} ] && [[ "${args[$((i + 1))]}" != -* ]]; then
echo "${args[$((i + 1))]}"
return 0
fi
echo "PARAM_WITHOUT_VALUE"
return 0
;;
esac
i=$((i + 1))
done
echo ""
return 0
}
# Check if we have named parameters
HAS_NAMED_PARAMS=false
for arg in "$@"; do
if [[ "$arg" == --* ]]; then
HAS_NAMED_PARAMS=true
break
fi
done
# Initialize variables
PAIRS_ARG=""
STRATEGY_ARG=""
CONFIG_ARG=""
# Parse parameters
if [ "$HAS_NAMED_PARAMS" = true ]; then
PAIRS_ARG=$(get_param_value "--pairs" "$@")
STRATEGY_ARG=$(get_param_value "--strategy" "$@")
if [ -z "$STRATEGY_ARG" ]; then
STRATEGY_ARG=$(get_param_value "-t" "$@")
fi
CONFIG_ARG=$(get_param_value "--config" "$@")
else
if [ $# -gt 0 ]; then
PAIRS_ARG="$1"
fi
if [ $# -gt 1 ]; then
STRATEGY_ARG="$2"
fi
if [ $# -gt 2 ]; then
CONFIG_ARG="$3"
fi
fi
# 处理交易对参数
if [ -n "$PAIRS_ARG" ]; then
PAIRS_FLAG="--pairs $PAIRS_ARG"
echo "Using pairs from parameter: $PAIRS_ARG"
else
# 使用默认的交易对列表
DEFAULT_PAIRS="BTC/USDT TON/USDT DOT/USDT XRP/USDT OKB/USDT SOL/USDT DOGE/USDT WCT/USDT TRUMP/USDT SUI/USDT PEPE/USDT TRB/USDT MASK/USDT UNI/USDT KAITO/USDT"
PAIRS_FLAG="--pairs $DEFAULT_PAIRS"
echo "No pairs parameter provided, using default pairs: $DEFAULT_PAIRS"
fi
# 如果命令行提供了有效的策略参数,覆盖.env文件设置
if [ -n "$STRATEGY_ARG" ] && [ "$STRATEGY_ARG" != "PARAM_WITHOUT_VALUE" ]; then
STRATEGY_CLASS_NAME="$STRATEGY_ARG"
STRATEGY_NAME="$STRATEGY_CLASS_NAME"
echo "Overriding strategy with command line parameter: $STRATEGY_CLASS_NAME"
# 自动匹配策略对应的配置文件(使用小写)
STRATEGY_CONFIG_LOWER="$(echo "$STRATEGY_ARG" | tr '[:upper:]' '[:lower:]').json"
echo "Checking auto-matched config file: freqtrade/config_examples/$STRATEGY_CONFIG_LOWER"
# 检查小写配置文件
if [ -f "./freqtrade/config_examples/$STRATEGY_CONFIG_LOWER" ]; then
CONFIG_FILE="$STRATEGY_CONFIG_LOWER"
echo "Auto-matched config file for strategy: $CONFIG_FILE"
else
echo "Warning: Auto-matched config file '$STRATEGY_CONFIG_LOWER' not found in config_examples directory"
echo "Using current config: $CONFIG_FILE"
fi
fi
# 如果命令行提供了配置参数,覆盖.env文件设置
if [ -n "$CONFIG_ARG" ]; then
CONFIG_FILE="$CONFIG_ARG"
echo "Overriding config with command line parameter: $CONFIG_FILE"
fi
# 检查指定的配置文件是否存在
echo "Checking config file: freqtrade/config_examples/$CONFIG_FILE"
if [ ! -f "./freqtrade/config_examples/$CONFIG_FILE" ]; then
echo "Warning: Config file freqtrade/config_examples/$CONFIG_FILE not found, using default configuration"
CONFIG_FILE="basic.json"
else
echo "Config file found: freqtrade/config_examples/$CONFIG_FILE"
fi
# 最后确认STRATEGY_NAME有值
if [ -z "$STRATEGY_NAME" ]; then
echo "Warning: STRATEGY_NAME is empty, using default FreqaiPrimer"
STRATEGY_NAME="FreqaiPrimer"
fi
# 确保STRATEGY_CLASS_NAME有值
if [ -z "$STRATEGY_CLASS_NAME" ]; then
STRATEGY_CLASS_NAME="$STRATEGY_NAME"
fi
# 在参数处理完成后输出最终使用的值
echo "Using strategy: $STRATEGY_NAME"
echo "Using config: $CONFIG_FILE"
cd ../
source .venv/bin/activate
# 杀死现有的 freqtrade-dryrun-* 容器
echo "Cleaning up existing freqtrade-dryrun-* containers..."
docker ps -a | grep 'freqtrade-dryrun-' | awk '{print $1}' | xargs -r docker kill 2>/dev/null || true
docker ps -a | grep 'freqtrade-dryrun-' | awk '{print $1}' | xargs -r docker rm 2>/dev/null || true
# 如果指定了 --pairs 参数,务态修改配置文件中的 pair_whitelist
if [ -n "$PAIRS_ARG" ]; then
echo "Updating pair_whitelist in config file with: $PAIRS_ARG"
TEMP_CONFIG="/tmp/freqtrade_config_$$.json"
# 使用 Python 修改 JSON 文件中的 pair_whitelist
python3 << EOF
import json
import os
# 读取原始配置文件
config_path = './freqtrade/config_examples/$CONFIG_FILE'
if not os.path.exists(config_path):
config_path = '../freqtrade/config_examples/$CONFIG_FILE'
with open(config_path, 'r') as f:
config = json.load(f)
# 解析 --pairs 参数中的币对(以空格分隔)
pairs = "$PAIRS_ARG".split()
# 更新 exchange 字段中的 pair_whitelist
if 'exchange' not in config:
config['exchange'] = {}
config['exchange']['pair_whitelist'] = pairs
print(f"Updated exchange.pair_whitelist to: {pairs}")
# 写入临时配置文件
with open('$TEMP_CONFIG', 'w') as f:
json.dump(config, f, indent=4)
print(f"Modified config saved to: $TEMP_CONFIG")
EOF
# 使用 volume 将临时配置文件挂载到容器
# 由于 freqtrade 加载多个配置文件时,列表类型字段会被完全覆盖,
# 我们需要确保两个配置文件都有相同的配置结构。
# 因此,我们在临时配置中只保留关键的修改字段,不包括 pairlists
echo "docker-compose run --rm -v $TEMP_CONFIG:/freqtrade/config_examples/$CONFIG_FILE freqtrade trade \
--logfile /freqtrade/user_data/logs/freqtrade.log \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--strategy $STRATEGY_CLASS_NAME \
--strategy-path /freqtrade/templates \
--dry-run"
docker-compose run --rm -v $TEMP_CONFIG:/freqtrade/config_examples/$CONFIG_FILE freqtrade trade \
--logfile /freqtrade/user_data/logs/freqtrade.log \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--strategy $STRATEGY_CLASS_NAME \
--strategy-path /freqtrade/templates \
--dry-run
# 清理临时文件
rm -f $TEMP_CONFIG
else
# 没有指定 --pairs使用原始配置文件
echo "docker-compose run --rm freqtrade trade \
--logfile /freqtrade/user_data/logs/freqtrade.log \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--strategy $STRATEGY_CLASS_NAME \
--strategy-path /freqtrade/templates \
--dry-run"
docker-compose run --rm freqtrade trade \
--logfile /freqtrade/user_data/logs/freqtrade.log \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--strategy $STRATEGY_CLASS_NAME \
--strategy-path /freqtrade/templates \
--dry-run
fi