//+------------------------------------------------------------------+
//| PriceAction_EA.mq4 |
//| Copyright 2024, MetaQuotes Ltd. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property strict
// Input parameters
input double LotSize = 0.1; // Lot size
input int Slippage = 3; // Slippage
input int TargetProfitMultiplier = 2; // Target profit multiplier based on ATR
input int StopLossMultiplier = 3; // Stop loss multiplier based on ATR
input int ATRPeriod = 14; // ATR period
input int BreakoutPeriod = 20; // Breakout period
input int MagicNumber = 123456; // Magic number for orders
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check for bullish price action, breakout, and open buy orders
if (IsBullishPriceAction() || IsBreakoutUp())
{
// Close any open sell orders
CloseSellOrders();
// Open a buy order if there are no existing buy orders
if (CountOpenBuyOrders() == 0)
{
OpenOrder(OP_BUY);
}
}
// Check for bearish price action, breakout, and open sell orders
if (IsBearishPriceAction() || IsBreakoutDown())
{
// Close any open buy orders
CloseBuyOrders();
// Open a sell order if there are no existing sell orders
if (CountOpenSellOrders() == 0)
{
OpenOrder(OP_SELL);
}
}
// Check for target profit and close positions
CheckTargetProfit();
}
//+------------------------------------------------------------------+
//| Check for bullish price action (Pin Bar, Bullish Engulfing) |
//+------------------------------------------------------------------+
bool IsBullishPriceAction()
{
// Check for Bullish Pin Bar
if (Close[1] > Open[1] && Low[1] < Low[2] && Low[1] < Low[3] && Close[1] > (Open[1] + High[1] + Low[1]) / 3)
{
Print("Bullish Pin Bar detected");
return true;
}
// Check for Bullish Engulfing
if (Close[1] > Open[1] && Open[1] < Close[2] && Close[1] > Open[2] && Open[2] > Close[2])
{
Print("Bullish Engulfing detected");
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Check for bearish price action (Pin Bar, Bearish Engulfing) |
//+------------------------------------------------------------------+
bool IsBearishPriceAction()
{
// Check for Bearish Pin Bar
if (Close[1] < Open[1] && High[1] > High[2] && High[1] > High[3] && Close[1] < (Open[1] + Low[1] + High[1]) / 3)
{
Print("Bearish Pin Bar detected");
return true;
}
// Check for Bearish Engulfing
if (Close[1] < Open[1] && Open[1] > Close[2] && Close[1] < Open[2] && Open[2] < Close[2])
{
Print("Bearish Engulfing detected");
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Check for breakout up |
//+------------------------------------------------------------------+
bool IsBreakoutUp()
{
double highestHigh = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, BreakoutPeriod, 1));
if (Close[0] > highestHigh)
{
Print("Breakout Up detected");
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Check for breakout down |
//+------------------------------------------------------------------+
bool IsBreakoutDown()
{
double lowestLow = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, BreakoutPeriod, 1));
if (Close[0] < lowestLow)
{
Print("Breakout Down detected");
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Open an order |
//+------------------------------------------------------------------+
void OpenOrder(int orderType)
{
double atr = iATR(NULL, 0, ATRPeriod, 0);
double price = (orderType == OP_BUY) ? Ask : Bid;
double stopLoss = (orderType == OP_BUY) ? price - StopLossMultiplier * atr : price + StopLossMultiplier * atr;
double takeProfit = (orderType == OP_BUY) ? price + TargetProfitMultiplier * atr : price - TargetProfitMultiplier * atr;
int ticket = OrderSend(Symbol(), orderType, LotSize, NormalizeDouble(price, Digits), Slippage, NormalizeDouble(stopLoss, Digits), NormalizeDouble(takeProfit, Digits), "PriceAction", MagicNumber, 0, (orderType == OP_BUY) ? clrGreen : clrRed);
if (ticket < 0)
{
Print("Failed to open order! Error code: ", GetLastError());
}
else
{
Print("Order opened successfully. Ticket: ", ticket);
}
}
//+------------------------------------------------------------------+
//| Close all open buy orders |
//+------------------------------------------------------------------+
void CloseBuyOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber)
{
if (!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clrNONE))
{
Print("Failed to close buy order! Error code: ", GetLastError());
}
else
{
Print("Buy order closed successfully. Ticket: ", OrderTicket());
}
}
}
}
}
//+------------------------------------------------------------------+
//| Close all open sell orders |
//+------------------------------------------------------------------+
void CloseSellOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber)
{
if (!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clrNONE))
{
Print("Failed to close sell order! Error code: ", GetLastError());
}
else
{
Print("Sell order closed successfully. Ticket: ", OrderTicket());
}
}
}
}
}
//+------------------------------------------------------------------+
//| Count the number of open buy orders |
//+------------------------------------------------------------------+
int CountOpenBuyOrders()
{
int count = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber)
{
count++;
}
}
}
return count;
}
//+------------------------------------------------------------------+
//| Count the number of open sell orders |
//+------------------------------------------------------------------+
int CountOpenSellOrders()
{
int count = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber)
{
count++;
}
}
}
return count;
}
//+------------------------------------------------------------------+
//| Check for target profit and close positions |
//+------------------------------------------------------------------+
void CheckTargetProfit()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber)
{
double openPrice = OrderOpenPrice();
double currentPrice = (OrderType() == OP_BUY) ? Bid : Ask;
double profitInPips = (OrderType() == OP_BUY ? currentPrice - openPrice : openPrice - currentPrice) / Point;
if (profitInPips >= TargetProfitMultiplier * iATR(NULL, 0, ATRPeriod, 0) / Point)
{
if (!OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(currentPrice, Digits), Slippage, clrNONE))
{
Print("Failed to close order! Error code: ", GetLastError());
}
else
{
Print("Order closed successfully. Ticket: ", OrderTicket());
}
}
}
}
}
Tidak ada komentar:
Posting Komentar