EA_2Warna_M5/EA_2Warna_M5.mq5
2026-07-21 22:43:00 +00:00

122 lines
No EOL
4.3 KiB
MQL5

//+------------------------------------------------------------------+
//| EA_2Warna_M5.mq5 |
//| Copyright 2026, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.20"
#include <Trade\Trade.mqh>
CTrade trade;
//--- Input Parameters
input double InpLotSize = 0.01; // Lot Size
input int InpStopLossPips = 0; // Stop Loss (0 = Tanpa SL, isi jika butuh)
input ulong InpMagicNumber = 123456; // Magic Number
//--- Global Variables
datetime g_lastBarTime;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
trade.SetExpertMagicNumber(InpMagicNumber);
g_lastBarTime = 0;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Cek Candle Baru
datetime currentBarTime = iTime(_Symbol, _Period, 0);
if(currentBarTime == g_lastBarTime)
return;
// Ambil Data 3 Candle Terakhir yang Sudah Selesai (Index 1, 2, 3)
MqlRates rates[];
ArraySetAsSeries(rates, true);
if(CopyRates(_Symbol, _Period, 1, 3, rates) < 3)
return;
// Definisi Warna Candle
// rates[0] = Candle 1 (terbaru selesai)
// rates[1] = Candle 2
// rates[2] = Candle 3 (paling lama)
bool c1_Bullish = (rates[0].close > rates[0].open); // Hijau
bool c1_Bearish = (rates[0].close < rates[0].open); // Merah
bool c2_Bullish = (rates[1].close > rates[1].open); // Hijau
bool c2_Bearish = (rates[1].close < rates[1].open); // Merah
bool c3_Bullish = (rates[2].close > rates[2].open); // Hijau
bool c3_Bearish = (rates[2].close < rates[2].open); // Merah
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
// --- LOGIKA CLOSE POSISI TERLEBIH DAHULU ---
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(PositionGetSymbol(i) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
{
ulong ticket = PositionGetInteger(POSITION_TICKET);
long posType = PositionGetInteger(POSITION_TYPE);
// Close BUY jika pola: Hijau, Hijau, lalu Merah
if(posType == POSITION_TYPE_BUY)
{
if(c3_Bullish && c2_Bullish && c1_Bearish)
{
trade.PositionClose(ticket);
}
}
// Close SELL jika pola: Merah, Merah, lalu Hijau
else if(posType == POSITION_TYPE_SELL)
{
if(c3_Bearish && c2_Bearish && c1_Bullish)
{
trade.PositionClose(ticket);
}
}
}
}
// --- LOGIKA ENTRY BARU (Jika tidak ada posisi terbuka) ---
if(PositionsTotal() == 0)
{
// Sinyal BUY: 2 Candle Hijau berturut-turut (Candle 2 & Candle 1)
if(c2_Bullish && c1_Bullish)
{
double sl = (InpStopLossPips > 0) ? ask - (InpStopLossPips * 10 * point) : 0;
if(trade.Buy(InpLotSize, _Symbol, ask, sl, 0, "EA 2 Warna Buy"))
{
g_lastBarTime = currentBarTime;
}
}
// Sinyal SELL: 2 Candle Merah berturut-turut (Candle 2 & Candle 1)
else if(c2_Bearish && c1_Bearish)
{
double sl = (InpStopLossPips > 0) ? bid + (InpStopLossPips * 10 * point) : 0;
if(trade.Sell(InpLotSize, _Symbol, bid, sl, 0, "EA 2 Warna Sell"))
{
g_lastBarTime = currentBarTime;
}
}
}
}
//+------------------------------------------------------------------+