Chiến lược thoái lui của EMA Golden Cross


Ngày tạo: 2023-12-21 11:48:54 sửa đổi lần cuối: 2023-12-21 11:48:54
sao chép: 2 Số nhấp chuột: 877
1
tập trung vào
1623
Người theo dõi

Chiến lược thoái lui của EMA Golden Cross

Tổng quan

Chiến lược EMA Gold Crossover là một chiến lược giao dịch định lượng dựa trên chỉ số EMA. Chiến lược này sử dụng đường cong EMA của ba chu kỳ khác nhau để xây dựng tín hiệu giao dịch và kết hợp với cơ chế điều chỉnh giá để thiết lập điểm dừng lỗ để thực hiện giao dịch tự động.

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

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

  • EMA1: được sử dụng để đánh giá giá trị hỗ trợ / kháng cự, chu kỳ ngắn, mặc định 33 chu kỳ.
  • EMA2: được sử dụng để lọc một số tín hiệu đảo ngược, có chu kỳ gấp 5 lần EMA1, mặc định là 165 chu kỳ.
  • EMA3: được sử dụng để đánh giá hướng xu hướng tổng thể, có chu kỳ gấp 11 lần EMA1, mặc định là 365 chu kỳ.

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

Tín hiệu đa đầu: Giá sẽ bị điều chỉnh lại sau khi sử dụng EMA1 cấu thành, tạo ra điểm thấp cao hơn trên EMA1 và không chạm vào EMA2. Sau khi đáp ứng điều kiện, hãy làm nhiều khi sử dụng EMA1 một lần nữa.

Tín hiệu đầu trống: Khả năng khúc phục xảy ra sau khi giá vượt qua EMA1 xuống, tạo ra điểm cao thấp hơn bên dưới EMA1 và không chạm vào EMA2. Sau khi đáp ứng điều kiện, hãy làm trống khi vượt qua EMA1 xuống một lần nữa.

Phương pháp dừng là điều chỉnh giá tối thiểu / giá tối đa. Cài đặt dừng là gấp 2 lần mức dừng.

Lợi thế chiến lược

Chiến lược này có những ưu điểm sau:

  1. Sử dụng chỉ số EMA để xây dựng tín hiệu giao dịch, độ tin cậy cao hơn.
  2. Kết hợp với cơ chế điều chỉnh giá, nó có thể tránh được sự cố.
  3. Điểm dừng lỗ được đặt ở mức cao thấp trước đó, điều khiển rủi ro hiệu quả.
  4. Cài đặt điểm dừng theo tỷ lệ dừng lỗ, đáp ứng yêu cầu tỷ lệ lỗ.
  5. Các tham số EMA có thể được điều chỉnh theo thị trường để phù hợp với các chu kỳ khác nhau.

Rủi ro chiến lược

Chiến lược này cũng có những rủi ro:

  1. Các chỉ số EMA bị tụt hậu và có thể bỏ lỡ điểm đảo ngược xu hướng.
  2. Phạm vi quay trở quá lớn so với EMA2 có thể tạo ra tín hiệu sai.
  3. Hành động dừng lỗ có thể bị phá vỡ.
  4. Thiết lập tham số không đúng có thể dẫn đến giao dịch quá thường xuyên hoặc bỏ lỡ cơ hội.

Các tham số có thể được tối ưu hóa bằng cách điều chỉnh chu kỳ EMA, điều chỉnh phạm vi giới hạn. Các tham số có thể được kết hợp với các chỉ số khác để lọc tín hiệu.

Hướng tối ưu hóa chiến lược

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

  1. Tăng khả năng đánh giá các chỉ số xu hướng, tránh giao dịch ngược. Ví dụ: tham gia MACD.
  2. Thêm chỉ số khối lượng giao dịch để tránh phá vỡ giả. Ví dụ: tham gia OBV.
  3. Tối ưu hóa tham số chu kỳ EMA, hoặc sử dụng EMA thích ứng.
  4. Các tham số tối ưu hóa động cho các phương pháp học máy như mô hình túi từ.
  5. Tham gia dự đoán mô hình, thiết lập dừng dừng tự điều chỉnh.

Tóm tắt

Chiến lược điều chỉnh chéo vàng EMA bằng cách xây dựng hệ thống giao dịch ba EMA, kết hợp với tính năng điều chỉnh giá thiết lập dừng lỗ, thực hiện giao dịch tự động. Chiến lược này kiểm soát rủi ro giao dịch hiệu quả và có thể được tối ưu hóa theo thị trường bằng cách điều chỉnh tham số. Nói chung, chiến lược này hợp lý và có thể áp dụng thực tế.

Mã nguồn chiến lược
/*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)





//