量化投资学习【常见策略】海龟交易系统

著名的商品投机家理查德.丹尼斯相信,伟大的交易员是后天培养的,他可以教会人们成为伟大的交易员。1983年,他挑选了13个人,对他们进行培训,并为他们提供真实的账户进行交易,希望证明自己的论断。他教给这13个学员一套完整的交易系统,由于学员们被称为“海龟”,后来人们就把这套系统称为海龟交易系统。下面将介绍这套交易系统的具体实现。

1.基本原理
原版的海龟交易系统包括市场(买什么),头寸规模(买卖多少),入市(何时买入),止损(何时退出亏损的头寸),离市(何时退出赢利的头寸),策略(下限价指令还是市价指令),本策略的大框架是按照原版的海龟策略搭建的,但是在具体实现方面有些改动。
市场:本策略中为简化起见,选取了一只股票,中国平安
头寸规模:本策略中最大头寸规模限制为4unit,关于unit的计算详见后文的策略流程
入市:策略中有两个入市系统,系统一和系统二
止损:海龟策略使用以价格为基础的止损条件,当股价比买入时股价低2N时将持有的头寸清空
离市:在当前股价比day_out天以来的最低价还低时把持有的头寸清空
策略:原版海龟交易策略是执行限价指令以避免对实际市场价格造成大的波动,本策略中由于是采用以前的数据进行回测,股票价格已经给定,不存在干扰市场的问题,因此都执行的是市价指令

2.变量计算及操作:
由于海龟系统涉及到的变量计算和操作比较多,因此下文把各个变量的计算及一些操作 出来进行说明

N的计算方式:N=(19×PDN+TR)/20,其中,PDN是指前一天的N,
TR(实际范围)=max(H-L,H-PDC,PDC-L)
式中:
H-当日最高价
L-当日最低价
PDC-前个交易日的收盘价
在本策略中,用于迭代的第一个N是采用days天内数据可用的那些天的TR的平均值
Unit的计算方式:unit=帐户净值的1%/(N×每点价值量)
每点价值量 是指一手合约的价格每变动一个最小单位,整个合约变动的价格。在中国股市中,股票价格变动的最小价格一般是0.01元,一手股票是100股,因此每点价值量就是0.01*100=1。
入市:价格形成突破,即当前股票价格>day_in天中股票的最高价格
对于系统一,day_in=short_in,对于系统二,day_in=long_in
对于系统一,如果上一次的突破实现了盈利,那么下一次就忽视掉系统一的突破信号
入市以后的增加单位:如果在持有头寸之后,股票价格在上次突破时的价格的基础上又上涨了0.5N,则再买入一单位股票
注:short_in是系统一入市时的参数,表示取多少天内的股票价格,例如,如果设置short_in=20,则当前股票价格>20天中股票的最高价格时算一次突破。
Long_in是系统二的入市参数,含义同short_in
增加单位:当股票价格>上次突破价格+0.5N时,在原来持有的头寸的基础上继续增加一单位
止损:如果股票价格相对于上次买入的价格下降了2N,则把持有的头寸清仓
离市:如果股票价格小于day_out天来得最低价,则清空持有的头寸
对于系统一,day_out=short_out。对于系统二,day_out=long_out
注:short_out是系统一的离市参数,如果股票价格小于short_out天中股票的最低价,则离市,long__out同理
调整资金规模:在将头寸全部清空之后,判断如果当前资金小于(1-loss)初始资金,则将可调用的资金缩减为adjust当前资金
注:loss,adjust是调整资金规模的参数,loss表示调整资金规模时的损失比例,即如果损失的资金占初始资金得比例达到loss时,需要调整资金规模。
Adjust表示资金规模的调整系数,即每次调整时,将可用资金缩减到adjust*当前资金
注:系统一和系统二是两个独立的系统,这两个系统的资金分配由ratio控制,系统一得到的资金占总资金的比例为ratio,系统二所占比例为1-ratio。此外,在购买股票和清空持有头寸时,两系统也是独立的,清空不是指把所有股票都清空,而是哪个系统需要清空就清空该系统内的股票。
3.海龟策略流程
计算N,unit,
判断是否调整资金规模,如果需要,则进行调整
进行系统一,根据当前价格,系统一中的头寸判断应该入市,还是增加单位,还是止损,还是离市
进行系统二,同样,根据当前价格,系统一中的头寸判断应该入市,还是增加单位,还是止损,还是离市

