### Get Timeframe Description Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Converts MQL5 timeframe constants (e.g., PERIOD_H1) into a human-readable string representation (e.g., "H1"). Useful for display or logging. ```mql5 string TimeFrameDescription(int TimeFrame) { string PeriodDesc = ""; switch (TimeFrame) { case PERIOD_M1: PeriodDesc = "M1"; break; case PERIOD_M5: PeriodDesc = "M5"; break; case PERIOD_M15: PeriodDesc = "M15"; break; case PERIOD_M30: PeriodDesc = "M30"; break; case PERIOD_H1: PeriodDesc = "H1"; break; case PERIOD_H4: PeriodDesc = "H4"; break; case PERIOD_D1: PeriodDesc = "D1"; break; case PERIOD_W1: PeriodDesc = "W1"; break; case PERIOD_MN1: PeriodDesc = "MN1"; break; } return PeriodDesc; } ``` -------------------------------- ### Get Current Hour Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Retrieves the current hour from the system time. This is a helper function used by other time-related utilities. ```mql5 int Hour() { MqlDateTime mTime; TimeCurrent(mTime); return(mTime.hour); } ``` -------------------------------- ### Check if Current Time is Within Interval Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Use this function to determine if the current trading hour falls within a specified start and end hour. Handles cases where the interval spans midnight. ```mql5 bool IsCurrentTimeInInterval(ENUM_HOUR Start, ENUM_HOUR End) { if (Start == End && Hour() == Start) return true; if (Start < End && Hour() >= Start && Hour() <= End) return true; if (Start > End && ((Hour() >= Start && Hour() <= 23) || (Hour() <= End && Hour() >= 0))) return true; return false; } ``` -------------------------------- ### Initialize MACD Indicator (OnInit) Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Initializes the MACD indicator by setting its short name, state variables, and validating input parameters. It also creates the MACD indicator handle, allocates memory for price data and indicator buffers, and configures plot properties. Ensure input parameters like MACDFastEMA, MACDSlowEMA, and MACDSMA are valid before initialization. ```mql5 int OnInit(void) { // Set indicator short name for display IndicatorSetString(INDICATOR_SHORTNAME, IndicatorName); // Initialize state variables LastNotificationTime = TimeCurrent(); Shift = CandleToCheck; // 0 for current candle, 1 for previous // Validate input parameters if ((MACDFastEMA <= 0) || (MACDFastEMA > MACDSlowEMA) || (MACDSMA <= 0)) { Print("Wrong input parameters."); return INIT_FAILED; } // Check for sufficient historical data if ((Bars(Symbol(), PERIOD_CURRENT) < MACDSlowEMA) || (Bars(Symbol(), PERIOD_CURRENT) < MACDSMA)) { Print("Not enough historical candles."); return INIT_FAILED; } // Create MACD indicator handle for data retrieval BufferMACDHandle = iMACD(Symbol(), PERIOD_CURRENT, MACDFastEMA, MACDSlowEMA, MACDSMA, MACDAppliedPrice); // Allocate price data arrays ArrayResize(Open, BarsToScan); ArrayResize(High, BarsToScan); ArrayResize(Low, BarsToScan); ArrayResize(Close, BarsToScan); ArrayResize(Time, BarsToScan); // Configure indicator buffers IndicatorSetInteger(INDICATOR_DIGITS, _Digits); ArraySetAsSeries(BufferMain, true); ArraySetAsSeries(BufferSignal, true); SetIndexBuffer(0, BufferMain, INDICATOR_DATA); SetIndexBuffer(1, BufferSignal, INDICATOR_DATA); // Set MACD display type (histogram or line) if (MACDType == MACD_TYPE_HISTOGRAM) PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_HISTOGRAM); else PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE); PlotIndexSetString(0, PLOT_LABEL, "MACD MAIN"); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MACDSlowEMA); PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE); PlotIndexSetString(1, PLOT_LABEL, "MACD SIGNAL"); PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, MACDSMA); return INIT_SUCCEEDED; } ``` -------------------------------- ### Retrieve Account Information Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Wrapper functions to easily access key account details such as company name, account name, login number, currency, balance, equity, and free margin. ```mql5 string AccountCompany() { return AccountInfoString(ACCOUNT_COMPANY); } string AccountName() { return AccountInfoString(ACCOUNT_NAME); } long AccountNumber() { return AccountInfoInteger(ACCOUNT_LOGIN); } string AccountCurrency() { return AccountInfoString(ACCOUNT_CURRENCY); } double AccountBalance() { return AccountInfoDouble(ACCOUNT_BALANCE); } double AccountEquity() { return AccountInfoDouble(ACCOUNT_EQUITY); } double AccountFreeMargin() { return AccountInfoDouble(ACCOUNT_MARGIN_FREE); } ``` -------------------------------- ### Configure MACD with Alert Input Parameters Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Defines the input parameters for MACD calculation, signal detection, notification preferences, and visual arrow customization in MQL5. ```mql5 // MACD Calculation Parameters input int MACDFastEMA = 12; // MACD Fast EMA Period input int MACDSlowEMA = 26; // MACD Slow EMA Period input int MACDSMA = 9; // MACD SMA Period (Signal Line) input ENUM_APPLIED_PRICE MACDAppliedPrice = PRICE_CLOSE; // Applied Price (Close, Open, High, Low, etc.) // Signal Detection Settings input ENUM_ALERT_SIGNAL AlertSignal = MACD_MAIN_SIGNAL_CROSS; // Alert trigger condition // Options: MACD_MAIN_SWITCH_SIDE (zero cross), MACD_MAIN_SIGNAL_CROSS, MACD_MAIN_ALL input ENUM_CANDLE_TO_CHECK CandleToCheck = CURRENT_CANDLE; // Which candle to analyze // Options: CURRENT_CANDLE (0), CLOSED_CANDLE (1) input ENUM_MACD_TYPE MACDType = MACD_TYPE_HISTOGRAM; // Display type // Options: MACD_TYPE_HISTOGRAM, MACD_TYPE_LINE input int BarsToScan = 500; // Historical bars to analyze // Notification Settings input bool EnableNotify = false; // Master switch for notifications input bool SendAlert = true; // Popup alert dialog input bool SendApp = false; // Push notification to MT5 mobile app input bool SendEmail = false; // Email notification // Arrow Customization - Switch Signals (Zero Line Cross) input bool EnableDrawArrows = true; // Enable visual arrows on chart input int ArrowBuySwitch = 241; // Buy arrow Wingdings code input int ArrowSellSwitch = 242; // Sell arrow Wingdings code input int ArrowSizeSwitch = 3; // Arrow size (1-5) input color ArrowColorBuySwitch = clrGreen; // Buy arrow color input color ArrowColorSellSwitch = clrRed; // Sell arrow color // Arrow Customization - Cross Signals (Signal Line Cross) input int ArrowBuyCross = 233; // Buy arrow Wingdings code input int ArrowSellCross = 234; // Sell arrow Wingdings code input int ArrowSizeCross = 3; // Arrow size (1-5) input color ArrowColorBuyCross = clrGreen; // Buy arrow color input color ArrowColorSellCross = clrRed; // Sell arrow color ``` -------------------------------- ### Implement NotifyHit for Signal Alerts Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt This function handles sending alerts via Alert, Email, or Push Notification. It includes duplicate prevention logic based on signal direction and candle timing. ```mql5 void NotifyHit() { if (!EnableNotify) return; if ((!SendAlert) && (!SendApp) && (!SendEmail)) return; // For closed candle mode, only notify once per candle if ((CandleToCheck == CLOSED_CANDLE) && (Time[0] <= LastNotificationTime)) return; ENUM_TRADE_SIGNAL Signal = IsSignal(0); // Reset tracking if no signal if (Signal == SIGNAL_NEUTRAL) { LastNotificationDirection = Signal; return; } // Prevent duplicate notifications for same signal if (Signal == LastNotificationDirection) return; // Build notification messages string EmailSubject = IndicatorName + " " + Symbol() + " Notification"; string EmailBody = AccountCompany() + " - " + AccountName() + " - " + IntegerToString(AccountNumber()) + "\r\n" + IndicatorName + " Notification for " + Symbol() + " @ " + EnumToString((ENUM_TIMEFRAMES)Period()) + "\r\n"; string AlertText = ""; string AppText = AccountCompany() + " - " + AccountName() + " - " + IntegerToString(AccountNumber()) + " - " + IndicatorName + " - " + Symbol() + " @ " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - "; string Text = EnumToString(Signal); // Signal type as text EmailBody += Text; AlertText += Text; AppText += Text; // Send notifications via enabled channels if (SendAlert) Alert(AlertText); if (SendEmail) { if (!SendMail(EmailSubject, EmailBody)) Print("Error sending email " + IntegerToString(GetLastError())); } if (SendApp) { if (!SendNotification(AppText)) Print("Error sending notification " + IntegerToString(GetLastError())); } // Update tracking variables LastNotificationTime = Time[0]; LastNotificationDirection = Signal; } ``` -------------------------------- ### Implement OnCalculate for Indicator Updates Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt This function is triggered on every tick to process new bars and update indicator buffers. It includes logic for efficient recalculation and arrow drawing. ```mql5 int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { // Detect new candle formation bool IsNewCandle = CheckIfNewCandle(); // Calculate how many bars need processing int counted_bars = 0; if (prev_calculated > 0) counted_bars = prev_calculated - 1; if (counted_bars < 0) return -1; if (counted_bars > 0) counted_bars--; int limit = rates_total - counted_bars; if (limit > BarsToScan) { limit = BarsToScan; if (rates_total < BarsToScan + MACDSlowEMA) limit = rates_total - MACDSlowEMA; } if (limit > rates_total - MACDSlowEMA) limit = rates_total - MACDSlowEMA; // Copy MACD data from built-in indicator if ((CopyBuffer(BufferMACDHandle, 0, 0, limit, BufferMain) <= 0) || (CopyBuffer(BufferMACDHandle, 1, 0, limit, BufferSignal) <= 0)) { Print("Failed to create the indicator! Error: ", GetLastErrorText(GetLastError()), " - ", GetLastError()); return 0; } if (IsStopped()) return 0; // Update price data arrays for (int i = limit - 1; (i >= 0) && (!IsStopped()); i--) { Open[i] = iOpen(Symbol(), PERIOD_CURRENT, i); Low[i] = iLow(Symbol(), PERIOD_CURRENT, i); High[i] = iHigh(Symbol(), PERIOD_CURRENT, i); Close[i] = iClose(Symbol(), PERIOD_CURRENT, i); Time[i] = iTime(Symbol(), PERIOD_CURRENT, i); } // Redraw all arrows on new candle or first calculation if ((IsNewCandle) || (prev_calculated == 0)) { if (EnableDrawArrows) DrawArrows(limit); CleanUpOldArrows(); // Remove arrows beyond BarsToScan } // Always update current bar arrow if (EnableDrawArrows) DrawArrow(0); // Check for notifications if (EnableNotify) NotifyHit(); return rates_total; } ``` -------------------------------- ### Draw Arrows for Trading Signals in MQL5 Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Use this function to draw visual arrows on the chart to indicate buy or sell signals. Configure arrow type, color, size, and position based on the signal. Ensure `IsSignal` and related constants are defined. ```mql5 void DrawArrow(int i) { RemoveArrowCurr(); // Clear current bar arrow for redraw ENUM_TRADE_SIGNAL Signal = IsSignal(i); if (Signal == SIGNAL_NEUTRAL) return; datetime ArrowDate = iTime(Symbol(), 0, i); string ArrowName = IndicatorName + "-ARWS-" + IntegerToString(ArrowDate); double ArrowPrice = 0; int ArrowType = 0; color ArrowColor = 0; int ArrowAnchor = 0; string ArrowDesc = ""; int ArrowSize = 0; // Configure arrow based on signal type if (Signal == SIGNAL_BUY_SWITCH) { ArrowPrice = Low[i]; // Place below candle ArrowType = ArrowBuySwitch; // Wingdings code ArrowColor = ArrowColorBuySwitch; ArrowSize = ArrowSizeSwitch; ArrowAnchor = ANCHOR_TOP; // Arrow points up ArrowDesc = "BUY (SWITCH)"; } else if (Signal == SIGNAL_SELL_SWITCH) { ArrowPrice = High[i]; // Place above candle ArrowType = ArrowSellSwitch; ArrowColor = ArrowColorSellSwitch; ArrowSize = ArrowSizeSwitch; ArrowAnchor = ANCHOR_BOTTOM; // Arrow points down ArrowDesc = "SELL (SWITCH)"; } else if (Signal == SIGNAL_BUY_CROSS) { ArrowPrice = Low[i]; ArrowType = ArrowBuyCross; ArrowColor = ArrowColorBuyCross; ArrowSize = ArrowSizeCross; ArrowAnchor = ANCHOR_TOP; ArrowDesc = "BUY (CROSS)"; } else if (Signal == SIGNAL_SELL_CROSS) { ArrowPrice = High[i]; ArrowType = ArrowSellCross; ArrowColor = ArrowColorSellCross; ArrowSize = ArrowSizeCross; ArrowAnchor = ANCHOR_BOTTOM; ArrowDesc = "SELL (CROSS)"; } // Create and configure the arrow object ObjectCreate(0, ArrowName, OBJ_ARROW, 0, ArrowDate, ArrowPrice); ObjectSetInteger(0, ArrowName, OBJPROP_COLOR, ArrowColor); ObjectSetInteger(0, ArrowName, OBJPROP_SELECTABLE, false); ObjectSetInteger(0, ArrowName, OBJPROP_HIDDEN, true); ObjectSetInteger(0, ArrowName, OBJPROP_ANCHOR, ArrowAnchor); ObjectSetInteger(0, ArrowName, OBJPROP_ARROWCODE, ArrowType); ObjectSetInteger(0, ArrowName, OBJPROP_WIDTH, ArrowSize); ObjectSetInteger(0, ArrowName, OBJPROP_STYLE, STYLE_SOLID); ObjectSetInteger(0, ArrowName, OBJPROP_BGCOLOR, ArrowColor); ObjectSetString(0, ArrowName, OBJPROP_TEXT, ArrowDesc); } ``` -------------------------------- ### Implement Trade Signal Detection Logic (IsSignal) Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Implements the logic to detect trade signals based on MACD behavior. This function checks for zero-line crossovers and signal line crossovers, returning the appropriate ENUM_TRADE_SIGNAL value. Ensure necessary buffers (BufferMain, BufferSignal) and configuration (AlertSignal, Shift) are set before calling. ```mql5 // Signal detection logic implementation ENUM_TRADE_SIGNAL IsSignal(int i) { int j = i + Shift; // Adjust index based on candle selection // Check for zero-line crossover (switch signals) if ((AlertSignal == MACD_MAIN_SWITCH_SIDE) || (AlertSignal == MACD_MAIN_ALL)) { // MACD crossed from negative to positive = BUY if ((BufferMain[j + 1] < 0) && (BufferMain[j] > 0)) return SIGNAL_BUY_SWITCH; // MACD crossed from positive to negative = SELL if ((BufferMain[j + 1] > 0) && (BufferMain[j] < 0)) return SIGNAL_SELL_SWITCH; } // Check for signal line crossover if ((AlertSignal == MACD_MAIN_SIGNAL_CROSS) || (AlertSignal == MACD_MAIN_ALL)) { // MACD crossed above signal line = BUY if ((BufferMain[j + 1] < BufferSignal[j + 1]) && (BufferMain[j] > BufferSignal[j])) return SIGNAL_BUY_CROSS; // MACD crossed below signal line = SELL if ((BufferMain[j + 1] > BufferSignal[j + 1]) && (BufferMain[j] < BufferSignal[j])) return SIGNAL_SELL_CROSS; } return SIGNAL_NEUTRAL; } ``` -------------------------------- ### Define Trade Signal Types (ENUM_TRADE_SIGNAL) Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Defines enumeration values for different trade signals generated by the MACD indicator, including BUY/SELL signals based on zero-line crossovers and signal line crossovers. Use these constants to interpret the signal returned by the IsSignal function. ```mql5 enum ENUM_TRADE_SIGNAL { SIGNAL_BUY_SWITCH = 1, // BUY signal when MACD crosses above zero SIGNAL_SELL_SWITCH = -1, // SELL signal when MACD crosses below zero SIGNAL_BUY_CROSS = 2, // BUY signal when MACD crosses above signal line SIGNAL_SELL_CROSS = -2, // SELL signal when MACD crosses below signal line SIGNAL_NEUTRAL = 0 // No signal detected }; ``` -------------------------------- ### Configure Plot Index Properties Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt Sets the drawing properties for a specific indicator plot, including type, style, width, color, and label. Call this function for each plot you intend to draw. ```mql5 void SetIndex(int Index, int Type, int Style, int Width, int Color, string Label) { PlotIndexSetInteger(Index, PLOT_DRAW_TYPE, Type); PlotIndexSetInteger(Index, PLOT_LINE_STYLE, Style); PlotIndexSetInteger(Index, PLOT_LINE_WIDTH, Width); PlotIndexSetInteger(Index, PLOT_LINE_COLOR, Color); PlotIndexSetString(Index, PLOT_LABEL, Label); } ``` -------------------------------- ### Clean Up Old Arrows in MQL5 Source: https://context7.com/gavindiaz/mt5-indicator-collection/llms.txt This function iterates through existing arrow objects on the chart and deletes those that fall outside the specified analysis window (`BarsToScan`). It's crucial for maintaining chart performance by removing outdated visual elements. ```mql5 void CleanUpOldArrows() { int total = ObjectsTotal(ChartID(), 0, OBJ_ARROW); for (int i = total - 1; i >= 0; i--) { string ArrowName = ObjectName(ChartID(), i, 0, OBJ_ARROW); datetime time = (datetime)ObjectGetInteger(ChartID(), ArrowName, OBJPROP_TIME); int bar = iBarShift(Symbol(), Period(), time); if (bar >= BarsToScan) ObjectDelete(ChartID(), ArrowName); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.