Chiến lược rút lui EMA

Tác giả:ChaoZhang, Ngày: 21-12-2023 11:48:54
Tags:

img

Tổng quan

Chiến lược EMA pullback là một chiến lược giao dịch định lượng dựa trên chỉ số EMA. Nó xây dựng các tín hiệu giao dịch bằng cách sử dụng ba đường cong EMA với các khoảng thời gian khác nhau và thiết lập dừng lỗ và lấy lợi nhuận dựa trên sự rút giá để tự động hóa giao dịch.

Nguyên tắc chiến lược

Chiến lược sử dụng ba đường cong EMA:

  • EMA1: để đánh giá mức hỗ trợ / kháng cự giảm giá, với thời gian tương đối ngắn, mặc định đến 33 thời gian.
  • EMA2: để lọc một số tín hiệu đảo ngược, với thời gian 5 lần EMA1, mặc định đến 165 thời gian.
  • EMA3: để xác định hướng xu hướng tổng thể, với thời gian 11 lần EMA1, mặc định đến 365 thời gian.

Các tín hiệu giao dịch được tạo theo logic sau:

Tín hiệu dài: Giá vượt trên EMA1, rút xuống dưới EMA1 tạo thành mức thấp hơn, với pullback không đạt đến EMA2.

Tín hiệu ngắn: Giá vượt dưới EMA1, kéo trở lại trên EMA1 tạo thành mức cao thấp hơn, với pullback không đạt đến EMA2.

Stop loss được thiết lập ở mức giá giảm thấp nhất / cao nhất cho dài / ngắn. Lợi nhuận được thiết lập ở mức gấp 2 lần stop loss.

Ưu điểm chiến lược

Chiến lược có những lợi thế sau:

  1. Các tín hiệu giao dịch được xây dựng bằng cách sử dụng chỉ số EMA đáng tin cậy.
  2. Giá rút lại tránh bị mắc kẹt hiệu quả.
  3. Stop loss được thiết lập ở mức kiểm soát cao / thấp trước đó rủi ro hiệu quả.
  4. Lấy lợi nhuận thỏa mãn tỷ lệ rủi ro-lợi nhuận.
  5. Các thông số EMA có thể điều chỉnh cho các chu kỳ khác nhau.

Rủi ro chiến lược

Chiến lược này cũng có một số rủi ro:

  1. EMA có hiệu ứng chậm trễ, có thể bỏ lỡ các điểm đảo ngược xu hướng.
  2. Phạm vi kéo trở lại quá lớn vượt quá EMA2 có thể tạo ra tín hiệu sai.
  3. Dừng lỗ có thể bị phá vỡ trong các thị trường xu hướng.
  4. Cài đặt tham số không đúng dẫn đến giao dịch quá mức hoặc bỏ lỡ cơ hội.

Các rủi ro có thể được giảm thiểu bằng cách điều chỉnh các khoảng thời gian EMA, giới hạn pullback vv. Các chỉ số khác cũng có thể được thêm vào các tín hiệu lọc.

Hướng dẫn tối ưu hóa

Chiến lược cũng có thể được tối ưu hóa trong các khía cạnh sau:

  1. Thêm chỉ số xu hướng để tránh giao dịch ngược xu hướng, ví dụ như MACD.
  2. Thêm chỉ số khối lượng giao dịch để tránh phá vỡ sai, ví dụ: OBV.
  3. Tối ưu hóa thời gian EMA hoặc sử dụng EMA thích nghi.
  4. Tối ưu hóa các tham số một cách năng động bằng cách sử dụng các mô hình học máy như túi từ.
  5. Thêm dự đoán mô hình để dừng lỗ thích nghi và lấy lợi nhuận.

Kết luận

Chiến lược EMA pullback xây dựng một hệ thống giao dịch sử dụng ba EMA và thiết lập dừng lỗ và lấy lợi nhuận dựa trên việc rút giá để tự động hóa giao dịch. Nó kiểm soát hiệu quả rủi ro giao dịch và có thể được tối ưu hóa bằng cách điều chỉnh các tham số dựa trên điều kiện thị trường. Nhìn chung chiến lược có logic hợp lý và có thể được áp dụng trong giao dịch thực tế.


