改进前
源策略是基于唐奇安上下轨,先看看15mink线USDT的2023.7.30-2024.7.30的结果
下面是2022-7-30到2023-7-30
下面是2021-7-30到2022-7-30
改进后
加入动态EMA和止盈止损
15mink线USDT的2023.8.30-2024.8.30的结果
下面是2022-7-30到2023-7-30
下面是2021-7-30到2022-7-30
改进点
主要缓和了21-22年亏损巨大,动态EMA可以根据市场状况(如ATR)自动调整,使得它能够适应不同的市场环境,从而减少对市场的滞后反应或过度敏感的问题。
源代码
//@version=5
strategy("Improved Strategy with Proportional Dynamic EMA", overlay=true)// Define variables
var float SWH = na // 多头趋势转换点
var float SWL = na // 空头趋势转换点
var int Qushi = -1 // 趋势状态 -1: 空头, 1: 多头
var int SW = 0 // 状态开关,标记趋势的转换
var float atrValue = na // 固定的ATR值// 定义基础的EMA周期
basePeriod1 = input.int(1,title="ema1")
basePeriod2 = input.int(70,title="ema2")
basePeriod3 = input.int(260,title="ema3")// 定义比例因子,用于根据某些条件调整周期 (这里我们用 ATR 作为调整因子)
atr50 = 10 * ta.atr(50) // 计算50周期ATR
proportionFactor = 1 + (atr50 / close) // ATR 与当前价格的比值作为调整比例// 动态调整后的EMA周期,按比例缩放
dynamicPeriod1 = basePeriod1 * proportionFactor
dynamicPeriod2 = basePeriod2 * proportionFactor
dynamicPeriod3 = basePeriod3 * proportionFactor// Moving averages with dynamic periods
ma1 = ta.ema(close, int(dynamicPeriod1)) // 动态调整的EMA5
ma2 = ta.ema(close, int(dynamicPeriod2)) // 动态调整的EMA47
ma3 = ta.ema(close, int(dynamicPeriod3)) // 动态调整的EMA54// Donchian Channel (唐奇安通道上下轨)
HH = ta.highest(high, 20)
LL = ta.lowest(low, 20)// Calculate ATR for the first 100 bars and fix it
if (na(atrValue)) // 如果 ATR 尚未设置atrValue := ta.atr(100) // 在前 100 根 K 线后,计算 ATR 并固定它// Trend detection based on MA crosses
if (ta.crossover(ma1, ma2) and ma2 > ma3)SW := 1 // 触发多头趋势开关SWH := HH // 记录唐奇安上轨if (ta.crossunder(ma1, ma2) and ma2 < ma3)SW := -1 // 触发空头趋势开关SWL := LL // 记录唐奇安下轨// Trend confirmation using fixed ATR and Donchian breakout
if (Qushi == -1 and SW == 1 and high > SWH + 4 * atrValue)Qushi := 1 // 转多头趋势label.new(bar_index, low, "多", color=color.red)if (Qushi == 1 and SW == -1 and low < SWL - 4 * atrValue)Qushi := -1 // 转空头趋势label.new(bar_index, high, "空", color=color.yellow)// Entry conditions for long position
if (strategy.position_size == 0 and Qushi == 1)strategy.entry("Long", strategy.long)label.new(bar_index, low, "开多", color=color.green)// Exit conditions for long position
if (strategy.position_size > 0 and Qushi == -1)strategy.close("Long")label.new(bar_index, high, "平多", color=color.orange)// --- Add Trailing Stop Loss and Take Profit ---// Calculate dynamic ATR for stop loss and take profit
dynamicATR = ta.atr(14) // 这里的ATR是动态变化的// Add floating stop loss (Trailing Stop)
strategy.exit("Exit Long", from_entry="Long", trail_offset=3 * dynamicATR, comment="浮动止损")// Add fixed take profit (止盈目标)
takeProfitPrice = strategy.position_avg_price + 5 * dynamicATR // 设置止盈目标为5倍ATR
strategy.exit("Take Profit", from_entry="Long", limit=takeProfitPrice, comment="浮动止盈")// Plot the moving averages and indicators on the chart
plot(ma1, color=color.red, title="Dynamic EMA5")
plot(ma2, color=color.black, title="Proportional Dynamic EMA47")
plot(ma3, color=color.purple, title="Proportional Dynamic EMA54")
plot(LL - 4*atrValue, color=color.blue, title="Donchian Low")
plot(HH + 4*atrValue, color=color.green, title="Donchian High")// 绘制比例因子
plot(proportionFactor, title="Proportion Factor", color=color.blue)