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

 
Mikhail Mishanin:

That's funny) it shouldn't be)

If the battle is over - trade with a normal spread, just take out its 'limit' in the input and just ignore the trade if the spread is above the limit.

Spreads can be stretched by dillings without rollover.

I have quite a few spread swings, from 10 units to 45

I have to wait to screen 45.

At the same time the function counts an average of 17, which is quite satisfying.

It goes up to 100-140 for 1 hour after rollover, I will run the code and see how it behaves.

 
Vitaly Muzichenko:

You could, however, prescribe it this way:

It should work properly.

I don't like this logic.

The first input to the function:

res = 0;

Therefore the condition

if(dt.hour==22 && res<_sp)

is not satisfied.

And I don't want to get into details further. What is this condition for all of a sudden?

   if(tc>LONG_MAX-1)
I don't understand the logic of it all.
 
Alexey Viktorov:

I don't like this logic.

The first input to the function:

res = 0;

Consequently, the condition

if(dt.hour==22 && res<_sp)

is not fulfilled.

And I don't want to get into it any further. What is this condition for all of a sudden?

   if(tc>LONG_MAX-1)
I don't understand the logic of it all.

1. This condition is only for 1 hour after the rollover, when the time is 22 hours. But this is only at the first start, while the terminal always works with the Expert Advisor, which means that it is empty only once.

Perhaps it should also be declared as static in case of change of timeframe

2. if(tc>LONG_MAX-1) = we zeroize the variable tc if we have exceeded the allowable limits of long. This is unlikely to happen, since the terminal sometimes overloads, for example at the weekend.

---

Changed code, this must be final:

void OnTick()
{
 int sp = SymbolInfoInteger(Symbol(),SYMBOL_SPREAD);
   if(CheckSpr(sp)) {
      // Здесь код отправки
   }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
MqlDateTime dt;
bool CheckSpr(int _sp)
{
   static int ts=0;
   static ulong tc=0;
   static int res=0;
   TimeGMT(dt);
   if(res<_sp && (dt.hour==22||dt.hour==23)) { // Если 2 часа после Rollover спред упадёт до нормального - торгуем
      Comment("Rollover: Спред: ",_sp,", Средний: ",res,", Тиков: ",tc);
      return(false);
   }
   tc++;
   ts += _sp;
   res =ts/tc;
   if(tc<500) {
      Comment("Тиков менее 500: ",tc,", Спред: ",_sp,", Средний: ",res);
      return(false);
   }
   if(res>_sp) {
      Comment("Торгуем: Спред: ",_sp,", Средний: ",res,", Тиков: ",tc);
      return(true);
   }
   Comment("Не торгуем - спред завышен: Спред: ",_sp,", Средний: ",res,", Тиков: ",tc);
   return(false);
}

P.S. Put it to the test

P.S. Revised the code

----

P.S.S.S. I added 1 hour more time after the rollover, otherwise I have very high spread on some dealers.

The final variant has worked 20 hours and the result is amazing

Average daily spread is 10-45 pps, but most of the time it is 10-17 pps, the average spread has been calculated as 19

Thank you all for your participation, if you have anything to add - write!

 
Vitaly Muzichenko:

Maybe it should also be declared static, in case of timeframe change

I've understood it, it seems to work, I take back "it shouldn't"), now I really just need to figure out the zeroing/assignment moments.

 

I've seen a discussion on the forum, but can't find it.

Need to be limited to one position on a bar that can be opened at any minute, this is the option now, but it's "heavy" in my opinion

if(Bars(Symbol(), PERIOD_CURRENT, Buy.LastOpenTime, TimeCurrent())==0) return; // Открывать не более 1 на баре

How can this be replaced to make it lighter?

 
Vitaly Muzichenko:

I've seen a discussion on the forum, but can't find it.

Need to be limited to one position on a bar that can be opened at any minute, this is an option now, but it's "heavy" in my opinion

How can this be replaced to make it lighter?

"You need to limit yourself to one position on the bar, which can be open at any minute..."

too general wording, do you mean that once on the bar in this symbol you can send OrderSend?

 
Mikhail Mishanin:

"You have to limit yourself to one position on the bar, which can be opened at any minute..."

the wording is too general, do you mean that once per bar in this symbol you can send OrderSend?

Yes. The Expert Advisor is working on the H1 timeframe and can open a position at 10:17, it needs not to open another one before 11:00, i.e. on the current bar.

 
Vitaly Muzichenko:

Yes. The EA is working on H1 time frame and can open a position at 10:17, you need to make sure that before 11:00, i.e. on the current bar - it does not open any more.

Yep, again, I think the devil is in the details.

open a position - set a "ban flag" and/or save the "time" (doesn't matter which one, as long as it is the same (source) for comparison)

a new bar is opened - the "ban flag" is set...

Question: What if the position has somehow changed?

Example of how I define a new bar - doTB divisor in seconds, for H1 it is 3600.0

BARii[].time - time of bars with M1, but it can be arbitrary and with any divide, if it is more or equal to 1 it means the "time" from different "bars

if(MathFloor(BARii[0].time/doTB)-MathFloor(BARii[1].time/doTB)>=1.0)//если бар закрылся
Совершение сделок - Торговые операции - Справка по MetaTrader 5
Совершение сделок - Торговые операции - Справка по MetaTrader 5
  • www.metatrader5.com
Торговая деятельность в платформе связана с формированием и отсылкой рыночных и отложенных ордеров для исполнения брокером, а также с управлением...
 
Mikhail Mishanin:

Yep, again incomplete conditions in my opinion, "the devil is in the details", in simple terms if the text

opened a position - set a "ban flag" and/or save the "time" (doesn't matter which one, as long as it's the same (source) for comparison)

a new bar is opened - the "ban flag" is set...

Question: What if the position has somehow changed?

Example of how I define a new bar - doTB divisor in seconds, for H1 it is 3600.0

BARii[].time - time of bars with M1, but it can be arbitrary and with any divide, if it is more or equal to 1, it means the "time" from different "bars

I was not satisfied with the option using flags at that moment; I do not remember why, but I replaced it with

if(Bars(Symbol(), PERIOD_CURRENT, Buy.LastOpenTime, TimeCurrent())==0) return; // Открывать не более 1 на баре

OK, I'll think about another implementation

 
Vitaly Muzichenko:

I wasn't happy with the flags option at the time, I don't remember why, but I replaced it with

Okay, I'll think about another implementation.

And what does this implementation hold back? Similar to my code determines two times in one "bar" or in different. Just in my variant "true" when in different "bars", in your variant "true" when in one bar, just bypass comparison == with "0" especially integer, wo, and I probably can change condition to >0.0 or >0.9, need to think...