help to code

 

Good evening to all, I appeal to you because I have a problem with my code, indeed normally it must perform an order every 100 min: buy if the market is up or sell if it is down. The problem is that my code only executes sales, but it does not make purchases. I can’t figure out why he won’t buy, can you help me ?

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

bool symbolIsAlreadyTrade(string symbole, uint period){
   
   if(HistorySelect(TimeCurrent() - period, TimeCurrent())){
     
      for(int i = 0; i < HistoryDealsTotal(); i += 1){
         
         ulong ticket = HistoryDealGetTicket(i);
         
         if(HistoryDealGetString(ticket, DEAL_SYMBOL) == symbole && HistoryDealGetInteger(ticket, DEAL_ENTRY) == (int)DEAL_ENTRY_IN){
            return true;
         }
      }
   }
   
   return false;
}

void OnTick(){

   if(!symbolIsAlreadyTrade(_Symbol, 100 * 60)){
   
      MqlRates mrate[];
      ArraySetAsSeries(mrate,true);
     
      if(CopyRates(_Symbol,_Period,0,11,mrate) > 0){
   
         if(mrate[1].close>mrate[10].close){
         
            double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            double stoploss = ask - 3000 * _Point ;
            double takeprofit = ask + 130 * _Point ;
            if(trade.Buy(0.01 , _Symbol, ask, stoploss, takeprofit)){
               
                  int code = (int) trade . ResultRetcode();
                  ulong ticket = trade . ResultOrder();
                  Print(" Code: " + (string) code);
                  Print(" Ticket: " + (string) ticket);
                 
              }
        }
        else if(mrate[1].close<mrate[10].close){
       
         double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         double sellStoploss = bid + 3000 * _Point ;
         double sellTakeprofit = bid - 130 * _Point ;
   
         if(trade.Sell(0.01, _Symbol, bid, sellStoploss, sellTakeprofit)){
            int code = (int) trade . ResultRetcode();
            ulong ticket = trade . ResultOrder();
            Print(" Code: " + (string) code);
            Print(" Ticket: " + (string) ticket);
           }
        }
    }
  }  
}
 

It's easy to check: the EA makes a 'BUY'

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(!symbolIsAlreadyTrade(_Symbol, 100 * 60))
     {
      MqlRates mrate[];
      ArraySetAsSeries(mrate,true);
      if(CopyRates(_Symbol,_Period,0,11,mrate) > 0)
        {
         if(mrate[1].close > mrate[10].close)
           {
            double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            double stoploss = ask - 3000 * _Point ;
            double takeprofit = ask + 130 * _Point ;
            if(trade.Buy(0.01, _Symbol, ask, stoploss, takeprofit))
              {
               int code = (int) trade . ResultRetcode();
               ulong ticket = trade . ResultOrder();
               Print(" Code: " + (string) code);
               Print(" Ticket: " + (string) ticket);
              }
           }
         /*else
            if(mrate[1].close < mrate[10].close)
              {
               double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
               double sellStoploss = bid + 3000 * _Point ;
               double sellTakeprofit = bid - 130 * _Point ;
               if(trade.Sell(0.01, _Symbol, bid, sellStoploss, sellTakeprofit))
                 {
                  int code = (int) trade . ResultRetcode();
                  ulong ticket = trade . ResultOrder();
                  Print(" Code: " + (string) code);
                  Print(" Ticket: " + (string) ticket);
                 }
              }*/
        }
     }
  }

Result:

 
double stoploss = ask - 3000 * _Point ;
double takeprofit = ask + 130 * _Point ;
⋮
double sellStoploss = bid + 3000 * _Point ;
double sellTakeprofit = bid - 130 * _Point ;

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 
Guillaume Roeder :

Vous achetez au Ask et vendez au Bid . Les ordres d'achat stop en attente deviennent des ordres de marché lorsqu'ils sont touchés et ouverts à la demande .

  1. Le TP/SL de votre ordre d'achat (ou l'entrée du Sell Stop/Sell Limit) est demandé lorsque le Bid / OrderClosePrice l' atteint. L'utilisation de Ask ± n , rend votre SL plus court et votre TP plus long, par le spread. Ne voulez-vous pas que le montant spécifié soit utilisé dans les deux sens ?

  2. Le TP/SL de votre ordre de vente (ou l'entrée de Buy Stop/Buy Limit) seraClosePrice lorsque le Ask / Order l' atteint. Pour déclencher près d'un prix d' offre spécifique , ajouter le spread moyen. MODE_SPREAD (Paul) - Forum de programmation MQL4 - Page 3 #25
              

  3. Les graphiques déterminés uniquement les prix des offres . Activez la ligne Ask pour voir l'ampleur du spread ( Outils → Options (contrôle+O) → graphiques → Afficher la ligne ask . ) ± 30 min. Mon GBPJPY (OANDA) montre un spread moyen = 26 points, mais un spread maximum moyen = 134 (votre courtisan sera similaire).

Okay thank you I understand, do you know how to replace the "double takeprofit = ask + 130 * _Point" by selling when a profit of 5 euros is realized ? Concretement instead of closed when the order has price 130 pips it will close when a profit of 5 euros will be realized.

 
Vladimir Karputov:

It's easy to check: the EA makes a 'BUY'

Result:

Thank you !!!