Discussion of article "Library for easy and quick development of MetaTrader programs (part VIII): Order and position modification events" - page 3

 
leonerd:

When a tick is received, on which a pending order is set and simultaneously activated (triggered) (I tested with Buy Stop), your engine does not register all events...

The TRADE_EVENT_PENDING_ORDER_PLASED event is received, but not TRADE_EVENT_PENDING_ORDER_ACTIVATED.

My code is something like this:

I first find the index of the old (processed) event in the loop, and then starting from it, I loop through all the new ones until the end. So, in a situation where on one tick a limit order is set and striggeren receives one event about setting... Please comment.

I call ProcessTradeEvents() in OnTick() after engine.OnTick(rates_data).

The order was placed by your engine. Maybe I still called it when there was no activation yet..... I'll check it with a manual order now.

 
leonerd:

The order was placed by your own engine. Maybe I still called it when there was no activation yet..... I'll check with a manual order now.

Yes, I think it's my hands that are crooked....

 
leonerd:

Yeah, I think my hands are crooked...

No, it's in the library. To be more precise, it is not in the library, but in the peculiarity of recording the history of orders in the terminal. In later versions of the library, this feature is taken into account and everything is tracked correctly. Try versions around the 30th - before the topic about indicators. I can't remember where it was fixed now. However, I wrote about it in a later article - there was a correction there.
 

Hello.

I encountered this problem while testing TestDoEasyPart08.mq5 - StopLoss and TakeProfit do not work. Although they are set - the corresponding lines appear on the chart, and messages to the journal come. If you switch on TrailStop, TakeProfit triggers, but StopLoss still does not. What can be the reason?

P.S. I run the same code on another computer (laptop) and it works fine. Although the same metatrader is installed there.

 
MQL_User #:

Hello.

I encountered this problem while testing TestDoEasyPart08.mq5 - StopLoss and TakeProfit do not work. Although they are set - the corresponding lines appear on the chart, and messages to the journal come. If you switch on TrailStop, TakeProfit triggers, but StopLoss still does not. What can be the reason?

P.S. I run the same code on another computer (laptop) and it works fine. Although the same metatrader is installed there.

Good evening! First of all, please show at least the messages that appear in the log.

Regards, Vladimir.

 
MQL_User #:

Hello.

I encountered this problem while testing TestDoEasyPart08.mq5 - StopLoss and TakeProfit do not work. Although they are set - the corresponding lines appear on the chart, and messages to the journal come. If you switch on TrailStop, TakeProfit triggers, but StopLoss still does not. What can be the reason?

P.S. I run the same code on another computer (laptop) and it works fine. Although the same metatrader is installed there.

If stop (limit) orders are placed, their triggering depends not on the programme, but on the broker. The order to place stop (limit) orders is sent to the server by the programme, and their triggering depends on the broker. Since they are placed (their lines are displayed on the chart), there are no errors on the part of the programme. Most likely, the price does not reach the stop (limit) orders.

 
MrBrooklin #:

Good evening, at least show the messages that are logged.

Regards, Vladimir.

MrBrooklin, Artem.

I did it in the strategy tester (as I understand, the broker has nothing to do with it).

The messages are as follows:

2022.07.03 11:16:39.380 2022.01.03 07:00:00 instant buy 0.1 @Si at 81011 sl: 80960 tp: 81060 (81010 / 81011 / 81010)
2022.07.03 11:16:39.380 2022.01.03 07:00:00 deal #2 buy 0.1 @Si at 81011 done (based on order #2)
2022.07.03 11:16:39.380 2022.01.03 07:00:00 deal performed [#2 buy 0.1 @Si at 81011]
2022.07.03 11:16:39.380 2022.01.03 07:00:00 order performed buy 0.1 at 81011 [#2 buy 0.1 @Si at 81011]

But on another computer (laptop), on which StopLoss and TakeProfit work correctly, the messages are like this:

2022.07.03 11:23:18.383 2022.01.03 07:00:12 instant buy 0.1 @Si at 81047 sl: 80996 tp: 81096 (81046 / 81047 / 81046)
2022.07.03 11:23:18.383 2022.01.03 07:00:12 deal #2 buy 0.1 @Si at 81047 done (based on order #2)
2022.07.03 11:23:18.383 2022.01.03 07:00:12 deal performed [#2 buy 0.1 @Si at 81047]
2022.07.03 11:23:18.383 2022.01.03 07:00:12 order executed buy 0.1 at 81047 [#2 buy 0.1 @Si at 81047]
2022.07.03 11:23:18.514 2022.01.03 07:00:12 - Position open: 2022.01.03 07.03 07:00:12.490 -
2022.07.03 11:23:18.514 2022.01.03 07:00:12 @Si Opened 0.10 Buy #2 [0.10 Market order Buy #2] at 81047, sl 80996, tp 81096, magik 123

Indeed, two lines more...

To simplify things a bit, I wrote an Expert Advisor with this simple code:

#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
#import "user32.dll"
  int GetAsyncKeyState(int a0);
#import
bool ZPressed, ZPress = false;
bool APressed, APress = false;
CTrade trade;

int OnInit()  {   return(INIT_SUCCEEDED);  }

void OnDeinit(const int reason)  {  }

void OnTick()
  {
  // "Z" button is pressed
  if(MathAbs(GetAsyncKeyState(90))>1) ZPressed = ZPress = true;
  else ZPress = false;
  if(ZPressed && !ZPress)
    {
    ZPressed = false;
    double Price = SymbolInfoDouble(Symbol(),SYMBOL_BID);
    double sl = Price - 30, tp = Price + 50;
    trade.Buy(1,Symbol(),0,sl,tp);
    printf("Buy"); 
    }
  // "A" button is pressed
  if(MathAbs(GetAsyncKeyState(65))>1) APressed = APress = true;
  else APress = false;
  if(APressed && !APress)
    {
    APressed = false;
    double Price = SymbolInfoDouble(Symbol(),SYMBOL_BID);
    double sl = Price + 30, tp = Price - 50;
    trade.Sell(1,Symbol(),0,sl,tp);
    printf("Sell");    
    }
  }

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {  }
//+------------------------------------------------------------------+

The point is that when you press (or rather, when you release) the "Z" key, it opens Buy, and when you press the "A" key, it opens Sell.

I ran it in the tester. The result is the same - on a desktop PC StopLoss and TakeProfit do not work, but on a laptop they do, i.e. everything works correctly.

I also tried running the code linked to at the end of this article on both PC and laptop in the tester, and the same thing happened - on PC it doesn't work, but on laptop it works.

So the conclusion is that either there is some setting in the tester that allows triggering sl and tp (which I don't know about), or the tester itself is not working correctly....

 
MQL_User #:

MrBrooklin, Artem.

I did it in the strategy tester (as I understand, the broker has nothing to do with it).

The messages are as follows:

2022.07.03 11:16:39.380 2022.01.03 07:00:00 instant buy 0.1 @Si at 81011 sl: 80960 tp: 81060 (81010 / 81011 / 81010)
2022.07.03 11:16:39.380 2022.01.03 07:00:00 deal #2 buy 0.1 @Si at 81011 done (based on order #2)
2022.07.03 11:16:39.380 2022.01.03 07:00:00 deal performed [#2 buy 0.1 @Si at 81011]
2022.07.03 11:16:39.380 2022.01.03 07:00:00 order performed buy 0.1 at 81011 [#2 buy 0.1 @Si at 81011]

But on another computer (laptop), on which StopLoss and TakeProfit work correctly, the messages are as follows:

2022.07.03 11:23:18.383 2022.01.03 07:00:12 instant buy 0.1 @Si at 81047 sl: 80996 tp: 81096 (81046 / 81047 / 81046)
2022.07.03 11:23:18.383 2022.01.03 07:00:12 deal #2 buy 0.1 @Si at 81047 done (based on order #2)
2022.07.03 11:23:18.383 2022.01.03 07:00:12 deal performed [#2 buy 0.1 @Si at 81047]
2022.07.03 11:23:18.383 2022.01.03 07:00:12 order executed buy 0.1 at 81047 [#2 buy 0.1 @Si at 81047]
2022.07.03 11:23:18.514 2022.01.03 07:00:12 - Position open: 2022.01.03 07:00:12.490 -
2022.07.03 11:23:18.514 2022.01.03 07:00:12 @Si Open 0.10 Buy #2 [0.10 Market order Buy #2] at 81047, sl 80996, tp 81096, magik 123

Indeed, two lines more...

To simplify things a bit, I wrote an Expert Advisor with this simple code:

The point is that when you press (or rather, when you release) the "Z" key, it opens Buy, and when you press the "A" key, it opens Sell.

I ran it in the tester. The result is the same - on a desktop PC StopLoss and TakeProfit do not work, but on a laptop they do, i.e. everything works correctly.

I also tried running the code linked at the end of this article on both PC and laptop in the tester, and the same thing happened - on PC it doesn't work, but on laptop it works.

So the conclusion is that either there is some setting in the tester that allows triggering sl and tp (which I don't know about), or the tester itself works incorrectly....

What is the spread in the tester?
 

The spread in the tester is 1. Both on PC and laptop. Symbol @Si.

Is there any way it can affect it?

I thought that maybe slippage (slippage) affects somehow and tried to change it. But it doesn't work.

 
MQL_User #:

The spread in the tester is 1. Both on PC and laptop. Symbol @Si.

Can this have any effect?

I thought maybe the slippage (slippage) was affecting it somehow and tried changing it. But it doesn't work.

In one of the cases, the price does not reach the stop level.