Opening one position per currency pair in mt5 - page 3

 
Vladimir Karputov:

You make one and the same annoying mistake: you mix PENDIND ORDERS and POSITIONS into one heap.

Forget the word "order" and never use it again. Now your main word is "position". Use only the word "position" and, accordingly, functions that work with "positions".

Thank you for pointing that out. So, is it possible for an EA to open two positions for each currency pair in say five different currency pairs? if yes, please share with me how to do it.

 
Gerald Masese :

Thank you for pointing that out. So, is it possible for an EA to open two positions for each currency pair in say five different currency pairs? if yes, please share with me how to do it.

First, decide: how to choose these currency pairs? Hard to set in the input parameters? Or set four parameters - each will have its own symbol, or set all four in one parameter, or ...

 
Vladimir Karputov:

First, decide: how to choose these currency pairs? Hard to set in the input parameters? Or set four parameters - each will have its own symbol, or set all four in one parameter, or ...

I am okay with having individual EA for each pair. The hard part is calling the PositionsTotal()<2 for each EA without this affecting what the other EAs are doing.

 for(int i=PositionsTotal()-1; i>=0; i--) // Count all currency pair positions
     {
      string symbol=PositionGetSymbol(i);
      if(_Symbol==symbol)  //If chart symbol equals position symbol
        {
         

I have been searching how to proceed beyond this point but I cant find any snippet on the same. 

 
Gerald Masese :

I am okay with having individual EA for each pair. The hard part is calling the PositionsTotal()<2 for each EA without this affecting what the other EAs are doing.

I have been searching how to proceed beyond this point but I cant find any snippet on the same. 

I showed the solution - how to get the number of BUY positions and the number of SELL positions at once for all symbols in one function:

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2021.03.21 16:04

Calculate positions for all symbols

Code: 'Calculate positions for all symbols.mq5

Objective: Several positions are open in the market. You need to get the number of positions (both BUY and SELL) for each symbol.

Suggested solution: the 'STRUCT_CALCULATE_POSITIONS' structure

//+------------------------------------------------------------------+
//| Structure Calculate Positions                                    |
//+------------------------------------------------------------------+
struct STRUCT_CALCULATE_POSITIONS
  {
   string            symbol;                 // position symbol
   int               count_buys;             // count position BUY
   int               count_sells;            // count position SELL
   //--- Constructor
                     STRUCT_CALCULATE_POSITIONS()
     {
      symbol                     = "";
      count_buys                 = 0;
      count_sells                = 0;
     }
  };

is created and this structure is filled in the 'CalculateAllPositions'

//+------------------------------------------------------------------+
//| Calculate all positions Buy and Sell                             |
//+------------------------------------------------------------------+
void CalculateAllPositions(STRUCT_CALCULATE_POSITIONS &SCalculatePositions[])
  {
   ArrayFree(SCalculatePositions);
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
        {
         string pos_symbol=m_position.Symbol();
         int size=ArraySize(SCalculatePositions);
         int find=false;
         for(int j=0; j<size; j++)
           {
            if(SCalculatePositions[j].symbol==pos_symbol)
              {
               if(m_position.PositionType()==POSITION_TYPE_BUY)
                  SCalculatePositions[j].count_buys=SCalculatePositions[j].count_buys+1;
               if(m_position.PositionType()==POSITION_TYPE_SELL)
                  SCalculatePositions[j].count_sells=SCalculatePositions[j].count_sells+1;
               find=true;
               break;
              }
           }
         if(!find)
           {
            ArrayResize(SCalculatePositions,size+1);
            SCalculatePositions[size].symbol=pos_symbol;
            if(m_position.PositionType()==POSITION_TYPE_BUY)
               SCalculatePositions[size].count_buys=SCalculatePositions[size].count_buys+1;
            if(m_position.PositionType()==POSITION_TYPE_SELL)
               SCalculatePositions[size].count_sells=SCalculatePositions[size].count_sells+1;
           }
        }
//---
   return;
  }

function.


For example, a test advisor has been created: first, it opens four positions and then (after calling 'CalculateAllPositions') unpacks the 'STRUCT_CALCULATE_POSITIONS' structure'

Result:

       [symbol] [count_buys] [count_sells]
   [0] "EURUSD"            1             1
   [1] "EURPLN"            1             0
   [2] "USDJPY"            1             0

 
Vladimir Karputov:

I showed the solution - how to get the number of BUY positions and the number of SELL positions at once for all symbols in one function:


Thank you for guiding me. Let me work out how to incorporate this code to what I posted. 

 
Vladimir Karputov:

I showed the solution - how to get the number of BUY positions and the number of SELL positions at once for all symbols in one function:


I have tried working around this structure but I haven't succeeded. See the sample screenshot

Files:
OnePos.jpg  244 kb
 
Gerald Masese :

I have tried working around this structure but I haven't succeeded. See the sample screenshot

You inattentively looked at the example to which I gave the link (I think that you did not even follow the link). Follow the link and see the code of the Expert Advisor in the example.

 

Hi, 

I have tried following the examples as given but I am missing a step and therefore, am not getting it right. I really need this to open two positions per currency pair. The 

Suggested solution: the 'STRUCT_CALCULATE_POSITIONS' structure is a bit tough for me to master right now. I am learning MT5 but I have started with areas that are simpler to grasp and comprehend. 
      //---Calculations for new positions
      //--- check if the symbol within the current chart is present in the open positions list list
      bool exist=false;

      for(int j=PositionsTotal()-1; j>=0; j--) // Count all open currency pair positions
      {
      string symbol=PositionGetSymbol(j);
      if(_Symbol==symbol)  //If chart symbol equals position symbol
      {
      exist=true;
      break;
      }
      if(!exist)
         if(maTrendFast[0]>maTrendSlow[0])
           {
            if(rates[1].high>rates[3].high && rates[1].close > rates[1].open && rates[2].close > rates[2].open)
               //CheckTrailingStop(Ask);
              {
               if(!PositionSelect(m_array_symbols[i]))
                  m_trade.Buy(0.01,m_array_symbols[i],Ask,0,0);

              }
           }
         else
            if(maTrendFast[0]<maTrendSlow[0])
              {
               if(rates[1].low<rates[3].low && rates[1].close<rates[1].open && rates[2].close<rates[2].open)
                  //CheckTrailingStop1(Bid);
                 {
                  if(!PositionSelect(m_array_symbols[i]))
                     m_trade.Sell(0.01,m_array_symbols[i],Bid,0,0);
                     }
                 }
              }
Files:
RoughWork.mq5  17 kb
 
Gerald Masese:

Hi, 

I have tried following the examples as given but I am missing a step and therefore, am not getting it right. I really need this to open two positions per currency pair. The 

Suggested solution: the 'STRUCT_CALCULATE_POSITIONS' structure is a bit tough for me to master right now. I am learning MT5 but I have started with areas that are simpler to grasp and comprehend. 

I finally managed to have just one position using 

     {
      string symbol=PositionGetSymbol(i);
      if(_Symbol==symbol)  //If chart symbol equals position symbol
         return;

     }

Now let me engage in mastering  'STRUCT_CALCULATE_POSITIONS' to have two positions per pair.

 
Gerald Masese :

I finally managed to have just one position using 

Now let me engage in mastering  ' STRUCT_CALCULATE_POSITIONS ' to have two positions per pair.

An example for you: No more than N positions for each symbol Simple

How to start with MQL5
How to start with MQL5
  • 2021.03.12
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Reason: