Opening one position per currency pair in mt5 - page 2

 
Vladimir Karputov:

You need to use this construction (catch a new bar)

Hi, it is still opening multiple positions of the same currency pair once conditions are met. I have failed to catch the cause.

 
Gerald Masese:

Hi, it is still opening multiple positions of the same currency pair once conditions are met. I have failed to catch the cause.

   string array_symbols[4] = {"EURUSD","GBPUSD","USDCHF","NZDUSD"};
   int total = ArraySize(array_symbols);
   for(int i=0; i<total; i++)
     {
      for(int j=PositionsTotal()-1; j>=0; j--) // returns the number of current positions
        {
         ulong ticket=PositionGetTicket(j);
         if(ticket>0);
           {
            if(PositionGetString(POSITION_SYMBOL)==array_symbols[i])
              {
               Print("This ",array_symbols[i]," has a position already");
               continue;
              }
            else
              {
               //Code to open trade with the chart symbol
              }
            return;                //Why is the return here the loop will not get further than the 1st position where ticket>0 and not the symbol searched for?
           }
        }
     }

 You are checking if there are trades open for the symbols in an array which are not necessarily the chart symbol.

 You are not waiting for either loop to complete before checking for conditions to open a new trade with the chart symbol.

 
Keith Watford:

 You are checking if there are trades open for the symbols in an array which are not necessarily the chart symbol.

 You are not waiting for either loop to complete before checking for conditions to open a new trade with the chart symbol.

Checking out if the array is part of the chart symbols could be the solution. If you have time, please show me on that code how to add that part. I really appreciate all the help you guys have given me. 

 
Gerald Masese:

Checking out if the array is part of the chart symbols could be the solution. If you have time, please show me on that code how to add that part. I really appreciate all the help you guys have given me. 

Why do you even need the array? Why not just check for the chart symbol and most important, complete the check before placing new trades.

Am I missing something?

 
Keith Watford :

Why do you even need the array? Why not just check for the chart symbol and most important, complete the check before placing new trades.

Am I missing something?

He needs an array, since he wants to trade with one Expert Advisor for several symbols.

 

Here is a version that opens one position at a time:

//+------------------------------------------------------------------+
//|                                            Code_From_Scratch.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.005"
//---
#include <Trade\Trade.mqh>
//---
CTrade         m_trade;                      // object of CTrade class
//---
datetime m_prev_bars       = 0;              // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   datetime time_0=iTime(Symbol(),PERIOD_M1,0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;
//---
   string m_array_symbols[4]  = {"EURUSD","GBPUSD","USDCHF","NZDUSD"};
   int total = ArraySize(m_array_symbols);
   for(int i=0; i<total; i++)
     {
      double Ask=SymbolInfoDouble(m_array_symbols[i],SYMBOL_ASK);
      double Bid=SymbolInfoDouble(m_array_symbols[i],SYMBOL_BID);
      MqlRates rates[];
      ArraySetAsSeries(rates,true);
      int copied=CopyRates(m_array_symbols[i],PERIOD_H1,0,4,rates);
      //---
      if(rates[1].close > rates[1].open && rates[2].close > rates[2].open)
        {
         if(!PositionSelect(m_array_symbols[i]))
            m_trade.Buy(0.01,m_array_symbols[i]);
        }
      else
         if(rates[1].close < rates[1].open && rates[2].close < rates[2].open)
           {
            if(!PositionSelect(m_array_symbols[i]))
               m_trade.Sell(0.01,m_array_symbols[i]);
           }
     }
  }
//+------------------------------------------------------------------+
Files:
Expert_1.mq5  3 kb
 
Keith Watford:

Why do you even need the array? Why not just check for the chart symbol and most important, complete the check before placing new trades.

Am I missing something?

Vladimir Karputov:

He needs an array, since he wants to trade with one Expert Advisor for several symbols.

Well, I did miss that.

I just saw that he was checking through different symbols but then checking conditions and placing trades on the chart symbol.

So I thought that if he is only placing trades with the chart symbol, why the need to check whether other symbols have open positions?

It didn't make sense to me.

 
Vladimir Karputov:

Here is a version that opens one position at a time:

Thank you very much. It is running one position per currency in the array. This way, I will manage to grow my small account.

 
Vladimir Karputov:

Here is a version that opens one position at a time:

Hi all,

I really appreciate the help you have given me in learning MT5. I have the same problem of the code opening multiple positions of one currency pair. I have resorted to have separate EAs for each pair but still I have not really managed to solve the problem. Kindly have a look at the modification I did on the earlier code Vladimir did for me. I could love to have say only two active positions for EURUSD, two active positions for GBPUSD and so on. Thank you for your help. 


//---Calculations for new positions

      int opened_orders =0;
      int MaxTradesPerPair = 2;
      string symbol = "EURUSD"; // string symbol
      for(int j = 0; j<OrdersTotal() ; j++)
        {

         if(OrderSelect(OrderGetTicket(j)) && Symbol() == symbol)
           {
            opened_orders++;
           }
        }

      if(opened_orders < MaxTradesPerPair)
        {
         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
RoughWork.ex5  64 kb
 
Gerald Masese :

Hi all,

I really appreciate the help you have given me in learning MT5. I have the same problem of the code opening multiple positions of one currency pair. I have resorted to have separate EAs for each pair but still I have not really managed to solve the problem. Kindly have a look at the modification I did on the earlier code Vladimir  did for me. I could love to have say only two active positions for EURUSD, two active positions for GBPUSD and so on. Thank you for your help. 


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".