Hi , i am wrestling with me ea to open a new position after it sets the initial one at be

 

As you can see in my code i tried to  use isPosOpen to make my ea stop opening more than 1 trade when the conditions are met, but i failed when i tried it to use it to stop my bot to open more than 1 trade when the prev trade is set to be. My plan is to keep opening trade when conditions are met if the previous trade is at be. Is there a way i can make ma ea to forget the trade as soon as it's set to be? I will post my full code here , as a description is a simple bullish engulfing EA. Thanks!


#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"


input double Lots= 0.1;
input ulong Magic = 1337;
input double TPoints = 100;
input double SlPoints= 100;
input double beTrigger = 400;
#include <Trade/Trade.mqh>



CTrade trade;

bool trade1;

int OnInit(){
   trade.SetExpertMagicNumber(Magic);   
   
   return(INIT_SUCCEEDED);
}



void OnDeinit(const int reason){
   
}
void OnTick(){

   double open1 = iOpen(_Symbol,PERIOD_CURRENT,1);
   double close1 = iClose(_Symbol,PERIOD_CURRENT,1);
   double open2 = iOpen(_Symbol,PERIOD_CURRENT,2);
   double close2 = iClose(_Symbol,PERIOD_CURRENT,2);
   double high2 = iHigh(_Symbol,PERIOD_CURRENT,2);
   double low2 = iLow(_Symbol,PERIOD_CURRENT,2);
   double bid  =SymbolInfoDouble(_Symbol,SYMBOL_BID);
   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
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;}                  
                         
       } 
     }   
   }
   if(open1 < close1 && high2 < close1 && !isPosOpen){
      double tp = ask + (TPoints *_Point);
      tp = NormalizeDouble(tp,_Digits);
    
      double sl = low2  - (SlPoints*+_Point); 
      sl = NormalizeDouble(sl,_Digits);
      if(trade.Buy(Lots,_Symbol,ask,sl,tp)){ 
      isPosOpen = true;
      }
   }  
   
}
 

You should look at the journal to see why it's not working (see what it's saying). When a position is open, you can't make a new position with the same lot size if you don't have enough free margin left. So that is already a potential problem. You'll need an automatic lot size calculation function. Someone wrote before about how to make this new trade on breakeven work but for MT4

https://www.mql5.com/en/code/35609

I didn't look at it, it might give you ideas even though you're working with MQL5

More Trade After Break Even
More Trade After Break Even
  • www.mql5.com
Have you ever wanted to add another trade only after the previous has been modified by either trailing stop or even break even , This code will help you to be able to implement that
 
Conor Mcnamara #:

You should look at the journal to see why it's not working (see what it's saying). When a position is open, you can't make a new position with the same lot size if you don't have enough free margin left. So that is already a potential problem. You'll need an automatic lot size calculation function. Someone wrote before about how to make this new trade on breakeven work but for MT4

https://www.mql5.com/en/code/35609

I didn't look at it, it might give you ideas even though you're working with MQL5

well , the issue is at it opens too many trades , not that it won't open any. i will check the link provided, thanks
 
metinique #:
well , the issue is at it opens too many trades , not that it won't open any. i will check the link provided, thanks

I think iTime will help you limit trades. Just know that the time variables below (on the global scope) will reset to 0 upon EA restart. The preferred way to limit trades is to call data from the appropriate trade pools, but this requires object-oriented programming which you're not using.

datetime dprevtime1;
ulong prevtime1 = ulong(dprevtime);
datetime dtime1 = iTime(_Symbol, PERIOD_CURRENT, 1);
ulong time1 = ulong(dtime1);

//...

if(your_trading_conditions && time1 > prevtime1)
 {
  your_order_send;
  dprevtime1 = dtime1;
 }

As a "hack" of sorts, you could alternatively create special terminal-wide GlobalVariables (GV's) that would persist through EA restart.

iTime - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Documentation on MQL5: Timeseries and Indicators Access / iTime
Documentation on MQL5: Timeseries and Indicators Access / iTime
  • www.mql5.com
Returns the opening time of the bar (indicated by the 'shift' parameter) on the corresponding chart. Parameters symbol [in]  The symbol name...
 
metinique #:
well , the issue is at it opens too many trades , not that it won't open any. i will check the link provided, thanks

solution:

int trade_limit = 2;

if(PositionsTotal() < trade_limit){

  //open the new trade
}
when one position is opened, PositionsTotal() = 0 (just so you know)
 
Conor Mcnamara #:

solution:

when one position is opened, PositionsTotal() = 0 (just so you know)

I posted similar code in the OP's other thread regarding same, but the OP is looking to perpetually stack/pyramid trades.

 
Ryan L Johnson #:

I posted similar code in the OP's other thread regarding same, but the OP is looking to perpetually stack/pyramid trades.

but when you do the condition with bar open time, you're not allowing the new trade to execute within the time of the same bar. This might not be efficient on higher timeframes, but could work on lower timeframes

PositionsTotal is a great function which I use a lot. It only concerns itself with opened positions. He can perpetually stack trades based on a limit, it constantly recomputes how many positions are open every tick.
 
Conor Mcnamara #:

but when you do the condition with bar open time, you're not allowing the new trade to execute within the time of the same bar. This might not be efficient on higher timeframes, but could work on lower timeframes

PositionsTotal is a great function which I use a lot. It only concerns itself with opened positions. He can perpetually stack trades based on a limit, it constantly recomputes how many positions are open every tick.

If you want the current bar open time, then use:

datetime dtime1 = iTime(_Symbol, PERIOD_CURRENT, 0);

The open price does not change intrabar regarless of timeframe.

My understanding is that the OP does not want rapid-fire trades sent upon the arrival of every tick within the same bar.

 
Ryan L Johnson #:

If you want the current bar open time, then use:

The open price does not change intrabar regarless of timeframe.

My understanding is that the OP does not want rapid-fire trades sent upon the arrival of every tick within the same bar.

well he has this "beTrigger" variable, my feeling was that he wants a new trade as soon as possible once breakeven is triggered because of how volatile the markets can be and how easily opportunities are lost. Your solution is fine for the use case though, I already do what you are doing in my own EA

 
Conor Mcnamara #:

well he has this "beTrigger" variable, my feeling was that he wants a new trade as soon as possible once breakeven is triggered because of how volatile the markets can be and how easily opportunities are lost. Your solution is fine for the use case though, I already do what you are doing in my own EA

I actually do what you're doing because I don't stack trades.😂

 
Thanks all for the answers , i will study all of them . And yes ,i am looking stack positions ,but controlled stacking. only after the 1st pos is at be and entry only after the coniditions are met again, i can't enter a trade on the same candle that places the trade at be , bcs as you can see in the code , i have a condition that sets the sl to be after the be trigger is hit and after a bearish candle (agains the trade) is printed.