
Chiến lược này là một hệ thống giao dịch định lượng dựa trên nhiều điểm giao nhau của đường trung bình động hàm mũ (EMA) và tối ưu hóa quá trình thoái lui. Phương pháp này sử dụng năm đường trung bình động EMA5, EMA8, EMA13, EMA21 và EMA50, đồng thời thực hiện mở lệnh theo đợt và đóng lệnh động bằng cách quan sát mối quan hệ chéo giữa các đường trung bình động của các giai đoạn khác nhau và mối quan hệ theo vị thế giữa giá và đường trung bình động. Chiến lược này áp dụng hệ thống quản lý tiền, chia các vị thế thành các tỷ lệ khác nhau như 20%, 40%, v.v. và dần dần xây dựng hoặc giảm các vị thế theo các tín hiệu khác nhau của thị trường.
Logic cốt lõi của chiến lược này bao gồm ba điều kiện tham gia chính và hai điều kiện thoát:
Chiến lược này xây dựng một hệ thống giao dịch tương đối hoàn chỉnh thông qua nhiều đường trung bình động giao nhau và hệ thống tối ưu hóa thoái lui. Ưu điểm của nó nằm ở nhiều cơ chế xác nhận và quản lý vị thế linh hoạt, nhưng nó cũng có những nhược điểm cố hữu như độ trễ trung bình động. Bằng cách giới thiệu các phương pháp tối ưu hóa như bộ lọc xu hướng, tính ổn định và lợi nhuận của chiến lược có thể được cải thiện hơn nữa. Chiến lược này phù hợp để sử dụng trong các thị trường có xu hướng rõ ràng và các nhà giao dịch cần tối ưu hóa các thông số dựa trên điều kiện thị trường thực tế.
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA Strategy with Price & EMA5 & EMA8 < EMA50 Condition", overlay=true, margin_long=100, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)
// ==============================
// INPUTS
// ==============================
lengthEMA5 = input.int(5, "EMA5 Length")
lengthEMA8 = input.int(8, "EMA8 Length")
lengthEMA13 = input.int(13, "EMA13 Length")
lengthEMA21 = input.int(21, "EMA21 Length")
lengthEMA50 = input.int(50, "EMA50 Length")
// Tam pozisyon boyutu (örnek: 100 birim)
full_position = 100.0
qty20 = full_position * 0.2
qty40 = full_position * 0.4
// ==============================
// EMA HESAPLAMALARI
// ==============================
ema5 = ta.ema(close, lengthEMA5)
ema8 = ta.ema(close, lengthEMA8)
ema13 = ta.ema(close, lengthEMA13)
ema21 = ta.ema(close, lengthEMA21)
ema50 = ta.ema(close, lengthEMA50)
// ==============================
// KESİŞİMLERİ TESPİT FONKSİYONLARI
// ==============================
crossUp(src1, src2) => ta.crossover(src1, src2)
crossDown(src1, src2) => ta.crossunder(src1, src2)
// ==============================
// STRATEJİ KOŞULLARI
// ==============================
// Adım 1: EMA5, EMA8’i yukarı keserse %20’lik alım
step1_condition = crossUp(ema5, ema8)
// Adım 2: EMA5, EMA8’i yukarı kestikten sonra EMA5, EMA13’ü de yukarı keserse %20 daha alım
step2_condition = crossUp(ema5, ema13)
// Adım 3: EMA8, EMA21’i yukarı keserse %40 alım
step3_condition = crossUp(ema8, ema21)
// Çıkış koşulları:
// EMA5, EMA13’ü aşağı keserse pozisyonun %50’sini kapat.
// EMA8, EMA21’i aşağı keserse tüm pozisyonu kapat.
half_close_condition = crossDown(ema5, ema13)
full_close_condition = crossDown(ema8, ema21)
// Düşüşlerde EMA50'ye dokunma -> %20 alım
pullback_condition = low <= ema50 or close <= ema50
// Fiyat tekrar EMA50'nin üzerine çıkarsa -> %20 alım
above_ema50_condition = crossUp(close, ema50)
// Yeni ek koşul:
// Fiyat, EMA5 ve EMA8’in herbiri EMA50’nin altındaysa tüm pozisyon kapat.
// Bu durum tam bir düşüş senaryosunu işaret eder.
all_below_condition = (close < ema50) and (ema5 < ema50) and (ema8 < ema50)
// Mevcut pozisyon büyüklüğü
pos_size = strategy.position_size
// ==============================
// POZİSYON GİRİŞLERİ
// ==============================
if (step1_condition and pos_size == 0)
strategy.entry("Step1", strategy.long, qty=qty20)
if (step2_condition and strategy.opentrades < 2)
strategy.entry("Step2", strategy.long, qty=qty20)
if (step3_condition and strategy.opentrades < 3)
strategy.entry("Step3", strategy.long, qty=qty40)
// Pullback: Fiyat EMA50'ye temas ederse ve pozisyon yoksa %20 alım
if (pullback_condition and strategy.opentrades == 0)
strategy.entry("Pullback", strategy.long, qty=qty20)
// Fiyat EMA50’nin üzerine çıkarsa ve pozisyon %100'e ulaşmamışsa %20 alım
if (above_ema50_condition and strategy.opentrades < 4)
strategy.entry("Above50", strategy.long, qty=qty20)
// ==============================
// POZİSYON YÖNETİMİ (ÇIKIŞLAR)
// ==============================
if (all_below_condition and strategy.opentrades > 0)
// Tüm pozisyonu kapat çünkü sert düşüş senaryosuna girildi
strategy.close("Step3")
strategy.close("Step2")
strategy.close("Step1")
strategy.close("Pullback")
strategy.close("Above50")
else
// Yarı kapatma (EMA5, EMA13 aşağı kesişimi)
if (half_close_condition)
totalTrades = strategy.opentrades
// Öncelikle en son açılan en büyük pozisyonu kapatarak kademeli küçültme
if (totalTrades >= 3)
strategy.close("Step3") // Bu 40% kapatır
else if (totalTrades == 2)
strategy.close("Step2") // Bu 20% kapatır
else if (totalTrades == 1)
strategy.close("Step1") // Bu da 20% kapatır (tamamen çıkar, ama basitlik için böyle)
// Tam kapatma (EMA8, EMA21 aşağı kesişimi)
if (full_close_condition)
// Açık olan tüm pozisyonları kapat
strategy.close("Step3")
strategy.close("Step2")
strategy.close("Step1")
strategy.close("Pullback")
strategy.close("Above50")
// ==============================
// GÖRSELLEŞTİRME
// ==============================
plot(ema5, "EMA5", color=color.new(color.yellow, 0))
plot(ema8, "EMA8", color=color.new(color.blue, 0))
plot(ema13, "EMA13", color=color.new(color.green, 0))
plot(ema21, "EMA21", color=color.new(color.red, 0))
plot(ema50, "EMA50", color=color.new(color.purple, 0))