Opening only 1 trade per currency pair at a time

To add comments, please log in or register
marlandhill
9
marlandhill  

Hi,

I'm trying to write a code that can trade 4 currency pairs at a time, but I only want a maximum of 1 position per currency pair open at any given moment. 

However, I do not know which Function I should be using for such an instruction.

I am currently using PositionsTotal()<4 to prevent more than 4 open trades from happening at any time.

Is there any function or method that can instruct the EA to have only a maximum of 1 unique currency pair open at any given time?


This is my current code for criteria to open and execute a Buy trade.

Any advice will be greatly appreciated! Thank you! :)

   if((PriceInfoEURUSD[0].close>MovingAverageArrayEURUSD[0])
   &&(PriceInfoEURUSD[0].close==PriceInfoEURUSD[HighestCandleEURUSD].high))
   {
   signalEURUSD="buyEURUSD";
   }
                
   //Buy EURUSD
   if (signalEURUSD =="buyEURUSD" && PositionsTotal()<4)
   trade.Buy(0.1,NULL,AskEURUSD,AskEURUSD -250 * _Point,AskEURUSD +250 * _Point),0,NULL);       //Lot size: 0.1; SL: 25pips; TP: 25pips
      
   Comment("Trade is "+signalEURUSD); 
William Roeder
19818
William Roeder  
&(PriceInfoEURUSD[0].close==PriceInfoEURUSD[HighestCandleEURUSD].high))
Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum
gammag
635
gammag  
William Roeder:
Doubles are rarely equal. Understand the links in:
          The == operand. - MQL4 programming forum

I agree with you.

is it actually "safe" to use CExpertTrade class then?

//+------------------------------------------------------------------+
//| Easy LONG trade operation                                        |
//+------------------------------------------------------------------+
bool CExpertTrade::Buy(double volume,double price,double sl,double tp,const string comment="")
  {
   double ask,stops_level;
//--- checking
   if(m_symbol==NULL)
      return(false);
   string symbol=m_symbol.Name();
   if(symbol=="")
      return(false);
//---
   ask=m_symbol.Ask();
   stops_level=m_symbol.StopsLevel()*m_symbol.Point();
   if(price!=0.0)
     {
      if(price>ask+stops_level)
        {
         //--- send "BUY_STOP" order
         return(OrderOpen(symbol,ORDER_TYPE_BUY_STOP,volume,0.0,price,sl,tp,
                m_order_type_time,m_order_expiration,comment));
        }
      if(price<ask-stops_level)
        {
         //--- send "BUY_LIMIT" order
         return(OrderOpen(symbol,ORDER_TYPE_BUY_LIMIT,volume,0.0,price,sl,tp,
                m_order_type_time,m_order_expiration,comment));
        }
     }
//---
   return(PositionOpen(symbol,ORDER_TYPE_BUY,volume,ask,sl,tp,comment));
  }

when stops_level is 0.0 (and it is very often, at least in my implementation), only if two double numbers (price and bid) are equal, sell order at market price is placed, otherwise, sell stop or sell limit order are opened.
it seems very unreliable to me, exactly due to comparison of two double numbers.

any experience (positive or negative) with that?

gammag
635
gammag  
marlandhill:

Hi,

I'm trying to write a code that can trade 4 currency pairs at a time, but I only want a maximum of 1 position per currency pair open at any given moment. 

However, I do not know which Function I should be using for such an instruction.

I am currently using PositionsTotal()<4 to prevent more than 4 open trades from happening at any time.

Is there any function or method that can instruct the EA to have only a maximum of 1 unique currency pair open at any given time?


This is my current code for criteria to open and execute a Buy trade.

Any advice will be greatly appreciated! Thank you! :)

i included parts of this library for exactly this reason, it provides you classes to access positions grouped by symbol, magic number and other criteria.
Check CPosition class

https://www.mql5.com/en/code/25104

MQL_Easy
MQL_Easy
  • www.mql5.com
MQL_Easy is an open source cross platform library for developing MQL4 and MQL5 applications. The purpose of this library is to make the mql development easy, safe and fast in order to focus more on implementing complex trading ideas. The cross platform property assure that the same piece of code works on both platforms. In addition, it has...
To add comments, please log in or register