基于真实波幅的加权移动平均跨期策略


创建日期: 2024-01-17 15:09:28 最后修改: 2024-01-17 15:09:28
复制: 1 点击次数: 572
avatar of ChaoZhang ChaoZhang
1
关注
1617
关注者

基于真实波幅的加权移动平均跨期策略

概述

本策略利用真实波幅(True Range)和加权移动平均(WMA)构建一个跨期指标,实现趋势判断。同时,它具有多个仓位累加的金字塔加仓机制,以及多个止损机制,旨在追求稳定盈利。

策略原理

该策略首先计算上涨波幅(sube)和下跌波幅(baja),然后分别计算快线(corto)周期和慢线(largo)周期的WMA。快慢线差值再次通过WMA计算出指标(ind)。当指标上穿0时产生买入信号,下穿0时产生卖出信号。

在入市后,策略预設5个仓位,按照等比(一倍)累加的方式实现金字塔加仓。同时设置了止损机制,之后开仓时都要判断当前浮盈是否低于止损线,从而控制风险。

优势分析

该策略集成了跨期判断、金字塔加仓、多重止损等机制,可以有效控制风险,追求稳定盈利。

跨期判断通过快慢线组合建立趋势判断体系,可以有效过滤市场噪音,识别趋势转折点。金字塔加仓可以在趋势开始阶段获利更多,多重止损机制可以有效控制单笔损失。

风险分析

该策略主要风险在于可能出现突发事件导致快速行情反转,触发止损cutoff而造成损失。此外,参数设置不当也会影响策略稳定性。

可以通过适当放宽止损线来应对行情反转的风险。优化参数设置,调整周期参数、仓位数等可提高策略稳定性。

优化方向

该策略可从以下几个方面进行优化:

  1. 增加统计学指标判断,结合波动率、成交量等指标修正参数,使策略更具适应性。

  2. 增加机器学习模型判断,利用LSTM等深度学习模型辅助判断,提高策略准确性。

  3. 优化仓位管理机制,可考虑随浮盈比例调整加仓幅度,让仓位增长更合理。

  4. 结合期货套保模型,利用期现套利进一步控制风险。

总结

本策略总体来说是一个基于真实波幅指标构建的跨周期趋势策略,具有金字塔加仓和多重止损机制,可以有效控制风险,追求稳定盈利,是一个非常实用的量化交易策略。但仍需注意行情反转和参数优化问题,可从统计学、机器学习等方面进行进一步优化。

策略源码
/*backtest
start: 2023-01-10 00:00:00
end: 2024-01-16 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

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

//@version=5
strategy("[MACLEN] Rangos", shorttitle="Rangos [https://t.me/Bitcoin_Maclen]", overlay=false )

//------WINDOW----------

i_startTime = input(defval = timestamp("01 Jan 2022 00:00 -0700"), title = "Start Time", group = "Backtest Window")
i_endTime = input(defval = timestamp("31 Dec 2025 00:00 -0700"), title = "End Time")
window = true

//-----------------------------

sube = close>close[1] ? ta.tr : 0
baja = close<close[1] ? ta.tr : 0

corto = input(10)
largo = input(30)
suavizado = input(10)

fastDiff = ta.wma(sube, corto) - ta.wma(baja,corto)
slowDiff = ta.wma(sube, largo) - ta.wma(baja, largo)
ind = ta.wma(fastDiff - slowDiff, suavizado)

iColor = ind>0 ? color.green : ind<0 ? color.red : color.black
plot(ind, color=iColor)
plot(0, color=color.white)

long = ind[1]<ind and ind[2]<ind[1] and ind<0
short = ind[1]>ind and ind[2]>ind[1] and ind>0

plotshape(long and not long[1], style = shape.xcross, color=color.green, location=location.bottom, size=size.tiny)
plotshape(short and not short[1], style = shape.xcross, color=color.red, location=location.top, size=size.tiny)

//Contratos
contrato1 = input(50000)/(16*close)
c1 = contrato1
c2 = contrato1
c3 = contrato1*2
c4 = contrato1*4
c5 = contrato1*8

//cap_enopentrade = strategy.opentrades == 1 ? c1: strategy.opentrades == 2 ? c1+c2: strategy.opentrades == 3 ? c1+c2+c3: strategy.opentrades == 4 ? c1+c2+c3+c4: strategy.opentrades == 5 ? c1+c2+c3+c4+c5 : 0
openprofit_porc = math.round((close-strategy.position_avg_price)/strategy.position_avg_price * 100,2)

porc_tp = input.float(6.5)
safe = input(-6)

//----------------Strategy---------------------------

if strategy.opentrades == 0
    strategy.entry('BUY1', strategy.long, qty=c1, when = long and not long[1] and window)

if strategy.opentrades == 1
    strategy.entry('BUY2', strategy.long, qty=c2, when = long and not long[1] and window and openprofit_porc<safe)

if strategy.opentrades == 2
    strategy.entry('BUY3', strategy.long, qty=c3, when = long and not long[1] and window and openprofit_porc<safe)

if strategy.opentrades == 3
    strategy.entry('BUY4', strategy.long, qty=c4, when = long and not long[1] and window and openprofit_porc<safe)

if strategy.opentrades == 4
    strategy.entry('BUY5', strategy.long, qty=c5, when = long and not long[1] and window and openprofit_porc<safe)

min_prof = strategy.openprofit>0

strategy.close_all(when=short and min_prof)

plot(openprofit_porc)