Questions from Beginners MQL5 MT5 MetaTrader 5 - page 302

 
mavar:

Cheers! Today I launched my first EA in MT5!

The right situation on the market came and the Expert Advisor started buying and buying!!! At least I managed to react in time. Can you tell me what is wrong in the code? I thought it should buy only one lot and immediately set a Stop Loss and Take Profit. As soon as he bought something, the variable Bye_opened should become true and it should not allow a subsequent trade until the stops are triggered. Also, how do I know the price of the last trade to put stops on it?


But it looks like I did something wrong. Please point out my mistake.

Ticks may come faster than the position is in the list of open positions. So, your check

if(PositionSelect(_Symbol)==true)

won't work for a while, and during that time several entries will pass. It is assumed that the above piece of code is located in the tick handler.

 
pako:

put this script in the scripts folder, open this script in a unitor, compile it

When the exchange opens, drag this script onto the chart

Thanks for the help, figured it out myself, everything worked.
 
Good evening everyone, share the code for the trawl. Thank you.
 
PokrovMT5:
Good evening everyone, please share the code for the trawl. Thanks.

Please use the site search - it's not hard:https://www.mql5.com/ru/search#!keyword=%D1%82%D1%80%D0%B0%D0%BB

 
barabashkakvn:

Please use the site search - it is not difficult:https://www.mql5.com/ru/search#!keyword=%D1%82%D1%80%D0%B0%D0%BB

Thank you!
 
Dima_S:

Ticks may come faster than the position is in the list of open. So, your check

will not work for some time, and during this time there will be multiple entries. It is assumed that the above piece of code is located in the tick handler.

Dima, the code is in the tick processing block. What's the right way to solve this problem? I had the same problem in QuickBooks, the robot often buys more than it should. I put it down to its second delay and simply added separate variable that changes when conditions are fulfilled, but it had some problems there too. Sometimes the order was rejected by the exchange and the variable already had the value "deal" and the robot didn't trade.


How to correctly solve the problem in MQL5, so the robot will buy a specified number of lots at a given condition and will not buy more than the specified number of lots until it has sold all the lots it has bought?

 

Should we pause after buying to wait for a response from the exchange for sure?

Sleep(1000); //Have you paused?

 

Stop loss does not move

writes:

--Modify order -> price open:1.3111 BID :1.3121 sl 1.3109
failed modify order #3 buy limit 1.00 at 1.31110 sl: 1.31080 tp: 0.00000 -> 1.31110, sl: 1.31090 tp: 0.00000 [Invalid stops]

--Modify order <- result:10016

Why?

void MoveStopLoss()
{
   ulong  ticket     = OrderGetTicket(0);
   double price      = OrderGetDouble(ORDER_PRICE_OPEN);
   double slossPrev  = OrderGetDouble(ORDER_SL);
   double sloss      = SymbolInfoDouble(_SYMBOL, SYMBOL_ASK) - SymbolInfoDouble(_SYMBOL, SYMBOL_POINT)*15;

      if (slossPrev < sloss)
      {
         MqlTradeRequest request={0};
         MqlTradeResult  result={0};
         request.action       = TRADE_ACTION_MODIFY;        // Тип выполняемого действия
         request.order        = ticket;                     // Тикет ордера (для модификации ордеров)
         request.price        = price;                      // Цена 
         request.sl           = sloss;                      // Уровень Stop Loss ордера
         request.tp           = 0;                          // Уровень Stop Loss ордера
         request.type_time    = ORDER_TIME_DAY;             // Тип ордера по времени действия
   
         Print ("--Modify order -> ", " price open:", price, " BID :", SymbolInfoDouble(_SYMBOL, SYMBOL_BID), " sl ", request.sl);
         bool ret = OrderSend (request, result);
         Print ("--Modify order <- результат:", result.retcode);      
      }
}
 
mq35:

Stop loss does not move

writes:

--Modify order -> price open:1.3111 BID :1.3121 sl 1.3109
failed modify order #3 buy limit 1.00 at 1.31110 sl: 1.31080 tp: 0.00000 -> 1.31110, sl: 1.31090 tp: 0.00000 [Invalid stops]

--Modify order <- result:10016

Why?

Try to normalise the SL.
 

Normalize does not help. The same thing, sop-loss is not pulled up

--Create order -> ASK:1.3121 price:1.3111 sl 1.3106

buy limit 1.00 EURUSD at 1.3111 (1.3121) sl: 1.3106 (1.3118 / 1.3121 / 1.3118)
--Create order <- result:10009

--Modify order -> price open:1.3111 BID :1.3119 sl 1.3107
order modified [#3 buy limit 1.00 EURUSD at 1.3111]

--Modify order <- result:10009

--Modify order -> price open:1.3111 BID :1.312 sl 1.3108
order modified [#3 buy limit 1.00 EURUSD at 1.3111]

--Modify order <- result:10009

--Modify order -> price open:1.3111 BID :1.3121 sl 1.3109
failed modify order #3 buy limit 1.00 at 1.31110 sl: 1.31080 tp: 0.00000 -> 1.31110, sl: 1.31090 tp: 0.00000 [Invalid stops]
--Modify order <- result:10016