//+------------------------------------------------------------------+ //| ChannelBreakout.mq4 | //| Copyright © 2023, Your Name | //| http://www.yourwebsite.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2023" #property link "http://www.ok.ro" #property version "1.00" #property strict // Define Input Parameters input int StopLoss = 0; // Stop Loss in pips (0 for automatic calculation) input int TakeProfit = 0; // Take Profit in pips (0 for automatic calculation) input int ADRThreshold1 = 200; // ADR threshold for the first condition input int ADRThreshold2 = 175; // ADR threshold for the second condition input int EntryOffsetPips = 10; // Offset for entry orders input int EntryHourGMT = 23; // Hour to enter pending orders (GMT time) input double RiskRewardRatio = 3.0; // Risk-reward ratio input int TrailingStopStart = 30; // Initial Trailing Stop in pips input int TrailingStepSteps = 20; // Trailing Step in pips input int TrailingStepStop = -1; // Trailing Step Stop (use StopLoss) input int MagicNumber = 0; // Magic Number for orders input int BuyStopAboveLL = 10; // Buy Stop Offset above Lowest Low in pips input int SellStopBelowHH = 10; // Sell Stop Offset below Highest High in pips // Define indicator handles int hhllHandle; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Create handles for HHLLindicators hhllHandle = iCustom(Symbol(), Period(), "HHLLindicators", 0, 0, 0); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Delete the indicator handles ObjectDelete(hhllHandle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Check if it's the specified GMT entry hour if (TimeHour(TimeCurrent()) == EntryHourGMT) { // Calculate ADR using the "Average Range" indicator on the 15-minute chart double ADR = iCustom(Symbol(), 15, "Average Range", 0, 0, 0); // Check ADR condition if (ADR >= 100) { // Get HH and LL values from the indicator double HighestHigh = iCustom(Symbol(), 15, "HHLLindicators", 0, hhllHandle); double LowestLow = iCustom(Symbol(), 15, "HHLLindicators", 1, hhllHandle); // Calculate entry levels double EntryBuy = HighestHigh + BuyStopAboveLL * Point; double EntrySell = LowestLow - SellStopBelowHH * Point; // Delete pending orders if they exist if (!DeletePendingOrders()) { Print("Error deleting pending orders."); return; } // Place pending buy-stop and sell-stop orders int ticketBuy = OrderSend(Symbol(), OP_BUYSTOP, 1.0, EntryBuy, 2, 0, 0, "", 0, clrNONE); int ticketSell = OrderSend(Symbol(), OP_SELLSTOP, 1.0, EntrySell, 2, 0, 0, "", 0, clrNONE); // Check if orders were placed successfully if (ticketBuy < 0 || ticketSell < 0) { Print("Error placing pending orders. Error code:", GetLastError()); return; } // Set Trailing Stop if enabled if (TrailingStepStop == -1) { if (!SetTrailingStop(ticketSell, EntrySell) || !SetTrailingStop(ticketBuy, EntryBuy)) { Print("Error setting trailing stops."); } } } else { Print("Average Daily Range (ADR) is below 100 pips. No trades will be placed."); } } } //+------------------------------------------------------------------+ //| Delete pending orders if they exist | //+------------------------------------------------------------------+ bool DeletePendingOrders() { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP) { if (OrderDelete(OrderTicket())) { Print("Pending order deleted successfully."); } else { Print("Error deleting pending order. Error code:", GetLastError()); return false; } } } } return true; } //+------------------------------------------------------------------+ //| Set Trailing Stop for a specific order | //+------------------------------------------------------------------+ bool SetTrailingStop(int orderTicket, double entryPrice) { double currentPrice = MarketInfo(Symbol(), MODE_BID); double stopLossPrice = entryPrice - TrailingStopStart * Point; if (currentPrice >= entryPrice + TrailingStepStop * Point) { stopLossPrice = currentPrice - TrailingStepStop * Point; } return OrderSend(OrderType(), OrderSymbol(), OrderLots(), stopLossPrice, 0, 0, 0, "", OrderMagicNumber(), clrNONE, orderTicket); }