vn.py,全称为VNPY,是一款基于Python的股票量化交易平台。它为用户提供了丰富的API接口,使得用户可以方便地开发、测试和运行量化交易策略。本文将详细介绍vn.py的使用方法,包括其基本功能、策略开发、回测和实盘交易等,旨在帮助读者掌握交易核心技巧。

一、vn.py概述

1.1 vn.py的特点

  • 跨平台:支持Windows、Linux和Mac操作系统。
  • 易用性:Python编程语言简洁易学,vn.py提供了丰富的API,降低了量化交易的学习门槛。
  • 高性能:vn.py底层采用C++编写,保证了交易速度和稳定性。
  • 社区活跃:vn.py拥有一个活跃的社区,用户可以互相交流和学习。

1.2 vn.py的组成

  • vnpy:vn.py的核心库,提供API接口。
  • vn.py.app:vn.py的图形化界面,用于监控交易数据、展示图表等。
  • vn.py.backtesting:vn.py的回测库,用于测试和优化交易策略。

二、vn.py基本功能

2.1 账户管理

vn.py支持连接多个券商账户,实现多账户交易。

from vnpy.api import *

acc = VnAccount()
acc.connect("tcp://127.0.0.1:9999", "your_account_id", "your_password")

2.2 市场数据

vn.py提供实时行情、历史行情、交易数据等功能。

from vnpy.api import *

md = VnMarketData()
md.subscribe("sh000001", "1min")
md.on Tick = onTick

def onTick(data):
    print(data)

2.3 交易执行

vn.py支持市价单、限价单、条件单等多种交易方式。

from vnpy.api import *

trade = VnTrade()
trade.connect("tcp://127.0.0.1:9999", "your_account_id", "your_password")

order = trade.create_order()
order.symbol = "sh000001"
order.price = 10.00
order.volume = 100
order.direction = Direction.Long
order.offset = Offset.Open
trade.send_order(order)

三、策略开发

3.1 策略框架

vn.py采用事件驱动的方式实现策略开发,策略框架如下:

  • 策略类:继承自vnpy.app.cta_strategy.CtaTemplate类。
  • 策略方法:包括on_initon_starton_stopon_tickon_bar等,用于处理不同事件。

3.2 策略示例

以下是一个简单的趋势跟踪策略示例:

from vnpy.app.cta_strategy import *

class TrendFollowingStrategy(CtaTemplate):
    author = "your_name"

    def __init__(self):
        self.short_pos = 0
        self.long_pos = 0

    def on_init(self):
        self.load_bar(10)

    def on_start(self):
        self.put_event()

    def on_stop(self):
        self.close_all()

    def on_tick(self, tick):
        self.update_tick_bar(tick)

    def on_bar(self, bar):
        self.calculate_position(bar)

        if self.short_pos > 0 and bar.close_price < bar.open_price:
            self.close_position(bar, Direction.Short, bar.close_price)

        if self.long_pos > 0 and bar.close_price > bar.open_price:
            self.close_position(bar, Direction.Long, bar.close_price)

        if bar.close_price > self.short_price and self.short_pos > 0:
            self.close_position(bar, Direction.Short, bar.close_price)

        if bar.close_price < self.long_price and self.long_pos > 0:
            self.close_position(bar, Direction.Long, bar.close_price)

    def calculate_position(self, bar):
        if bar.close_price > self.long_price:
            self.long_price = bar.close_price
        elif bar.close_price < self.short_price:
            self.short_price = bar.close_price

        if bar.close_price > self.long_price + self.trend_threshold:
            self.long_pos = 1
            self.short_pos = 0
        elif bar.close_price < self.short_price - self.trend_threshold:
            self.short_pos = 1
            self.long_pos = 0
        else:
            self.long_pos = 0
            self.short_pos = 0

    def close_position(self, bar, direction, price):
        if direction == Direction.Long:
            order = self.buy(bar.close_price, bar.volume)
            self.close_order(order)
        elif direction == Direction.Short:
            order = self.sell(bar.close_price, bar.volume)
            self.close_order(order)

3.3 策略优化

vn.py提供了回测和优化工具,帮助用户优化策略。

from vnpy.app.cta_strategy import *

# 设置回测参数
start_time = "20210101"
end_time = "20210131"
freq = "1d"
init_cash = 1000000
slippage = 0.0001
commission_ratio = 0.0001

# 运行回测
engine = Engine()
engine.run_backtesting(TrendFollowingStrategy, start_time, end_time, freq, init_cash, slippage, commission_ratio)

四、实盘交易

4.1 实盘前准备

  • 注册券商账户并开通量化交易权限。
  • 安装vn.py软件并连接券商。
  • 开发、测试和优化策略。

4.2 实盘注意事项

  • 合理设置仓位和止损止盈。
  • 注意资金管理和风险控制。
  • 关注市场变化,及时调整策略。

五、总结

vn.py是一款功能强大的股票量化交易平台,可以帮助用户开发、测试和运行量化交易策略。通过本文的介绍,读者可以了解vn.py的基本功能、策略开发、回测和实盘交易等方面的知识,为掌握交易核心技巧奠定基础。在实际应用中,读者应根据自身需求,不断学习和实践,提高量化交易水平。