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

 
Vadim Novikov:

Good afternoon to everyone who cares!


I'm an advanced user. But I don't write that often. That's why. I can't keep up with some innovations. And sometimes I just forget things.


Please help me with one question. I can't remember one thing.


Here's the situation. I've defined extern variables. I've defined many of them. 20 variables.They are displayed in the input menu of the program, when you attach this program to the chart.


Here's the question. I can't remember. How do I separate blocks of these variables with comments? In the input menu. For example:


This is a block on changing MA variables (comment)

Period MA

Setting method MA

Price MA

This is a block on changing MACD variables (comment)

..........................

...........................

...........................

This is a block on changing BB variables (comment)

.........................

..............................

...........................


It seems that before I entered such comments with code comment function.That is, I wrote // or /* */. But now something does not work.

   extern string     a1             = "Это  блок  по  изменению   переменных   MA (комментарий)";
   ..............
   ..............
   ..............
   extern string     a2             = "Это  блок  по  изменению  переменных   MACD (комментарий)";

Like this

 
OrderStopLoss() outputs two decimal places. Can I force it to output three decimal places?
 
Alekseu Fedotov:

Like this.

Thanks, that reminds me!!!

 
Igor Golieniev:

Try it like this:

Print("SL: ", DoubleToString(OrderStopLoss(), Digits()));

This is to show all significant digits of the quote. If some other number of digits is needed, replace Digits() with a specific number.

 
Good evening, any tips please. In OnInit function creates two horizontal lines, in OnTick two functions, one sends push and mail notification if Bid > first line, second if Bid < second line. Bid=line did not do it, to avoid gaps. Of course, I also faced with the fact that if the condition is fulfilled, the notification comes with every tick, which is bad. How to solve this problem? You can set timeout or number of notifications in Standard Alert settings.
 
Ihor Herasko:

Try it like this:

This is to show all significant digits of the quote. If you need any other number of digits, replace Digits() with a specific number.

Now it prints correctly 15,155, however - invalid stoploss for OrderModify function

This problem is only with Silver in OrderModify

void Trailing()
  {
   if(Digits==3 || Digits==5)
     {
      TrailingStep *= 10;
      TrailingOpen *= 10;
      TrailingStop *= 10;
     }
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY)
              {
               if(Bid>OrderOpenPrice()+TrailingOpen*Point)
                 {
                  if(OrderStopLoss()<Bid-(TrailingStop+TrailingStep)*Point)
                    {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),0,0))
                        Print("Oshibka =", DoubleToString(OrderStopLoss(), Digits()));
                    }
                 }
              }
            if(OrderType()==OP_SELL)
              {
               if(Ask<OrderOpenPrice()-TrailingOpen*Point)
                 {
                  if(OrderStopLoss()>Ask+(TrailingStop+TrailingStep)*Point)
                    {
                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailingStop*Point,Digits),0,0))
                        Print("Oshibka", DoubleToString(OrderStopLoss(), Digits()));
                    }
                 }
              }
           }
        }
     }
  }
 
Igor Golieniev:

Now outputs correctly in Print 15,155 , however - invalid stoploss for OrderModify function

This problem is only with Silver in OrderModify

There are two errors in this code:

  1. Lack of checking for minimum allowable stop size (it's called Stop Level).
  2. Incorrect comparison of real numbers.

To solve the first problem we need to get the current Stop Level:

double fStopLevel = SymbolInfoInteger(Symbol(), SYMBOL_TRADE_STOPS_LEVEL) * Point();

If fStopLevel turns out to be zero and the account type is not ECN, then fStopLevel should be forced to be equal to three spreads.

Before setting stops (both Stop Loss and Take Profit) check that the new level is at or above the Stop Level from the order close price. To check the stop for a Buy order this is done as follows:

if (Bid - fNewSL - fStopLevel < -Point() / 10)
{
  // Такой стоп ставить нельзя. Ближайший возможный уровень: Bid - fStopLevel
}

For a Sell order stop:

if (fNewSL - Ask - fStopLevel < -Point() / 10)
{
   // Stop Loss на цене fNewSL ставить нельзя. Ближайший возможный уровень: Ask + fStopLevel
}


The solution to the second problem: Compare real values with some accuracy, as the equality of real numbers cannot be achieved always and everywhere. I have already given an example of comparison above in the code of stop level validation.

 
Ihor Herasko:

There are two errors in this code that are striking:

  1. Lack of check on the minimum allowable stop size (called Stop Level).
  2. Incorrect comparison of real numbers.

To solve the first problem we need to get the current Stop Level:

If fStopLevel turns out to be zero and the account type is not ECN, then fStopLevel should be forced to be equal to three spreads.

Before setting stops (both Stop Loss and Take Profit) check that the new level is at or above the Stop Level from the order close price. To check the stop for a Buy order this is done as follows:

For a Sell order stop:


The solution to the second problem: Compare real values with some accuracy, as the equality of real numbers cannot be achieved always and everywhere. I already gave an example of comparison above in the code of stop level validation.

Thank you.

However, the question is not closed. OrderModify starts to trigger immediately when an order is opened. Where is the error? (I repeat - on all(all) currencies/futures it works, on Silver it does not)

 
Igor Golieniev:

Thank you.

However, the question is not closed. OrderModify starts to trigger immediately when an order is opened. Where is the error? (I repeat - on all(all) currencies/futures it works, on Silver it does not)

1. Directive

#property strict

are you using ?

2) Does this happen with Sell and Buy, or only with Sell?

3. is the Stop loss set at position opening or should the trader set it if possible?

Совершение сделок - Торговые операции - MetaTrader 5
Совершение сделок - Торговые операции - MetaTrader 5
  • www.metatrader5.com
Торговая деятельность в платформе связана с формированием и отсылкой рыночных и отложенных ордеров для исполнения брокером, а также с управлением текущими позициями путем их модификации или закрытия. Платформа позволяет удобно просматривать торговую историю на счете, настраивать оповещения о событиях на рынке и многое другое. Открытие позиций...
 
Igor Golieniev:

Thank you.

However, the issue is not closed. OrderModify starts working immediately when an order is opened. Where is the error? (I repeat - it works on all(all) currencies/futures, it does not work on Silver)

Show the log snippet (if online, the Experts tab) where you can see the market order opening and modifications and indicate which TrailingStop and TrailingStep values were used.

Reason: