How can i make my bot open a new trade after setting the initial one at be

 

Hi , new to mql5,i conditioned my bot to open only one trade with a loop , but now i want it to open a new trade after the initial one is set to be . so the bot set's the sl to be but then opens inifite trade when the conditions are met again. 

i will post the code here. any way that i can repeat the loop? The code to open the trade comes right after this loop function, everything works fine until the bot tries to open the second trade, i know the part where i set isPosOpen = false; is not good. 

bool isPosOpen = false;   
   for(int i=PositionsTotal()-1;i >= 0;i--){
      ulong posTicket = PositionGetTicket(i);    
      double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
      ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
      double posSl = PositionGetDouble(POSITION_SL);
      double posTP = PositionGetDouble(POSITION_TP);    
      string posSymbol = PositionGetString(POSITION_SYMBOL);
      if(posSymbol !=_Symbol) continue;      
      ulong posMagic = PositionGetInteger(POSITION_MAGIC);
      if(posMagic != Magic) continue;   
      isPosOpen = true;
     if(posType == POSITION_TYPE_BUY)
        {if(bid> posOpenPrice +(beTrigger*_Point))
           {if(open1 >close1)
              {trade.PositionModify(posTicket,posOpenPrice,posTP);  
              }if(posSl >= posOpenPrice)
                 { isPosOpen = false;
                  for(int i=PositionsTotal()-1;i >= 0;i--)
                    {if(posSymbol !=_Symbol) continue;
                     if(posMagic != Magic) continue;
                     isPosOpen = true;
                     
                    }
  
                 }
        } 
     
     }   
   }
 

Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.12.18
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
metinique:

Hi , new to mql5,i conditioned my bot to open only one trade with a loop , but now i want it to open a new trade after the initial one is set to be . so the bot set's the sl to be but then opens inifite trade when the conditions are met again. 

i will post the code here. any way that i can repeat the loop? The code to open the trade comes right after this loop function, everything works fine until the bot tries to open the second trade, i know the part where i set isPosOpen = false; is not good. 

If you're only sending 2 orders within your code, just get rid of the second loop and isPosOpen, and then condition your second order send upon:

if(PositionsTotal() == 1)
 
Ryan L Johnson #:

If you're only sending 2 orders within your code, just get rid of the second loop and isPosOpen, and then condition your second order send upon:


well , thanks for your answer but i need more than 2 trades, i need the bot to keep opening trades when the conditions are met , only after the trade before is at be

 
metinique #:

well , thanks for your answer but i need more than 2 trades, i need the bot to keep opening trades when the conditions are met , only after the trade before is at be

You need to create a frequency and how often to check the signal conditions. Even if your code worked as you suggest, it would open a new trade on every tick that the signal condition was true.

I recommend that you download several ea codes from codebase and learn how those limit new trades to open at new candles, and/or whenever.