Made my first EA in MQL4 but no longer attaches to charts, what could be the issue?

 

I assume me deleting the .ex4 caused the problem (accidentally deleted it thinking it was a duplicate) but, it could be another reason as it worked prior to that.

Now, when I attempt to attach it to charts. It comes back with an error that it is not an EA. My code has no errors, so I'm a bit lost what's wrong.

I've attempted to compile the EA, so i created a new .ex4 and tried copying my code into a new EA, but the same problem. It won't attach to the charts because it doesn't read it as an EA anymore.

When i first started, there were no issues.

Any ideas? I checked my experts and have the error "'EA' is not expert and can not be executed"


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

// Dynamically generated yearly copyright
string CopyrightYearly;

// 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

    // 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

//+------------------------------------------------------------------+
//| 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 balance = AccountBalance();
    string message = "Your Account Balance is: £" + DoubleToString(balance, 2);
    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 * 0.0001;
    } else {
        stopLossPrice = entryPrice + maxLossInPips * 0.0001;
    }

    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 = "The current spread (points) is: ";
    }
    
    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 OnStart() {
    // 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);
}
Documentation on MQL5: Python Integration / order_calc_margin
Documentation on MQL5: Python Integration / order_calc_margin
  • www.mql5.com
order_calc_margin - Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Gregory Delazeri:

I assume me deleting the .ex4 caused the problem (accidentally deleted it thinking it was a duplicate) but, it could be another reason as it worked prior to that.

Now, when I attempt to attach it to charts. It comes back with an error that it is not an EA. My code has no errors, so I'm a bit lost what's wrong.

I've attempted to compile the EA, so i created a new .ex4 and tried copying my code into a new EA, but the same problem. It won't attach to the charts because it doesn't read it as an EA anymore.

When i first started, there were no issues.

Any ideas? I checked my experts and have the error "'EA' is not expert and can not be executed"

Are you sure that you the file that you are trying to attach to the charts -- is the same filename that you have recompiled? ie in past i thought I have a duplicate, and deleted the file or just part of the filename, and then tried to rename the file again as original name, but then I found that I had 2 or more files with similar names, but each had _2 or _3 or _4 to the end. Check that you are trying to attach the same file to the chart -- that you are recomplied.

 
Gregory Delazeri:

I assume me deleting the .ex4 caused the problem (accidentally deleted it thinking it was a duplicate) but, it could be another reason as it worked prior to that.

Now, when I attempt to attach it to charts. It comes back with an error that it is not an EA. My code has no errors, so I'm a bit lost what's wrong.

I've attempted to compile the EA, so i created a new .ex4 and tried copying my code into a new EA, but the same problem. It won't attach to the charts because it doesn't read it as an EA anymore.

When i first started, there were no issues.

Any ideas? I checked my experts and have the error "'EA' is not expert and can not be executed"


It works when i put it into the scripts folder. so it is a script, not an ea. However I dont understand why there is a OnTicks function if it is a script.

 

Do you think it would be a good idea to delete any .ex4 files i have then so i could recompile and perhaps that could fix it?

Revo Trades #:

Are you sure that you the file that you are trying to attach to the charts -- is the same filename that you have recompiled? ie in past i thought I have a duplicate, and deleted the file or just part of the filename, and then tried to rename the file again as original name, but then I found that I had 2 or more files with similar names, but each had _2 or _3 or _4 to the end. Check that you are trying to attach the same file to the chart -- that you are recomplied.

 

Might be a simple mistake, very new to coding this so it is a lot to take in. If I understand correctly, the OnTicks functions should only exist in scripts rather than EA. so better to remove that?

Revo Trades #:

It works when i put it into the scripts folder. so it is a script, not an ea. However I dont understand why there is a OnTicks function if it is a script.

 
Gregory Delazeri #:

Might be a simple mistake, very new to coding this so it is a lot to take in. If I understand correctly, the OnTicks functions should only exist in scripts rather than EA. so better to remove that?

OnTicks usually will only occur in EAs.

Similarly, OnStart usually occurs only in scripts.

Whereas, you have both in the code.

 

Ah! i see, it works now. I switched OnStart to OnTicks and it addressed it. Thanks!

Revo Trades #:

OnTicks usually will only occur in EAs.

Similarly, OnStart usually occurs only in scripts.

Whereas, you have both in the code.

 
Gregory Delazeri #:

Ah! i see, it works now. I switched OnStart to OnTicks and it addressed it. Thanks!

you're welcome and thanks for responding. Most peeps dont report that their issue is fixed, nor mention what they did to fix it.

 

No worries, and thank you for helping!. A bit disappointed that I don't know the difference between the script and main ea though.

I wonder, was it a waste of time adding a dynamic copyright? I'm considering just converting it back towards a simple static copyright to make the code smaller

I think I probably should have started this in mt5 aswell instead since it is backward compatible. But perhaps it is easy enough to convert to mt5 when i am done?

Revo Trades #:

you're welcome and thanks for responding. Most peeps dont report that their issue is fixed, nor mention what they did to fix it.

 
Gregory Delazeri #:

No worries, and thank you for helping!. A bit disappointed that I don't know the difference between the script and main ea though.

I wonder, was it a waste of time adding a dynamic copyright? I'm considering just converting it back towards a simple static copyright to make the code smaller

I think I probably should have started this in mt5 aswell instead since it is backward compatible. But perhaps it is easy enough to convert to mt5 when i am done?

I do not see any issue either way; but i do think that when the compiler does "its thing", i think the #property lines are automaticly put into the preprocessor-process anyways. (i think that is the terms) hahaha

 
Gregory Delazeri #:

I think I probably should have started this in mt5 aswell instead since it is backward compatible. But perhaps it is easy enough to convert to mt5 when i am done?

You will have to change the MarketInfo to SymbolInfoDouble, but otherwise, it looks to me to be right after you change that, yeah. MT5 does not have MarketInfo

Reason: