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

 
Artyom Trishkin #:

The first call to CopyRates() initiated historical data swap (don't run for a week and you'll get the same again, well, maybe not a week, but some time).
You just need to check how much data there is on the server and compare the available amount to the Expert Advisor. If it is less than it is required - leave the EA until the next tick. At the next tick, check again. If the required amount of history is not obtained in a certain amount of attempts - then this error should be processed as required.

Thank you for the information!

If you have time, please advise how to correct it:

Two opposite targets are set on the history N candles ago: Close[N]+50*Point and Close[N]-50*Point.
Then there is a condition that the price should reach one of the targets (if (High[i]> Close[N]+50*Point) or (if (Low[i]<Close[N]-50*Point)
When the condition is met, the distance from Close[N] to High[i] is written into x[high]=High[i] array.
Then it is randomly thrown to any of the graphs at any time.

And when checking via Print(x[high]), 1-2 out of ten values show less than 50! One has 12, the other 49. Although, it is strictly stated+50*Point. 8-9 are correct (over 50) and 1-2 are abnormal. It is not through the tester, but with a real chart I place an Expert Advisor (without any trading functions); it works with the history and displays incorrect results.

The larger the value is, the fewer such errors occur. I thought it might be the spread that interferes, but... MT4 doesn't seem to have spread on history

 
Artis98 #:

Hello Dear forum users and experienced programmers! I need help in completing the EA)). Please advise how to add 2 things to the code advisor:

1. Here is the situation: I open an order (1st) with TP and SL and a certain lot (probably 0.01). If this order closes at take profit - cool, nothing changes. But if this (1st) order is closed at SL, the next order immediately after that (2nd) is opened with a 2 times larger lot (0.01*2=0.02). If this 2nd order is closed at Take Profit, then the next order (3rd order) is opened with a regular lot, i.e., 2 times larger lot is needed only for the next order (2nd order) after the previous (1st) was closed at SL. If the 3rd order will be closed in SL for the second time in a row, the lot will remain increased by 2x until there will be no TP close. I hope I have described in detail the logic of what needs to be added;


2. and the second thing we have to add is the SL movement. now let me explain it with an example: let's assume we have an order with basic parameters T.p. + 300, S.l. - 500. The price moves in the required direction and passes +100 pips from the opening price of the order. Once the price reaches +100 from the opening price of the order, SL should move from the base -500 to +90. I.e. when the price reaches +100 pips from the opening price of the order, we transfer the order to Breakeven by setting S,L to +90 pips. After that, if the price moves further in the desired direction and passes by another + 50 pips - S.L. is moved by 50 pips => it should now be at + 140 pips from the opening price of the order. I.e. the S.L. first moves from its base position to + 90 pips from the opening price of the order and then moves if it passes by + 50 pips ( - 500 => + 90 => + 140 => + 190 etc.). And so it goes either until the price reaches T.P. or when the trend reverses and hits the S.L.


I hope I described everything in details, and I really hope for your help, because by adding these missing elements, the Expert Advisor will be ready)) If you have any questions, please ask))) I am pasting a part of the code of the Expert Advisor below:

On the first point: before you open a position, look how the previous one was closed. If you take the SL, then the lot should be double. The lot should not be increased by half of the lot of the previous position, but set in the settings or somewhere in the code. Then before opening, you will check the condition: if the previous position was closed by the SL, then open a position with the double lot.

As for the second one: I posted a sample trailing stop in this thread a long time ago. Look for it. Better in my profile in messages - you'll find it there faster than to browse 100+ pages here. Then on the basis of the example found, you will try to create your own trawl.

 
Artyom Trishkin #:

The first call to CopyRates() initiated historical data swapping (don't run for a week and you'll get the same again, well, maybe not a week, but some time).
You just need to check how much data there is on the server and compare the available amount to the Expert Advisor. If it is less than it is required - leave the EA until the next tick. At the next tick, check again. If a required amount of attempts is not obtained - the error is handled as required.

Can we get the number of loaded bars programmatically?

I myself just look at the date in the archive of quotes for each TF and try not to go beyond them in calculations.

This task may be unnecessary in this form. It would be more correct to get a flag when the right amount of bars are loaded, but it may never be loaded for one or another symbol.

 
Artyom Trishkin #:

On the first point: before opening a position, look at how the previous position closed. If by SL, then the lot should be double. The lot should not be doubled from the lot of the previous position, but from the set in the settings or somewhere in the code. Then before opening, you will check the condition: if the previous position was closed by the SL, then open a position with the double lot.

I think I have already changed it in the code:

