Quantopian 做空恐慌指数回测

《做空恐慌指数Python回测》 上一篇文章谈到自己用Python来做回测,我们可以借助一些成熟的量化回测平台来回测自己的策略,比自己写的更加容易能发现更多问题。

1.具体代码

"""
Short VIXY base on how much it rise from bottom
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
 
def initialize(context):
    """
    Called once at the start of the algorithm.
    """   
    context.vxx = symbol('VIXY')
    #1.2 means rise 20% from bottom
    context.shortSthartPercent = 1.2
    #how many position to short
    context.shortPositionPercent = -0.25
    #how much percent profit when to cover short position
    context.coverPricePercent = 0.15
    
    context.beforeShortPrice = 0
    context.minPrice = 100
    context.prePrice = 0
    # Before Market 3 minute do action
    schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_close(minutes=3))
     
 
def my_rebalance(context,data):
    """
    Execute orders according to our schedule_function() timing. 
    """
    today_price = data.history(context.vxx, "price", 1, "1m")[-1]
    #split check
    if today_price > context.prePrice * 2:
        context.minPrice = context.minPrice * 5
        context.beforeShortPrice = context.beforeShortPrice * 5
    if today_price < context.minPrice:
        context.minPrice = today_price
    if today_price >= context.minPrice * context.shortSthartPercent and context.portfolio.positions[context.vxx].amount == 0:
        if data.can_trade(context.vxx):
            order_target_percent(context.vxx, context.shortPositionPercent)
        context.beforeShortPrice = today_price
    if context.portfolio.positions[context.vxx].amount != 0 and today_price <= context.beforeShortPrice * (1-context.coverPricePercent):
        if data.can_trade(context.vxx):
            order_target_percent(context.vxx, 0)
        
    context.prePrice = today_price    
    pass

2.回测结果

Quantopian 做空恐慌指数回测_第1张图片

3. 局限性

首先是不推荐把这个策略用于实盘的,因为做空VIXY或者做空一些股票是有局限性的。局限性如下:

  1. 证券商有权限随时平仓做空仓位,事后通知
  2. 非常时期做空利率非常高,高达20%或更多
  3. 做空比做多更快吞噬保证金,做空杠杆ETF会更加快吞噬保证金。杠杆ETF上涨100%的情况还是容易出现

本文只能算是一个Quantopian上的一个小练习,不作为投资依据。

你可能感兴趣的:(量化投资)