PHP Code:
//+------------------------------------------------------------------+
//| Kinetik_Otonom_Block.mq5 |
//| Kinetik Komuta Merkezi |
//+------------------------------------------------------------------+
#property copyright "Kinetik Otonom Sistem"
#property version "1.00"
#include <Trade\Trade.mqh>
// --- KULLANICI AYARLARI ---
input double InpLotSize = 0.1; // Ýþlem Hacmi (Lot)
input ulong InpMagicNumber = 100100; // Uzman Danýþman Kimliði (Magic Number)
// --- GLOBAL NESNELER VE DEÐÝÞKENLER ---
CTrade trade;
int handle_atr5, handle_atr20, handle_atr14;
datetime last_bar_time = 0;
// Ayna (Mutlak Çekirdek) Hafýza Deðiþkenleri
int a_trend = 0;
double a_sar = 0.0, a_ep = 0.0, a_af = 0.0;
double prev_low1 = 0.0, prev_low2 = 0.0, prev_high1 = 0.0, prev_high2 = 0.0;
//+------------------------------------------------------------------+
//| BAÞLANGIÇ (INITIALIZATION) |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(InpMagicNumber);
// ATR Sensörlerinin Tanýmlanmasý
handle_atr5 = iATR(_Symbol, _Period, 5);
handle_atr20 = iATR(_Symbol, _Period, 20);
handle_atr14 = iATR(_Symbol, _Period, 14);
if(handle_atr5 == INVALID_HANDLE || handle_atr20 == INVALID_HANDLE || handle_atr14 == INVALID_HANDLE)
{
Print("ATR Sensörleri yüklenemedi!");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| HER YENÝ TÝK (FÝYAT HAREKETÝ) GELDÝÐÝNDE ÇALIÞACAK BLOK |
//+------------------------------------------------------------------+
void OnTick()
{
// 1. MÜHÜR: Sadece yeni bar (mum) kapandýðýnda iþlem yap (Sýfýr Repaint)
datetime current_time = iTime(_Symbol, _Period, 0);
if(current_time == last_bar_time) return;
// Veri Dizilerinin Hazýrlanmasý
double atr5[1], atr20[1], atr14[1];
MqlRates rates[];
ArraySetAsSeries(rates, true);
// Son 35 mumun verisini çekiyoruz (Dinamik blok hesaplamasý için gerekli)
if(CopyRates(_Symbol, _Period, 1, 35, rates) < 35) return;
if(CopyBuffer(handle_atr5, 0, 1, 1, atr5) <= 0) return;
if(CopyBuffer(handle_atr20, 0, 1, 1, atr20) <= 0) return;
if(CopyBuffer(handle_atr14, 0, 1, 1, atr14) <= 0) return;
double close1 = rates[0].close;
double high1 = rates[0].high;
double low1 = rates[0].low;
double open1 = rates[0].open;
// 2. MUTLAK AYNA ÇEKÝRDEÐÝ GÜNCELLEMESÝ (SAR Mantýðý)
if(a_trend == 0)
{
a_trend = (close1 >= open1) ? 1 : -1;
a_sar = (a_trend > 0) ? low1 : high1;
a_ep = (a_trend > 0) ? high1 : low1;
a_af = 0.02;
}
else
{
double nextsar = a_sar;
if(a_trend > 0)
{
if(high1 > a_ep) { a_ep = high1; a_af = MathMin(0.2, a_af + 0.02); }
nextsar = a_sar + a_af * (a_ep - a_sar);
if(prev_low1 != 0 && prev_low2 != 0) nextsar = MathMin(MathMin(prev_low1, prev_low2), nextsar);
if(nextsar > low1) { a_trend = -1; nextsar = a_ep; a_ep = low1; a_af = 0.02; }
}
else
{
if(low1 < a_ep) { a_ep = low1; a_af = MathMin(0.2, a_af + 0.02); }
nextsar = a_sar + a_af * (a_ep - a_sar);
if(prev_high1 != 0 && prev_high2 != 0) nextsar = MathMax(MathMax(prev_high1, prev_high2), nextsar);
if(nextsar < high1) { a_trend = 1; nextsar = a_ep; a_ep = high1; a_af = 0.02; }
}
a_sar = nextsar;
}
// Ayna Geçmiþini Güncelle
prev_low2 = prev_low1; prev_low1 = low1;
prev_high2 = prev_high1; prev_high1 = high1;
// 3. KÝNETÝK OTONOM BLOK HESAPLAMASI (Sensör)
double hiz_orani = MathMin(atr5[0] / atr20[0], 1.5);
int otonom_periyot = (int)MathFloor(30.0 - (15.0 * hiz_orani));
otonom_periyot = (int)MathMax(5, MathMin(30, otonom_periyot));
double blok_toplam = 0.0;
// rates dizisi Series olarak ayarlandýðý için rates[0] son kapanan mumdur.
for(int i = 0; i < otonom_periyot; i++)
{
blok_toplam += rates[i].close;
}
double dinamik_blok_ort = (otonom_periyot > 0) ? (blok_toplam / otonom_periyot) : close1;
// 4. MÜHÜRLÜ KARAR VE SÝNYAL ÜRETÝMÝ
bool buy_sig = (close1 > dinamik_blok_ort) && (a_trend == 1);
bool sell_sig = (close1 < dinamik_blok_ort) && (a_trend == -1);
// Risk Yönetimi (ATR 14 tabanlý Dinamik TP/SL)
double d_atr = atr14[0];
double sl_distance = d_atr * 2.0;
double tp_distance = d_atr * 4.0;
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// 5. ÝÞLEM YÖNETÝMÝ (Emir Ýletimi)
if(buy_sig)
{
ClosePositions(POSITION_TYPE_SELL); // Açýk Short varsa kapat
if(!PositionExists(POSITION_TYPE_BUY))
{
trade.Buy(InpLotSize, _Symbol, ask, ask - sl_distance, ask + tp_distance, "Kinetik AL");
}
}
if(sell_sig)
{
ClosePositions(POSITION_TYPE_BUY); // Açýk Long varsa kapat
if(!PositionExists(POSITION_TYPE_SELL))
{
trade.Sell(InpLotSize, _Symbol, bid, bid + sl_distance, bid - tp_distance, "Kinetik SAT");
}
}
// Döngü sonunda bar zamanýný kaydet
last_bar_time = current_time;
}
//+------------------------------------------------------------------+
//| POZÝSYON KONTROL YARDIMCI FONKSÝYONLARI |
//+------------------------------------------------------------------+
bool PositionExists(ENUM_POSITION_TYPE type)
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
{
if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == type)
return true;
}
}
return false;
}
void ClosePositions(ENUM_POSITION_TYPE type)
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
{
if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == type)
{
trade.PositionClose(ticket);
}
}
}
}
//+------------------------------------------------------------------+
Yer Ýmleri