/*backtest
start: 2023-11-20 00:00:00
end: 2023-12-20 00:00:00
period: 1h
basePeriod: 15m
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/
// created by Space Jellyfish
//@version=4

strategy("EMA pullback strategy", overlay = true, initial_capital=10000, commission_value = 0.075)

target_stop_ratio = input(title="Take Profit Stop Loss ratio", type=input.float, defval=2.06, minval=0.5, maxval=100)
riskLimit_low =  input(title="lowest risk per trade", type=input.float, defval=0.008, minval=0, maxval=100)
riskLimit_high =  input(title="highest risk per trade", type=input.float, defval=0.02, minval=0, maxval=100)
//give up the trade, if the risk is smaller than limit, adjust position size if risk is bigger than limit

ema_pullbackLevel_period = input(title="EMA1 for pullback level Period", type=input.integer, defval=33, minval=1, maxval=10000)
ema_pullbackLimiit_period = input(title="EMA2 for pullback limit Period", type=input.integer, defval=165, minval=1, maxval=10000)
ema_trend_period = input(title="EMA3 for trend Period", type=input.integer, defval=365, minval=1, maxval=10000)

startDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer, defval=2018, minval=2008, maxval=2200)

inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0))

ema_pullbackLevel = ema(close, ema_pullbackLevel_period)
ema_pullbackLimit = ema(close, ema_pullbackLimiit_period)
ema_trendDirection = ema(close, ema_trend_period)

//ema pullback 
float pricePullAboveEMA_maxClose = na
float pricePullAboveEMA_maxHigh = na

float pricePullBelowEMA_minClose = na
float pricePullBelowMA_minLow = na

if(crossover(close, ema_pullbackLevel))
    pricePullAboveEMA_maxClose := close
    pricePullAboveEMA_maxHigh := high
else
    pricePullAboveEMA_maxClose := pricePullAboveEMA_maxClose[1]
    pricePullAboveEMA_maxHigh := pricePullAboveEMA_maxHigh[1]

if(close > pricePullAboveEMA_maxClose)
    pricePullAboveEMA_maxClose := close
if(high > pricePullAboveEMA_maxHigh)
    pricePullAboveEMA_maxHigh := high

if(crossunder(close, ema_pullbackLevel))
    pricePullBelowEMA_minClose := close
    pricePullBelowMA_minLow := low
else
    pricePullBelowEMA_minClose :=pricePullBelowEMA_minClose[1]
    pricePullBelowMA_minLow:=pricePullBelowMA_minLow[1]
    
if(close < pricePullBelowEMA_minClose)
    pricePullBelowEMA_minClose := close
if(low < pricePullBelowMA_minLow)
    pricePullBelowMA_minLow := low


long_strategy = crossover(close, ema_pullbackLevel) and pricePullBelowEMA_minClose < ema_pullbackLimit and ema_pullbackLevel>ema_trendDirection 
short_strategy = crossunder(close, ema_pullbackLevel) and pricePullAboveEMA_maxClose > ema_pullbackLimit and ema_pullbackLevel<ema_trendDirection


var open_long_or_short = 0// long = 10000, short = -10000, no open = 0

//check if position is closed
if(strategy.position_size == 0)
    open_long_or_short := 0
else
    open_long_or_short := open_long_or_short[1]

float risk_long = na
float risk_short = na
float stopLoss = na
float takeProfit = na
float entry_price = na

float entryContracts = 0



risk_long := risk_long[1]
risk_short := risk_short[1]
    
//open a position determine the position size
if (strategy.position_size == 0 and long_strategy and inDateRange)
    risk_long := (close - pricePullBelowMA_minLow) / close

    if(risk_long < riskLimit_high)
        entryContracts := strategy.equity / close
    else
        entryContracts := (strategy.equity * riskLimit_high / risk_long)/close
    
    if(risk_long > riskLimit_low)
        strategy.entry("long", strategy.long, qty = entryContracts, when = long_strategy)


    open_long_or_short := 10000
    
if (strategy.position_size == 0 and short_strategy and inDateRange)
    risk_short := (pricePullAboveEMA_maxHigh - close) / close
    if(risk_short < riskLimit_high)
        entryContracts := strategy.equity / close
    else
        entryContracts := (strategy.equity * riskLimit_high / risk_short)/close

    if(risk_short > riskLimit_low)
        strategy.entry("short", strategy.short, qty = entryContracts, when = short_strategy)

    
    open_long_or_short := -10000

//take profit / stop loss
if(open_long_or_short == 10000)

    stopLoss :=   strategy.position_avg_price*(1 - risk_long)
    takeProfit :=  strategy.position_avg_price*(1 + target_stop_ratio * risk_long)
    entry_price := strategy.position_avg_price
    strategy.exit("Long exit","long", limit = takeProfit , stop = stopLoss)
    
if(open_long_or_short == -10000)
    stopLoss :=  strategy.position_avg_price*(1 + risk_short)
    takeProfit :=  strategy.position_avg_price*(1 - target_stop_ratio * risk_short)
    entry_price := strategy.position_avg_price
    strategy.exit("Short exit","short", limit = takeProfit, stop = stopLoss)



plot(ema_pullbackLevel, color=color.aqua,  title="ema pullback level")
plot(ema_pullbackLimit, color=color.purple,  title="ema pullback limit")
plot(ema_trendDirection, color=color.white,  title="ema trend")

plot(entry_price, color = color.yellow, linewidth = 1, style = plot.style_linebr)
plot(stopLoss, color = color.red, linewidth = 1, style = plot.style_linebr)
plot(takeProfit, color = color.green, linewidth = 1, style = plot.style_linebr)





//

Thêm nữa