天勤量化写一个趋势类策略
时间: 2023-03-14 22:10:35 浏览: 352
策略是由多种不同的技术指标和投资理念组成的,因此可以根据市场趋势,个人偏好以及投资风格来定制。例如,可以使用技术指标(如移动平均线,布林带等)来识别市场趋势,选择合适的投资机会,并设定合适的止损和止盈点。
相关问题
用天勤sdk写一个双均线策略
好的,以下是使用天勤SDK编写的双均线策略代码:
```python
import tqsdk as ts
api = ts.TqApi()
# 定义K线参数
symbol = "SHFE.cu2109" # 合约代码
duration_seconds = 60 # K线周期
kline_length = 20 # K线长度
# 获取K线数据
klines = api.get_kline_serial(symbol, duration_seconds, data_length=kline_length)
# 定义策略参数
short_period = 5 # 短期均线周期
long_period = 10 # 长期均线周期
# 计算均线
short_ma = api.ma(klines.close, short_period)
long_ma = api.ma(klines.close, long_period)
# 打印均线数据
print("短期均线:", short_ma[-1])
print("长期均线:", long_ma[-1])
# 判断交易信号
if short_ma[-1] > long_ma[-1]:
print("买入信号")
elif short_ma[-1] < long_ma[-1]:
print("卖出信号")
else:
print("观望")
```
以上代码实现了以下功能:
1. 获取上期所铜2109合约最近20根1分钟K线数据。
2. 计算短期(5根K线)和长期(10根K线)均线。
3. 判断当前交易信号:若短期均线上穿长期均线,产生买入信号;若短期均线下穿长期均线,产生卖出信号;否则产生观望信号。
你可以根据需要修改参数和实现更复杂的策略。
天勤量化双均线策略
### 天勤量化双均线策略实现
天勤量化是一款专注于期货市场的程序化交易平台,支持Python脚本编写交易逻辑。以下是基于天勤量化的双均线策略实现代码示例:
#### 1. 策略概述
双均线策略是一种趋势跟踪技术分析方法,通过计算短期和长期的移动平均线来判断市场走势。当短期均线上穿长期均线时发出买入信号;当短期均线下穿长期均线时发出卖出信号。
#### 2. 实现代码
以下是一个完整的天勤量化双均线策略代码示例:
```python
from tqsdk import TqApi, TqSim, TqBacktest, TargetPosTask
# 初始化API对象并设置模拟账户
api = TqApi(TqSim(), backtest=TqBacktest(start_dt="2013-01-01", end_dt="2015-12-31"))
# 定义合约名称
symbol = "DCE.m1701"
# 获取K线数据
klines = api.get_kline_serial(symbol, duration_seconds=24 * 60 * 60)
# 设置均线参数
short_period = 5 # 短周期均线
long_period = 10 # 长周期均线
# 计算短期和长期均线
klines["ma_short"] = klines.close.rolling(short_period).mean()
klines["ma_long"] = klines.close.rolling(long_period).mean()
# 创建目标仓位管理器
target_pos = TargetPosTask(api, symbol)
while True:
api.wait_update()
if api.is_changing(klines.iloc[-1]): # 当最新一根K线发生变化时执行逻辑
ma_short_last = klines.ma_short.iloc[-1]
ma_long_last = klines.ma_long.iloc[-1]
if ma_short_last > ma_long_last and klines.ma_short.iloc[-2] <= klines.ma_long.iloc[-2]:
print(f"金叉:{klines.datetime.iloc[-1]} 发出买入信号")
target_pos.set_target_volume(1) # 开仓一手多单
elif ma_short_last < ma_long_last and klines.ma_short.iloc[-2] >= klines.ma_long.iloc[-2]:
print(f"死叉:{klines.datetime.iloc[-1]} 发出卖出信号")
target_pos.set_target_volume(-1) # 开仓一手空单
# 关闭API连接
api.close()
```
#### 3. 参数说明
- **`TqApi`**: 提供与天勤平台交互的功能接口。
- **`TqSim`**: 使用模拟账户进行测试。
- **`TargetPosTask`**: 自动调整目标持仓数量的任务类。
- **`duration_seconds`**: K线周期,单位为秒(此处设为一天)。
- **`short_period` 和 `long_period`**: 分别表示短期和长期均线的时间窗口长度[^1]。
#### 4. 数据可视化
为了更直观地展示策略效果,在实际应用中可以加入绘图功能。虽然天勤本身不提供内置绘图工具,但可以通过导出数据到外部库完成此操作。例如,利用Matplotlib绘制价格曲线以及两条均线:
```python
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 5))
plt.plot(klines.index[-len(klines):], klines.close[-len(klines):], lw=2, label='Price')
plt.plot(klines.index[-len(klines):], klines.ma_short[-len(klines):], lw=2, ls="--", label=f'MA({short_period})')
plt.plot(klines.index[-len(klines):], klines.ma_long[-len(klines):], lw=2, ls="-.", label=f'MA({long_period})')
buy_points = (klines.ma_short > klines.ma_long) & ~(klines.ma_short.shift() > klines.ma_long.shift())
sell_points = (klines.ma_short < klines.ma_long) & ~(klines.ma_short.shift() < klines.ma_long.shift())
plt.scatter(buy_points[buy_points].index, klines.close[buy_points], marker="^", color="red", s=80, label="Buy Signal")
plt.scatter(sell_points[sell_points].index, klines.close[sell_points], marker="v", color="green", s=80, label="Sell Signal")
plt.legend(loc="upper left")
plt.grid(True)
plt.show()
```
#### 5. 性能评估
根据引用[2]中的回测参数设定,该策略在特定时间段内的表现可能受到市场波动性和手续费成本的影响。因此建议结合具体品种的历史数据进行全面验证后再投入真实资金运行。
---
阅读全文
相关推荐














