//+------------------------------------------------------------------+ //| Test_ATR_Creation.mq5 | //| Diagnostic Script for ATR Error | //+------------------------------------------------------------------+ #property script_show_inputs input string InpSymbol = ""; // Symbol (empty = current) input ENUM_TIMEFRAMES InpPeriod = PERIOD_CURRENT; // Timeframe input int InpATRPeriod = 14; // ATR Period //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { Print("==== ATR Creation Diagnostic Test ===="); Print("Testing ATR indicator creation with different methods..."); // Get symbol string symbol = InpSymbol; if(StringLen(symbol) == 0) symbol = _Symbol; Print("Symbol: ", symbol); Print("Timeframe: ", EnumToString(InpPeriod)); Print("ATR Period: ", InpATRPeriod); // Check bars availability int bars = Bars(symbol, InpPeriod); Print("Available bars: ", bars); if(bars < InpATRPeriod + 1) { Print("WARNING: Not enough bars for ATR calculation!"); } // Test 1: Standard creation Print("\nTest 1: Standard iATR creation"); ResetLastError(); int handle1 = iATR(symbol, InpPeriod, InpATRPeriod); int error1 = GetLastError(); Print("Handle: ", handle1, ", Error: ", error1); if(handle1 != INVALID_HANDLE) { double buffer[1]; if(CopyBuffer(handle1, 0, 0, 1, buffer) > 0) { Print("ATR Value: ", DoubleToString(buffer[0], 5)); } else { Print("Failed to copy buffer, Error: ", GetLastError()); } IndicatorRelease(handle1); } // Test 2: NULL symbol Print("\nTest 2: Using NULL for symbol"); ResetLastError(); int handle2 = iATR(NULL, InpPeriod, InpATRPeriod); int error2 = GetLastError(); Print("Handle: ", handle2, ", Error: ", error2); if(handle2 != INVALID_HANDLE) { double buffer[1]; if(CopyBuffer(handle2, 0, 0, 1, buffer) > 0) { Print("ATR Value: ", DoubleToString(buffer[0], 5)); } IndicatorRelease(handle2); } // Test 3: Explicit timeframe Print("\nTest 3: Using explicit M1 timeframe"); ResetLastError(); int handle3 = iATR(symbol, PERIOD_M1, InpATRPeriod); int error3 = GetLastError(); Print("Handle: ", handle3, ", Error: ", error3); if(handle3 != INVALID_HANDLE) { double buffer[1]; if(CopyBuffer(handle3, 0, 0, 1, buffer) > 0) { Print("ATR Value: ", DoubleToString(buffer[0], 5)); } IndicatorRelease(handle3); } // Test 4: Manual ATR calculation Print("\nTest 4: Manual ATR calculation"); double manual_atr = CalculateATRManually(symbol, InpPeriod, InpATRPeriod); Print("Manual ATR: ", DoubleToString(manual_atr, 5)); // Test 5: Check for custom indicator Print("\nTest 5: Check for custom indicator file"); string custom_path = "Average True Range"; ResetLastError(); int handle5 = iCustom(symbol, InpPeriod, custom_path, InpATRPeriod); int error5 = GetLastError(); Print("Custom indicator handle: ", handle5, ", Error: ", error5); if(handle5 != INVALID_HANDLE) { Print("WARNING: Found custom 'Average True Range' indicator!"); IndicatorRelease(handle5); } Print("\n==== Diagnostic Complete ===="); // Error code explanations Print("\nCommon Error Codes:"); Print("4302 - Invalid parameters for indicator"); Print("4401 - Indicator resource error"); Print("4807 - Custom indicator not found"); // Recommendations Print("\nRecommendations:"); if(error1 == 4302) { Print("1. Ensure sufficient historical data is loaded"); Print("2. Check if symbol is properly initialized"); Print("3. Try using NULL instead of _Symbol"); Print("4. Check for custom indicator conflicts"); } } //+------------------------------------------------------------------+ //| Calculate ATR Manually | //+------------------------------------------------------------------+ double CalculateATRManually(string symbol, ENUM_TIMEFRAMES timeframe, int period) { double atr = 0; double sum_tr = 0; // Calculate True Range for each bar for(int i = 1; i <= period; i++) { double high = iHigh(symbol, timeframe, i); double low = iLow(symbol, timeframe, i); double close_prev = iClose(symbol, timeframe, i + 1); // True Range = max(High-Low, |High-Close_prev|, |Low-Close_prev|) double tr = MathMax(high - low, MathMax(MathAbs(high - close_prev), MathAbs(low - close_prev))); sum_tr += tr; } // Simple moving average of True Range if(period > 0) atr = sum_tr / period; return atr; }