import numpy as np      
import pandas as pd
from pandas import Series,DataFrame
import random
import math
# 定义一个全局变量, 保存要操作的证券
security='600196.XSHG'
# 设置我们要操作的股票池, 这里我们只操作一支股票
set_universe([security])
set_benchmark('600196.XSHG')
#设置回测条件
set_commission(PerTrade(buy_cost=0.0008, sell_cost=0.0015, min_cost=5))
set_slippage(FixedSlippage(0))
#调整资金规模时的临界损失比例
loss=0.1
#调整资金规模时调整后的资金占当前资金的比例
adjust=0.8
#计算第一个N时取得股票数据的天数
days=20
#系统一入市时股票价格需要高于short_in天内的最高价
short_in=20
#系统二入市时股票价格需要高于long_in天内的最高价
long_in=55
#系统一离市时股票价格需要低于short_out天内的最低价
short_out=10
#系统二离市时股票价格需要低于long_out天内的最低价
long_out=20
#系统一和系统二的资金分配比例,系统一得到ratio*总资金,系统二得到(1-ratio)*总资金
ratio=0.7
#单一市场中的头寸规模限制
limit=4
#记录策略运行了多少天
pdn=0
#记录N值
N=[]
#记录系统一中股票的单位数
sys1=0 
#记录系统二中股票的单位数
sys2=0
#判断操作是对系统一还是系统二,值为‘True’是对系统一,‘False’是对系统二
short='False'
#用unit来保存一单位表示多少股票,默认值为1000
unit=1000
#记录系统一形成突破时的股票价格
break_price1=0 
#记录系统二形成突破时的股票价格
break_price2=0
#记录分钟
minutes=0

#计算股票的N值 
def Calcu_N(context,paused):
    #在策略运行了days-1天时,计算前days-1天的平均实际范围
    if pdn==days-1:
        #取出day-1天来得最高价,最低价,前一天的收盘价
        price=attribute_history(security,days-1,'1d', ('high','low','pre_close'),skip_paused=True)
        #如果不是所有的这day-1天都没有数据,算出这些天的实际范围的平均值
        TR=[]
        for i in range(0,days-1):
            h_l=price['high'][i]-price['low'][i]
            h_pdc=price['high'][i]-price['pre_close'][i]
            pdc_l=price['pre_close'][i]-price['low'][i]
            temp=max(h_l,h_pdc,pdc_l)
            TR.append(temp)
        ATR=np.mean(np.array(TR))
        N.append(ATR)

    #如果策略运行天数已经达到了days天
    else:
        #如果股票停牌,则将运行天数减1
        if paused==True:
            global pdn
            pdn=pdn-1
        #如果未停牌,则利用迭代,计算N值,并保存在列表N中
        else:
            price=attribute_history(security,1,'1d', ('high','low','pre_close'),skip_paused=True)
            h_l=price['high'][0]-price['low'][0]
            h_pdc=price['high'][0]-price['pre_close'][0]
            pdc_l=price['pre_close'][0]-price['low'][0]
            temp=max(h_l,h_pdc,pdc_l)
            TR.append(temp)
            ATR=np.mean(np.array(TR))
            N.append(ATR)

#止损
def Stop_Loss(current_price):
    #如果对系统一操作,则将突破价设置为系统一的突破价,如果是对系统二,则设置为系统二的
    if short=='True':
        break_price=break_price1
    else:
        break_price=break_price2
    #如果当前价格比上次的突破价低2N,则清空头寸
    #并相应的更改相应系统中的股票单位数
    if current_price<break_price-2*N[-1]:
        if short=='True':
            order(security,-sys1)
            global sys1
            sys1=0  
        else:
            order(security,-sys2)
            global sys2 
            sys2=0

