The errors you're encountering are due to missing or incorrect references to built-in MQL5 variables and functions. Let me fix these issues and provide a corrected version of the EA.
Fixed MQL5 Code (IBR.mq5)
//+------------------------------------------------------------------+
//| IBR.mq5 |
//| Forex Robot |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Forex Robot"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#include <Trade\Trade.mqh>
#include <Indicators\Indicator.mqh>
//--- Input Parameters ---
input string GeneralSettings = "--- General Settings ---";
input long EA_MagicNumber = 12345;
input int NY_TimeZone_Offset = -4; // -4 for EDT, -5 for EST
input string TimeSettings = "--- IOB Time Range (NY Time) ---";
input int StartTime_Hours = 8;
input int StartTime_Minutes = 20;
input int EndTime_Hours = 9;
input int EndTime_Minutes = 20;
input string RiskSettings = "--- Trading Settings ---";
input double Fixed_Lot_Size = 0.1;
input double Risk_Percentage = 1.0;
input bool Use_Risk_Management = true;
input string TrailingSettings = "--- Trailing Stop ---";
input bool Use_Trailing_Stop = true;
input double Trailing_Stop_Pips= 20;
input double Trailing_Stop_Step= 5;
input string BreakevenSettings = "--- Breakeven ---";
input bool Use_Breakeven = true;
input double Breakeven_Trigger_Pips = 10;
input double Breakeven_Pips = 2; // Move SL to BE + this amount
input string Visuals = "--- Visual Settings ---";
input bool Draw_IOB_Lines = true;
input bool Draw_Fibonacci_Lines = true;
//+------------------------------------------------------------------+
//--- Global Variables ---
CTrade m_trade;
static bool is_range_found = false;
static double iob_high = 0;
static double iob_low = 0;
static long position_ticket = 0;
static ENUM_POSITION_TYPE position_type;
static datetime current_day = 0;
// --- Fibonacci Levels ---
static double fib_45_level = 0;
static double fib_80_level = 0;
static double fib_minus1_level = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Validate inputs
if(Use_Risk_Management && Risk_Percentage <= 0)
{
Print("Invalid Risk Percentage. Set to 1% or higher.");
return INIT_FAILED;
}
if(!Use_Risk_Management && Fixed_Lot_Size <= 0)
{
Print("Invalid Fixed Lot Size. Set to 0.01 or higher.");
return INIT_FAILED;
}
m_trade.SetExpertMagicNumber(EA_MagicNumber);
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check for new day to reset logic
datetime new_day_time = (datetime)TimeCurrent();
new_day_time = new_day_time - (new_day_time % (24 * 60 * 60)); // Reset to midnight
if (new_day_time > current_day)
{
current_day = new_day_time;
is_range_found = false;
position_ticket = 0;
iob_high = 0;
iob_low = 0;
ObjectsDeleteAll(0, "IOB_");
ObjectsDeleteAll(0, "FIB_");
}
// Phase 1: Find IOB Range
if (!is_range_found && TimeCurrent() > GetNYTime(EndTime_Hours, EndTime_Minutes))
{
if (FindIOBRange())
{
is_range_found = true;
if(Draw_IOB_Lines)
{
ObjectCreate(0, "IOB_High", OBJ_HLINE, 0, 0, iob_high);
ObjectSetInteger(0, "IOB_High", OBJPROP_COLOR, clrRed);
ObjectCreate(0, "IOB_Low", OBJ_HLINE, 0, 0, iob_low);
ObjectSetInteger(0, "IOB_Low", OBJPROP_COLOR, clrBlue);
}
}
}
if (!is_range_found) return;
// Phase 2: Check for Breakout & Plot Fibs
if (position_ticket == 0) // Only look for breakouts if no position is open
{
double current_high = iHigh(NULL, PERIOD_CURRENT, 0);
double current_low = iLow(NULL, PERIOD_CURRENT, 0);
if (current_high > iob_high)
{
// Bullish Breakout
PlotFibonacci(iob_low, iob_high, true);
// Check for Entry
if (SymbolInfoDouble(NULL, SYMBOL_BID) > fib_45_level)
{
OpenTrade(POSITION_TYPE_BUY);
}
}
else if (current_low < iob_low)
{
// Bearish Breakout
PlotFibonacci(iob_high, iob_low, false);
// Check for Entry
if (SymbolInfoDouble(NULL, SYMBOL_ASK) < fib_45_level)
{
OpenTrade(POSITION_TYPE_SELL);
}
}
}
// Phase 3: Manage Open Position
if (position_ticket != 0)
{
ManagePosition(position_ticket);
}
}
//+------------------------------------------------------------------+
//| Helper Functions |
//+------------------------------------------------------------------+
datetime GetNYTime(int hours, int minutes)
{
// This function converts NY time to local server time
datetime time_ny = (datetime)TimeCurrent();
time_ny = time_ny - (time_ny % (60*60)); // Reset to current hour
time_ny = time_ny + (hours - (int)(TimeCurrent() / 3600 % 24) + NY_TimeZone_Offset) * 60*60 + minutes * 60;
return time_ny;
}
bool FindIOBRange()
{
datetime start_time = GetNYTime(StartTime_Hours, StartTime_Minutes);
datetime end_time = GetNYTime(EndTime_Hours, EndTime_Minutes);
int start_bar = iBarShift(NULL, PERIOD_CURRENT, start_time);
int end_bar = iBarShift(NULL, PERIOD_CURRENT, end_time);
if(start_bar < 0 || end_bar < 0) return false;
iob_high = -1;
iob_low = DBL_MAX;
for(int i = end_bar; i <= start_bar; i++)
{
double high = iHigh(NULL, PERIOD_CURRENT, i);
double low = iLow(NULL, PERIOD_CURRENT, i);
if(high > iob_high) iob_high = high;
if(low < iob_low) iob_low = low;
}
return (iob_high != -1 && iob_low != DBL_MAX);
}
void PlotFibonacci(double start_price, double end_price, bool is_bullish)
{
string fib_name = "FIB_" + (string)TimeCurrent();
ObjectCreate(0, fib_name, OBJ_FIBO, 0, TimeCurrent(), start_price, TimeCurrent(), end_price);
// Set Fibonacci levels
ObjectSetInteger(0, fib_name, OBJPROP_LEVELS, 3);
ObjectSetDouble(0, fib_name, OBJPROP_LEVELVALUE, 0, 0.45);
ObjectSetDouble(0, fib_name, OBJPROP_LEVELVALUE, 1, 0.80);
ObjectSetDouble(0, fib_name, OBJPROP_LEVELVALUE, 2, -1.0);
// Calculate Fibonacci levels
double range = end_price - start_price;
fib_45_level = start_price + (is_bullish ? 0.45 : 0.55) * range;
fib_80_level = start_price + (is_bullish ? 0.80 : 0.20) * range;
fib_minus1_level = start_price + (is_bullish ? -1.0 : 2.0) * range;
}
void OpenTrade(ENUM_POSITION_TYPE type)
{
double lot_size = CalculateLotSize();
double price = (type == POSITION_TYPE_BUY) ? SymbolInfoDouble(NULL, SYMBOL_ASK) : SymbolInfoDouble(NULL, SYMBOL_BID);
double sl = (type == POSITION_TYPE_BUY) ? fib_80_level : fib