Why my expert advisor does not close trades?

 

hi, i recently made a simple expert advisor, all i just want is to open, for example, a sell trade when a sell signal appears in the chart, that trade would be open until a buy signal appears, when the buy signal appears it would close the sell signal and immediately open a buy trade so on and so forth. The problem is that the ea actually make the first trade but it does nothing when the other signal appears, it does not close the trade neither open the other one. Also, i have this ea running on xauusd m15, xauusd 1h, eurusd 15m, and eurusd 1h charts, but they not run all at once, it run only on xauusd 15m chart but when i turned down the ea it instantly begin to run on xauusd 1h chart son on and so forth, i need to solve that problem too.

Thanks in advance

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

#include <Trade/Trade.mqh>

int handle;
CTrade trade;
string Tradetype;
ulong ticket;

int OnInit()
  {
   handle = iCustom(_Symbol, PERIOD_CURRENT, "Market//Boom and crash smasher", 8, 1);
  
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }
void OnTick(){
   
   double arrSellSignals[];
   CopyBuffer(handle, 0, 0, 1, arrSellSignals);
   
   double arrBuySignals[];
   CopyBuffer(handle, 1, 0, 1, arrBuySignals);
   
   if(arrSellSignals[0] > 0){
      if(PositionsTotal() == 0){
         Print("Sell Signal");
         trade.Sell(0.04);
         Tradetype = "sell";
         ticket = trade.ResultDeal();
      }else{
         if(Tradetype == "buy"){
            trade.PositionClose(ticket);
            trade.Sell(0.04);
            Tradetype = "sell";
            ticket = trade.ResultDeal();
         }
      }
   }
      
   if(arrBuySignals[0] > 0){
      if(PositionsTotal() == 0){
         Print("Buy Signal");
         trade.Buy(0.04);
         Tradetype = "buy";
         ticket = trade.ResultDeal();
      }else{
         if(Tradetype == "sell"){
            trade.PositionClose(ticket);
            trade.Buy(0.04);
            Tradetype = "buy";
            ticket = trade.ResultDeal();
         }
      }
   }
}
   
 
Your topic has been moved to the section: Expert Advisors and Automated Trading — Please consider which section is most appropriate — https://www.mql5.com/en/forum/443428#comment_49114884
 
Thejudg 47:
   double arrSellSignals[];    CopyBuffer(handle, 0, 0, 1, arrSellSignals);       double arrBuySignals[];    CopyBuffer(handle, 1, 0, 1, arrBuySignals);

use ArraySetAsSeries:

   double arrSellSignals[];
   ArraySetAsSeries(arrSellSignals, true);
   CopyBuffer(handle, 0, 0, 1, arrSellSignals);
   
   double arrBuySignals[];
   ArraySetAsSeries(arrBuySignals, true);
   CopyBuffer(handle, 1, 0, 1, arrBuySignals);

In order to use the EA on several charts simultaneously you have to use magic numbers.