MACD策略到底应该怎么使用?

macd
标签: #<Tag:0x00007fcf65d832e0>

(htly) #1
克隆策略

1. 策略参数

In [28]:
import talib
instruments = ['600519.SHA']
start_date = '2014-09-17'# 起始时间    
end_date = '2017-08-14' # 结束时间

2. MACD策略

In [ ]:
策略简介


MACD称为指数平滑移动平均线是从双指数移动平均线发展而来的.
- MACD运用

当macd线上穿signal线时买入股票

当macd线下穿signal线时卖出股票
In [32]:
def initialize(context):
   
    context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) # 设置手续费
    # 使用MACD需要设置长短均线和macd平均线的参数
    context.short = 12
    context.long = 26
    context.smoothperiod = 9
    context.observation = 50
     
def handle_data(context, data):
    
    if context.trading_day_index < context.observation:  
        return
    
    sid = context.symbol(instruments[0])
    # 读取历史数据
    prices = data.history(sid, 'price', context.observation, '1d')
    # 用Talib计算MACD取值,得到三个时间序列数组,分别为macd, signal 和 hist
    macd, signal, hist = talib.MACD(np.array(prices), context.short, context.long, context.smoothperiod)
 
    # 计算现在portfolio中股票的仓位
    cur_position = context.portfolio.positions[sid].amount
    
    # 策略逻辑
    # 卖出逻辑(下穿)
    if macd[-1] - signal[-1] < 0 and macd[-2] - signal[-2] > 0:
        #进行清仓
        if cur_position > 0 and data.can_trade(sid):
            context.order_target_value(sid, 0)

    # 买入逻辑(上穿)
    if macd[-1] - signal[-1] > 0 and macd[-2] - signal[-2] < 0:
        if cur_position == 0 and data.can_trade(sid):
        # 满仓入股
            context.order_target_percent(sid, 1)
            
m_macd = M.trade.v2( 
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open',
    order_price_field_sell='open',
    capital_base=float("1.0e6"),
    benchmark='000300.INDX',
)
[2017-08-14 19:06:30.019020] INFO: bigquant: backtest.v7 start ..
[2017-08-14 19:06:33.810992] INFO: Performance: Simulated 709 trading days out of 709.
[2017-08-14 19:06:33.812320] INFO: Performance: first open: 2014-09-17 13:30:00+00:00
[2017-08-14 19:06:33.813305] INFO: Performance: last close: 2017-08-14 19:00:00+00:00
  • 收益率43.09%
  • 年化收益率13.58%
  • 基准收益率54.67%
  • 阿尔法0.06
  • 贝塔0.22
  • 夏普比率0.45
  • 收益波动率20.51%
  • 信息比率-0.11
  • 最大回撤33.49%
[2017-08-14 19:06:35.796581] INFO: bigquant: backtest.v7 end [5.777557s].

3.双均线策略

策略简介:

  • 短期均线上穿长期均线,做多

  • 短期均线下穿长期均线,做空

In [30]:
def initialize(context):
    
    context.short_period = 5 # 短期均线
    context.long_period = 45 # 长期均线 
    context.observation = 50

def handle_data(context, data):
    
    if context.trading_day_index < context.observation :  
        return
    
    k = instruments[0] # 标的为字符串格式
    sid = context.symbol(k) # 将标的转化为equity格式
    price = data.current(sid, 'price') # 最新价格
 
    short_mavg = data.history(sid, 'price',context.short_period, '1d').mean() # 短期均线值
    long_mavg = data.history(sid, 'price',context.long_period, '1d').mean() # 长期均线值

    cash = context.portfolio.cash # 现金
    cur_position = context.portfolio.positions[sid].amount # 持仓
    
    # 交易逻辑
    if short_mavg > long_mavg and cur_position == 0 and data.can_trade(sid):  
        context.order(sid, int(cash/price/100)*100) # 买入
        
    elif short_mavg < long_mavg and cur_position > 0 and data.can_trade(sid):  
        context.order_target_percent(sid, 0) # 全部卖出
        
m_dual_ma = M.trade.v2(
    instruments=instruments,
    start_date=start_date,
    end_date=end_date,
    initialize=initialize,
    handle_data=handle_data,
    order_price_field_buy='open',
    order_price_field_sell='open',
    capital_base=1000000,
    )
[2017-08-14 18:30:04.106994] INFO: bigquant: backtest.v7 start ..
[2017-08-14 18:30:08.489895] INFO: Performance: Simulated 709 trading days out of 709.
[2017-08-14 18:30:08.493021] INFO: Performance: first open: 2014-09-17 13:30:00+00:00
[2017-08-14 18:30:08.495493] INFO: Performance: last close: 2017-08-14 19:00:00+00:00
  • 收益率231.17%
  • 年化收益率53.05%
  • 基准收益率54.67%
  • 阿尔法0.45
  • 贝塔0.32
  • 夏普比率2.06
  • 收益波动率23.57%
  • 信息比率1.25
  • 最大回撤14.67%
[2017-08-14 18:30:10.390979] INFO: bigquant: backtest.v7 end [6.283966s].

MACD 有例子吗