Help with my EA

ultimate3101

Trader
Mar 6, 2024
5
0
6
24
Hi Guys!
I need Help with the Code of an EA.
So I created my EA, and Now No compile Errors exist anymore, but the Problem is, it doesnt Take any trades..

Let me summarize the Logic fast
So, there is a Candle Time variable.
The High and Low of the Candle will be used as Zone and then we wait for a Candle breakout, with the specified min. ATR Settings and SMA Settings.
And Entry on Close, so the Candle needs to Close above/under the Zone for a Buy/sell and it's Rest of Body or Wick should Touch the Zone. (I will send some Pictures to clarify the logic)
Also spreads Settings, is to make TP smaller and SL wider, so you dont get Wicked Out/dont reach your tp..
And thats how SL works:
If it's a buy, SL will be the Low of that Candle that we used in the Settings (Here 10:00)
And If it's a sell, it will be the high of that candle
ofcourse because of spreads Settings, it will get wider (Here 2 Pips)
And Im also Not Understanding how to Programm the Max Orders code


So yeah Here is the Code:
MQL4:
// Define parameters
input string candleTime = "10:00";
Input int maxOrders = 1;
input int tpPips = 200;
input int magicNumber = 282518;
input double lots = 0.01;
input int atrPeriod = 14;
input double minAtrLevel = 1.00;
input int smaPeriod = 200;
input int bePoints = 100;
input int trailStartPoints = 110;
input int trailStepPoints = 50;
input int trailStopPoints = 300;
input int spreadPips = 2;
 
// Global variables
double zoneHigh, zoneLow;
 
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
    // Add any initialization code here
    return(INIT_SUCCEEDED);
}
 
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
    // Check if it's the specified candle time
    if (TimeHour(TimeCurrent()) == StringToInteger(StringSubstr(candleTime, 0, 2)) &&
        TimeMinute(TimeCurrent()) == StringToInteger(StringSubstr(candleTime, 3, 2)))
    {
        zoneHigh = High[1];
        zoneLow = Low[1];
 
        // Check if the ATR level meets the criteria
        double atrValue = iATR(_Symbol, 0, atrPeriod);
 
        if (atrValue >= minAtrLevel * Point)
        {
            // Check for buy condition
            if (Close[1] > zoneHigh && Close[1] > iMA(_Symbol, 0, smaPeriod, 0, MODE_SMA, PRICE_CLOSE, 0))
            {
                // Buy logic
                double entryPriceBuy = Ask;
                double slPriceBuy = zoneLow - spreadPips * Point;
                double tpPriceBuy = entryPriceBuy + tpPips * Point - spreadPips * Point;
                ModifyOrder(entryPriceBuy, slPriceBuy, tpPriceBuy);
            }
            // Check for sell condition
            else if (Close[1] < zoneLow && Close[1] < iMA(_Symbol, 0, smaPeriod, 0, MODE_SMA, PRICE_CLOSE, 0))
            {
                // Sell logic
                double entryPriceSell = Bid;
                double slPriceSell = zoneHigh + spreadPips * Point;
                double tpPriceSell = entryPriceSell - tpPips * Point + spreadPips * Point;
                ModifyOrder(entryPriceSell, slPriceSell, tpPriceSell);
            }
        }
    }
}
 
// Function to modify order
void ModifyOrder(double entryPrice, double slPrice, double tpPrice)
{
    // Apply spread adjustment
    slPrice += spreadPips * Point;
    tpPrice -= spreadPips * Point;
 
    int ticket = OrderSend(_Symbol, OP_BUY, lots, entryPrice, 3, slPrice, tpPrice, "Buy Order", magicNumber, 0, Blue);
 
    // Check if the order is opened successfully
    if (ticket > 0)
    {
        Print("Order opened successfully. Ticket: ", ticket);
    }
    else
    {
        Print("Error opening order. Error code: ", GetLastError());
    }
}
 
//+------------------------------------------------------------------+

I tried to ask chatgpt But litterally gave me the Code lightly updated with many comments..
Didnt Help me, thats why I want to ask you Guys, so you hopefully can Help me fixing it.

MQL4:
// Define parameters
input string candleTime = "10:00";
input int tpPips = 200;
input int magicNumber = 282518;
input double lots = 0.01;
input int atrPeriod = 14;
input double minAtrLevel = 1.00;
input int smaPeriod = 200;
input int bePoints = 100;
input int trailStartPoints = 110;
input int trailStepPoints = 50;
input int trailStopPoints = 300;
input int spreadPips = 2;
 
