- OKX交易自动化 (okx-auto-position, okx-crypto, okx-exchange) - 交易信号处理 (signal-confirmation-templates, trading-signal-aggregator) - 量化因子挖掘 (quant-factor-mining) - 长桥集成 (longbridge-cli, longbridge-python-sdk) - 六合彩分析 (lottery-hk) - 股息投资 (dividend-investing, dividend-scanner) - 日内交易 (intraday-trading) - 同花顺 (tonghuashun)
69 lines
2.0 KiB
Markdown
69 lines
2.0 KiB
Markdown
# Trend Analysis for Position Decisions
|
|
|
|
Use EMA12/EMA26 slope on 4H candles to determine if position direction is correct.
|
|
|
|
## Algorithm
|
|
|
|
```python
|
|
def calc_ema(closes, period):
|
|
if len(closes) < period:
|
|
return closes[-1]
|
|
multiplier = 2 / (period + 1)
|
|
ema = closes[0]
|
|
for price in closes[1:]:
|
|
ema = (price - ema) * multiplier + ema
|
|
return ema
|
|
|
|
def analyze_trend(inst_id):
|
|
# Get 4H candles
|
|
candles = get_candles(inst_id, "4H", 30)
|
|
closes = [c.close for c in candles]
|
|
|
|
ema12 = calc_ema(closes[-12:], 12)
|
|
ema26 = calc_ema(closes[-26:], 26)
|
|
|
|
slope = (ema12 - ema26) / ema26 * 100
|
|
|
|
if slope > 0.5: return 'strong_up'
|
|
if slope < -0.5: return 'strong_down'
|
|
if abs(slope) < 0.1: return 'ranging'
|
|
return 'weak_trend'
|
|
```
|
|
|
|
## Position Decision Rules
|
|
|
|
| 趋势 | 做多持仓 | 做空持仓 |
|
|
|------|----------|----------|
|
|
| strong_up | ✅ 持有 | ❌ 平仓 |
|
|
| weak_up | ✅ 持有 | ⚠️ 观察 |
|
|
| ranging | ⚠️ 观察 | ⚠️ 观察 |
|
|
| weak_down | ⚠️ 观察 | ✅ 持有 |
|
|
| strong_down | ❌ 平仓 | ✅ 持有 |
|
|
|
|
## Decision Flow
|
|
|
|
```
|
|
信号/定期检查
|
|
↓
|
|
分析趋势 (EMA12 vs EMA26)
|
|
↓
|
|
├─ 趋势正确 + 保证金充足 → 加仓
|
|
├─ 趋势正确 + 保证金不足 → 持有
|
|
├─ 趋势错误 → 平仓
|
|
└─ 无趋势 → 观察或平仓
|
|
```
|
|
|
|
## Real Example (2026-07-02)
|
|
|
|
| 币种 | 方向 | EMA12 | EMA26 | 斜率 | 趋势 | 决定 |
|
|
|------|------|-------|-------|------|------|------|
|
|
| ETH | 🟩多 | 1646.37 | 1616.26 | +1.86% | strong_up | ✅ 持有 |
|
|
| BTC | 🟥空 | 60567 | 60090 | +0.79% | strong_up | ❌ 平仓 |
|
|
| SNDK | 🟩多 | 1961.92 | 2029.02 | -3.31% | strong_down | ❌ 平仓 |
|
|
| SKHYNIX | 🟩多 | 1513.35 | 1605.48 | -5.74% | strong_down | ❌ 平仓 |
|
|
| MU | 🟩多 | 1032.60 | 1075.03 | -3.95% | strong_down | ❌ 平仓 |
|
|
| HYPE | 🟥空 | 64.90 | 64.29 | +0.96% | strong_up | ❌ 平仓 |
|
|
| SOL | 🟥空 | 78.81 | 76.28 | +3.31% | strong_up | ❌ 平仓 |
|
|
|
|
Result: Closed 6 incorrect positions, kept ETH (trend correct).
|