资源加载中... loading...

MACD威利策略

Author: 雨幕(youquant), Date: 2022-06-02 15:31:23
Tags: MACDEMA

该策略主要针对倒卖/盘中交易制定。它可能被用来识别短期期权交易的进入/退出信号。当使用常规会话条数据在5分钟到15分钟的时间范围内使用时,它在热门股票上表现良好。它结合了3个流行的指标,EMA、MACD和William%range,以生成长信号和短信号。

  • EMA: 默认值为200 EMA线。

  • MACD: 快/慢信号输入的默认长度为12/26。

  • William%R-平滑(已发布): 这是一个自定义指标,从原始William%R线生成两条移动平均线。

工作原理:

  • 进入条件: 1、均线上方/下方收盘时的多头/空头进场 2、MACD线高于/低于信号线时的多/空进入(多头直方图>0,空头直方图<0) 3、William%R快速MA线高于/低于慢速MA线时的多/空进入

  • 退出条件: 1、当MACD线低于信号线时,退出多头,反之亦然,退出空头。 2、当William%R fast MA line低于slow MA line时,退出多头,反之亦然,退出空头。 3、当William%R fast MA line必须在超买(-20)限额以下时多头退出,当超卖(-80)限额以上时空头退出。

请注意,未针对任何特定股票/工具优化参数

享受~~!!

策略回测

img

img

img


/*backtest
start: 2022-01-01 09:00:00
end: 2022-06-01 15:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["ContractType","rb2210",360008]]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © platsn

//@version=5
strategy("MACD Willy Strategy", overlay=true, pyramiding=1, initial_capital=10000) 

// ******************** Trade Period **************************************
// startY = input(title='Start Year', defval=2011, group = "Trading window")
// startM = input.int(title='Start Month', defval=1, minval=1, maxval=12, group = "Trading window")
// startD = input.int(title='Start Day', defval=1, minval=1, maxval=31, group = "Trading window")
// finishY = input(title='Finish Year', defval=2050, group = "Trading window")
// finishM = input.int(title='Finish Month', defval=12, minval=1, maxval=12, group = "Trading window")
// finishD = input.int(title='Finish Day', defval=31, minval=1, maxval=31, group = "Trading window")
//timestart = timestamp(startY, startM, startD, 00, 00)
//timefinish = timestamp(finishY, finishM, finishD, 23, 59)
// t1 = time(timeframe.period, "0945-1545:23456") 
// window = time >= timestart and time <= timefinish and t1 ? true : false 
// t2 = time(timeframe.period, "0930-1555:23456")
// window2 = time >= timestart and time <= timefinish and t2 ? true : false 

// leverage = input.float(1, title="Leverage (if applicable)", step=0.1, group = "Trading Options")
// reinvest = input.bool(defval=false,title="Reinvest profit", group = "Trading Options")
// reinvest_percent = input.float(defval=20, title = "Reinvest percentage", group="Trading Options")
// entry_lookback = input.int(defval=10, title="Lookback period for entry condition", group = "Trading Options")

// -------------------------------------------- Data Source --------------------------------------------

src = input(title="数据源", defval=close)

// ***************************************************************************************************** Daily ATR *****************************************************
atrlen = input.int(14, minval=1, title="ATR 周期", group = "Daily ATR")
iPercent = input.float(5, minval=1, maxval=100, step=0.1, title="% ATR 用于 止损 / 止盈", group = "Daily ATR")
 
percentage = iPercent * 0.01
datr = request.security(syminfo.tickerid, "1D", ta.rma(ta.tr, atrlen))
datrp = datr * percentage

// plot(datr,"Daily ATR")
// plot(datrp, "Daily % ATR")

//*********************************************************** VIX volatility index ****************************************

//VIX = request.security("VIX", timeframe.period, close)
//vix_thres = input.float(20.0, "VIX Threshold for entry", step=0.5, group="VIX Volatility Index")

// ************************************************ Volume ******************************************************

vol_len = input(50, '成交量 均线 周期')
avg_vol = ta.sma(volume, vol_len)

//-------------------------------------------------------- Moving Average ------------------------------------

emalen1 = input.int(200, minval=1, title='EMA周期', group= "Moving Averages")
ema1 = ta.ema(src, emalen1)

// ------------------------------------------ MACD ------------------------------------------
// Getting inputs
fast_length = input(title="快线周期", defval=12)
slow_length = input(title="慢线周期", defval=26)
signal_length = input.int(title="信号平滑",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="震荡 均线 类型",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="信号 均线 类型", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD 线", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "信号线", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "上方扩大", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "减小", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "下方扩大", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "减小", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

// ---------------------------------------- William %R --------------------------------------
w_length = input.int(defval=34, title="William %R周期", minval=1)
w_upper = ta.highest(w_length)
w_lower = ta.lowest(w_length)

w_output = 100 * (close - w_upper) / (w_upper - w_lower)

fast_period = input(defval=5, title='Smoothed %R 周期')
slow_period = input(defval=13, title='慢线 EMA 周期')

w_fast_ma = ta.wma(w_output,fast_period)
w_slow_ma = ta.ema(w_output,slow_period)



// ------------------------------------------------ Entry Conditions ----------------------------------------

L_entry1 = close > ema1 and hist > 0 and w_fast_ma > w_slow_ma 
S_entry1 = close < ema1 and hist < 0 and w_fast_ma < w_slow_ma 

// -------------------------------------------------- Entry -----------------------------------------------

//profit = strategy.netprofit
//trade_amount = math.floor(strategy.initial_capital*leverage / close) 

//if strategy.netprofit > 0 and reinvest
//    trade_amount := math.floor((strategy.initial_capital+(profit*reinvest_percent*0.01))*leverage / close) 
//else
//    trade_amount := math.floor(strategy.initial_capital*leverage/ close) 


if L_entry1 //and window
    strategy.entry("Long", strategy.long)

if S_entry1 //and window
    strategy.entry("Short", strategy.short)

// --------------------------------------------------- Exit Conditions -------------------------------------

L_exit1 = hist < 0 and w_fast_ma < w_slow_ma and w_fast_ma < -20
S_exit1 = hist > 0 and w_fast_ma > w_slow_ma and w_fast_ma > -80

// ----------------------------------------------------- Exit ---------------------------------------------

if L_exit1 //and window2
    strategy.close("Long")
    
if S_exit1 //and window2
    strategy.close("Short")

// if time(timeframe.period, "1530-1600:23456")
//     strategy.close_all()
template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6