Expert Advisor - Entry Signals

 
Hi folks,

I am having problem with my tester not showing entry signals even when I test the strategy live(the trades don't open). The same strategy works fine on the account with both strategy tester and live test, the entry signals show. So, I am assuming that they must some setting that I must set. 

I checked the visual mode when I do strategy test. But most important thing is that the different strategies are complied with no errors.


This class of the strategy I used .mqh 

class CSpreadChecker
{
   private:
      double spread;
      string spreadMessage;
   
   public:
      void CSpreadChecker();
      string CheckSpread(double pBid, double pAsk, double pMaxSpread);
}; 

void CSpreadChecker::CSpreadChecker(void)
{
  spreadMessage = "";
}

string CSpreadChecker::CheckSpread(double pBid,double pAsk, double pMaxSpread)
{
   // Use the input parameters to calculatte the bid spread
   spread = (pAsk - pBid)/_Point;
   
   // Change the message in the window depending in the size of the spread
   if(spread > pMaxSpread)
   {
      StringConcatenate(spreadMessage, "ALERT: the spread is greater than the maximum allowable spread:", DoubleToString(spread,0));
   }
   else
   {
      StringConcatenate(spreadMessage, "The current spread is: ", DoubleToString(spread,0));
   }
   return spreadMessage;
}

This is the .mq5 file, the strategy related to the above code.

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Spread_Checker.mqh>;
input double maxSpread = 5.0;

input int momentumPeriod = 14; // Momentum Period
input double momentumThreshold = 100; // Threshold for Signals
input ENUM_APPLIED_PRICE momentumPrice = PRICE_CLOSE; // Applied Price
input double tradeVolume = 0.1; // Trade Volume (Lots)

CSpreadChecker spreadChecker;

//Global Variables
bool gBuyPlaced, gSellPlaced;

long LastFiveVolumes[] = {0,0,0,0,0};

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+---------------------- --------------------------------------------+
void OnDeinit(const int reason)
  {
//---
     Comment("");
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

// This is a function to calculate and return the spread
double CalculateSpread(double pBid, double pAsk)
{
   double spread = (pAsk - pBid)/_Point; 
   return spread;
}
void OnTick()
  {
//---Indicator Sector
      double momentum[];
      ArraySetAsSeries(momentum, true);
      
      int momentumHandle = iMomentum(_Symbol, PERIOD_CURRENT, momentumPeriod, momentumPrice);
      CopyBuffer(momentumHandle, 0, 0, 3, momentum);
      
      double currentMomentum = momentum[0];
      
//---Change the message depending on the state of the indicator
      string indicatorMessage = "";
      

//--- Trade Logic

      MqlTradeRequest request;
      MqlTradeResult results;
      ZeroMemory(request);
      ZeroMemory(results);
       
      long ticket = 0;
      if(PositionSelect(_Symbol))
      {
         ticket = PositionGetInteger(POSITION_TICKET);
         
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            gBuyPlaced = true;
            gSellPlaced = false;
         }
         else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         {
            gBuyPlaced = false;
            gSellPlaced = true;
         }
          
      }
       

//---Indicator Logic

// Open a buy order, close any sell orders
            
      if(currentMomentum > momentumThreshold)
      {
         indicatorMessage = "Price momentum is positive, GO LONG!!";
         // Close sell
         if(gSellPlaced == true && ticket > 0)
         {
            PositionSelectByTicket(ticket);
            request.action = TRADE_ACTION_DEAL;
            request.type = ORDER_TYPE_BUY;
            request.symbol = _Symbol;
            request.position = ticket;
            request.volume = PositionGetDouble(POSITION_VOLUME);
            request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            request.deviation = 50;
            request.comment = "Sell Close";
            
            bool sent = OrderSend(request, results);
            Print("Sell trade close request sent: ", sent);
         }
         
         // Open a buy order
         if(gBuyPlaced == false)
         {
            request.action = TRADE_ACTION_DEAL  ;
            request.type = ORDER_TYPE_BUY;
            request.symbol = _Symbol;
            request.position = 0;
            request.volume = tradeVolume;
            request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            request.sl = 0;
            request.tp = 0;
            request.deviation = 50;
            request.comment = "Buy Open"; 
            
            bool sent = OrderSend(request, results);
            Print("Buy trade open request sent", sent);
         }
        
      }
      else
      {
         indicatorMessage = "Price momentum is negative, GO SHORT!!!";
         // Close buy
         if(gBuyPlaced == true && ticket > 0)
         {
            PositionSelectByTicket(ticket);
            request.action = TRADE_ACTION_DEAL;
            request.type = ORDER_TYPE_SELL;
            request.symbol = _Symbol;
            request.position = ticket;
            request.volume = PositionGetDouble(POSITION_VOLUME);
            request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            request.deviation = 50;
            request.comment = "Buy Close";
            
            bool sent = OrderSend(request, results);
            Print("Buy trade close request sent: ", sent);
         }
         
         // Open a buy order
         if(gSellPlaced == false)
         {
            request.action = TRADE_ACTION_DEAL  ;
            request.type = ORDER_TYPE_SELL;
            request.symbol = _Symbol;
            request.position = 0;
            request.volume = tradeVolume;
            request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            request.sl = 0;
            request.tp = 0;
            request.deviation = 50;
            request.comment = "Sell Open"; 
            
            bool sent = OrderSend(request, results);
            Print("Sell trade open request sent", sent);
         }
      }

//---Check the spread
      double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      string message = spreadChecker.CheckSpread(bid,ask, maxSpread);
      Comment(message);
      
//---Populate arrary with the last 5 volumes on the chart
     for(int i=0; i<ArraySize(LastFiveVolumes); i++)
     {
        LastFiveVolumes[i] = iVolume(_Symbol,PERIOD_CURRENT,i);
        ArrayPrint(LastFiveVolumes);
     }
  
//---Comment the list of the last 5 volumes on the chart
     string VolumeMessage;
 
     int j = 0;
     while(j<ArraySize(LastFiveVolumes))
     {
        StringAdd(VolumeMessage, IntegerToString(LastFiveVolumes[j]));
        StringAdd(VolumeMessage, "\t ");
        j++; //!Important!
     
     }
     StringAdd(message, "\nLast 5 volumes: \n");
     StringAdd(message, VolumeMessage);
     
     StringAdd(message, "\n \n");
     StringAdd(message, indicatorMessage);
     Comment(message);

  }
//+------------------------------------------------------------------+

Please help with this, because all the strategy I code myself don't work on my account but work on other account. And the other thing is that the example or robots that came with the mql5 work and shows entry signals.

If you can copy and check if the strategy works on your side and maybe the is a line of code needed for the entry signals to be visible and trade to open.

The screenshot shows that the strategy complied.


I really appreciate any help.
 
MolemoDibe_27:
Hi folks,

I am having problem with my tester not showing entry signals even when I test the strategy live(the trades don't open). The same strategy works fine on the account with both strategy tester and live test, the entry signals show. So, I am assuming that they must some setting that I must set. 

I checked the visual mode when I do strategy test. But most important thing is that the different strategies are complied with no errors.


This class of the strategy I used .mqh 

This is the .mq5 file, the strategy related to the above code.

Please help with this, because all the strategy I code myself don't work on my account but work on other account. And the other thing is that the example or robots that came with the mql5 work and shows entry signals.

If you can copy and check if the strategy works on your side and maybe the is a line of code needed for the entry signals to be visible and trade to open.

The screenshot shows that the strategy complied.


I really appreciate any help.

According to what I read, all forms of immediate execution orders require the  type_filling field to be set.

Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Interaction between the client terminal and a trade server for executing the order placing operation is performed by using trade requests. The...
 
Thank you, I got it to work on the same code provided before, I was just using wrong order. However I cant get it to work on time based strategy.

The code was in MQL4, i used ChartGPT to help with conversion to MQL5.

I don't not if the limit are not met or they is a problem with the code.

I would appreciate any help that might be with the code for entry signals or anything.

Thanks in advance.



//+------------------------------------------------------------------+
//|                                                         Demo.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.01"

#property description "Demo"
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 5
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 5
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

#define PLOT_MAXIMUM_BARS_BACK 5000
#define OMIT_OLDEST_BARS 50

double Buffer1[]; // Buy signals
double Buffer2[]; // Sell signals

datetime time_alert;
double myPoint;
input double tradeVolume = 0.1; // Trade volume (lots)
input double maxSpread = 5.0;   // Maximum allowable spread (points)

// Order placement function
bool PlaceOrder(bool isBuy)
{
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   ZeroMemory(result);

   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.volume = tradeVolume;
   request.type = isBuy ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
   request.price = isBuy ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);
   request.deviation = 50;
   request.type_filling = ORDER_FILLING_IOC;
   request.type_time = ORDER_TIME_GTC;
   request.comment = isBuy ? "Buy Order" : "Sell Order";

   if (!OrderSend(request, result))
   {
      Print("Order placement failed. Error: ", GetLastError());
      return false;
   }

   Print("Order placed successfully. Order ID: ", result.order);
   return true;
}

// Alert function for logging or messaging
void myAlert(string type, string message)
{
   if (type == "print")
      Print(message);
   else if (type == "error")
      Print(type + " | Demo @ " + Symbol() + ", " + IntegerToString(Period()) + " | " + message);
   else if (type == "indicator")
      Print(type + " | Demo @ " + Symbol() + ", " + IntegerToString(Period()) + " | " + message);
}

// Initialization function
int OnInit()
{
   // Set up indicator buffers
   SetIndexBuffer(0, Buffer1, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MathMax(Bars(_Symbol, _Period) - PLOT_MAXIMUM_BARS_BACK + 1, OMIT_OLDEST_BARS + 1));
   PlotIndexSetInteger(0, PLOT_ARROW, 241);

   SetIndexBuffer(1, Buffer2, INDICATOR_DATA);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, MathMax(Bars(_Symbol, _Period) - PLOT_MAXIMUM_BARS_BACK + 1, OMIT_OLDEST_BARS + 1));
   PlotIndexSetInteger(1, PLOT_ARROW, 242);

   // Initialize myPoint for broker digits
   myPoint = Point();
   if (Digits() == 5 || Digits() == 3)
      myPoint *= 10;

   return (INIT_SUCCEEDED);
}

