myTestFreqAI/tools/backtest.sh
zhangkun9038@dingtalk.com 9c57aea106 revert to 8ba203f2
2025-06-21 12:41:45 +00:00

107 lines
3.2 KiB
Bash
Executable File
Raw 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=TheForceV7"
echo "CONFIG_FILE=basic.json"
echo "TEST_BRANCH=theforce-noai-test"
echo "DRYRUN_BRANCH=theforce-noai-dryrun"
echo ""
exit 1
fi
# 加载 .env 文件中的变量
export $(grep -v '^#' .env | xargs)
# 设置默认值
STRATEGY_NAME=${STRATEGY_NAME:-TheForceV7}
CONFIG_FILE=${CONFIG_FILE:-basic.json}
echo "Using strategy: $STRATEGY_NAME"
echo "Using config: $CONFIG_FILE"
echo "Using testBranch: $TEST_BRANCH"
# Parse command line arguments
START_DATE=${1:-$(date -d "2 days ago" +"%Y%m%d")}
END_DATE=${2:-$(date -d "tomorrow" +"%Y%m%d")}
PARAMS_NAME=$(echo "$STRATEGY_NAME" | tr '[:upper:]' '[:lower:]')
# Parse command line arguments
START_DATE_RAW=${1:-$(date -d "2 days ago" +"%Y-%m-%d %H:%M:%S")}
END_DATE_RAW=${2:-$(date -d "tomorrow" +"%Y-%m-%d %H:%M:%S")}
# 判断是否包含空格,如果是,则按完整时间解析并转为时间戳
if [[ "$START_DATE_RAW" == *" "* ]]; then
START_DATE=$(date -d "$START_DATE_RAW" +"%s")
else
# 兼容旧格式20250616 -> 转换为 2025-06-16 00:00:00 再转为时间戳
START_DATE=$(date -d "${START_DATE_RAW:0:4}-${START_DATE_RAW:4:2}-${START_DATE_RAW:6:2} 00:00:00" +"%s")
fi
if [[ "$END_DATE_RAW" == *" "* ]]; then
END_DATE=$(date -d "$END_DATE_RAW" +"%s")
else
END_DATE=$(date -d "${END_DATE_RAW:0:4}-${END_DATE_RAW:4:2}-${END_DATE_RAW:6:2} 00:00:00" +"%s")
fi
cd ../
source .venv/bin/activate
rm -rf user_data/models/*
rm -rf ./freqtrade/user_data/data/backtest_results/*
rm -fr ./user_data/dryrun_results/*
rm result/*
hyperopt_config="${STRATEGY_NAME%.py}.json"
#docker-compose -f docker-compose_backtest.yml run --rm freqtrade >output.log 2>&1
echo "docker-compose run --rm freqtrade backtesting \
--logfile /freqtrade/user_data/logs/freqtrade.log \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--strategy-path /freqtrade/templates \
--strategy $STRATEGY_NAME \
--timerange $START_DATE-$END_DATE \
--fee 0.0008 \
--cache none >output.log"
#LightGBMRegressorMultiTarget
docker-compose run --rm freqtrade backtesting \
--logfile /freqtrade/user_data/logs/freqtrade.log \
--freqaimodel LightGBMRegressorMultiTarget \
--config /freqtrade/config_examples/$CONFIG_FILE \
--strategy-path /freqtrade/templates \
--strategy $STRATEGY_NAME \
--timerange $START_DATE-$END_DATE \
--fee 0.0008 \
--breakdown day \
--cache none >output.log 2>&1
sed -i 's/\x1B\[[0-9;]*m//g' output.log
#python3 tools/filter.py
rm ./result/*.json -fr
rm ./result/*.py -fr
mv ./user_data/backtest_results/* ./result/
cd ./result
# 查找当前目录下的所有 zip 文件
zip_files=(*.zip)
# 检查是否只有一个 zip 文件
if [ ${#zip_files[@]} -eq 1 ]; then
# 解压缩该 zip 文件到当前目录
unzip "${zip_files[0]}"
rm *.zip
rm *.feather
else
echo "当前目录下没有 zip 文件或者有多个 zip 文件,无法操作。"
fi
cd -
sed -i 's/\x1B\[[0-9;]*m//g' output.log
#python3 ../filter.py
cp output.log result/ -f
cd tools/
python tradestocsv.py
python analytic.py >../result/analytic.log
cd ../