Class function and other artifact .....

 

Hi guys,

I have a script composed of two scripts: PanelDialog.mqh and TradeManager.mq5.
I'm trying to access the function CheckAndCancelOrders(); , which is inside a class that, in theory, should be fully public, so I should be able to access it. However, there's no way to make it work.

Basically, when I press a button, I send an order (all of this happens in PanelDialog.mqh), and up to this point, everything is fine. But then, I want to check whether the order is still open or has been stopped.

To do this, I created a function inside the class COrderExecutor , called CheckAndCancelOrders(); , but I can't call it in any way.

Could someone give me some guidance?
Thanks!

PanelDialog.mqh

//+------------------------------------------------------------------+
//|                                                  PanelDialog.mqh |
//|                             Copyright 2000-2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Controls\Dialog.mqh>
#include <Controls\Button.mqh>
#include <Controls\Edit.mqh>
#include <Controls\ListView.mqh>
#include <Controls\ComboBox.mqh>
#include <Controls\SpinEdit.mqh>
#include <Controls\RadioGroup.mqh>
#include <Controls\CheckGroup.mqh>
#include <Controls\Label.mqh>
#include <Trade\Trade.mqh>

CTrade trade;  // Creiamo un oggetto della classe CTrade

CPositionInfo position;
COrderInfo order;
CArrayLong arr_tickets;

extern COrderExecutor *orderExecutor;

COrderExecutor *orderExecutor = NULL;


string InpCloseMode="";
string InpCloseSymbol="";
string InpCloseProfit="";
string InpCloseType="";
string InpCloseOrders="";
int InpClosePartial=100;

double g_UpperLinePrice = 0;
double g_LowerLinePrice = 0;
  
//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
//--- indents and gaps
#define INDENT_LEFT                         (11)      // indent from left (with allowance for border width)
#define INDENT_TOP                          (11)      // indent from top (with allowance for border width)
#define INDENT_RIGHT                        (11)      // indent from right (with allowance for border width)
#define INDENT_BOTTOM                       (11)      // indent from bottom (with allowance for border width)
#define CONTROLS_GAP_X                      (10)      // gap by X coordinate
#define CONTROLS_GAP_Y                      (10)      // gap by Y coordinate
//--- for buttons
#define BUTTON_WIDTH                        (100)     // size by X coordinate
#define BUTTON_HEIGHT                       (20)      // size by Y coordinate
//--- for the indication area
#define EDIT_HEIGHT                         (20)      // size by Y coordinate
//+------------------------------------------------------------------+
//| Class CPanelDialog                                               |
//| Usage: main dialog of the SimplePanel application                |
//+------------------------------------------------------------------+
class CPanelDialog : public CAppDialog
  {
private:
   CButton           m_buttonSell;                       // the button object
   CButton           m_buttonBuy;                       // the button object

public:
                     CPanelDialog(void);
                    ~CPanelDialog(void);

                    //void CheckAndCancelOrders(); // Metodo per l OCO ordr cancel order
           
   
   //--- create
   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
   //--- chart event handler
   virtual bool      OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);

protected:
   //--- create dependent controls
   bool              CreateEdit(void);
   bool              CreateButtonSell(void);
     
   //--- internal event handlers
   virtual bool      OnResize(void);
   //--- handlers of the dependent controls events
   void              OnClickButtonSell(void);
   void              OnClickButtonBuy(void);
 
    };
//+------------------------------------------------------------------+
//| Event Handling                                                   |
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(CPanelDialog)
    ON_EVENT(ON_CLICK,m_buttonSell,OnClickButtonSell)
    ON_EVENT(ON_CLICK,m_buttonBuy,OnClickButtonBuy)

    
EVENT_MAP_END(CAppDialog)
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CPanelDialog::CPanelDialog(void)
  {
  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CPanelDialog::~CPanelDialog(void)
  {
  }
//+------------------------------------------------------------------+
//| Create                                                           |
//+------------------------------------------------------------------+
bool CPanelDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
  {
  if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))
     return(false);

     return(false);   
  if(!CreateRadioOptionClosePartialPosition())
     return(false);                                                                                        
//--- succeed
   return(true);
  }

//+------------------------------------------------------------------+
//| Handler of resizing                                              |
//+------------------------------------------------------------------+
bool CPanelDialog::OnResize(void)
  {
//--- call method of parent class
   if(!CAppDialog::OnResize()) return(false);
//--- coordinates
   int x=ClientAreaLeft()+INDENT_LEFT;

   return(true);
  }

//+------------------------------------------------------------------+
//| Event handler                                                    |
//+------------------------------------------------------------------+
void CPanelDialog::OnClickButtonSell(void)
 