// Main calculation function
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[])
{
   int limit = rates_total - prev_calculated;

   // Initialize arrays
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);

   if (prev_calculated < 1)
   {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
   }
   else
      limit++;

   // Main calculation loop
   for (int i = limit - 1; i >= 0; i--)
   {
      if (i >= MathMin(PLOT_MAXIMUM_BARS_BACK - 1, rates_total - 1 - OMIT_OLDEST_BARS))
         continue;

      int barshift_M1 = iBarShift(Symbol(), PERIOD_M1, time[i]);
      if (barshift_M1 < 0)
         continue;

      double spread = (SymbolInfoDouble(_Symbol, SYMBOL_ASK) - SymbolInfoDouble(_Symbol, SYMBOL_BID)) / _Point;

      if (iClose(NULL, PERIOD_M1, barshift_M1) > iOpen(NULL, PERIOD_M1, barshift_M1))
      {
         Buffer1[i] = iLow(NULL, PERIOD_M1, barshift_M1);
         if (i == 0 && time[0] != time_alert)
         {
            if (spread <= maxSpread)
            {
               if (PlaceOrder(true))
                  myAlert("indicator", "Buy Order Placed");
            }
            else
               myAlert("error", "Spread too high for Buy order");
            time_alert = time[0];
         }
      }
      else
      {
         Buffer1[i] = EMPTY_VALUE;
      }

      if (iClose(NULL, PERIOD_M1, barshift_M1) < iOpen(NULL, PERIOD_M1, barshift_M1))
      {
         Buffer2[i] = iHigh(NULL, PERIOD_M1, barshift_M1);
         if (i == 0 && time[0] != time_alert)
         {
            if (spread <= maxSpread)
            {
               if (PlaceOrder(false))
                  myAlert("indicator", "Sell Order Placed");
            }
            else
               myAlert("error", "Spread too high for Sell order");
            time_alert = time[0];
         }
      }
      else
      {
         Buffer2[i] = EMPTY_VALUE;
      }
   }
   return (rates_total);
}
 
MolemoDibe_27 #: The code was in MQL4, i used ChartGPT to help with conversion to MQL5.

Please, don't request help for ChatGPT (or other A.I.) generated code. It generates horrible code, mixing MQL4 and MQL5.

Either learn to code manually or use the Freelance section for such requests — https://www.mql5.com/en/job

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.01.01
  • www.mql5.com
The largest freelance service with MQL5 application developers