基本交易策略I

基本交易策略I


目录

1 均线策略

1.1 C.J.Neely, P.A.Weller, Technical analysis in the foreign exchange market, Working Paper, Federal Reserve of St.Louis,2011.

1) 策略

2) 收益率回测

1.2 Valeriy Zakamulin, Market timing with moving averages,2017

1) 移动平均的几种类型

I 基本类

i. 简单移动平均 Simple Moving Average(SMA)

ii. 线性移动平均 Linear Moving Average(LMA)

iii. 指数移动平均 Exponential Moving Average(EMA)

II 进阶:平均中的平均 Moving Averages of Moving Averages

i. 三角移动平均 Triangular Moving Average

ii.双/三指数平滑 Double and Triple Exponential Smoothing

III 混合移动平均 Double Exponential Moving Average (DEMA), by Mulloy (1994a)

2) 均线策略

i. 动量法则 Momentum Rule

ii.均线策略 Moving Average Change of Direction Rule

iii. Price Minus Moving Average Rule

iv. 均线交叉 Moving Average Crossover Rule (MAC)

v.多均线策略

vi.均线收敛/发散规则 Moving Average Convergence/Divergence Rule

1.3 Valeriy Zakamulin, A Comprehensive Look at the Empirical Performance of Moving Average Trading Strategies, SSRN, Dec. 11,2015

说明:1.3和1.2基本一致,故略

1.4 F. Papailias, D.D. Thomakos, An improved moving average technical trading rule, Physica A 428(2015),458-469

属于对均线策略的优化,不知这个优化策略是否权威,所以暂时没有复制算法

2 创新高策略

用到的交易指标基本都是wind接口可以直接获取的数据


正文

说明:设我们获取到的数据集命名为为df。

1 均线策略

1.1 C.J.Neely, P.A.Weller, Technical analysis in the foreign exchange market, Working Paper, Federal Reserve of St.Louis,2011.

An MA rule compares a short and a long moving average of past prices and generates a buy (sell) signal if the short moving average intersects the long moving average from below (above). For example, one widely used rule, which we write as MA(5, 20), compares a 5-day and a 20-day moving average.

1) 策略

import pandas as pd
import numpy as np
import math


## 均线构建 (参数n为均线构建期间)
def MA(n):
    df['MA'+str(n)] = pd.rolling_mean(df['close'], n)
    print(df.head())

MA(5)  # 5日均线
MA(20) # 20日均线


## 多空信号
lst_signal=[]
df['signal']=df['MA5']-df['MA20']
for i in df['signal']:
    if i>0:
        lst_signal.append(1)   # 多头信号
    else:
        lst_signal.append(0)   # 空头信号

df['signal']=lst_signal

2)收益率回测

%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2009.52.06.png

%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2009.52.15.png

%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2009.52.21.png

Therefore, the cumulative excess return, r , for a trading strategy over the period from time zero to time T is given by
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2009.52.27.png

# 构建指标z
lst_z=[]
for j in df['signal']:
    if i=1:
        lst_z.append(1)   # 多头信号
    else:
        lst_z.append(-1)   # 空头信号

df['z']=lst_z

# 构建指标R
s_t1=pd.Series(df['close'].values).shift(1)
df['s_t1']=list(s_t1)   # 超前变量的设置,用shift函数
df['R_t1']=df['s_t1']/df['close']*df['i_']/df['i']
df['log_s_t1']=[math.log(i) for i in df['s_t1']]
df['log_s']=[math.log(j) for j in df['close']]
df['log_i_']=[math.log(i+1) for i in df['i_']]
df['log_i']=[math.log(j+1) for j in df['i']]
df['r_t1']=df['log_s_t1']-df['log_s']+df['log_i_']-df['log_i']
r=sum(df['z']*df['r+1'])+ math.log((1-c)/(1+c))    # r为交易期间使用策略获得的累计超额收益
1.2 Valeriy Zakamulin, Market timing with moving averages,2017

很详细的一本文献,详尽地介绍了关于均线的一切。

1) 移动平均的几种类型

I 基本类

i. 简单移动平均 Simple Moving Average(SMA)
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2010.31.04.png

# 简单移动平均 SMA
def SMA(n):
    df['SMA'+str(n)] = pd.rolling_mean(df['close'], n)
    print(df.head())

ii. 线性移动平均 Linear Moving Average(LMA)
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2010.33.53.png

# 线性移动平均 LMA
def LMA(n):
    LMA=0
    for i in range(5):
        p=pd.Series(df['close'].values).shift(i)
        LMA=(5-i)*p+LMA
        df['LMA'+str(5)]=list(LMA*2/(1+5)/5)

LMA(n)
df.head(10)

iii. 指数移动平均 Exponential Moving Average(EMA)

%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2010.51.06.png

# 指数移动平均 EMA
def EMA(n):
    df['EMA'+str(n)] = pd.ewma(df['close'], n)
    print(df.head())

II 进阶:平均中的平均 Moving Averages of Moving Averages

i. 三角移动平均 Triangular Moving Average

%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.03.35.png
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2010.59.08.png