// Global variables
double zoneHigh, zoneLow;
 
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
    // Add any initialization code here
    return(INIT_SUCCEEDED);
}
 
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
    // Check if it's the specified candle time
    if (TimeHour(TimeCurrent()) == StringToInteger(StringSubstr(candleTime, 0, 2)) &&
        TimeMinute(TimeCurrent()) == StringToInteger(StringSubstr(candleTime, 3, 2)))
    {
        zoneHigh = High[1];
        zoneLow = Low[1];
 
        // Check if the ATR level meets the criteria
        double atrValue = iATR(_Symbol, 0, atrPeriod);
 
        if (atrValue >= minAtrLevel * Point)
        {
            // Check for buy condition
            if (Close[1] > zoneHigh && Close[1] > iMA(_Symbol, 0, smaPeriod, 0, MODE_SMA, PRICE_CLOSE, 0))
            {
                // Buy logic
                double entryPriceBuy = Ask;
                double slPriceBuy = zoneLow - spreadPips * Point;
                double tpPriceBuy = entryPriceBuy + tpPips * Point - spreadPips * Point;
                ModifyOrder(entryPriceBuy, slPriceBuy, tpPriceBuy);
            }
            // Check for sell condition
            else if (Close[1] < zoneLow && Close[1] < iMA(_Symbol, 0, smaPeriod, 0, MODE_SMA, PRICE_CLOSE, 0))
            {
                // Sell logic
                double entryPriceSell = Bid;
                double slPriceSell = zoneHigh + spreadPips * Point;
                double tpPriceSell = entryPriceSell - tpPips * Point + spreadPips * Point;
                ModifyOrder(entryPriceSell, slPriceSell, tpPriceSell);
            }
        }
    }
}
 
// Function to modify order
void ModifyOrder(double entryPrice, double slPrice, double tpPrice)
{
    // Apply spread adjustment
    slPrice += spreadPips * Point;
    tpPrice -= spreadPips * Point;
 
    int ticket = OrderSend(_Symbol, OP_BUY, lots, entryPrice, 3, slPrice, tpPrice, "Buy Order", magicNumber, 0, Blue);
 
    // Check if the order is opened successfully
    if (ticket > 0)
    {
        Print("Order opened successfully. Ticket: ", ticket);
    }
    else
    {
        Print("Error opening order. Error code: ", GetLastError());
    }
}
 
//+------------------------------------------------------------------+



Yeah thats the Code, it Took a Lot of effort, but it keeps Not working, thanks in Advance for helping
 

Attachments

  • Screenshot_2024-03-07-20-00-09-383_net.metaquotes.metatrader5-edit.jpg
    Screenshot_2024-03-07-20-00-09-383_net.metaquotes.metatrader5-edit.jpg
    102.6 KB · Views: 3
  • Screenshot_2024-03-07-20-01-40-664_net.metaquotes.metatrader5-edit.jpg
    Screenshot_2024-03-07-20-01-40-664_net.metaquotes.metatrader5-edit.jpg
    99.1 KB · Views: 3
  • Screenshot_2024-03-07-20-02-52-812_net.metaquotes.metatrader5-edit.jpg
    Screenshot_2024-03-07-20-02-52-812_net.metaquotes.metatrader5-edit.jpg
    97.8 KB · Views: 2
  • Screenshot_2024-03-07-20-05-24-413_net.metaquotes.metatrader5-edit.jpg
    Screenshot_2024-03-07-20-05-24-413_net.metaquotes.metatrader5-edit.jpg
    108 KB · Views: 2
  • Screenshot_2024-03-07-20-07-10-423_net.metaquotes.metatrader5-edit.jpg
    Screenshot_2024-03-07-20-07-10-423_net.metaquotes.metatrader5-edit.jpg
    101.6 KB · Views: 2
  • Screenshot_2024-03-08-07-11-39-365_net.metaquotes.metatrader5-edit.jpg
    Screenshot_2024-03-08-07-11-39-365_net.metaquotes.metatrader5-edit.jpg
    115.4 KB · Views: 3
Last edited by a moderator:

Enivid

Administrator
Staff member
Nov 30, 2008
19,168
1,494
144
Odesa
www.earnforex.com
There are multiple issues in your code. The ones I've noticed are the following:
1. Your code executes only if the current candle started at 10:00 server time. Then all your checks are for the previous candle. So, if you are on H1, it performs all the checks for the 9:00 candle. Is that how you intend it to work?
2. When you are reading the ATR value, you don't set the Shift value - not sure how that even compiles without errors.
3. All your entry conditions are checking whether Close of the previous candle is higher than the High of the previous candle or whether that Close is lower than the Low of the same candle. Obviously, neither condition will ever be true.
4. Your ModifyOrder() function (which is actually used for entry) can only open buy orders.
5. The same function applies some spread adjustments to SL and TP, but it looks like you already do those once before calling the function.
 

ultimate3101

Trader
Mar 6, 2024
5
0
6
24
There are multiple issues in your code. The ones I've noticed are the following:
1. Your code executes only if the current candle started at 10:00 server time. Then all your checks are for the previous candle. So, if you are on H1, it performs all the checks for the 9:00 candle. Is that how you intend it to work?
2. When you are reading the ATR value, you don't set the Shift value - not sure how that even compiles without errors.
3. All your entry conditions are checking whether Close of the previous candle is higher than the High of the previous candle or whether that Close is lower than the Low of the same candle. Obviously, neither condition will ever be true.
4. Your ModifyOrder() function (which is actually used for entry) can only open buy orders.
5. The same function applies some spread adjustments to SL and TP, but it looks like you already do those once before calling the function.
Oh okay, thanks for this!
Well my Goal was to create A bot that uses the 10:00 Candle as Zone, and then If a Candle touches the Zone while also Closing above the zone, while atr value is over 1.00, it should Open buy/sell
It was Designed for 5m and also the ma is for Filtering buy/sell.
And because of the spread, I wanted, that this function modifies TP and and SL based on spread. And that the bot First Opens an Order and then then modifies it.


I think I'll need some time to fix it