
이 전략은 다중 지수 이동 평균(EMA) 교차와 되돌림 최적화를 기반으로 하는 양적 거래 시스템입니다. EMA5, EMA8, EMA13, EMA21, EMA50의 5개 이동평균을 사용하고, 다양한 기간의 이동평균 간의 교차관계와 가격과 이동평균 간의 포지션 관계를 관찰하여 일괄 포지션 오픈 및 동적 청산을 실현합니다. 이 전략은 자금 관리 시스템을 채택하여 포지션을 20%, 40% 등 다양한 비율로 나누고 다양한 시장 신호에 따라 점진적으로 포지션을 늘리거나 줄입니다.
전략의 핵심 논리에는 세 가지 주요 진입 조건과 두 가지 종료 조건이 포함됩니다.
이 전략은 다중 이동평균 교차와 되돌림 최적화 시스템을 통해 비교적 완전한 거래 시스템을 구축합니다. 이 방법은 다양한 확인 메커니즘과 유연한 포지션 관리가 장점이지만, 이동평균 지연과 같은 본질적인 단점도 가지고 있습니다. 트렌드 필터와 같은 최적화 방법을 도입함으로써 전략의 안정성과 수익성을 더욱 개선할 수 있습니다. 이 전략은 추세가 뚜렷한 시장에서 사용하기에 적합하며, 트레이더는 실제 시장 상황에 따라 매개변수를 최적화해야 합니다.
/*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))