void CPanelDialog::OnClickButtonExEcuteOCO(void)
{
    double lotSize    = StringToDouble(m_edit.Text());  // Legge il valore della dimensione del lotto
    double entryPrice1 = StringToDouble(m_editPrice1.Text()); // Prezzo di ingresso per il primo ordine
    double stopLoss1   = StringToDouble(m_editPrice1STP1oco.Text());  // Stop Loss (0 se non vuoi impostarlo)
    double takeProfit1 = StringToDouble(m_editPrice1TP1oco.Text());  // Take Profit (0 se non vuoi impostarlo)
    
    double entryPrice2 = StringToDouble(m_editPrice2.Text()); // Prezzo di ingresso per il secondo ordine  
    double stopLoss2   = StringToDouble(m_editPrice2STP2oco.Text());  // Stop Loss (0 se non vuoi impostarlo)
    double takeProfit2 = StringToDouble(m_editPrice2TP2oco.Text());  // Take Profit (0 se non vuoi impostarlo)

    // Verifica se i prezzi sono validi
    if (entryPrice1 == 0 || entryPrice2 == 0)
    {
        MessageBox("Devi inserire entrambi i prezzi di ingresso.", "Errore", MB_OK | MB_ICONERROR);
        return;
    }

    // Ottieni il prezzo attuale
    double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);  // Prezzo attuale di mercato (Bid)
    Print("Prezzo attuale: ", currentPrice);
    
    // Determina il tipo di ordine in base alla selezione del radio group
    ENUM_ORDER_TYPE orderType1 = ORDER_TYPE_BUY_STOP;  // Default a BUY_STOP
    ENUM_ORDER_TYPE orderType2 = ORDER_TYPE_SELL_STOP; // Default a SELL_STOP

    // Prima logica per il primo ordine (entryPrice1)
    if (m_radioGroupOCO.Value() == 1)  // BUY STOP - SELL LIMIT
    {

    }

    // Stampa i dettagli prima di eseguire gli ordini
    Print("Esecuzione ordini:");
    Print("Lotto: ", lotSize);
    Print("Entry Price 1: ", entryPrice1, ", Stop Loss 1: ", stopLoss1, ", Take Profit 1: ", takeProfit1);
    Print("Entry Price 2: ", entryPrice2, ", Stop Loss 2: ", stopLoss2, ", Take Profit 2: ", takeProfit2);
    Print("Tipo ordine 1: ", EnumToString(orderType1));  // Usa EnumToString su una variabile di tipo ENUM_ORDER_TYPE
    Print("Tipo ordine 2: ", EnumToString(orderType2));  // Usa EnumToString su una variabile di tipo ENUM_ORDER_TYPE

    // Crea l'oggetto della classe COrderExecutor
    COrderExecutor orderExecutor(lotSize, entryPrice1, stopLoss1, takeProfit1,
                                  entryPrice2, stopLoss2, takeProfit2, orderType1, orderType2);
       // Rimuove le linee se il valore è 0
        ObjectDelete(0, "UpperLine");
        ObjectDelete(0, "LowerLine");
    // Esegui gli ordini
    orderExecutor.ExecuteOrders();
}



  
  
 void CPanelDialog::OnChangeRadioOptionCloseInALL_PROFLOSS(void)
  {

  }
  
  void CPanelDialog::OnChangeRadioOptionCloseInALL_SIDE(void)
  {

  }
  
  void CPanelDialog::OnChangeRadioOptionClosePartialPosition(void)
  {

  } 
//+------------------------------------------------------------------+

//  EXTRA FUNCTION 

//+------------------------------------------------------------------+
//| Check for permission to perform automated trading                |
//+------------------------------------------------------------------+
bool CheckTradingPermission()
  {

  }


//+------------------------------------------------------------------+
//| Checks if the market is currently open for the specified symbol  |
//+------------------------------------------------------------------+
bool CheckSessionTrade(const string symbol)
  {

  }
 
  
//+------------------------------------------------------------------+
//| Close market positions                                           |
//+------------------------------------------------------------------+
void ClosePositions(uint specific_ticket)
{

}

//+------------------------------------------------------------------+
//| Delete pending orders                                            |
//+------------------------------------------------------------------+
void DeletePendingOrders()
  {

  }
  
 void CloseAndReversePosition(uint ticket)
{

}

void ReadLinesPrice(double &upperPrice, double &lowerPrice)
{

}


class COrderExecutor
{
private:
    double m_lotSize;
    double m_entryPrice1;
    double m_stopLoss1;
    double m_takeProfit1;
    double m_entryPrice2;
    double m_stopLoss2;
    double m_takeProfit2;
    int m_orderType1; // Tipo di ordine per il primo
    int m_orderType2; // Tipo di ordine per il secondo
    int ticket1, ticket2;
    
public:
    // Costruttore
    COrderExecutor(double lotSize, double entryPrice1, double stopLoss1, double takeProfit1,
                   double entryPrice2, double stopLoss2, double takeProfit2, int orderType1, int orderType2)
    {
        m_lotSize = lotSize;
        m_entryPrice1 = entryPrice1;
        m_stopLoss1 = stopLoss1;
        m_takeProfit1 = takeProfit1;
        m_entryPrice2 = entryPrice2;
        m_stopLoss2 = stopLoss2;
        m_takeProfit2 = takeProfit2;
        m_orderType1 = orderType1;
        m_orderType2 = orderType2;
    }

