1.6 KiB
1.6 KiB
Pine Script to Python 通用转换规则
1. 数据结构映射
| Pine Script | Python (pandas) | 说明 |
|---|---|---|
close |
df["Close"] |
收盘价序列 |
open |
df["Open"] |
开盘价序列 |
high |
df["High"] |
最高价序列 |
low |
df["Low"] |
最低价序列 |
volume |
df["Volume"] |
成交量序列 |
time |
df.index (datetime index) |
时间戳 |
2. 常用指标函数映射
移动平均 (Moving Averages)
| Pine Script | Python (pandas-ta) | 说明 |
|---|---|---|
ta.sma(close, length) |
ta.sma(df["Close"], length=5) |
简单移动平均 |
ta.ema(close, length) |
ta.ema(df["Close"], length=20) |
指数移动平均 |
ta.wma(close, length) |
ta.wma(df["Close"], length=10) |
加权移动平均 |
动量指标 (Momentum)
| Pine Script | Python (pandas-ta) | 说明 |
|---|---|---|
ta.rsi(close, length) |
ta.rsi(df["Close"], length=14) |
相对强弱指标 |
ta.macd(close, fast, slow, signal) |
ta.macd(df["Close"], fast=12, slow=26, signal=9) |
MACD指标 |
3. 变量和状态管理
- Pine Script 是向量式语言,每根K线执行一次
- Python 使用 pandas 进行向量化操作,效率更高
- 对于需要状态记忆的变量(如跨K线的累加值),在 pandas 中使用
cumsum()、shift()等方法
4. 推荐库
- pandas: 数据处理和分析
- pandas-ta: Pine Script风格的技术指标库
- numpy: 数值计算
- yfinance: 获取历史数据(可选)
- matplotlib: 可视化(可选)