量化交易系统开发-实时行情自动化交易-8.11.国信lquant平台

19年创业做过一年的量化交易但没有成功,作为交易系统的开发人员积累了一些经验,最近想重新研究交易系统,一边整理一边写出来一些思考供大家参考,也希望跟做量化的朋友有更多的交流和合作。

接下来会对于国信lquant平台介绍。

LQuant是国信证券推出的量化交易开发平台,提供专业的量化开发工具和实盘交易支持。它集成了Python编程环境、丰富的金融数据接口、强大的回测引擎和策略部署功能,能够满足用户从策略开发到实盘交易的全流程需求。

以下是一个基于LQuant平台的均线交叉策略开发和回测实例,完整展示其功能和应用。


1. 策略背景:双均线交叉策略

策略逻辑
双均线交叉策略是一种经典的趋势跟随型策略,适用于中短期交易:

  • 买入信号:当短期均线(如5日均线)向上穿过长期均线(如20日均线)时,触发买入。
  • 卖出信号:当短期均线向下穿过长期均线时,触发卖出。

适用范围
本策略适用于股票、指数、ETF等趋势明显的品种。


2. 环境配置与数据获取

在LQuant平台开发量化策略,需先设置开发环境并获取数据。

(1)环境初始化

LQuant提供基于Python的开发接口,用户可通过在线IDE进行策略编写。

(2)数据获取

以下代码展示了如何在LQuant中获取目标标的的历史行情数据:

from lquant import LQBacktest

# 初始化回测环境
bt = LQBacktest()
bt.set_universe(['000001.SZ'])  # 设定标的:平安银行
bt.set_benchmark('000300.SH')  # 基准为沪深300指数

# 获取历史行情数据
data = bt.history('000001.SZ', start='2022-01-01', end='2023-01-01', fields=['close'])
print(data.head())  # 输出数据头部

3. 策略开发

在LQuant平台,策略通过回测框架实现。以下代码展示如何实现双均线交叉策略。

(1)策略核心逻辑
# 定义策略逻辑
def strategy(context, data):
    # 设置短期和长期均线窗口
    short_window = 5
    long_window = 20
    
    # 计算均线
    data['short_ma'] = data['close'].rolling(window=short_window).mean()
    data['long_ma'] = data['close'].rolling(window=long_window).mean()

    # 获取当前持仓信息
    current_position = context.get_position('000001.SZ')
    
    # 判断买卖信号
    if data['short_ma'].iloc[-1] > data['long_ma'].iloc[-1] and current_position == 0:
        context.order_target_percent('000001.SZ', 1)  # 全仓买入
        print("买入平安银行")
    elif data['short_ma'].iloc[-1] < data['long_ma'].iloc[-1] and current_position > 0:
        context.order_target_percent('000001.SZ', 0)  # 清仓卖出
        print("卖出平安银行")
(2)回测流程集成

将策略集成到LQuant回测框架中运行:

# 配置回测参数
bt.set_strategy(strategy)  # 设置策略函数
bt.set_cash(100000)  # 初始资金
bt.set_period('2022-01-01', '2023-01-01')  # 回测时间段

# 执行回测
result = bt.run()

4. 回测结果分析

LQuant回测引擎会生成详细的策略报告,包括收益率曲线、交易信号、绩效指标等。

回测结果示例
指标
累计收益率 12.4%
最大回撤 -5.3%
夏普比率 1.25
胜率 60%
可视化展示

LQuant平台支持内置图表工具,也可结合Matplotlib展示策略表现:

import matplotlib.pyplot as plt

# 绘制收益率曲线
plt.figure(figsize=(10, 6))
plt.plot(result['portfolio_value'], label='Strategy')
plt.plot(result['benchmark_value'], label='Benchmark', linestyle='--')
plt.legend()
plt.title('Strategy vs Benchmark Performance')
plt.show()

5. 策略优化

(1)参数调优

通过网格搜索优化短期和长期均线的参数组合:

# 网格搜索优化参数
best_params = None
best_performance = -float('inf')

for short in range(3, 10):
    for long in range(15, 30):
        if short >= long:
            continue
        bt.set_strategy(strategy)
        bt.set_cash(100000)
        bt.run(short_window=short, long_window=long)
        performance = bt.get_performance()

        if performance['total_returns'] > best_performance:
            best_performance = performance['total_returns']
            best_params = (short, long)

print(f"最佳参数组合:短期={best_params[0]}, 长期={best_params[1]}")
(2)加入风险管理

为策略加入止盈止损功能以控制风险:

def strategy_with_risk_control(context, data):
    short_window = 5
    long_window = 20
    stop_loss = 0.05  # 最大亏损5%
    take_profit = 0.10  # 最大收益10%

    data['short_ma'] = data['close'].rolling(window=short_window).mean()
    data['long_ma'] = data['close'].rolling(window=long_window).mean()

    current_position = context.get_position('000001.SZ')
    current_price = data['close'].iloc[-1]

    # 判断止损和止盈
    if current_position > 0:
        entry_price = context.get_position_cost('000001.SZ')
        if (current_price / entry_price - 1) > take_profit or (current_price / entry_price - 1) < -stop_loss:
            context.order_target_percent('000001.SZ', 0)
            print("止盈或止损,平仓卖出")
    
    # 判断均线信号
    if data['short_ma'].iloc[-1] > data['long_ma'].iloc[-1] and current_position == 0:
        context.order_target_percent('000001.SZ', 1)
        print("买入平安银行")
    elif data['short_ma'].iloc[-1] < data['long_ma'].iloc[-1] and current_position > 0:
        context.order_target_percent('000001.SZ', 0)
        print("卖出平安银行")

6. 实盘部署

LQuant支持策略从回测到实盘的无缝切换。实盘部署步骤如下:

  1. 策略上线:将回测完成的策略部署到策略库中。
  2. 绑定账户:绑定国信证券实盘账户。
  3. 启动策略:启动策略后,平台会自动监控并执行交易信号。