Chiến lược giao dịch thích nghi dựa trên nhiều EMA Crossovers

Tác giả:ChaoZhang, Ngày: 2023-09-26 14:41:00
Tags:

Tổng quan

Chiến lược này thực hiện giao dịch dài / ngắn thích nghi bằng cách sử dụng nhiều bộ chỉ số EMA. Nó áp dụng EMA với các tham số khác nhau để vào và ra dựa trên xu hướng dài hạn và ngắn hạn của thị trường. Chiến lược tự động nhận ra thị trường bò / gấu và sử dụng stop loss độc lập để kiểm soát rủi ro.

Chiến lược logic

Chiến lược này chủ yếu sử dụng nguyên tắc chéo của các chỉ số EMA. Long khi EMA nhanh vượt qua EMA chậm, và ngắn khi vượt qua dưới. Nó thiết lập nhiều EMA và chọn các thông số khác nhau dựa trên xu hướng thị trường. Cụ thể, khi đánh giá xu hướng dài hạn là tăng, một bộ EMA dài hạn được sử dụng cho tín hiệu dài; khi giảm, một bộ EMA ngắn hạn khác được sử dụng cho ngắn hạn. Exits cũng áp dụng các EMA thời gian khác nhau. Stop loss sử dụng stop trailing tỷ lệ phần trăm cố định dựa trên hướng vị trí.

Phân tích lợi thế

  • Nhiều bộ EMA thích nghi hoạt động linh hoạt trên các thị trường khác nhau.
  • Việc phân biệt bò và gấu làm cho tín hiệu rõ ràng hơn.
  • Các thông số nhập / xuất độc lập cho phép định vị chính xác.
  • Lãi suất dừng lỗ cố định có hiệu quả kiểm soát rủi ro.
  • Logic chiến lược trực quan và dễ hiểu và thực hiện.

Rủi ro và cải tiến

  • EMA có thể tạo ra tín hiệu sai, điều chỉnh tham số là chìa khóa.
  • Định giá dừng lỗ có thể không theo dõi các biến động lớn.
  • Cần thêm bộ lọc như khối lượng để tăng độ bền.
  • Các tham số có thể được tự động tối ưu hóa bằng các thuật toán học máy.
  • Xem xét sử dụng stop loss động như ATR thay vì cố định.

Tóm lại

Chiến lược đạt được hiệu ứng thích nghi bằng cách tận dụng nhiều đường chéo EMA, giữ lợi thế của EMA và làm cho chiến lược linh hoạt hơn.


/*backtest
start: 2023-08-26 00:00:00
end: 2023-09-07 00:00:00
period: 12h
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/
// © str1nger
//@version=4

// strategy(title="BTC - 4hr - Long/Short", shorttitle="BTC - 4hr - Long/Short", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=75,commission_type=strategy.commission.percent, commission_value=0.075)//////<---Uses a percentage of starting equity

//DATE RANGE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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=2020, minval=2000, maxval=2100)
endDate = input(title="End Date", type=input.integer,
     defval=1, minval=1, maxval=31)
endMonth = input(title="End Month", type=input.integer,
     defval=12, minval=1, maxval=12)
endYear = input(title="End Year", type=input.integer,
     defval=2021, minval=2000, maxval=2100)

inDateRange =  true


//EMAs//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
//11,33,3,40
lof= input(11, title="Long Open - Fast", step=1)
los= input(33, title="Long Open - Slow", step=1)
lcf= input(3, title="Long Close - Fast", step=1)
lcs= input(40, title="Long Close - Slow", step=1)
ema_long_open_fast = ema(close, lof)
ema_long_open_slow = ema(close, los)
ema_long_close_fast= ema(close, lcf)
ema_long_close_slow = ema(close, lcs)
//SHORT
//5,11,4,7
sof= input(5, title="Short Open - Fast", step=1)
sos= input(11, title="Short Open - Slow", step=1)
scf= input(4, title="Short Close - Fast", step=1)
scs= input(7, title="Short Close - Slow", step=1)
ema_short_open_fast = ema(close, sof)
ema_short_open_slow = ema(close, sos)
ema_short_close_fast = ema(close, scf)
ema_short_close_slow = ema(close, scs)


//CONDITIONS///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
openlong = crossover(ema_long_open_fast, ema_long_open_slow)
closelong = crossover(ema_long_close_slow, ema_long_close_fast)
//1.7%
long_loss_percent = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1.7) * 0.01
long_stop_price = strategy.position_avg_price * (1 - long_loss_percent)
//SHORT
openshort = crossover(ema_short_open_slow, ema_short_open_fast)
closeshort = crossover(ema_short_close_fast, ema_short_close_slow)
//0.4%
short_loss_percent = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=0.4) * 0.01
short_stop_price = strategy.position_avg_price * (1 + short_loss_percent)


//PLOT EMAs////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
plot(ema_long_open_fast, "Long EMA open lower", linewidth=1, color=color.green)
plot(ema_long_open_slow, "Long EMA close upper", linewidth=1, color=color.green)
plot(ema_long_close_fast, "Long close lower", linewidth=1, color=color.red)
plot(ema_long_close_slow, "Long close upper", linewidth=1, color=color.red)
//SHORT
plot(ema_short_open_fast, "Short open fast", linewidth=1, color=color.green)
plot(ema_short_open_slow, "Short open slow", linewidth=1, color=color.green)
plot(ema_short_close_fast, "Short close fast", linewidth=1, color=color.red)
plot(ema_short_close_slow, "Short close slow", linewidth=1, color=color.red)


//LONG-TERM TRENDS
//LONG 144
long_term_trend_longs= input(144, title="Long-term trend - Longs", step=1)
lttl= ema(close, long_term_trend_longs)
plot(lttl, "Long-term trend - Longs", linewidth=2, color=color.blue)
//SHORT 89
long_term_trend_shorts= input(89, title="Long-term trend - Shorts", step=1)
ltts = ema(close, long_term_trend_shorts)
plot(ltts, "Long-term trend - Shorts", linewidth=2, color=color.blue)


//STRATEGY//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG
if (inDateRange and openlong and (close > lttl))
    strategy.entry("OL", long=true, comment="##insert open long comment here##")
if (inDateRange and closelong)
    strategy.close("OL", comment="##insert close long comment here##")
if strategy.position_size > 0
    strategy.exit("L-SL", stop=long_stop_price, comment="##insert long stop-loss comment here##")
//SHORT  
if (inDateRange and openshort and (close < ltts))
    strategy.entry("OS", long=false, comment="##insert open short comment here##")
if (inDateRange and closeshort)
    strategy.close("OS", comment="##insert close short comment here##")
if strategy.position_size < 0
    strategy.exit("S-SL", stop=short_stop_price, comment="##inster short stop-loss comment here##")




Thêm nữa