How can hold a position for n number of bars in an EA?

 
How can hold a trade for n number of bars in an EA also if the ea code find conditions to open another trade?
 
Dario Capasso:
How can hold a trade for n number of bars in an EA also if the ea code find conditions to open another trade?

You could identify the bar number, add a value to it and wait for that bar - some example code below

//+------------------------------------------------------------------+
//|                                            429450-BarCounter.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

long    barNo     = SeriesInfoInteger(_Symbol, PERIOD_CURRENT, SERIES_BARS_COUNT);
long    barNoExit = barNo+1;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   PrintFormat("barNo = %d barNoExit=%d", barNo, barNoExit);
   if(SeriesInfoInteger(_Symbol, PERIOD_CURRENT, SERIES_BARS_COUNT) == barNoExit)
     {
      ExpertRemove();      
     }
   Sleep(5000);
  }
//+------------------------------------------------------------------+
 
Dario Capasso: How can hold a trade for n number of bars in an EA also if the ea code find conditions to open another trade?

You are trying to learn MQL in an disorderly or undisciplined manner, making you question things in a disjointed way.

I suggest you first study EAs in the CodeBase and reference the documentation as you study them.

Make small changes to the CodeBase examples as you experiment and learn the languages and how the platform operates.

Do this over and over until you fully understand the underlying platform and how to use the language properly.

Giving you answers to these disjointed queries is not going to help you and will only make it more difficult for you going forward.

 
Fernando Carreiro #:

You are trying to learn MQL in an disorderly or undisciplined manner, making you question things in a disjointed way.

I suggest you first study EAs in the CodeBase and reference the documentation as you study them.

Make small changes to the CodeBase examples as you experiment and learn the languages and how the platform operates.

Do this over and over until you fully understand the underlying platform and how to use the language properly.

Giving you answers to these disjointed queries is not going to help you and will only make it more difficult for you going forward.

Yeah I'm doing it, already done some good progress imho. The only problem for me it that come from ProRealtime where the language is quite different but don't worry, i have been a programmer for a long time. As soon as i understand well the basic concepts and way mql works i will improve alone fast ;)

 
R4tna C #:

You could identify the bar number, add a value to it and wait for that bar - some example code below

Nice idea, i will try. Thanks!