Independent EAs on different chart symbols

To add comments, please log in or register
Nelson Wanyama
421
Nelson Wanyama  

Hello everyone. I'm sorry if this has been posted before. I just can't seem to find it anywhere. I placed same EA on three different charts but something is not working out as expected. I used the code attached to limit the number of trades per symbol but it seems that the EAs on the other charts cannot open other trades since PositionsTotal is not zero. Please, I need some advice or a different working code. 

 if (PositionsTotal()==0)
      {
      trade.Sell(0.01,NULL,Bid,(Ask+50*_Point),NULL, NULL);  //TP (Bid-150 * _Point)    SL (Ask+50*_Point)
      }
Vladimir Karputov
Moderator
180958
Vladimir Karputov  
Nelson Wanyama :

Hello everyone. I'm sorry if this has been posted before. I just can't seem to find it anywhere. I placed same EA on three different charts but something is not working out as expected. I used the code attached to limit the number of trades per symbol but it seems that the EAs on the other charts cannot open other trades since PositionsTotal is not zero. Please, I need some advice or a different working code. 

PositionsTotal - PositionsTotal Returns the number of open positions (the number of ALL open positions on this trading account).

You need to carry out a position calculation for EVERY sybmol.

More or less like this:

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.000"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input ulong    InpDeviation         = 10;          // Deviation, in points (1.00045-1.00055=10 points)
input bool     InpPrintLog          = false;       // Print log
input ulong    InpMagic             = 300;         // Magic number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
   RefreshRates();
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(IsPositionExists())
      return;
   if(!RefreshRates())
      return;
//---
   m_trade.Sell(m_symbol.LotsMin(),
                m_symbol.Name(),
                m_symbol.Bid(),
                m_symbol.NormalizePrice(m_symbol.Bid()+50*m_symbol.Point()),
                m_symbol.NormalizePrice(m_symbol.Bid()-150*m_symbol.Point()));
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Is position exists                                               |
//+------------------------------------------------------------------+
bool IsPositionExists(void)
  {
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
            return(true);
//---
   return(false);
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
Trade Functions / PositionsTotal - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Test_EA.mq5 8 kb
Nelson Wanyama
421
Nelson Wanyama  
Vladimir Karputov:

PositionsTotal - PositionsTotal Returns the number of open positions (the number of ALL open positions on this trading account).

You need to carry out a position calculation for EVERY sybmol.

More or less like this:

Thanks.  Exactly what I need
William Roeder
20683
William Roeder  
Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number 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
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
Nelson Wanyama
421
Nelson Wanyama  
William Roeder:
Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number 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
          MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
Thank you for the response 
To add comments, please log in or register