What could be the issue with my code?

 

FX Bot File

//+------------------------------------------------------------------+
//|                             Ignite FX Automated Trader Robot.mq4 |
//|                                                 Gregory Delazeri |
//|                                                                  |
//+------------------------------------------------------------------+
// Global variable to store the copyright text
string CopyrightText;

// Dynamically generated yearly copyright
string CopyrightYearly;

#include "CustomFunctions.mqh"

// Expert initialization function
int OnInit() {
    // Set the copyright text dynamically
    CopyrightText = "Gregory Delazeri Copyright " + IntegerToString(TimeYear(TimeLocal()));
    
    // Set the yearly copyright property
    CopyrightYearly = "Gregory Delazeri Copyright " + IntegerToString(TimeYear(TimeLocal()));
    
    // Other properties
    #property copyright CopyrightYearly
    #property link      ""
    #property version   "1.00"
    #property strict

    // Include the CustomFunctions.mqh file
#include "CustomFunctions.mqh"

    // Initialization code here
    // For example: Initialize variables, set up indicators, or connect to external systems

    return (INIT_SUCCEEDED);
}

// User input zone for the user to edit these settings
input double   max_spread = 7;                  // Max Spread
input double   StopLoss = 50.0;                 // Default StopLoss Value
input string   CurrencySymbol = "£";            // Default Currency Symbol
input int      DecimalPlaces = 2;               // Default Decimal Places
input int      maxLossinPipsShort = 20;         // Max Loss in Pips for Short
input int      maxLossinPipsLong = 20;          // Max Loss in Pips for Long
input double   lotSize = 0.01;                  // Lot size for Orders

//+------------------------------------------------------------------+
//| Function to display the day of the week using an alert           |
//+------------------------------------------------------------------+
void DayOfWeekAlert() {
    int dayOfWeek = DayOfWeek();
    
    // Close trades before the weekend finishes
    if (dayOfWeek == 1) {
        Alert("We are Monday. Let's try to enter new trades.");
    } else if (dayOfWeek >= 2 && dayOfWeek <= 4) {
        Alert("It's day " + IntegerToString(dayOfWeek) + ". Let's try to enter new trades or close existing trades.");
    } else if (dayOfWeek == 5) {
        Alert("It's Friday. Don't enter new trades and close existing trades.");
    } else if (dayOfWeek == 6 || dayOfWeek == 0) {
        Alert("It's the weekend, no trading!");
    }
    // END of DATE CODE
}

// A function to display user's account balance and details with alerts
void DisplayAccountBalance() {
    double accBalance = AccountBalance();
    string message = "Account Balance: " + CurrencySymbol + DoubleToStr(accBalance, DecimalPlaces) + "\n" +
                     "Your Stop Loss % is: 2%\n" +
                     "Your Max Loss in " + CurrencySymbol + " value: " + CurrencySymbol + DoubleToStr(0.02 * accBalance, DecimalPlaces);
    Alert(message);
}

// Function to calculate stop loss based on the trading position
double GetStopLossPrice(bool bIsLongPosition, double entryPrice, int maxLossInPips) {
    double stopLossPrice;

    if (bIsLongPosition) {
        stopLossPrice = entryPrice - maxLossInPips * CustomFunctions::GetPipValue(Symbol(), Digits);
    } else {
        stopLossPrice = entryPrice + maxLossInPips * CustomFunctions::GetPipValue(Symbol(), Digits);
    }

    return stopLossPrice;
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
    double current_spread = MarketInfo(Symbol(), MODE_SPREAD);
    string spread_message = "";
    
    // Get the value of the spread in points
    if (current_spread > max_spread) {
        spread_message = "ALERT: Spread Greater than Max Spread: "; 
    } else {
        spread_message = "Current Spread (points): ";
    }
    
    Comment(spread_message + DoubleToStr(current_spread, 0));
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
    // Perform cleanup or final operations before the EA shuts down
}

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnTicks() {
    // Display day of the week alerts three times
    Alert("First time:");
    DayOfWeekAlert();
    
    Alert("Second time:");
    DayOfWeekAlert();
    
    Alert("Third time:");
    DayOfWeekAlert();
    
    // Call the function to display the account balance
    DisplayAccountBalance();

    // Call the GetStopLossPrice function with sample values
    double calculatedStopLoss = GetStopLossPrice(true, 1.2255, 20);
    string stopLossMessage = "Calculated Stop Loss: " + DoubleToStr(calculatedStopLoss, 5);
    Alert(stopLossMessage);
}

// https://docs.mql4.com/function_indices 
// functions from mql4 file use #include <CustomFunctions>

Perhaps I have some unnecessary code here? ^^ but i am getting these errors i am struggling to fix

- 'CustomFunctions' - import not defined Ignite FX Automated Trader Robot.mq4 79 54

- }' - semicolon expected Ignite FX Automated Trader Robot.mq4 80 5

- 'CustomFunctions' - some operator expected Ignite FX Automated Trader Robot.mq4 79 54

- 'else' - illegal 'else' without matching 'if' Ignite FX Automated Trader Robot.mq4 80 7

- can't open "C:\Program Files (x86)\MetaTrader 4\MQL4\Experts\CustomFunctions.mqh" include file Ignite FX Automated Trader Robot.mq4 29 11





My Custom Functions file (no issues detected)

//+------------------------------------------------------------------+
//|                                              CustomFunctions.mqh |
//|                                                 Gregory Delazeri |
//|                                                                  |
//+------------------------------------------------------------------+
#property link      ""
#property version   "1.00"
#property strict

//This custom functions .mqh will be the origin of my other EA's

double GetPipValue()
{
   if(_Digits >= 4)
   {
      return 0.0001;
   }
   else
   {
      return 0.001;
   }
}

//+------------------------------------------------------------------+
//| Buying and Shorting       /        Calculate Take Profit         |
//+------------------------------------------------------------------+

double CalculateTakeProfit(bool isLong, double entryPrice, int pips)
{
    double takeProfit; 
    if (isLong)
    {
        takeProfit = entryPrice + pips * GetPipValue();
    }
    else
    {
        takeProfit = entryPrice - pips * GetPipValue(); 
    }

    return takeProfit;
}

//+------------------------------------------------------------------+
//| Buying and Shorting       /        Calculate Stop Loss           |
//+------------------------------------------------------------------+

double CalculateStopLoss(bool isLong, double entryPrice, int pips)
{
   double stopLoss;
   if(isLong)
   {
      stopLoss = entryPrice - pips * GetPipValue();
   }
   else
   {
      stopLoss = entryPrice + pips * GetPipValue();
   }
   return stopLoss;
}

//+------------------------------------------------------------------+
//| Risk Management                                                  |
//+------------------------------------------------------------------+

// Risk parameters
input double riskPercentage = 1.0;  // Risk per trade as a percentage of account balance
input int maxRiskPips = 50;         // Maximum number of pips to risk per trade

// Function to calculate lot size based on risk percentage
double CalculateLotSize(double entryPrice, double stopLossPrice)
{
   double riskAmount = riskPercentage / 100.0 * AccountBalance();
   double pipValue = MarketInfo(Symbol(), MODE_POINT);
   double riskInPips = MathAbs(entryPrice - stopLossPrice) / pipValue;

// Ensure the risk does not exceed the maximum allowed
   riskInPips = MathMin(riskInPips, maxRiskPips);

   return riskAmount / (riskInPips * pipValue * MarketInfo(Symbol(), MODE_MARGINREQUIRED));
}

// Function to calculate dynamic stop loss
double CalculateDynamicStopLoss(bool isLong, double entryPrice)
{
   double atrValue = iATR(Symbol(), 0, 14, 0); // You can adjust the ATR period as needed

   if (isLong)
   {
      return entryPrice - atrValue;
   }
   else
   {
      return entryPrice + atrValue;
   }
}

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Gregory Delazeri: What could be the issue with my code?

Do you know how to code in MQL?

You are including a header file within the middle of a function body, and defining "#property" statements within a function body too.

It does not seem you understand how to code and are only doing "copy/paste" without regard for the context.

// Expert initialization function
int OnInit() {
    // Set the copyright text dynamically
    CopyrightText = "Gregory Delazeri Copyright " + IntegerToString(TimeYear(TimeLocal()));
    
    // Set the yearly copyright property
    CopyrightYearly = "Gregory Delazeri Copyright " + IntegerToString(TimeYear(TimeLocal()));
    
    // Other properties
    #property copyright CopyrightYearly
    #property link      ""
    #property version   "1.00"
    #property strict

    // Include the CustomFunctions.mqh file
#include "CustomFunctions.mqh"

    // Initialization code here
    // For example: Initialize variables, set up indicators, or connect to external systems

    return (INIT_SUCCEEDED);
}
 
  1. Fernando Carreiro #: You are including a header file within the middle of a function body, and defining "#property" statements within a function body too.

    That's one. You can't define functions withing other functions.

  2. //+------------------------------------------------------------------+
    //| Script program start function                                    |
    //+------------------------------------------------------------------+
    void OnTicks() {

    You define a new function but never call it.

  3.       stopLoss = entryPrice - pips * GetPipValue();
       }
       else
       {
          stopLoss = entryPrice + pips * GetPipValue();
    

    A PIP does not have a value, it is a unit of distance. Only distance times lots has a value. You want the size of a PIP.

    PIP, Point, or Tick are all different in general.
              Ticks, PIPs or points in the GUI. Make up your mind. - MQL4 programming forum #1 (2014)
              Percentage in point - Wikipedia

    Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers (if any still exists), exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a logical PIP is and use that, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018)


  4. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)