    // Metodo per eseguire gli ordini in base ai parametri
    void ExecuteOrders()
    {
      
    }
   
    
  void CheckAndCancelOrders()
    {
       
    }
};


TradeManager.mq5

//+------------------------------------------------------------------+
//|                                                  SimplePanel.mq5 |
//|                             Copyright 2000-2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#include "PanelDialog.mqh"


CPanelDialog ExtDialog;
input string b1="-=== OPTION ===-"; 
input double LotSize=0.10;          // Lot Size Default
input int MagikNumb=123456;         // Magic Number (change when use in other chart)
input uint RTOTAL = 5;              // Retries to close order and pending order 
input uint SLEEPTIME      = 1000;
input bool InpAsyncMode   = true;
input bool InpDisAlgo     = false;  
input int  limitStartPipS = 10;     // Limit Open from/to 10 pips actual price
input int  PipsBreakEven = 2;       // How many pips of Break Even
input int  PipsBreakEvenTrigger = 10; // When Trigger BE , afetr 10 pips

extern COrderExecutor *orderExecutor;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//--- create application dialog
   if(!ExtDialog.Create(0,"Trade Manager",150,50,50,450,460))
     return(INIT_FAILED);
//--- run application
   if(!ExtDialog.Run())
     return(INIT_FAILED);
//--- succeed
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy application dialog
   ExtDialog.Destroy(reason);
  }
  
  
  void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   ExtDialog.ChartEvent(id,lparam,dparam,sparam);
  }
//+------------------------------------------------------------------+


   void OnTick()
    {
        // Controlla se uno dei due ordini è stato eseguito e cancella l'altro
         orderExecutor.CheckAndCancelOrders();
    }

my error

'COrderExecutor' - declaration without type     PanelDialog.mqh 23      8
'COrderExecutor' - unexpected token, probably type is missing?  PanelDialog.mqh 25      1
'*' - semicolon expected        PanelDialog.mqh 25      16
 

Is it supposed to manage 1 order at a time ?

if yes :

remove these in PanelDialog.mqh

extern COrderExecutor *orderExecutor;

COrderExecutor *orderExecutor = NULL;

replace with 

class COrderExecutor;

then after the class body of the COrderExecutor in PanelDialog.mqh , add

COrderExecutor orderExecutor;

In the function 

void CPanelDialog::OnClickButtonExEcuteOCO(void)

remove the :

  COrderExecutor orderExecutor(lotSize, entryPrice1, stopLoss1, takeProfit1,
                                  entryPrice2, stopLoss2, takeProfit2, orderType1, orderType2);

replace with :

  COrderExecutor temp(lotSize, entryPrice1, stopLoss1, takeProfit1,
                                  entryPrice2, stopLoss2, takeProfit2, orderType1, orderType2);

orderExecutor=temp;

on TradeManager.mq5 delete :

extern COrderExecutor *orderExecutor;

in general have a setup and reset function for your classes , will make your life easier

 
Lorentzos Roussos #:

Is it supposed to manage 1 order at a time ?

if yes :

remove these in PanelDialog.mqh

replace with 

then after the class body of the COrderExecutor in PanelDialog.mqh , add

In the function 

remove the :

replace with :

on TradeManager.mq5 delete :

in general have a setup and reset function for your classes , will make your life easier

thanks for rply , i did do your mod , but return me this error 'COrderExecutor' - wrong parameters count    PanelDialog.mqh    2168    22
   in this part  

then after the class body of the COrderExecutor in PanelDialog.mqh , add

COrderExecutor orderExecutor;
do you have some idea   thanks  again 
 
Stefano Cerbioni #:
thanks for rply , i did do your mod , but return me this error 'COrderExecutor' - wrong parameters count    PanelDialog.mqh    2168    22
   in this part  

then after the class body of the COrderExecutor in PanelDialog.mqh , add

do you have some idea   thanks  again 

you don't have a parameterless constructor. you need pass the parameters or if you don't have the values available in global context you need instantiate it with new operator when you can (in OnInit or whatever).

*or add a default constructor to your class and create a function to set/change the parameters.

Documentation on MQL5: Language Basics / Operators / Object Create Operator new
Documentation on MQL5: Language Basics / Operators / Object Create Operator new
  • www.mql5.com
The new operator automatically creates an object of a corresponding size, calls the object constructor and returns a descriptor of created object ...
 
Samuel Manoel De Souza #:

you don't have a parameterless constructor. you need pass the parameters or if you don't have the values available in global context you need instantiate it with new operator when you can (in OnInit or whatever).

*or add a default constructor to your class and create a function to set/change the parameters.

sorry  but  i understund little  without example , i am not very good  with class istance new . somthing  method  , is  first time for me  sorry if  could give me example you are  welcome .thanks  again for you rply i try tounderstund