Allow only one pending position.

 

I want to have a code to allow only one position waiting for my code

Improperly formatted code removed by moderator.
 

When you post code please use the Code button (Alt+S) !

...

Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

...

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
//+------------------------------------------------------------------+
//| Search trading signals                                           |
//+------------------------------------------------------------------+
bool SearchTradingSignals(ENUM_ORDER_TYPE type = WRONG_VALUE)
  {
   //if(iTime(m_symbol.Name(),InpWorkingPeriod,0)==m_last_deal_in) // on one bar - only one deal
     // return(true);


   int size_need_position=ArraySize(SPosition);
   if(size_need_position>0)
      return(true);
   
   int size_need_pending=ArraySize(SPending); 
   
   int count_buys;
   int count_sells;
   CalculateAllPositions(count_buys,count_sells);
   
   if(InpMaxPositions!=0&&count_buys+count_sells>=InpMaxPositions)
      return(true);
      
      
//--- open a sell if there is no open position
   if(count_buys+count_sells==0)
     {
      ArrayResize(SPosition,size_need_position+1);
      SPosition[size_need_position].pos_type=POSITION_TYPE_SELL;
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal SELL");
      
      ArrayResize(SPending,size_need_pending+1);
      SPending[size_need_pending].pending_type=ORDER_TYPE_BUY_STOP;
      SPending[size_need_pending].volume=InpVolumeHedge;
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal BUY STOP");
      return(true);
     }
     
   if(type==ORDER_TYPE_BUY_STOP && !m_pause_buy)
    {
      ArrayResize(SPending,size_need_pending+1);
      SPending[size_need_pending].pending_type=ORDER_TYPE_BUY_STOP;
      SPending[size_need_pending].volume= GetMartinLot(InpVolumeHedge,count_buys+count_sells);
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal BUY STOP");
      return(true);
    
    }    
   //if(count_buys==1&&count_sells==1&&spike_open_price==0.0)
   //   spike_open_price= iOpen(m_symbol.Name(),Period(),0);
      
   if(type==ORDER_TYPE_SELL_STOP && !m_pause_sell)
  
    {
      ArrayResize(SPending,size_need_pending+1);
      SPending[size_need_pending].pending_type=ORDER_TYPE_SELL_STOP;
      SPending[size_need_pending].volume= GetMartinLot(InpVolumeHedge,count_sells+count_buys);
      SPending[size_need_pending].price= m_last_deal_price_b-(m_last_deal_price_b-m_last_deal_price_s)/InpPendingIndentRatio;
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal SELL STOP");
      return(true);
    
    }
   
//---
   return(true);
  }
 
Lamiki #:
Please, don't request help for ChatGPT (or other A.I.) generated code. It generates horrible code, mixing MQL4 and MQL5. Please use the Freelance section for such requests — https://www.mql5.com/en/job
 
Fernando Carreiro # : S'il vous plaît, ne demandez pas d'aide pour le code généré par ChatGPT (ou autre IA) . Il génère un code horrible, mélangeant MQL4 et MQL5. Veuillez utiliser la section Freelance pour de telles demandes — https://www.mql5.com/en/job

this is not the chatgpt code but it is part of the code of my robot created by a developer here on mql5 but currently it is no longer reachable on telephone

then I want the robot to add a single pending position, this is fine in the strategy tester but on a demo account it opens more than two pending positions.

 
//+------------------------------------------------------------------+
//| Search trading signals                                           |
//+------------------------------------------------------------------+
bool SearchTradingSignals(ENUM_ORDER_TYPE type = WRONG_VALUE)
  {
   //if(iTime(m_symbol.Name(),InpWorkingPeriod,0)==m_last_deal_in) // on one bar - only one deal
     // return(true);

   
   int size_need_position=ArraySize(SPosition);
   if(size_need_position>0)
      return(true);
   
   int size_need_pending=ArraySize(SPending); 
  
   
   
   int count_buys;
   int count_sells;
   CalculateAllPositions(count_buys,count_sells);
   
   if(InpMaxPositions!=0&&count_buys+count_sells>=InpMaxPositions)
      return(true);
      
      
//--- open a sell if there is no open position
   if(count_buys+count_sells==0)
     {
      ArrayResize(SPosition,size_need_position+1);
      SPosition[size_need_position].pos_type=POSITION_TYPE_SELL;
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal SELL");
      
      ArrayResize(SPending,size_need_pending+1);
      SPending[size_need_pending].pending_type=ORDER_TYPE_BUY_STOP;
      SPending[size_need_pending].volume=InpVolumeHedge;
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal BUY STOP");
      return(true);
     }
     
   if(type==ORDER_TYPE_BUY_STOP && !m_pause_buy)
  
    {
     if(size_need_pending<1)
   {
      ArrayResize(SPending,size_need_pending+1);
      SPending[size_need_pending].pending_type=ORDER_TYPE_BUY_STOP;
      SPending[size_need_pending].volume= GetMartinLot(InpVolumeHedge,count_buys+count_sells);
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal BUY STOP");
      return(true);
     
    }
     }    
   //if(count_buys==1&&count_sells==1&&spike_open_price==0.0)
   //   spike_open_price= iOpen(m_symbol.Name(),Period(),0);
      
   if(type==ORDER_TYPE_SELL_STOP && !m_pause_sell)
   
    {
    {
    if(size_need_pending<1)
   
    
      ArrayResize(SPending,size_need_pending+1);
      SPending[size_need_pending].pending_type=ORDER_TYPE_SELL_STOP;
      SPending[size_need_pending].volume= GetMartinLot(InpVolumeHedge,count_sells+count_buys);
      SPending[size_need_pending].price= m_last_deal_price_b-(m_last_deal_price_b-m_last_deal_price_s)/InpPendingIndentRatio;
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", OK: ","Signal SELL STOP");
      return(true);
    
   
    }
   }
//---
   return(true);
  }
 
Lamiki #: this is not the chatgpt code but it is part of the code of my robot created by a developer here on mql5 but currently it is no longer reachable on telephone then I want the robot to add a single pending position, this is fine in the strategy tester but on a demo account it opens more than two pending positions.

If you are not a coder then you will probably not be able to understand any advice given to you, nor are we going to code it for you.

You have only provided a small part of the code and it may not even be relevant within the context of the entire program.

So, I suggest you hire another coder to make the necessary changes for you.