#入市
def Sys_In(highest,day_in,context,current_price,cash):
    #取出day_in天以来的最高价
    price=attribute_history(security,day_in,'1d',('high','open'))
    #如果当前价格高于day_in天的最高价,则形成突破
    if current_price>max(price['high']) and current_price>=highest:
        #计算可以买的股票数量
        num_of_shares=cash/current_price
        #如果可以买的数量不小于一单位,且目前持有的股票数量未达到限制的数量,则买入
        if num_of_shares>=unit:
            if short=='True':
                if sys1<int(limit*unit):
                    order(security,+int(unit))
                    #买入后,相应的更新持有的股票数及突破价格
                    global sys1
                    sys1=sys1+int(unit)
                    global break_price1
                    break_price1=current_price
            else:
                if sys2<int(limit*unit):
                    order(security,+int(unit))
                    global sys2
                    sys2=sys2+int(unit)
                    global break_price2
                    break_price2=current_price

#增加单位    
def Sys_Add(day_in,context,current_price,cash):
    #根据short的值判断是对哪个系统操作,以对突破价格赋值
    if short=='True':
        break_price=break_price1
    else:
        break_price=break_price2
    #如果当前价格比上次的突破价格高0.5*N,则增加一单位
    if current_price>=break_price+0.5*N[-1]: 
        num_of_shares=cash/current_price
        if num_of_shares>=unit: 
            if short=='True':
                if sys1<int(limit*unit):
                    order(security,+int(unit))
                    global sys1
                    sys1=sys1+int(unit)
                    global break_price1
                    break_price1=current_price
            else:
                if sys2<int(limit*unit):
                    order(security,+int(unit))
                    global sys2
                    sys2=sys2+int(unit)
                    global break_price2
                    break_price2=current_price

# 离市
def Sys_Out(day_out,current_price,context):
    #取出day_out天以来的最低价
    price=attribute_history(security,day_out,'1d',('high','low'),skip_paused=True)
    #如果股票当前价格比day_out天的最低价低,则清空系统内的头寸
    #并将相应系统的头寸数量置为0
    if current_price<min(price['low']):
        if short=='True':
            if sys1>0:
                order(security,-sys1)
                global sys1
                sys1=0
        else:
            if sys2>0:
                order(security,-sys2)
                global sys2
                sys2=0

#每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次
def handle_data(context, data):
    global minutes
    minutes=minutes+1
    price=attribute_history(security,minutes,'1m',('high','price','open'),skip_paused=True)
    #取得从今天开盘为止的最高价
    highest=max(price['open'])
    #用paused保存股票是否停牌
    paused=data[security].paused
    #用dt保存当前时间
    dt=context.current_dt
    #保存当前股票价格
    current_price=data[security].price
    #保存资产组合的总值
    value=context.portfolio.portfolio_value
    #在每天开市时将策略运行时间加1
    if dt.hour==9 and dt.minute==30:
        global minutes
        minutes=0
        global pdn 
        pdn=pdn+1
        #在运行时间达到days-1天时计算头寸单位
        if pdn==days-1:
            Calcu_N(context,paused)
    #运行时间达到days天时开始执行各种买卖操作
    if pdn>=days:
        if pdn==days:
            global break_price1
            break_price1=current_price*5+1
        #如果股票不停牌
        if paused==False:
            #取得当前现金
            cash=context.portfolio.cash
            #如果空仓了
            if sys1==0 and sys2==0:
                #调整资金规模
                if context.portfolio.portfolio_value<(1-loss)*context.portfolio.starting_cash:
                    cash=adjust*cash
                    value=adjust*value
            #每点价值量(yuan per point)
            ypp=1.0
            #价值量波动性 value volatility
            vv=ypp*N[-1]
            #计算一单位的数量
            global unit
            unit=value*0.01/vv
            #将short置为‘True’,对系统一进行操作
            global short
            short='True'
            #如果系统一没有头寸,并且当前价格比上次的突破价低,入市
            if sys1==0:
                #入市
                Sys_In(highest,short_in,context,current_price,ratio*cash)
            #如果已经有了头寸,则进行止损或者增加单位
            else:
                #止损
                Stop_Loss(current_price)
                #增加单位
                Sys_Add(short_in,context,current_price,ratio*cash)    
            #离市
            Sys_Out(short_out,current_price,context)
            #将short置为‘False’,对系统二进行操作
            global short
            short='False'
            if sys2==0:
                #入市
                Sys_In(highest,long_in,context,current_price,(1-ratio)*cash)
            else:
                Stop_Loss(current_price)
                Sys_Add(long_in,context,current_price,(1-ratio)*cash)
            #离市
            Sys_Out(long_out,current_price,context)   

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ydjcs567/article/details/65028396