// Include the necessary libraries and indicators
#include <Trade\Trade.mqh>
#include <Indicators\Indicators.mqh>
 
// Define the input parameters
input int EntrySignalTF = 15; // Signal detection timeframe (in minutes)
input int ConfirmationTF1 = 30; // Confirmation timeframe 1 (in minutes)
input int ConfirmationTF2 = 60; // Confirmation timeframe 2 (in minutes)
input int TradeExecutionTF = 5; // Trade execution timeframe (in minutes)
input double StopLoss = 50.0; // Stop loss (in pips)
input double TakeProfit = 100.0; // Take profit (in pips)
 
// Declare global variables
CTrade trade; // Trade object
CIndicators indicators; // Indicator object
 
// Define object names for drawing lines
string buyLineObjectName = "BuyLine";
string sellLineObjectName = "SellLine";
 
// Define the ENUM_DIRECTION type
enum ENUM_DIRECTION
{
    DIRECTION_NONE,  // No specific direction
    DIRECTION_UP,    // Upward direction
    DIRECTION_DOWN   // Downward direction
};
 
void OnInit()
{
    trade.SetExpertMagicNumber(123456);  // Set a unique magic number for the expert advisor
    }
// Define the OnTick() function
void OnTick()
{
    // Check if the current bar is complete for the entry signal timeframe
    if (TimeCurrent() % (EntrySignalTF * 60) != 0)
        return;
 
    // Calculate the entry signal indicators on M15 timeframe
    double bollingerUpper = iBands(Symbol(), PERIOD_M15, 20, 0, 2.0, PRICE_CLOSE);
    double bollingerLower = iBands(Symbol(), PERIOD_M15, 20, 2, -2.0, PRICE_CLOSE);
    double rsiValue = iRSI(Symbol(), PERIOD_M15, 14, PRICE_CLOSE);
    double stochasticValue = iStochastic(Symbol(), PERIOD_M15, 5, 3, 3, MODE_SMA, STO_LOWHIGH);
    double macdHistogram = iMACD(Symbol(), PERIOD_M15, 12, 26, 9, PRICE_CLOSE);
    double rejectionTop = iHigh(Symbol(), PERIOD_M15, 1) - bollingerUpper; // Rejection from upper Bollinger Band
    double rejectionBottom = bollingerLower - iLow(Symbol(), PERIOD_M15, 1); // Rejection from lower Bollinger Band
 
    // Calculate the trend direction on M30 timeframe
    ENUM_DIRECTION trendM30 = GetTrendDirection(PERIOD_M30);
 
    // Calculate the trend direction on H1 timeframe
    ENUM_DIRECTION trendH1 = GetTrendDirection(PERIOD_H1);
 
    // Check buy entry conditions on M15 timeframe
    if (stochasticValue < 20 && rsiValue < 30 && macdHistogram < 0 && rejectionBottom > 0 && trendM30 == DIRECTION_UP && trendH1 == DIRECTION_UP)
    {
        // Check buy confirmation conditions on M5 timeframe
        bool isBuyConfirmed = CheckConfirmationConditions(PERIOD_M5, bollingerLower);
 
        if (isBuyConfirmed)
        {
            // Open a buy trade
            trade.Buy(0.01, Symbol(), 0, 0, StopLoss, TakeProfit);
 
            // Draw a line to mark the anticipated reversal point on M15 timeframe
            double reversalPrice = NormalizeDouble(bollingerLower, _Digits);
            DrawLine(buyLineObjectName, reversalPrice);
        }
    }
 
    // Check sell entry conditions on M15 timeframe
    if (stochasticValue > 80 && rsiValue > 70 && macdHistogram > 0 && rejectionTop > 0 && trendM30 == DIRECTION_DOWN && trendH1 == DIRECTION_DOWN)
    {
        // Check sell confirmation conditions on M5 timeframe
        bool isSellConfirmed = CheckConfirmationConditions(PERIOD_M5, bollingerUpper);
 
        if (isSellConfirmed)
        {
            // Open a sell trade
            trade.Sell(0.01, Symbol(), 0, 0, StopLoss,TakeProfit);
 
            // Draw a line to mark the anticipated reversal point on M15 timeframe
            double reversalPrice = NormalizeDouble(bollingerUpper, _Digits);
            DrawLine(sellLineObjectName, reversalPrice);
        }
    }
}
 
// Function to check confirmation conditions on a specified timeframe
bool CheckConfirmationConditions(ENUM_TIMEFRAMES timeframe, double bollingerLevel)
{
    double confirmationBollinger = iBands(Symbol(), timeframe, 20, 2, 0, PRICE_CLOSE);
    double confirmationClose = iClose(Symbol(), timeframe, 1);
 
    if (confirmationClose < confirmationBollinger)
    {
        // Check additional confirmationconditions on the specified timeframe, such as other indicators or price patterns.
        return true;
    }
 
    return false;
}
 
// Function to get the trend direction on a specified timeframe
ENUM_DIRECTION GetTrendDirection(ENUM_TIMEFRAMES timeframe)
{
    double maFast = iMA(Symbol(), timeframe, 50, 0, MODE_EMA, PRICE_CLOSE);
    double maSlow = iMA(Symbol(), timeframe, 200, 0, MODE_EMA, PRICE_CLOSE);
 
    if (maFast > maSlow)
    {
        return DIRECTION_UP;
    }
    else if (maFast < maSlow)
    {
        return DIRECTION_DOWN;
    }
    else
    {
        return DIRECTION_NONE;
    }
}
 
// Function to draw a line on the chart
void DrawLine(string objectName, double price)
{
    ObjectDelete(0, objectName);
    ObjectCreate(0, objectName, OBJ_HLINE, 0, 0, price);
    ObjectSetInteger(0, objectName, OBJPROP_COLOR, clrRed);
    ObjectSetInteger(0, objectName, OBJPROP_STYLE, STYLE_SOLID);
}
 
// Define the OnDeinit() function
void OnDeinit(const int reason)
{
    // Close any open trades
    //trade.CloseAll();
 
    // Delete the drawn lines
    ObjectDelete(0, buyLineObjectName);
    ObjectDelete(0, sellLineObjectName);
}