Kod:
// Kinetik Komuta Merkezi - V9.4 EKRDEKL ARBTRAJ MATRIX
// =============================================================================
// V9.3'e teekkrle. Bu srm, V9.3'n iskeletini ve estetiini koruyup
// mantksal sklatrmalar getirir:
//
// [1] cephenin GEREKTEN kullanlmas.
// V9.3'te B/C cephesi hesaplanyor ama hi sinyale girmiyordu. V9.4'te
// A/B ve A/C e zamanl extreme olmal (AND); B/C filtre grevi grr
// eer B/C de extreme ise rejim-wide bir sapma vardr, pair-specific
// deil, ileme girilmez. Bylece "Tri-Core" ismi gerekten cepheli.
//
// [2] Simetrik giri/k mant.
// V9.3 girii AND ile sklatrm, k OR ile gevetmiti bu
// konverjansn orta noktasnda erken kmaya yol ayordu. V9.4 her iki
// ynde AND kullanr.
//
// [3] Rejim Krlma Stop'u (yeni).
// Cointegration krlrsa spread asla geri gelmez. Z, entryZ stopZmult
// eiini aarsa pozisyon zorla kapatlr "umut etmek" yerine kayp
// snrlanr.
//
// [4] HUD ile strateji mant bire bir.
// V9.3 HUD'unda |z| >= entryZ aktif saylrken strateji iaretli z
// kullanyordu baz durumlarda HUD "AKTF" gsterir ama emir
// gelmezdi. V9.4'te ikisi de iaretli z'yi kullanr.
//
// [5] Parametre haline geldi: korelasyon penceresi, integrity arlklar,
// BC filtre eii, stop arpan. Empirik ayar kolayl iin.
//
// NOTLAR (limitler):
// Bu strateji yalnz A bacanda pozisyon alr; gerek market-neutral
// pair trading iin B/C bacaklarn broker tarafnda manuel veya ayr
// bir strateji ile karlamak gerekir. Backtest P&L'i tek bacakl.
// log(A/B) sabit 1:1 hedge varsayar. Fiyat seviyeleri ok farkl
// hisselerde dinamik β (rolling regression) daha dorudur sonraki
// srme aday.
// Spread'in duraanl (cointegration) ayrca test edilmeli; integrity
// skoru bunun zayf bir vekilidir.
// =============================================================================
//@version=6
strategy("V9.4 Tri-Core Arbitraj", overlay=true, initial_capital=10000,
default_qty_type=strategy.percent_of_equity, default_qty_value=100,
commission_type=strategy.commission.percent, commission_value=0.04,
pyramiding=1)
// ═════════════════════════════════════════════════════════════════════════
// 1. GRDLER VE CEPHANELK
// ═════════════════════════════════════════════════════════════════════════
grp_sym = "Hedef Hisseler (gen Kombinasyon)"
symA = input.symbol("BIST:MCARD", "A Hissesi", group=grp_sym)
symB = input.symbol("BIST:NETCD", "B Hissesi", group=grp_sym)
symC = input.symbol("BIST:DOFRB", "C Hissesi", group=grp_sym)
grp_logic = "ift Namlulu Motor Ayarlar"
longZLength = input.int(240, "Ar Namlu (Uzun Z-Score)", minval=100, maxval=500, group=grp_logic)
shortZLength = input.int(80, "Hafif Namlu (Ksa Z-Score)", minval=20, maxval=150, group=grp_logic)
entryZ = input.float(1.70, "Taarruz Snr (+/-)", step=0.1, group=grp_logic)
exitZ = input.float(0.50, "Tahliye Snr (Ksa Namluya Gre)", step=0.1, group=grp_logic)
stopZmult = input.float(1.60, "Rejim Krlma arpan (stop)", step=0.1, minval=1.1, group=grp_logic,
tooltip="Z, entryZ bu arpan geerse pozisyon zorla kapatlr cointegration krlmas varsaylr.")
minIntegrity = input.float(55.0, "Minimum Gvenlik Puan", minval=0.0, maxval=100.0, group=grp_logic)
grp_integrity = "ntegrity Arlklar"
w_corr = input.float(0.45, "Korelasyon Arl", minval=0.0, maxval=1.0, step=0.05, group=grp_integrity)
w_drift = input.float(0.30, "Mean Drift Arl", minval=0.0, maxval=1.0, step=0.05, group=grp_integrity)
w_vol = input.float(0.25, "Vol Expansion Arl", minval=0.0, maxval=1.0, step=0.05, group=grp_integrity)
corrLen = input.int(60, "Korelasyon Penceresi", minval=20, maxval=240, group=grp_integrity)
grp_bc = "BC Cephesi (Filtre)"
use_bc_filter = input.bool(true, "BC sakin deilse ilem ama", group=grp_bc,
tooltip="A/B ve A/C extreme iken B/C de extreme ise sapma pair-specific deil rejim-wide'dr; ilem atlanr.")
bc_calm_mult = input.float(1.00, "BC Sakinlik Eii ( entryZ)", step=0.1, minval=0.5, group=grp_bc)
grp_time = "Zaman Kalkan"
hide_session = input.session("1810-0950", "Gizlenecek l Saatler", group=grp_time)
tz = input.string("UTC+3", "Saat Dilimi", group=grp_time)
in_hide_session = not na(time(timeframe.period, hide_session, tz))
should_hide = (dayofweek == dayofweek.saturday or dayofweek == dayofweek.sunday) or in_hide_session
// ═════════════════════════════════════════════════════════════════════════
// 2. VER STHBARATI
// ═════════════════════════════════════════════════════════════════════════
cA_raw = request.security(symA, timeframe.period, close, gaps=barmerge.gaps_on)
cB_raw = request.security(symB, timeframe.period, close, gaps=barmerge.gaps_on)
cC_raw = request.security(symC, timeframe.period, close, gaps=barmerge.gaps_on)
var float cA = na
var float cB = na
var float cC = na
if not should_hide and cA_raw > 0 and cB_raw > 0 and cC_raw > 0
cA := cA_raw
cB := cB_raw
cC := cC_raw
// ═════════════════════════════════════════════════════════════════════════
// 3. KANTTATF MOTOR
// ═════════════════════════════════════════════════════════════════════════
f_arb_engine(price1, price2) =>
float ratio = (not na(price1) and not na(price2)) ? (price1 / price2) : na
float logRatio = not na(ratio) ? math.log(ratio) : na
float meanL = ta.sma(logRatio, longZLength)
float devL = ta.stdev(logRatio, longZLength)
float zLong = devL > 0 ? (logRatio - meanL) / devL : na
float meanS = ta.sma(logRatio, shortZLength)
float devS = ta.stdev(logRatio, shortZLength)
float zShort = devS > 0 ? (logRatio - meanS) / devS : na
float ret1 = (not na(price1) and not na(price1[1])) ? math.log(price1 / nz(price1[1], price1)) : na
float ret2 = (not na(price2) and not na(price2[1])) ? math.log(price2 / nz(price2[1], price2)) : na
float pairCorr = ta.correlation(ret1, ret2, corrLen)
float volShortInt = ta.stdev(logRatio, 20)
float volBaseInt = ta.stdev(logRatio, corrLen)
float volExpansion = volBaseInt > 0 ? volShortInt / volBaseInt : na
float meanDrift = (devL > 0 and not na(meanL[20])) ? math.abs(meanL - meanL[20]) / devL : na
float corrNorm = na(pairCorr) ? 0.0 : math.max(0, math.min(1, (pairCorr + 1.0) / 2.0))
float driftNorm = na(meanDrift) ? 0.0 : math.max(0, math.min(1, 1.0 - meanDrift / 0.45))
float volNorm = na(volExpansion) ? 0.0 : math.max(0, math.min(1, 1.0 - math.max(0, volExpansion - 1.0) / 0.45))
float wsum = w_corr + w_drift + w_vol
float integrityScore = wsum > 0
? math.max(0, math.min(100, 100.0 * (w_corr * corrNorm + w_drift * driftNorm + w_vol * volNorm) / wsum))
: 0.0
bool integrityOk = integrityScore >= minIntegrity
[zLong, zShort, integrityScore, integrityOk]
[zL_AB, zS_AB, int_AB, ok_AB] = f_arb_engine(cA, cB) // Cephe 1: A vs B
[zL_AC, zS_AC, int_AC, ok_AC] = f_arb_engine(cA, cC) // Cephe 2: A vs C
[zL_BC, zS_BC, int_BC, ok_BC] = f_arb_engine(cB, cC) // Cephe 3: B vs C (filtre)
// ═════════════════════════════════════════════════════════════════════════
// 4. STRATEJ FZYONU Simetrik AND mant + BC filtresi
// ═════════════════════════════════════════════════════════════════════════
// Felsefe: A hissesi B ve C ile karlatrldnda HER K cephede de
// ayn ynde extreme ise, anomali muhtemelen A'ya zgdr. Tek cephede
// extreme ise B veya C anomali olabilir; A'nn yn belirsizdir.
bool bc_calm = not use_bc_filter or
(math.abs(zL_BC) < entryZ * bc_calm_mult and math.abs(zS_BC) < entryZ * bc_calm_mult)
bool t_long_giris = zL_AB <= -entryZ and zS_AB <= -entryZ and ok_AB and
zL_AC <= -entryZ and zS_AC <= -entryZ and ok_AC and
bc_calm and barstate.isconfirmed
bool t_short_giris = zL_AB >= entryZ and zS_AB >= entryZ and ok_AB and
zL_AC >= entryZ and zS_AC >= entryZ and ok_AC and
bc_calm and barstate.isconfirmed
if t_long_giris
strategy.entry("LONG", strategy.long)
if t_short_giris
strategy.entry("SHORT", strategy.short)
// Tahliye simetrik AND: her iki cephe de mean'e dnm olmal
bool tahliye_long = zS_AB >= -exitZ and zS_AC >= -exitZ
bool tahliye_short = zS_AB <= exitZ and zS_AC <= exitZ
if strategy.position_size > 0 and tahliye_long and barstate.isconfirmed
strategy.close("LONG", comment="Makas Kapand")
if strategy.position_size < 0 and tahliye_short and barstate.isconfirmed
strategy.close("SHORT", comment="Makas Kapand")
// Rejim Krlma Stop'u Z, entryZ stopZmult'u aarsa cointegration krld say
float stop_threshold = entryZ * stopZmult
bool stop_long = strategy.position_size > 0 and (zL_AB <= -stop_threshold or zL_AC <= -stop_threshold)
bool stop_short = strategy.position_size < 0 and (zL_AB >= stop_threshold or zL_AC >= stop_threshold)
if (stop_long or stop_short) and barstate.isconfirmed
strategy.close_all(comment="REJM KIRILMA! Stop")
// Acil Tahliye (Gvenlik hlali) V9.3'ten korundu
bool acil_tahliye = (not ok_AB and math.abs(zL_AB) >= entryZ) or
(not ok_AC and math.abs(zL_AC) >= entryZ)
if acil_tahliye and strategy.position_size != 0 and barstate.isconfirmed
strategy.close_all(comment="INTEGRITY BLOCK! Tahliye")
// ═════════════════════════════════════════════════════════════════════════
// 5. KOMUTA PANEL (HUD) strateji mantyla bire bir
// ═════════════════════════════════════════════════════════════════════════
color position_bar_col = strategy.position_size > 0 ? color.new(color.aqua, 0)
: strategy.position_size < 0 ? color.new(color.fuchsia, 0)
: color.new(color.gray, 50)
barcolor(position_bar_col, title="Operasyon Bar Rengi")
float br_kar = strategy.grossprofit
float br_zarar = strategy.grossloss
float kar_faktoru = br_zarar == 0 ? (br_kar > 0 ? 99.99 : 0.0) : (br_kar / br_zarar)
if barstate.islast
string nA = str.replace_all(symA, "BIST:", "")
string nB = str.replace_all(symB, "BIST:", "")
string nC = str.replace_all(symC, "BIST:", "")
color c_bull = color.rgb(34, 197, 166)
color c_bear = color.rgb(244, 114, 182)
color c_neut = color.rgb(245, 189, 92)
color c_filter = color.rgb(160, 120, 220)
var table hud = table.new(position.top_right, 4, 7,
bgcolor=color.new(#0D0D0D, 15),
border_color=color.new(color.gray, 70),
border_width=1)
// st Balk
table.cell(hud, 0, 0, "⚔️ V9.4 TRI-CORE MATRIX ⚔️", text_color=color.white,
text_size=size.normal, bgcolor=color.new(#181818, 0), text_halign=text.align_center)
table.merge_cells(hud, 0, 0, 3, 0)
// Stun Balklar
table.cell(hud, 0, 1, "Kombinasyon (Cephe)", text_color=color.gray, text_size=size.small, bgcolor=color.new(#111111, 0))
table.cell(hud, 1, 1, "Z-Score (Ar/Hafif)", text_color=color.gray, text_size=size.small, bgcolor=color.new(#111111, 0))
table.cell(hud, 2, 1, "Gvenlik", text_color=color.gray, text_size=size.small, bgcolor=color.new(#111111, 0))
table.cell(hud, 3, 1, "Sistem Emri", text_color=color.gray, text_size=size.small, bgcolor=color.new(#111111, 0))
// aretli (signed) Z aktiflik testi strateji mantyla AYNI
bool ab_long_side = zL_AB <= -entryZ and zS_AB <= -entryZ and ok_AB
bool ab_short_side = zL_AB >= entryZ and zS_AB >= entryZ and ok_AB
bool ac_long_side = zL_AC <= -entryZ and zS_AC <= -entryZ and ok_AC
bool ac_short_side = zL_AC >= entryZ and zS_AC >= entryZ and ok_AC
// 1. Cephe (A vs B)
string emir_AB = ab_short_side ? nA + " SAT\n" + nB + " AL"
: ab_long_side ? nA + " AL\n" + nB + " SAT"
: "BEKLE"
color c_AB = ab_short_side ? c_bear : ab_long_side ? c_bull : color.gray
table.cell(hud, 0, 2, nA + " / " + nB, text_color=color.white, text_size=size.small, bgcolor=color.new(#161616, 0))
table.cell(hud, 1, 2, str.tostring(zL_AB, "#.##") + " | " + str.tostring(zS_AB, "#.##"),
text_color=(ab_long_side or ab_short_side) ? c_neut : color.white, text_size=size.small, bgcolor=color.new(#161616, 0))
table.cell(hud, 2, 2, str.tostring(int_AB, "#"), text_color=ok_AB ? c_bull : c_bear, text_size=size.small, bgcolor=color.new(#161616, 0))
table.cell(hud, 3, 2, emir_AB, text_color=c_AB, text_size=size.small, bgcolor=color.new(#161616, 0))
// 2. Cephe (A vs C)
string emir_AC = ac_short_side ? nA + " SAT\n" + nC + " AL"
: ac_long_side ? nA + " AL\n" + nC + " SAT"
: "BEKLE"
color c_AC = ac_short_side ? c_bear : ac_long_side ? c_bull : color.gray
table.cell(hud, 0, 3, nA + " / " + nC, text_color=color.white, text_size=size.small, bgcolor=color.new(#1A1A1A, 0))
table.cell(hud, 1, 3, str.tostring(zL_AC, "#.##") + " | " + str.tostring(zS_AC, "#.##"),
text_color=(ac_long_side or ac_short_side) ? c_neut : color.white, text_size=size.small, bgcolor=color.new(#1A1A1A, 0))
table.cell(hud, 2, 3, str.tostring(int_AC, "#"), text_color=ok_AC ? c_bull : c_bear, text_size=size.small, bgcolor=color.new(#1A1A1A, 0))
table.cell(hud, 3, 3, emir_AC, text_color=c_AC, text_size=size.small, bgcolor=color.new(#1A1A1A, 0))
// 3. Cephe (B vs C) FLTRE: ilem retmez, karar onaylar/bloklar
string emir_BC = bc_calm ? "SAKN (onay)" : "REJM (blok)"
color c_BC = bc_calm ? c_bull : c_filter
table.cell(hud, 0, 4, nB + " / " + nC + " (filtre)", text_color=color.white, text_size=size.small, bgcolor=color.new(#161616, 0))
table.cell(hud, 1, 4, str.tostring(zL_BC, "#.##") + " | " + str.tostring(zS_BC, "#.##"),
text_color=color.white, text_size=size.small, bgcolor=color.new(#161616, 0))
table.cell(hud, 2, 4, str.tostring(int_BC, "#"), text_color=ok_BC ? c_bull : c_bear, text_size=size.small, bgcolor=color.new(#161616, 0))
table.cell(hud, 3, 4, emir_BC, text_color=c_BC, text_size=size.small, bgcolor=color.new(#161616, 0))
// Sistem Karar Satr gerek strateji kts (AB ∧ AC ∧ bc_calm)
bool active_long = ab_long_side and ac_long_side and bc_calm
bool active_short = ab_short_side and ac_short_side and bc_calm
string sistem_emri = active_long ? "LONG " + nA + " (AL)"
: active_short ? "SHORT " + nA + " (SAT)"
: "POZSYON YOK"
color c_sistem = active_long ? c_bull : active_short ? c_bear : color.gray
table.cell(hud, 0, 5, "Sistem Karar", text_color=color.white, text_size=size.small, bgcolor=color.new(#0a0a0a, 0))
table.merge_cells(hud, 0, 5, 2, 5)
table.cell(hud, 3, 5, sistem_emri, text_color=c_sistem, text_size=size.small, bgcolor=color.new(#0a0a0a, 0))
// Kr Faktr
table.cell(hud, 0, 6, "Sistem Kr Faktr", text_color=color.white, text_size=size.small, bgcolor=color.new(#111111, 0))
table.merge_cells(hud, 0, 6, 2, 6)
table.cell(hud, 3, 6, str.tostring(kar_faktoru, "#.##"),
text_color=kar_faktoru >= 1.2 ? c_bull : c_bear, text_size=size.small, bgcolor=color.new(#111111, 0))
Yer mleri