Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one problem in my code, that is
Execute Max Open Orders in an MT5 EA


The lines of codes inside the .mq5 is not complete, thereby making it difficult
for Max Open Orders to work.
I need a function/codes that won't allow the EA if the Maximum number
of specified(to be input in the UI) orders are already opened

I was able to add that display on UI by myself but I could not figure out the right codes to execute it. it is not limiting maximum order.

brife description about it
--------------------------
MT5 EA is Trading software. code is working properly, but my problem is that, I have one textbox in which i enter max open order for example 10. if 10 orders are already Open, then need a function/codes that won't allow the EA if the Maximum number
of specified orders are already opened. means not more then 10. that is my problem. in my code i have try it by making funcion inpMaxOrder(), but its not work due to my problem.

What I have tried:

C++
int OnInit()
  {
   ExtMaxOrder = * m_adjusted_point;
   
//--- create handle of the indicator iMACD
   handle_iMACD=iMACD(m_symbol.Name(),MACD_period,MACD_fast_ema_period,MACD_slow_ema_period,MACD_signal_period,MACD_applied_price);
//--- if the handle is not created 
   if(handle_iMACD==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(MACD_period),
                  GetLastError());
      //--- the indicator is stopped early 
      return(INIT_FAILED);
     }
//--- create handle of the indicator iStochastic
   handle_iStochastic=iStochastic(m_symbol.Name(),Period(),Sto_Kperiod,Sto_Dperiod,Sto_slowing,Sto_ma_method,Sto_price_field);
//--- if the handle is not created 
   if(handle_iStochastic==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the iStochastic indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early 
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }

void PrintResult(CTrade &trade,CSymbolInfo &symbol)
  {
   Print("Code of request result: "+IntegerToString(trade.ResultRetcode()));
   Print("code of request result: "+trade.ResultRetcodeDescription());
   Print("deal ticket: "+IntegerToString(trade.ResultDeal()));
   Print("order ticket: "+IntegerToString(trade.ResultOrder()));
   Print("volume of deal or order: "+DoubleToString(trade.ResultVolume(),2));
   Print("price, confirmed by broker: "+DoubleToString(trade.ResultPrice(),symbol.Digits()));
   Print("current bid price: "+DoubleToString(trade.ResultBid(),symbol.Digits()));
   Print("current ask price: "+DoubleToString(trade.ResultAsk(),symbol.Digits()));
   Print("broker comment: "+trade.ResultComment());
  }

void Trailing()
  {
   if(InpTrailingStop==0)
      return;
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
              {
               if(m_position.PriceCurrent()-m_position.PriceOpen()>ExtTrailingStop+ExtTrailingStep)
                  if(m_position.StopLoss()<m_position.pricecurrent()-(exttrailingstop+exttrailingstep))
 {
="" if(!m_trade.positionmodify(m_position.ticket(),
="" m_symbol.normalizeprice(m_position.pricecurrent()-exttrailingstop),
="" m_position.takeprofit()))
="" print("modify="" ",m_position.ticket(),
="" "="" position="" -=""> false. Result Retcode: ",m_trade.ResultRetcode(),
                              ", description of result: ",m_trade.ResultRetcodeDescription());
                     continue;
                    }
              }
            else
              {
               if(m_position.PriceOpen()-m_position.PriceCurrent()>ExtTrailingStop+ExtTrailingStep)
                  if((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTrailingStop+ExtTrailingStep))) || 
                     (m_position.StopLoss()==0))
                    {
                     if(!m_trade.PositionModify(m_position.Ticket(),
                        m_symbol.NormalizePrice(m_position.PriceCurrent()+ExtTrailingStop),
                        m_position.TakeProfit()))
                        Print("Modify ",m_position.Ticket(),
                              " Position -> false. Result Retcode: ",m_trade.ResultRetcode(),
                              ", description of result: ",m_trade.ResultRetcodeDescription());
                    }
              }

           }
  }

// Max Open Orders                                                         |
bool inpMaxOrder() 
{
   if(OrdersTotal() >= InpMaxOrder)  
      return(true);                  
    return(false);
}
Posted
Updated 26-Mar-20 22:45pm
v3
Comments
Richard MacCutchan 27-Mar-20 4:21am    
You need to explain (as I suggested yesterday) exactly what the problem is, and where in the code it occurs. And it may help (or not) to explain what MT5 EA is.
Member 9720862 27-Mar-20 4:39am    
MT5 EA is Trading software. code is working properly, but my problem is that, I have one textbox in which i enter max open order for example 10. if 10 orders are already Open, then need a function/codes that won't allow the EA if the Maximum number
of specified orders are already opened. means not more then 10. that is my problem. in my code i have try it by making funcion inpMaxOrder(), but its not work due to my problem.

1 solution

C++
// Max Open Orders                                                         |
bool inpMaxOrder() 
{
   if(OrdersTotal() >= InpMaxOrder)  
      return(true);                  
    return(false);
}

Firstly, are you sure this should return true if OrdersTotal is greater than InpMaxOrder? I would expect the opposite.

And secondly, I cannot see anywhere in the preceding code that you actually call this function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900