Discussion of article "Library for easy and quick development of MetaTrader programs (part XXII): Trading classes - Base trading class, verification of limitations" - page 2

 
Artyom Trishkin:

So after event processing, at the end of the event type check module, what do you have in the last_event variable? That's what you need to reset in the EA. And not forcibly call the method of resetting the last event of the library - the library itself calls it right after sending the event to the chart and writing it to the list of events.

Yes. I just missed and didn't put one line at the end.

last_event = engine.LastTradeEvent();
 
Alexey Viktorov:

Yeah. I just missed one line at the end.

You can just equate it to TRADE_EVENT_NO_EVENT.
 
Artyom Trishkin:
You can just equate it to TRADE_EVENT_NO_EVENT.

It's not working. It prints on every tick. The order is activated on the fifth tick, it prints "1" 4 times in the log and then it prints "20" on every tick.

//--- If the last trade event has changed
 if(engine.LastTradeEvent() != last_event)
  {
   last_event=engine.LastTradeEvent();
   switch(last_event)
    {
     case TRADE_EVENT_PENDING_ORDER_PLASED :
     Print(__FUNCTION__, "***", last_event);
     break;
     case TRADE_EVENT_PENDING_ORDER_REMOVED :
     
     break;
     case TRADE_EVENT_PENDING_ORDER_ACTIVATED :
      {
       Print(__FUNCTION__, "***", last_event);
       //OrderActivated();
     break;
      }
     case TRADE_EVENT_POSITION_OPENED :
     
     break;
     case TRADE_EVENT_POSITION_CLOSED :
     
     break;
//---
     
     default :
      break;
    }
   last_event = TRADE_EVENT_NO_EVENT;
  }
 
Alexey Viktorov:

It's not working. It prints on every tick. The order is activated on the fifth tick, it prints "1" 4 times in the log and then it prints "20" on each tick.

We need to see what you have created there. It works in test Expert Advisors.
I'm on my mobile. I'll be there in about five hours.
 
Artyom Trishkin:
We need to see what you've done. It's working in the test advisors.
I'm on my mobile. I won't be there for another five hours.

I haven't written anything yet. Above the code in the previous post is just this.

//--- Initialisation of the last trade event
 static ENUM_TRADE_EVENT last_event = WRONG_VALUE;
//--- If working in the tester
 if(MQLInfoInteger(MQL_TESTER))
  {
   engine.OnTimer();
  }
And below the code, opening two orders OP_SELLLIMIT and OP_SELLSTOP
 
Alexey Viktorov:

I haven't created anything yet. Above the code in the previous message is only this.

And below the code, opening two orders OP_SELLLIMIT and OP_SELLSTOP.
I need to see the whole code. Throw it in a private message, or here if it is not a secret behind seven seals.
 
Artyom Trishkin:
I need to see the whole code. Throw it in a private message, or here if it's not a secret behind seven seals.

There's nothing there yet.

/********************************************************************\
|Only_BUY_or_only_SELL.mq4 |
|© 2019, Alexey Viktorov |
| https://www.mql5.com/en/users/alexeyvik/news |
\********************************************************************/
#property copyright "© 2019, Alexey Viktorov"
#property link      "https://www.mql5.com/en/users/alexeyvik/news"
#property version   "1.00"
#property strict
#define  ndd(A) NormalizeDouble(A, _Digits)
//---
#include <DoEasy\Engine.mqh>
CEngine        engine;
//---
enum MY_ORDER_TYPE
{
 BUY   = OP_BUY,
 SELL  = OP_SELL,
};

struct POZ
{
 double profit;
 ulong  ticket;
 double priceGrid;
 double price;
}poz[];

sinput  MY_ORDER_TYPE   ordType         = SELL;   // Direction of trade
sinput  double          lot             = 0.01;   // Lot size
 input  int             StepGrid        = 50;     // Grid spacing
 input  double          desiredProfit   = 10;     // Desired profit
sinput  bool            Should_I_open   = false;  // Should I open a new series of positions?
sinput  int             magick          = 1;      // Magick number
//---
int slip = 100;
double contract, stepGrid, priceGrid;
int totalPositions, levelNumb;//, levelBUY, levelSELL;

/*******************Expert initialization function*******************/
int OnInit()
{
 stepGrid = StepGrid*_Point;
 return(INIT_SUCCEEDED);
}/*******************************************************************/

/************************Expert tick function************************/
void OnTick()
{
//--- Initialisation of the last trade event
 static ENUM_TRADE_EVENT last_event = WRONG_VALUE;
//--- If working in the tester
 if(MQLInfoInteger(MQL_TESTER))
  {
   engine.OnTimer();
  }
//--- If the last trade event has changed
 if(engine.LastTradeEvent() != last_event)
  {
 //Print(__FUNCTION__, "***", last_event);
   last_event=engine.LastTradeEvent();
   switch(last_event)
    {
     case TRADE_EVENT_PENDING_ORDER_PLASED :
     Print(__FUNCTION__, "***", last_event);
     break;
     case TRADE_EVENT_PENDING_ORDER_REMOVED :
     
     break;
     case TRADE_EVENT_PENDING_ORDER_ACTIVATED :
      {
       Print(__FUNCTION__, "***", last_event);
       //OrderActivated();
     break;
      }
     case TRADE_EVENT_POSITION_OPENED :
     
     break;
     case TRADE_EVENT_POSITION_CLOSED :
     
     break;
     
     default :
      break;
    }
   last_event = TRADE_EVENT_NO_EVENT;
  }
 
 if(Should_I_open && OrdersTotal() == 0)
  {
   priceGrid = ndd(ordType == SELL ? Bid+stepGrid/2 : Bid-stepGrid/2);
   ENUM_ORDER_TYPE type = ENUM_ORDER_TYPE(ordType == OP_SELL ? OP_SELLLIMIT : OP_BUYSTOP);
   if(OrderSend(_Symbol, type, lot, priceGrid, slip, 0.0, 0.0, (string)priceGrid, magick) <= 0)
    Print(__FUNCTION__, GetLastError());
   priceGrid = ndd(ordType == SELL ? Bid-stepGrid/2 : Bid+stepGrid/2);
   type = ENUM_ORDER_TYPE(ordType == OP_SELL ? OP_SELLSTOP : OP_BUYLIMIT);
   if(OrderSend(_Symbol, type, lot, priceGrid, slip, 0.0, 0.0, (string)priceGrid, magick) <= 0)
    Print(__FUNCTION__, GetLastError());
  }
  return;
}/*******************************************************************/

void OrderActivated()
{
 Print(__FUNCTION__, "***", ENUM_TRADE_EVENT(engine.LastTradeEvent()));
 
}/*******************************************************************/

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
//--- Initialisation of the last trade event
 static ENUM_TRADE_EVENT last_event=WRONG_VALUE;
//--- If working in the tester
 if(MQLInfoInteger(MQL_TESTER))
  {
   engine.OnTimer();
  }
//--- If the last trade event has changed
 if(engine.LastTradeEvent()!=last_event)
  {
   last_event=engine.LastTradeEvent();
  }
}/*******************************************************************/

/***************************Timer function***************************/
void OnTimer()
{
 if(!MQLInfoInteger(MQL_TESTER))
  engine.OnTimer();
}/*******************************************************************/

void OnDeinit(const int reason)
{
 Comment("");
}/*******************************************************************/
 
Alexey Viktorov:

Well, there's nothing there yet

I have to run your code to get an answer. I'll get back to you when I'm there.
 
Artyom Trishkin:
I have to run your code to get an answer. I'll be there, I'll get back to you.

I know you're not home and I'm not rushing you.

 
Alexey Viktorov:

I know you're not home and I'm not rushing you.

No, on the contrary - I'm not at work, but at home ;)