EA does not respond

 

Hi everyone I am seeking help on something I do not understand (code at the end of page). When I using this code with the parameters I filled in, and I fill in the desired time for trade 1, it works totally fine. But whenever I change the parameters, sometimes it works and sometimes it does not seem to respond at all. For example if I change the stoploss into 40 it does not work (it does not give an error but just does not place the trades) but when I change takeprofit into 300 it works. Does anyone have any clue why this happens?


I am using this code:

#include <Trade/Trade.mqh>
CTrade trade;

// Parameters

input int TAKEPROFIT = 200; // Take profit in pips

input int STOPLOSS = 45; // Stoploss in pips 

input double LOT = 2.2;  // Lot Size

input string symbols = "EURUSD";   // Pair

input int InpClose = 60; // Time for Trade closing (seconds)

input string openinghour_min = "00:00:00"; // Trading Time 1
input string closinghour_min = ""; // Trading Time 2
input string closinghour_min1 = ""; // Trading Time 3

// Functie

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

  }

void OnTick()
  {

   double Ask = NormalizeDouble(SymbolInfoDouble(symbols, SYMBOL_ASK), _Digits); //Ask price
   double Bid = NormalizeDouble(SymbolInfoDouble(symbols, SYMBOL_BID), _Digits); //Bid price

   datetime localtime = TimeLocal();
   string hourmin = TimeToString(localtime, TIME_SECONDS);
  
   if((((StringSubstr(hourmin, 0, 8) == openinghour_min && openinghour_min != "00:00:00") || (StringSubstr(hourmin, 0, 8) == closinghour_min && closinghour_min != "00:00:00") || (StringSubstr(hourmin, 0, 8) == closinghour_min1 && closinghour_min1 != "00:00:00")) &&  PositionsTotal() < 1))
     {
      trade.Buy(LOT, symbols, Ask, NormalizeDouble((Ask - STOPLOSS * _Point), _Digits), NormalizeDouble((Ask + TAKEPROFIT * _Point), _Digits), NULL);

      trade.Sell(LOT, symbols, Bid, NormalizeDouble((Bid + STOPLOSS * _Point), _Digits), NormalizeDouble((Bid - TAKEPROFIT * _Point), _Digits), NULL);

     }

   TimeCloser();
  }

void TimeCloser() // Function to close trades after one minute
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      string symbol = PositionGetSymbol(i);
      if(_Symbol == symbols)
        {
         if((PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL))
           {
            long openT = PositionGetInteger(POSITION_TIME);
            int diff = (int)(TimeCurrent() - openT);
            if(diff >= InpClose)
              {
               ulong positionticket = PositionGetInteger(POSITION_TICKET);
               trade.PositionClose(positionticket, 9);
              }
           }
  
         else
            if((PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY))
              {
               long openT = PositionGetInteger(POSITION_TIME);
               int diff = (int)(TimeCurrent() - openT);
               if(diff >=  InpClose)
                 {
                  ulong positionticket = PositionGetInteger(POSITION_TICKET);
                  trade.PositionClose(positionticket, 9);
                 }
              }
    
        }
     }

  }

 

Hi,

I'm still a beginner and maybe my answer won't help you, but since you're talking about the SL, isn't it a problem of minimum stop loss distance because some brokers ask for a minimum distance? with this fonction


int  StopsLevel() const
Documentation sur MQL5: Bibliothèque Standard / Classes pour le Trading / CSymbolInfo / StopsLevel
Documentation sur MQL5: Bibliothèque Standard / Classes pour le Trading / CSymbolInfo / StopsLevel
  • www.mql5.com
StopsLevel - CSymbolInfo - Classes pour le Trading - Bibliothèque Standard - Référence MQL5 - Référence sur le langage de trading algorithmique/automatisé pour MetaTrader 5
 
Even if your EA is not for the Market, you should still apply these checks in your own code. So read and apply the following to your code ...

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.