Running EA on multiple charts

 

Hi guys, 

I'm a beginner (mt5) and trying to understand how I can apply an ea to multiple charts without them interferring. Every time a buy order is open on a chart it gets closed by the ea triggering a sell on another pair, same happens for SELL->BUY. 

Any idea on why this may happen? 

 
Joseph Catchpole: I'm a beginner (mt5) and trying to understand how I can apply an ea to multiple charts without them interferring. Every time a buy order is open on a chart it gets closed by the ea triggering a sell on another pair, same happens for SELL->BUY. Any idea on why this may happen? 

In your code, you must differentiate between the symbol being use and the Magic Number to differentiate them. Also read the following comments by William Roeder:

Forum on trading, automated trading systems and testing trading strategies

How to make trailing stoploss work on select trades

William Roeder, 2022.02.13 20:05

  1. Not a good idea to use comments, brokers can change comments, including complete replacement.

  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

    Use two MN.


 
Fernando Carreiro #:

In your code, you must differentiate between the symbol being use and the Magic Number to differentiate them. Also read the following comments by William Roeder:


Thank you so much for your answer. This is my code, any idea on how i can implement magic numbers?
 

Code:

***

 
Joseph Catchpole #:

Code:

***

1.  Please insert the code correctly: when editing a message, press the button    Codeand paste your code into the pop-up window 

OR

2. use button  Attach file

3. You are making a BIG MISTAKE: you are creating the 'iBands' indicator handle on each tick - remember that according to the MQL5 style, the indicator handle should be created ONCE and it is better to do it in OnInit!

4. Never ignore error messages and warnings:

 
#include <Trade/Trade.mqh>

int totBars;
input double lots = 1.0;
input double equityFactor = 1;
CTrade trade;

int OnInit(){
   totBars=iBars(_Symbol,_Period);

   return(INIT_SUCCEEDED);

}
  
void OnDeinit(const int reason){

   
}
  
  
void OnTick(){


   int bars = iBars(_Symbol,_Period);
   
   if(totBars < bars){

      totBars = bars;
   
      double MiddleBandArray[];
      double UpperBandArray[];
      double LowerBandArray[];
      
      ArraySetAsSeries(MiddleBandArray,true);
      ArraySetAsSeries(UpperBandArray,true);
      ArraySetAsSeries(LowerBandArray,true);
      
      int BollingerBandsDefinition = iBands(_Symbol,_Period,20,0,2,PRICE_CLOSE);
      
      
      CopyBuffer(BollingerBandsDefinition,0,1,2,MiddleBandArray);
      CopyBuffer(BollingerBandsDefinition,1,1,2,UpperBandArray);
      CopyBuffer(BollingerBandsDefinition,2,1,2,LowerBandArray);
      
      double close = iClose(_Symbol,_Period,1);
      double close2 = iClose(_Symbol,_Period,2);
      double distance = UpperBandArray[1]-LowerBandArray[1]; 
      
      for (int i= PositionsTotal()-1; i>=0; i--){
         ulong posTicket = PositionGetTicket(i);
         
         if(PositionSelectByTicket(posTicket)){
            double posOpen = PositionGetDouble(POSITION_PRICE_OPEN);
            double posSl = PositionGetDouble(POSITION_SL);
            double posTp = PositionGetDouble(POSITION_TP);
            
            ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
         
            if(posType == POSITION_TYPE_BUY){
               if(close < UpperBandArray[0] && close2 > UpperBandArray[1]){
                  
                  trade.PositionClose(posTicket);

               }
            }
         
         if(posType == POSITION_TYPE_SELL){
            if(close >LowerBandArray[0] && close2 < LowerBandArray[1] ){
               trade.PositionClose(posTicket);
               
            }
         }
      
         }
      }
      
      if(close < UpperBandArray[0] && close2 > UpperBandArray[1] ){
         
         
         double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         bid = NormalizeDouble(bid,_Digits);
         /*double sl = bid+distance;
         sl = NormalizeDouble(sl,_Digits);
         double tp = bid-distance;
         tp = NormalizeDouble(tp,_Digits);*/
         
         int nshort = 0;
         for (int i= PositionsTotal()-1; i>=0; i--){
            ulong posTicket = PositionGetTicket(i);
            
            if(PositionSelectByTicket(posTicket)){
               
               ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
            
               if(posType == POSITION_TYPE_SELL){
                  nshort ++;
               }
              }
          } 
          //lots = NormalizeDouble(lots,2);
          if(nshort == 0){
            trade.Sell((lots),_Symbol,bid);
            }else if(nshort == 1){
            trade.Sell(NormalizeDouble(lots*0.5,2),_Symbol,bid);
            }else if(nshort == 2){
            trade.Sell(NormalizeDouble(lots*0.2,2),_Symbol,bid);
            }else{
              trade.Sell(NormalizeDouble(lots*0.1,2),_Symbol,bid);
            }
          Print("Ashort size = ", lots);
         
         
         
      } else if(close >LowerBandArray[0] && close2 < LowerBandArray[1]){
         Print("Close below Lower BB");
         
         double ask = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         ask = NormalizeDouble(ask,_Digits);
         /*double sl = ask - distance;
         sl = NormalizeDouble(sl,_Digits);
         double tp = ask + distance;
         tp = NormalizeDouble(tp,_Digits);
         */
         int nlong = 0;
         for (int i= PositionsTotal()-1; i>=0; i--){
            ulong posTicket = PositionGetTicket(i);
            
            if(PositionSelectByTicket(posTicket)){
               
               ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
            
               if(posType == POSITION_TYPE_BUY){
                  nlong ++;
               }
              }
          }
          
          //lots = NormalizeDouble(lots,2);
          if(nlong == 0){
            trade.Buy(NormalizeDouble(lots,2),_Symbol,ask);
            }else if(nlong == 1){
            trade.Buy(NormalizeDouble(lots*0.5,2),_Symbol,ask);
            }else if(nlong == 2){
            trade.Buy(NormalizeDouble(lots*0.2,2),_Symbol,ask);
            }else{
              trade.Buy(NormalizeDouble(lots*0.1,2),_Symbol,ask);
            }
          Print("Active longs= ",nlong);
         
      }
      
      Comment("upper",UpperBandArray[0],"\n","Lower ",LowerBandArray[0],"\n","close",DoubleToString(close));
     } 
     
  }
 
Joseph Catchpole #:

Forum on trading, automated trading systems and testing trading strategies

Running EA on multiple charts

Vladimir Karputov, 2022.02.15 08:46

1. ***

2. ***

3. You are making a BIG MISTAKE: you are creating the 'iBands' indicator handle on each tick - remember that according to the MQL5 style, the indicator handle should be created ONCE and it is better to do it in OnInit!

4. Never ignore error messages and warnings:


 

thank you for the tip, anything on the magicnumbers?

 

Hi there,


Can I use one EA for differend charts?

The following charts I would like to add the EA, Trendline Break.


XAGEUR

XTIUSD

AUDUSD

USDCAD

USDJPY

XAUUSD

With XAGEUR, it works but when I attach the same EA to XTIUSD it does not work.


I tried to create for every chart a new EA, so that the Magic Number stays unique.

However, it only works for XAGEUR and not with XTIUSD and AUDUSD.


Can someone provide me some advice

Reason: