// Copyright 2024 MetaTrader5 #property copyright "Example" #define INPUT_SYMBOLS "Symbols" // Text field for comma-separated symbols #define INPUT_TIMEFRAME PERIOD_D1 // User-defined timeframe #define INPUT_SCAN_INTERVAL 60 // Scan interval in seconds (default: 60) int InpSymbols[]; int InpTimeframe; // Constant representing the timeframe int InpScanInterval; long LastScanTime; // Function to check if Heiken Ashi candle is bullish (approximation) bool IsBullishHeikenAshiApprox(int index) { // Get most recent Heiken Ashi close price (assuming previous calculation) double prev_heiken_close = iClose(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access close price (index 0) // Get current open, high, and low prices double open = iOpen(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access open price (index 0) double high = iHigh(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access high price (index 0) double low = iLow(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access low price (index 0) // Approximate Heiken Ashi open price (using current Open) double heiken_open = (prev_heiken_close + open + low) / 3.0; // Approximate Heiken Ashi close price (average of open and current high) double heiken_close = (heiken_open + open + high) / 2.0; return (heiken_close > heiken_open); } // Function to check if Heiken Ashi candle is bearish (approximation) bool IsBearishHeikenAshiApprox(int index) { // Get most recent Heiken Ashi close price (assuming previous calculation) double prev_heiken_close = iClose(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access close price (index 0) // Get current open, high, and low prices double open = iOpen(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access open price (index 0) double high = iHigh(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access high price (index 0) double low = iLow(Symbol(), INPUT_TIMEFRAME, 0)[0]; // Use INPUT_TIMEFRAME constant, index 0 for current bar, and access low price (index 0) // Approximate Heiken Ashi open price (using current Open) double heiken_open = (prev_heiken_close + open + low) / 3.0; // Approximate Heiken Ashi close price (average of open and current high) double heiken_close = (heiken_open + open + high) / 2.0; return (heiken_close < heiken_open); } void OnTick() { // Get current time in milliseconds long current_time = TimeCurrent(); // Check if sufficient time has passed since the last scan if (current_time - LastScanTime >= InpScanInterval * 1000) { LastScanTime = current_time; // Get current timeframe int timeframe = Period(); // Check if timeframe matches user-defined timeframe if (timeframe != InpTimeframe) { return; } // Check for open trades if (PositionsTotal() > 0) { // Loop through open trades for (int i = 0; i < PositionsTotal(); i++) { POSITION_DATA position = PositionGet(i); string symbol = position.symbol; // Check if symbol matches any symbol in the list if (StringFind(InpSymbols, symbol) != -1) { // Check trade direction and Heiken Ashi signal for the matching symbol if (position.type == POSITION_TYPE_BUY && IsBearishHeikenAshiApprox(0)) { CloseOrder(position.ticket, POSITION_CLOSE_BY, position.volume, position.slippage, comment="Close on Heiken Ashi Bearish"); } else if (position.type == POSITION_TYPE_SELL && IsBullishHeikenAshiApprox(0)) { CloseOrder(position.ticket, POSITION_CLOSE_BY, position.volume, position.slippage, comment="Close on Heiken Ashi Bullish"); } else { // Print message for debugging purposes (optional) //Print("Skipping position on ", symbol, " (no Heiken Ashi signal)"); } } } } } }