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

EHMA范围策略

Author: 雨幕(youquant), Date: 2022-05-31 11:21:06
Tags: EHMAHMA

此脚本是@borserman的HMA指标脚本的修改版本。 EHMA的所有功劳都归他所有:)

除了EHMA之外,该脚本还可以在EHMA周围的范围内工作(可以修改),以抵抗虚假信号。很多时候,一个K线BAR的收盘价会低于移动平均线,而下一个K线BAR的收盘价又会反转,这会吞噬你的利润。尤其是在较短的时间段上,但在波动较大的较长时间段上,这可能会使策略不具吸引力。

在EHMA附近的范围内,只有当一根K线BAR越过上限时,该策略才会进入多头/空头位置。反之亦然,只有当一根K线BAR穿过较低范围时,它才会进入短/出口长位置。这避免了在EHMA范围内波动的酒吧头寸&只有在市场对其方向有信心的情况下才进入头寸。尽管如此,伪造仍然是可能的,但频率要低得多。与常规EHMA策略相比,该策略进行了回溯测试(并对各种设置进行了实验),该版本似乎更加稳健和有利可图!

免责声明 请记住,过去的表现可能并不代表未来的结果。 由于各种因素,包括不断变化的市场条件,该策略的表现可能不再像历史回溯测试那样好。 这篇文章和脚本没有提供任何财务建议。

回测测试

img

img

img


/*backtest
start: 2021-12-01 00:00:00
end: 2022-05-30 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["ContractType","hc2210",360008]]
*/

// Credit is due where credit is due:
// Hull Moving Average: developed by Alan Hull
// EHMA: coded by Twitter @borserman
// I've built on their work in an attempt to create a strategy more robust to fake moves
// @0xLetoII

//@version=4
//strategy(
//  title="EHMA Range Strategy",
//  process_orders_on_close=true,
//  explicit_plot_zorder=true,
//  overlay=true, 
//  initial_capital=1500, 
//  default_qty_type=strategy.percent_of_equity, 
//  commission_type=strategy.commission.percent, 
//  commission_value=0.085,
//  default_qty_value=100
//  )
  

// Position Type
pos_type = input(defval = "Both", title="持仓设置", options=["Both", "Long", "Short"])

// Inputs
Period = input(defval=300, title="周期")
RangeWidth = input(defval=0.02, step=0.01, title="范围宽度")
sqrtPeriod = sqrt(Period)

// Function for the Borserman EMA
borserman_ema(x, y) =>
    alpha = 2 / (y + 1)
    sum = 0.0
    sum := alpha * x + (1 - alpha) * nz(sum[1])

// Calculate the Exponential Hull Moving Average
EHMA = borserman_ema(2 * borserman_ema(close, Period / 2) - borserman_ema(close, Period), sqrtPeriod)

// Create upper & lower bounds around the EHMA for broader entries & exits
upper = EHMA + (EHMA * RangeWidth)
lower = EHMA - (EHMA * RangeWidth)

// Plots
EHMAcolor = (close > EHMA ? color.green : color.red)
plot(EHMA, color=EHMAcolor, linewidth=2)
plot(lower, color=color.orange, linewidth=2)
plot(upper, color=color.blue, linewidth=2)


// Strategy
long = close > upper
exit_long = close < lower
short = close < lower
exit_short = close > upper


// Calculate start/end date and time condition
//startDate  = input(timestamp("2017-01-01T00:00:00"))
//finishDate = input(timestamp("2029-01-01T00:00:00"))
 
time_cond  = true


// Entries & Exits
if pos_type == "Both"
    strategy.entry("Long", strategy.long, comment="Long", when=long and time_cond)
    strategy.close("Long", comment="Exit Long", when=exit_long and time_cond)
    strategy.entry("Short", strategy.short, comment="Short", when=short and time_cond)
    strategy.close("Short", comment="Exit Short", when=exit_short and time_cond)
if pos_type == "Long"
    strategy.entry("Long", strategy.long, comment="Long", when=long and time_cond)
    strategy.close("Long", comment="Exit Long", when=exit_long and time_cond)
if pos_type == "Short"
    strategy.entry("Short", strategy.short, comment="Short", when=short and time_cond)
    strategy.close("Short", comment="Exit Short", when=exit_short and time_cond)
template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6