Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1494

 
Roman Kutemov #:
Select the period on which you want to run your data. And then on the tab of the obtained parameters, right-click on the desired one and select single test.

Thanks.
And how to do everything in the scope, and not poke at each parameter?

 

Could you please help me with the calculations?


I need to have a take profit 3 times the stop. It seems to work, but something is wrong)). The robot trades on gold and indices, but refuses to trade on currencies (although it calculates everything correctly).

First I set the stop on the low of candle 1:

//---Устанавливаем стоп
   double stoploss = iLow(_Symbol,PERIOD_CURRENT,1);
      Print("Цена стопа ", stoploss);

Then I calculate the distance from ASK to the stop in points, and assign this value to the pert points:

//---Опредеяем размер стопа в пунктах
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double low = iLow(_Symbol, 0, 1);
   double points = NormalizeDouble((ask - low) / _Point, _Digits);
      Print("Размер стопа ", points);

After that I calculate the stop also in pips:

//---Считаем тейкпрофит
   double TP = NormalizeDouble(points * 3, _Digits);
   Print("Размер тейка ", TP);


Then I open a deal and add TR to the ASK :

//---Открываем сделку на покупку
         if(Close > maHandle && Close > Open)
            {
               if(trade.Buy(Lot,_Symbol,SYMBOL_ASK,stoploss, ask + TP,"Это я, твой робот") == true)
                  {
                     Print("Открыта покупка");
                  }


And in the tester all calculations are correct, in 5-digit points, the stop is also on Low1, the size of the stop is also correct in 5-digit points. But there is something wrong with the take profit, it is not 507 pips, but 5070 pips.



 

Hello, Sergey.

When calculating the stop, you go from the distance in price to the distance in pips. Then, multiplying by 3, you get the distance to takeout also in pips. And then you add the number of pips to the ask price.

Try not to divide by the size of one pip or then remember to multiply by it again.

//---Устанавливаем стоп
   double stoploss = iLow(_Symbol,PERIOD_CURRENT,1);
      Print("Цена стопа ", stoploss);

//---Опредеяем размер стопа
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double low = iLow(_Symbol, 0, 1);
   double points = NormalizeDouble((ask - low) / _Point, _Digits);
   double points = NormalizeDouble((ask - low), _Digits);
      
//--- ...

Or like this

//---Устанавливаем стоп
   double stoploss = iLow(_Symbol,PERIOD_CURRENT,1);
   Print("Цена стопа ", stoploss);

//---Опредеяем размер стопа в пунктах (это целое число)
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double low = iLow(_Symbol, 0, 1);
   int points = (ask - low) / _Point;
   Print("Размер стопа ", points);

//---Считаем тейкпрофит
   int TP = points * 3;
   Print("Размер тейка ", TP);

//---Открываем сделку на покупку
         if(Close > maHandle && Close > Open)
            {
               if(trade.Buy(Lot,_Symbol,SYMBOL_ASK,stoploss, NormalizeDouble(ask + TP * _Point, _Digits),"Это я, твой робот") == true)
                  {
                     Print("Открыта покупка");
                  }
 
Yuriy Bykov #:

Hello, Sergei.


Thank you very much! Really got lost in the prices/items. I'll try it now)))

 
Now everything is counted and set correctly, but only on USDJPY, XAUUSD and any index. But on currencies like USDCAD, EURUSD the owl does not trade and sends error 4756. I looked up the value in the help, but it did not help)
 
Sergey Izhutov #:
Now everything is counted and set correctly, but only on USDJPY, XAUUSD and any index. But on currencies like USDCAD, EURUSD the owl does not trade and sends error 4756. I looked up the value in the help, but it did nothing)

Do like in the previous post: provide the code that opens positions for you. Then, perhaps, you will be able to give some advice. There should not be any fundamental difference between USDJPY and USDCAD, most likely there is some trivial error.

 
Yuriy Bykov #:

Do as in the last post:

Here is the code

//---Открываем сделку на покупку
         if(Close > maHandle && Close > Open)
            {
               if(trade.Buy(Lot,_Symbol,SYMBOL_ASK,stoploss, ask + BuyTP,"Это я, твой робот") == true)
                  {
                     Print("Открыта покупка");
                  }
                 else
                     {
                         // Получение кода ошибки
                         int error_code = GetLastError();
                         Print("Ошибка отправки заявки: ", GetLastError());
                     }
                  
                     return;
            }
            
    //---Открываем сделку на продажу
         if(Close < maHandle && Close < Open)
            {
               if(trade.Sell(Lot,_Symbol,SYMBOL_ASK,stoploss,ask - SellTP,"Это я, твой робот") == true)
                  {
                     Print("Открыта продажа");
                  }
                 else
                     {
                         // Получение кода ошибки
                         int error_code = GetLastError();
                         Print("Ошибка отправки заявки: ", GetLastError());
                     }
                  
                     return;
            }
 

Most likely the error is here:

if(Close > maHandle && Close > Open)

According to your logic maHandle should be the price, and this is most likely a handle, i.e. an integer - the serial number of the indicator among all loaded indicators. Look in the MQL5 Help for the correct way to get indicator values.

 
Yuriy Bykov #:

There's probably a mistake here:

According to your logic maHandle should be the price, and this is most likely a handle, i.e. an integer - the serial number of the indicator among all loaded indicators. Look in the MQL5 Help for the correct way to get indicator values.

I thought the same thing in the evening, that I should remove it. I haven't made friends with indices yet. I just vaguely remember that in 4 I just wrote that Close > MA and everything worked there. I'll try commenting out the handle and see what the results are

 
Sergey Izhutov #:

That's what I was thinking tonight, too, that I should put it away. I haven't made friends with the indices yet. I just vaguely remember in 4 just writing that Close > MA and it worked there. I'll try commenting out the handle and see what the results are

I was once recommended to "make friends" with MetaEditor debugger. After I got more or less acquainted with the debugger, 99.9% of questions disappeared by themselves. You put any variables in the observation and see their values, then compare them and understand where in the code there is a problem. It is a very convenient tool. I recommend it.

Regards, Vladimir.

Reason: