//+------------------------------------------------------------------+
//| AyuRobot.mq5 |
//| EA M1 real-time, entry searah candle, close jika lawan arah |
//+------------------------------------------------------------------+
#property strict
#include <Trade/[Link]>
CTrade trade;
input double LotSize = 0.1; // Ukuran lot
input ENUM_TIMEFRAMES TimeFrame = PERIOD_M1; // Timeframe candle
input bool CloseOnOpposite = true; // Tutup posisi jika candle berlawanan
int lastDirection = 0;
int OnInit()
{
lastDirection = 0;
return(INIT_SUCCEEDED);
}
void OnTick()
{
double openPrice = iOpen(_Symbol, TimeFrame, 0);
double closePrice = iClose(_Symbol, TimeFrame, 0);
int currentDirection = 0;
if(closePrice > openPrice) currentDirection = 1;
else if(closePrice < openPrice) currentDirection = -1;
bool hasPosition = false;
long posType = -1;
ulong posTicket = 0;
for(int i = 0; i < PositionsTotal(); i++) // Loop yang benar
{
if(hasPosition && CloseOnOpposite)
{
if((currentDirection == 1 && posType == POSITION_TYPE_SELL) ||
(currentDirection == -1 && posType == POSITION_TYPE_BUY))
{
[Link](posTicket);
hasPosition = false;
}
}
if(!hasPosition && currentDirection != 0)
{
if(currentDirection == 1)
[Link](LotSize, _Symbol, 0, 0, 0, "AyuRobot Buy");
else if(currentDirection == -1)
[Link](LotSize, _Symbol, 0, 0, 0, "AyuRobot Sell");
}
lastDirection = currentDirection;
}