# 三角移动平均
def TMA(n):
    df['TMA'+str(n)] = pd.rolling_mean(df['SMA'+str(n)], n)
    print(df.head())

ii.双/三指数平滑 Double and Triple Exponential Smoothing

双指数:EMA(EMA(n))
三指数:EMA(EMA(EMA(n)))

# 双指数平均dEMA
def dEMA(n):
    df['dEMA'+str(n)] = pd.ewma(df['EMA'+str(n)], n)
    print(df.head())

# 三指数平均tEMA
def tEMA(n):
    df['tEMA'+str(n)] = pd.ewma(df['dEMA'+str(n)], n)
    print(df.head())    

III 混合移动平均
Double Exponential Moving Average (DEMA), by Mulloy (1994a)
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.08.43.png

Triple Exponential Moving Average (TEMA), by Mulloy (1994b)
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.08.50.png

# DEMA
def DEMA(n):
    df['DEMA'+str(n)] = 2*df['EMA'+str(n)] - df['dEMA'+str(n)]
    print(df.head())

# TEMA
def TEMA(n):
    df['TEMA'+str(n)] = 3*df['EMA'+str(n)] - 3*df['dEMA'+str(n)]+df['tEMA'+str(n)] 
    print(df.head())

2) 均线策略

一般说来,策略的形成有两个步骤

step1:At the first step, the value of a technical trading indicator is computed using the past prices including the last closing price
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.14.20.png

step2:At the second step, the value of the technical indicator is translated into a trading signal.
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.14.37.png

i. 动量法则 Momentum Rule

指标:
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.20.02.png

信号:当出现连续下跌时,为卖出信号;反之为多头信号。

# 动量指标的构建
def MOM(n):
    close_lag=pd.Series(df['close'].values).shift(1-n) #滞后n-1期
    df['close_lag']=list(close_lag)  
    df['MOM'+str(n)] = df['close'] - df['close_lag']
    print(df.head())

ii.均线策略 Moving Average Change of Direction Rule

指标:

%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.24.15.png

信号:连续上涨为多头信号。A Buy signal is generated when the value of a moving average has increased over the last period.

# MACD指标的构建
def MACD(n):
    MA_lag=pd.Series(df['SMA'+str(n)].values).shift(-1) #滞后1期
    df['MA_lag'+str(n)]=list(MA_lag)  
    df['MACD'+str(n)] = df['SMA'+str(n)]-df['MA_lag'+str(n)]
    print(df.head())

iii. Price Minus Moving Average Rule

指标:
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.31.34.png

信号:A Buy signal is generated when the last closing price is above the moving average. Otherwise, if the last closing price is below the moving average, a Sell signal is generated.

# PMA指标的构建 & 与交易信号
def PMA(n):
    df['PMA'+str(n)] = df['close']-df['SMA'+str(n)]
    signal_lst=[]
    for i in df['PMA'+str(n)]:
        if i>0:
            signal_lst.append(1) 
        else:
            signal_lst.append(0)
    df['signal_PMA']=signal_lst # signal变量为1则表示多头信号,signal为零则是空头信号
    print(df.head())

iv. 均线交叉 Moving Average Crossover Rule (MAC)

指标:
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.39.51.png

信号:
多头:when a shorter moving average crosses either above or below a longer moving average (“golden cross”)

# MAC指标的构建 & 与交易信号
def MAC(n,m): 
    df['MAC'+str(n)+'_'+str(m)] = df['MAC'+str(n)]-df['MAC'+str(m)]  # 参数n小于参数m
    signal_lst=[]
    for i in df['MAC'+str(n)+'_'+str(m)]:
        if i>0:
            signal_lst.append(1) 
        else:
            signal_lst.append(0)
    df['signal_MAC']=signal_lst # signal变量为1则表示多头信号,signal为零则是空头信号
    print(df.head())

v.多均线策略

注:文章中并未给出具体规则。

• When all moving averages are moving in the same direction (that is, parallel), the trend is said to be strong because all of them are largely in agreement.

• When moving averages in a ribbon start to converge or diverge, a trend change has already begun to occur.

• When all moving averages converge and fluctuate more than usual, the price moves sideways.

vi.均线收敛/发散规则 Moving Average Convergence/Divergence Rule

指标:
%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202018-01-09%2011.50.55.png

信号:
A Buy (Sell) signal is gernerated when MAC increases (decreases).

def delta_MAC(n,m): 
    MAC_lag=pd.Series(df['MAC'+str(n)+'_'+str(m)].values).shift(-1) #滞后1期
    df['MAC_lag'+str(n)+'_'+str(m)]=list(MAC_lag)  
    df['deltaMAC'+str(n)+'_'+str(m)] = df['MAC'+str(n)+'_'+str(m)]-  df['MAC_lag'+str(n)+'_'+str(m)] # 参数n小于参数m
    signal_lst=[]
    for i in df['deltaMAC'+str(n)+'_'+str(m)]:
        if i>0:
            signal_lst.append(1) 
        else:
            signal_lst.append(0)
    df['signal_deltaMAC']=signal_lst # signal变量为1则表示多头信号,signal为零则是空头信号
    print(df.head())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值