//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
 double Lots()  // Расчет объема лота
  {
   int n=0;
   double Lotscount;
   double OL=Lot;
   
   
   if(Lot>0) return(LOT());          // "если во внешней переменной "Lot" стоит число больше 0 => будет фиксированный лот без авт. расчёта" 
   Lotscount=AccountFreeMargin()/10000;  // раcчёт лота - свободная маржа/9000 => 100/9000=0.01 лота 
   Lotscount=MathMin(15,MathMax(0.01,Lotscount));
    if(Lotscount<0.1) Lotscount=NormalizeDouble(Lotscount,2);
    else 
     {
       if(Lotscount<1) Lotscount=NormalizeDouble(Lotscount,1);
      else       Lotscount=NormalizeDouble(Lotscount,0);
     }
     
     for (int j = OrdersHistoryTotal()-1; j >= 0; j--)
   {
      if (OrderSelect(j, SELECT_BY_POS,MODE_HISTORY))
      {
          if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            if (OrderProfit()<0) 
             {
               if (n==0) OL=NormalizeDouble(OrderLots()*K_Martin,DigitsLot);
               n++; 
               if (OrderProfit()>0) return(Lotscount);
            }
            else
            {
               if (n==0) {Comment("2");return(Lotscount);}
               else {Comment("3");return(OL);}
            }
         }
      }
   }
      return(Lotscount);
 }
//------------------------------------------------------------------
double LOT()
{
   int n=0;
   double OL=Lot;
   for (int j = OrdersHistoryTotal()-1; j >= 0; j--)
   {
      if (OrderSelect(j, SELECT_BY_POS,MODE_HISTORY))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            if (OrderProfit()<0) 
            {
               if (n==0) OL=NormalizeDouble(OrderLots()*K_Martin,DigitsLot);
               n++; 
               if (OrderProfit()>0) return(Lot);}
            }
            else
            {
               if (n==0) {Comment("2");return(Lot);}
               else {Comment("3");return(OL);}
            }
         }
      }
   }
   return(OL);
}

Or is it wrong? Also, I don't know how to change two things related to lot. The first is that an order with a doubled lot should be opened only once and only on one order (the next order after the one with S.L.). And the second, which I do not know how to add, is to make it so that in the case of two S.L.'s in a row (two orders closed in a row by S.L.) the third order will not open with a lot increased by another 2 (2*2*2), but only once (2*2) like the second order.

 
Artis98 #:

I think I've already changed that in the code:

Or is it the wrong thing? Also, I don't know how to change two things about lot. The first is to make an order with double lot open only once and only on one order (the next order after the one with S.L.). And the second, which I do not know how to add, is to make it so that in the case of two S.L.'s in a row (two orders closed with S.L. in a row) the third order will not open with a lot increased by another 2 (2*2*2), but only once (2*2) like the second order.

You have TWO order types - order number 1 - an order with a regular lot, and order number 2 - an order with a larger lot. Lots are incremented only by SL type #1.

Save the order type in a "comment" for example. Better yet, memorize it yourself. It is a good habit to remember everything about your orders.

 
Maxim Kuznetsov #:

you have TWO order types - order number 1 - an order with a regular lot, and order number 2 - an order with an increased lot. Lots are incremented only by SL type #1.

Save the order type in a "comment" for example. Or, better yet, memorize it yourself. It is a good habit to remember everything about your orders.

We do not need to memorize anything (especially since memorizing requires further rebuilding). It is sufficient for him to know how the previous position was closed. That is all. There is no need to know neither two, nor three, nor one hundred in a row or one hundred not closed by stoploss, but only the last position. If it was closed by Stop Loss, then the next one should be closed with double lot. But the lot should not be doubled from the lot of the previous position, but from the set in the program normal lot. Then if position is closed not at Stop Loss, then the next position should be opened with the same lot. If position is closed by Stop Loss, then double the lot for the next position. That is all. You do not need to know anything else.

 
Artyom Trishkin #:

He does not need to memorise anything (especially as memorisation requires subsequent recovery). It only needs to know how the past position closed. That's all. He does not need to know two, or three, or a hundred in a row, or not closed at stoploss, but only the past position. If it was closed by Stop Loss, then the next one should be closed with double lot. But the lot should not be doubled from the lot of the previous position, but from the set in the program normal lot. Then if position is closed not at Stop Loss, then the next position should be opened with the same lot. If position is closed by Stop Loss, then double the lot for the next position. That is all. You do not need to know anything else.

Don't tell people what to do.

You have to remember your data and work with it

 
Artyom Trishkin #:

All he needs to know is how the past position closed. That's all. You don't need to know two, three, a hundred in a row or not in a row closed at stoploss, just the past position. If it was closed at stoploss, then the next one should be with double lot. If the position was closed at stoploss, then open the next one with double lot.

This is what I need, that's actually the question - how to write it in the code?

 

Hello!
Can someone please tell me how to force the compiler to compile for MT4 and not for MT5?

If the file has the extension *.mq4, I'm fine, if it has the extension *.mqh, then the compiler accepts it as *.mq5 and errors occur.

 
Sergey Zhukov #:

Hello!
Can someone please tell me how to force the compiler to compile for MT4 and not for MT5?

If the file has the extension *.mq4, I'm fine, if it has the extension *.mqh, then the compiler accepts it as *.mq5 and errors occur.

Give us the file. That's something new, I've never heard that before.
Reason: