Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1534

 
Eugen8519:

This is how the order closes

Do as I wrote above...

Or post the OnTick() code

 
Eugen8519:

This is how the order closes

This structure should be

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(UseTimeLimit)
     {
       YesStop=true;
       MqlDateTime str1;
       TimeToStruct(TimeCurrent() , str1);
       if(str1.hour > startHour && str1.hour < stopHour)
          YesStop=false;
     }
   if(YesStop==false)
     {
      if(EMA0...)
        {
         .....
         OPENORDER("Sell");
        }
   
      if(EMA0...)
        {
         .....
         OPENORDER("Buy");
        }
     }
//---
   if(EMA0...)
     {
      .....
      CLOSEORDER("Sell");
     }

   if(EMA0...) 
     {
      .....
      CLOSEORDER("Buy");
     }
  }
//+------------------------------------------------------------------+
void CLOSEORDER(string ord)
  {
   .....
  }
//---
void OPENORDER(string ord)
  {
   .....
  }
 
Thanks for the tip, I'll check it out tonight and report back later.
 
MakarFX:

This structure should be

If I am not mistaken, it can be written as follows

   if(UseTimeLimit)
     {
       YesStop=true;
       MqlDateTime str1;
       TimeCurrent(str1);
       if(str1.hour > startHour && str1.hour < stopHour)
          YesStop=false;
     }
 
Eugen8519:
Well thanks for the advice, I'll check it out tonight and report back later.

if UseTimeLimit is set in the settings when starting the owl, then

this function is better in OnInit() instead of OnTick()

 
Vitaly Muzichenko:

If I'm not mistaken, you can write it like this

I actually write it like this.

if(TimeHour(time[i])>=Time_Start&&TimeHour(time[i])<TimeFinish)

or even simpler

if(Hour()>=Time_Start&&Hour()<TimeFinish)
 

Good afternoon, could you please tell me how to correctly average the indicator line? The basic curve with period Per_1 is drawn normally (Buffer_1[]), until I add a block of simple averaging with Buffer_2[] with averaging period Per_2.

int OnCalculate(default parameters)

{

int i, j, limit;

if(rates_total <= Per_1)

return(0);

//The last calculated bar will be recalculated

limit = rates_total - prev_calculated - Per_1;

if(prev_calculated > 0)

limit++;

//calculate and fill in indicator buffers

for(i=0; i < limit; i++)

{

//Calculate the main line of the indicator

Buffer_1[i] = Formula of the curve

double Sum = 0;

for(j = i; j < Per_2 + i; j++)

{

//Calculate line of averaging

Sum += Buffer_1[j];

Buffer_2[i] = Sum / Per_2;

}

}

return(rates_total);

}

 
Oleksandr Nozemtsev:

Good afternoon!

Insert code

Here seems to be the answer to your question
 
Eugen8519:
That's the problem, it only opens and closes starthour->stophour orders
And open orders are not closed after stophour

Because you cannot trade after stophour

I've been writing the logic of opening before closing, trawling and other position maintenance for a long time. For the most part it is true, before you open something else, you have to check what is already open.

Your logic is correct, you just need to swap the blocks:

MqlDateTime str;
void OnTick()
 {
 // Close Positions
 TrailingStop();

 if(условие_закрытия)
  {
   ClosePos();
  }

 // Open Positions
 if(UseTimeLimit)
  {
    TimeCurrent(str);
    if(str.hour > startHour && str.hour < stopHour)
     {
      OpesPos(...);
      ...
     }
  }
// end
}
 

Cool! It works, thank you!

Reason: