trading.ai/CRYPTO_README.md
2025-11-02 10:41:17 +08:00

268 lines
7.8 KiB
Markdown
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.

# 加密货币市场K线形态策略使用指南
本项目已支持将K线形态策略应用于加密货币市场(Binance)可以同时扫描A股和加密货币市场。
## 新增功能
### 1. Binance数据获取器
- **文件**: `src/data/binance_fetcher.py`
- **功能**:
- 获取Binance交易所的K线数据
- 支持多种时间周期(1d, 4h, 1h等)
- 获取热门交易对(按24h交易量排序)
- 支持交易对搜索和筛选
- 自动缓存机制减少API调用
### 2. 加密货币K线形态策略
- **文件**: `src/strategy/crypto_kline_pattern_strategy.py`
- **特点**:
- 继承自原有的`KLinePatternStrategy`
- 适配加密货币市场特点(24小时交易、高波动性)
- 支持USDT、BTC等多种计价货币
- 可配置最小交易量过滤
### 3. 加密货币市场扫描脚本
- **文件**: `crypto_scanner.py`
- **功能**: 扫描Binance热门交易对识别K线形态信号
## 安装依赖
```bash
# 安装python-binance库
pip install -r requirements.txt
```
或单独安装:
```bash
pip install python-binance>=1.0.19
```
## 配置说明
### 1. 编辑配置文件
打开 `config/config.yaml`配置Binance API(可选):
```yaml
data_source:
# Binance API配置(获取公开数据不需要API密钥)
binance_api_key: "" # 可选
binance_api_secret: "" # 可选
binance_testnet: false # 是否使用测试网
```
### 2. 策略参数配置
`config/config.yaml` 中配置加密货币策略参数:
```yaml
strategy:
crypto_kline_pattern:
enabled: true # 是否启用
min_entity_ratio: 0.55 # 阳线实体最小占比55%
final_yang_min_ratio: 0.40 # 突破阳线实体最小占比40%
max_turnover_ratio: 100.0 # 最大换手率(加密货币较高)
timeframes: ["4hour", "daily", "weekly"] # 时间周期4小时、日线、周线
scan_symbols_count: 100 # 扫描交易对数量
quote_asset: "USDT" # 计价货币
min_volume_usdt: 1000000 # 最小24h交易量(美元)
# 回踩监控配置
pullback_tolerance: 0.02 # 回踩容忍度2%
monitor_days: 30 # 监控天数
pullback_confirmation_days: 7 # 回踩确认天数
```
## 使用方法
### 方法1: 使用启动脚本(推荐)
```bash
# 扫描100个热门交易对
./start_crypto_scanner.sh
# 扫描50个热门交易对
./start_crypto_scanner.sh 50
# 扫描200个热门交易对
./start_crypto_scanner.sh 200
```
### 方法2: 直接运行Python脚本
```bash
# 扫描100个热门交易对(默认)
python crypto_scanner.py
# 扫描指定数量的交易对
python crypto_scanner.py 50
```
### 方法3: 在代码中使用
```python
from src.data.binance_fetcher import BinanceFetcher
from src.strategy.crypto_kline_pattern_strategy import CryptoKLinePatternStrategy
from src.utils.notification import NotificationManager
# 初始化数据获取器
data_fetcher = BinanceFetcher()
# 初始化策略
strategy_config = {
'min_entity_ratio': 0.55,
'timeframes': ['4hour', 'daily', 'weekly'], # 4小时、日线、周线
'quote_asset': 'USDT'
}
notification_config = {'dingtalk': {'enabled': False}}
notification_manager = NotificationManager(notification_config)
strategy = CryptoKLinePatternStrategy(
data_fetcher,
notification_manager,
strategy_config
)
# 扫描市场
results = strategy.scan_market(max_symbols=100)
```
## 定时任务配置
### 使用crontab定时扫描
编辑 `crontab/crypto-scanner` 文件已包含预设的定时任务:
```bash
# 每天早上8点扫描
0 8 * * * root cd /app && python crypto_scanner.py 100
# 每天下午4点扫描
0 16 * * * root cd /app && python crypto_scanner.py 100
# 每天晚上12点扫描
0 0 * * * root cd /app && python crypto_scanner.py 100
# 每4小时扫描(适合4小时K线)
0 */4 * * * root cd /app && python crypto_scanner.py 50
# 周日深度扫描
0 10 * * 0 root cd /app && python crypto_scanner.py 200
```
加载定时任务:
```bash
# 在Docker容器中
crontab crontab/crypto-scanner
# 或在本地系统
crontab -e
# 然后复制crypto-scanner内容
```
## 策略说明
### K线形态识别
与A股策略相同识别"两阳线+阴线+突破阳线"形态:
1. **基础形态**(前3根K线): 阳线 + 阳线 + 阴线
2. **前两根阳线**: 实体部分占振幅55%以上
3. **突破确认**: 第4/5/6根K线中任意一根突破阴线最高价
4. **突破阳线**: 实体占振幅40%以上收盘在EMA20上方
5. **回踩确认**: 价格先创新高,再回踩到阴线最高价附近
### 加密货币市场特点
- **24小时交易**: 没有开盘收盘限制
- **高波动性**: 换手率阈值设置较高(100%)
- **支持多周期**: 4小时线(4h)、日线(1d)、周线(1w)
- **实时数据**: 可以随时获取最新K线数据
## 输出结果
### 日志文件
- 位置: `logs/crypto_scanner_YYYY-MM-DD.log`
- 包含: 详细的扫描过程、信号发现、错误信息
### 数据库存储
扫描结果会自动保存到MySQL数据库:
- 扫描会话信息
- 发现的信号详情
- 形态形成记录
### 钉钉通知
如果配置了钉钉webhook会自动发送:
- 信号汇总通知(每10个信号一组)
- 回踩提醒(每5个提醒一组)
- 包含交易对、价格、形态类型等关键信息
## 常见问题
### 1. 如何选择扫描的交易对数量?
- **日常监控**: 50-100个交易对速度快
- **全面扫描**: 100-200个交易对覆盖更全
- **深度分析**: 200+个交易对,周末或特殊时段
### 2. 需要Binance API密钥吗?
- **不需要**: 获取K线、ticker等公开数据不需要API密钥
- **需要**: 如果要查询账户信息、下单等操作才需要
### 3. 时间周期如何选择?
- **weekly(周线)**: 适合长线信号,信号质量最高,趋势更明确
- **daily(日线)**: 适合中线信号,平衡信号质量和频率
- **4hour(4小时)**: 适合短线交易,信号更频繁,适合波段操作
### 4. 与A股策略有什么区别?
- **数据源**: Binance API vs Tushare API
- **交易时间**: 24小时 vs 交易日限制
- **换手率**: 阈值更高,适应加密货币高波动
- **计价单位**: USDT/BTC vs 人民币
### 5. 如何同时运行A股和加密货币扫描?
两个脚本独立运行,互不影响:
```bash
# A股扫描
./start_market_scanner.sh 200
# 加密货币扫描
./start_crypto_scanner.sh 100
```
## 注意事项
1. **API限制**: Binance对API调用频率有限制建议控制扫描频率
2. **网络连接**: 需要稳定的网络连接访问Binance API
3. **数据准确性**: 加密货币市场波动大,建议结合其他指标综合判断
4. **风险提示**: 本工具仅用于技术分析,不构成投资建议
## 技术架构
```
TradingAI/
├── src/
│ ├── data/
│ │ ├── binance_fetcher.py # Binance数据获取器(新增)
│ │ └── tushare_fetcher.py # A股数据获取器(原有)
│ └── strategy/
│ ├── crypto_kline_pattern_strategy.py # 加密货币策略(新增)
│ └── kline_pattern_strategy.py # A股策略(原有)
├── crypto_scanner.py # 加密货币扫描脚本(新增)
├── market_scanner.py # A股扫描脚本(原有)
├── start_crypto_scanner.sh # 加密货币启动脚本(新增)
├── start_market_scanner.sh # A股启动脚本(原有)
└── crontab/
├── crypto-scanner # 加密货币定时任务(新增)
└── market-scanner # A股定时任务(原有)
```
## 更新日志
### v1.1.0 (2025-10-12)
- ✨ 新增Binance数据获取器
- ✨ 新增加密货币K线形态策略
- ✨ 新增加密货币市场扫描脚本
- ✨ 新增定时任务配置
- 📝 更新配置文件支持加密货币
- 📝 新增使用文档
## 贡献
欢迎提交Issue和Pull Request来改进此功能!
## 许可
与主项目保持一致