Expert Advisor

 

Can anyone check out the following script, and explain why it has not yet generated any trade, and whether it well ever trade.

//+------------------------------------------------------------------+
//| Expert Advisor: XBRUSD EMA Scalper - 30 Ticks Target             |
//+------------------------------------------------------------------+
#property copyright "OpenAI"
#property version   "1.00"
#property strict
#include <Trade\Trade.mqh>
CTrade trade;
input double LotSize        = 0.5;
input double TakeProfitTicks = 30;     // 30 ticks target
input double StopLossTicks  = 15;      // 15 ticks stop
input int EMAFastPeriod     = 9;
input int EMASlowPeriod     = 21;
input double TickSize       = 0.01;    // Tick size for XBRUSD
int fastMAHandle, slowMAHandle;
//+------------------------------------------------------------------+
int OnInit()
  {
   fastMAHandle = iMA(_Symbol, _Period, EMAFastPeriod, 0, MODE_EMA, PRICE_CLOSE);
   slowMAHandle = iMA(_Symbol, _Period, EMASlowPeriod, 0, MODE_EMA, PRICE_CLOSE);
   if (fastMAHandle == INVALID_HANDLE || slowMAHandle == INVALID_HANDLE)
     {
      Print("Failed to create MA handles.");
      return(INIT_FAILED);
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnTick()
  {
   // Don't enter if a trade is already open
   if (PositionSelect(_Symbol))
      return;
   double maFast[], maSlow[];
   if (CopyBuffer(fastMAHandle, 0, 0, 3, maFast) < 3 || CopyBuffer(slowMAHandle, 0, 0, 3, maSlow) < 3)
     {
      Print("Failed to read MA buffers.");
      return;
     }
   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double tp = TakeProfitTicks * TickSize;
   double sl = StopLossTicks * TickSize;
   // Buy condition: Fast MA crosses above Slow MA and bullish candle
   if (maFast[1] < maSlow[1] && maFast[0] > maSlow[0])
     {
      MqlRates rates[];
      if (CopyRates(_Symbol, _Period, 0, 2, rates) < 2) return;
      if (rates[0].close > rates[0].open)
        {
         trade.Buy(LotSize, _Symbol, ask, ask - sl, ask + tp);
         Print("Buy order placed.");
        }
     }
   // Sell condition: Fast MA crosses below Slow MA and bearish candle
   else if (maFast[1] > maSlow[1] && maFast[0] < maSlow[0])
     {
      MqlRates rates[];
      if (CopyRates(_Symbol, _Period, 0, 2, rates) < 2) return;
      if (rates[0].close < rates[0].open)
        {
         trade.Sell(LotSize, _Symbol, bid, bid + sl, bid - tp);
         Print("Sell order placed.");
        }
     }
  }
//+------------------------------------------------------------------+ 
 

Hello,

try with :

if (!PositionSelect ( _Symbol ))
       return ;
 
S Davidovits:

Can anyone check out the following script, and explain why it has not yet generated any trade, and whether it well ever trade.

Look up ArraySetAsSeries. also you should use NormalizeDouble your TP and SL that have resulted from your calculation, or you may get 4756 errors.

And also note that codes by AI and ChatGPT are often ridiculed on this forum. you should have removed the OpenAI reference in the code.

mafast maslo should be in global enviro, not in OnTick. This is common issue with AI code.

And after looking at code again, it has too many issues for me think that it is worth fixing. I would also remove MqlRates and instead, use CopyClose with an array called close on the global enviro.

There could be other issues, but not going to respond further. AI code is worthless 8/10 times.
 

Too many things here which warps you from the correct path of making an EA.

1. No magic number

2. Overcomplication (no need for using MqlRates struct)

3. The AI making *sinister* mistakes ie. not using ArraySetAsSeries when multiple bars are used in CopyBuffer (which will make the bot trade randomly)


Don't waste your energy on the wrong thing. Look at articles here which discuss how to code an EA (even at beginner level). Too many people are overlooking this section of the forum https://www.mql5.com/en/articles/15299

Introduction to MQL5 (Part 8): Beginner's Guide to Building Expert Advisors (II)
Introduction to MQL5 (Part 8): Beginner's Guide to Building Expert Advisors (II)
  • 2024.07.18
  • www.mql5.com
This article addresses common beginner questions from MQL5 forums and demonstrates practical solutions. Learn to perform essential tasks like buying and selling, obtaining candlestick prices, and managing automated trading aspects such as trade limits, trading periods, and profit/loss thresholds. Get step-by-step guidance to enhance your understanding and implementation of these concepts in MQL5.
 
Hi,
I purchase an EA 
However when I click to install it it does  not work.
I talk to the seller About it 
He told me to contact MQL5 support 
Is Anyone face this issue and know how to fix it ? 
     Thank you 
 
Stevenson Aubry #:
Hi,
I purchase an EA 
However when I click to install it it does  not work.
I talk to the seller About it 
He told me to contact MQL5 support 
Is Anyone face this issue and know how to fix it ? 
     Thank you 

It works. If iit does not work for you so it means that you are doing something wrong.
There are the instructions about HowTo:

How to buy, install, test and use a MT4 Expert Advisor - How to buy and install a Metatrader 4 Expert Advisor
How to buy, install, test and use a MT4 Expert Advisor - How to buy and install a Metatrader 4 Expert Advisor
  • 2021.03.30
  • Eleni Anna Branou
  • www.mql5.com
Here is a step by step guide for buying, installing, testing and using a metatrader 4 expert advisor 1. On the left side of the market tab, click the purchased option and then click the blue install button on the right side, of the product you t see your purchase when you click the purchased option, check the below mentioned issues that you have internet explorer or microsoft edge latest version installed on your computer or vps that you are using the latest build of metatrader 4 that you are logged into your mql5 account in mt4 community tab, using your login - not your email that your mql5 account password is less than 10 characters if your password is 10 or more characters - change mql5 account password t see the product on the list of your purchases in the terminal, try the following your metatrader terminals go to c community delete all files starting from mql4