Objects instantiated in OnInit does not work in OnTick

 
Hello all.
I have 3 objects instantiated from a class in OnInit and I need to use these Objects in OnTick too, However without instantiating them again in OnTick it is not possible.
I have tried passing them by reference to OnTIck and that did not work either(in this case the OnTick function does not execute at all).
I had 3 different constructors for OnInit,OnDeinit and OnTick at the beginning(and this worked just fine) but now I want to have just one constructor.
Thank you in advance.
 
Mobin9179:
Hello all.
I have 3 objects instantiated from a class in OnInit and I need to use these Objects in OnTick too, However without instantiating them again in OnTick it is not possible.
I have tried passing them by reference to OnTIck and that did not work either(in this case the OnTick function does not execute at all).
I had 3 different constructors for OnInit,OnDeinit and OnTick at the beginning(and this worked just fine) but now I want to have just one constructor.
Thank you in advance.
Code please...

Nothing shown to help you with.
 
Dominik Christian Egert #:
Code please...

Nothing shown to help you with.
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include<MA.mqh>
#include <Trade\Trade.mqh>
#include <Money_Management.mqh>
//#include <RSIClass.mqh>

CTrade Trade;
MqlTradeRequest Request;

/*enum CustomEnum{
   val1 = 0, //Forex
   val2 = 1, //Material
   val3 = 2 //Stock
};
input CustomEnum val = val1;*/

double Balance = AccountInfoDouble(ACCOUNT_BALANCE);

input bool reverse1buy = false;
input bool reverse2buy = false;
input bool reverse3buy = false;
input bool reverse1sell = false;
input bool reverse2sell = false;
input bool reverse3sell = false;

Money_Management MM(risk);
input int SL = 100; //SL
input int TP = 100; //TP


void OnInit()
{
   
   TradeMA MA_1(days1);
   TradeMA MA_2(days2);
   TradeMA MA_3(days3);
   //CRSIClass RSI_1(InpPeriodRSI);
   
};

void OnDeinit(const int reason)
  {
      TradeMA MA_1();
      TradeMA MA_2();
      TradeMA MA_3(); 
      Alert("OnDeinit Worked");
  };

void OnTick(TradeMA &MA_1 , TradeMA &MA_2 , TradeMA &MA_3)
  {
  Print("ONTICK WORKING");
     
   //TradeMA MA_1(days1);
   //TradeMA MA_2(days2);
   //TradeMA MA_3(days3);
   
   MA_1.TradeMAOnTick();
   MA_2.TradeMAOnTick();
   MA_3.TradeMAOnTick();
   //RSI_1.OnCalculate();
   
   bool ma1buy = MA_1.aboveSetter();
   if (reverse1buy)
   {
      ma1buy = !ma1buy;
   }; 
   bool ma2buy = MA_2.aboveSetter();
   if (reverse2buy)
   {
      ma2buy = !ma2buy;
   }; 
   bool ma3buy = MA_3.aboveSetter();
   if (reverse3buy)
   {
      ma3buy = !ma3buy;
   }; 
  
   
   bool ma1sell = MA_1.belowSetter();
   if (reverse1sell)
   {
      ma1sell = !ma1sell;
   }; 
   bool ma2sell = MA_2.belowSetter();
   if (reverse2sell)
   {
      ma2sell = !ma2sell;
   }; 
   bool ma3sell = MA_3.belowSetter();
   if (reverse3sell)
   {
      ma3sell = !ma3sell;
   }; 
  
   bool buy = ma1buy && ma2buy && ma3buy;
   Alert("ma1buy = " + ma1buy + "   ma2buy =" + ma2buy + "   ma3buy = " + ma3buy);
   bool sell = ma1sell&& ma2sell && ma3sell;
   

   if(buy && PositionsTotal() == 0  )
      {
         double Ask =SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         Trade.Buy(MM.VolumeCalculator(SL),NULL,Ask,Ask - SL*_Point, Ask + TP*_Point,NULL);
         if(Trade.ResultRetcode() == 10009 || Trade.ResultRetcode() == 10008)
         {           
            Print("Buy Order Placed");
            Print("New balance is: " + AccountInfoDouble(ACCOUNT_BALANCE));
         }
         else
         {
             Print("Buy Order could not be placed" , Trade.ResultRetcode());
         }  
      };
    if( sell && PositionsTotal() == 0 )
    {
        double Bid =SymbolInfoDouble(_Symbol,SYMBOL_BID);
        Trade.Sell(MM.VolumeCalculator(SL),NULL,Bid,Bid + SL*_Point, Bid - TP*_Point,NULL);
        if(Trade.ResultRetcode() == 10009 || Trade.ResultRetcode() == 10008)
         {           
            Print("Buy Order Placed");
            Print("New balance is: " + AccountInfoDouble(ACCOUNT_BALANCE));
         }
          else
         {
           Print("Sell order could not be placed" , Trade.ResultRetcode());
         }
    }   
  };
 
Mobin9179 #:
//+------------------------------------------------------------------+
//|                                               Moving_Avergae.mqh |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
input int days1 = 7; //First MAperiod
input int days2 = 14; // Second MAperiod
input int days3 = 21;//Third MAperiod

input ENUM_MA_METHOD method = MODE_EMA; //MA calculation method
input ENUM_TIMEFRAMES  TimeFrame =  PERIOD_CURRENT; //TimeFrame

class TradeMA
  {
   private:
      int MA_handle;
      int TagNumber;
      double MAarray[];
      int MAperiod;
      bool above;
      bool below;
      MqlTick Tick;
      MqlRates Rates[];
      MqlTradeRequest TradeRequest;
      MqlTradeResult TradeResult;
    
   public:
//------------------------------------------------------------ONINIT CONSTRUCTOR
      TradeMA(int days)
         :MAperiod(days)
      {
         SetPeriodDays(days);
         HandlerFiller(MAperiod , method , TimeFrame);
         DispMessage();
      };
//---------------------------------------------------------------ONDEINIT CONSTRUCTOR
      TradeMA()
      {
         IndicatorRelease(MA_handle);
      };
//-----------------------------------------------------------------ONTICK CONSTRUCTOR
      /*
      TradeMA(int days , int TagNumber)
         : MAperiod(days)
      {
         //HandlerFiller(MAperiod);
         /*CopyRates(_Symbol,TimeFrame,0,MAperiod,Rates);
         ArraySetAsSeries(Rates, 1);
         ArraySetAsSeries(MAarray, 1);
         CopyBuffer(MA_handle,0,0,3,MAarray);
         SymbolInfoTick(_Symbol,Tick);
         aboveSetter();
         belowSetter();*/       
      //};
      
//------------------------------------------------------------AUXILLARY METHODES
      void SetPeriodDays(int days)
      {
         MAperiod = days;
      };

      void HandlerFiller(int MAperiod , ENUM_MA_METHOD method , ENUM_TIMEFRAMES TimeFrame) 
      {
         MA_handle = iMA(_Symbol,TimeFrame,MAperiod,0,method,PRICE_CLOSE);
         if(MA_handle < 0 )
         {
            Alert("Handler could'nt be created" , GetLastError());
         }
         else
         {
            Print("Handler succesfully made");
         }
      };

      void DispMessage()const
      {
         Print("Hello and welcome to the " , _Symbol , " chart");
      };

      bool aboveSetter()
      {           
         return above = MAarray[0] < iClose(_Symbol,TimeFrame,0);          
      };

      bool belowSetter()
      {
         return below = MAarray[0] > iClose(_Symbol,TimeFrame,0);            
      };
      
      void TradeMAOnTick()
      {
         CopyRates(_Symbol,TimeFrame,0,MAperiod,Rates);
         ArraySetAsSeries(Rates, 1);
         ArraySetAsSeries(MAarray, 1);
         CopyBuffer(MA_handle,0,0,3,MAarray);
         SymbolInfoTick(_Symbol,Tick);
         //aboveSetter();
         //belowSetter();
      }      
      
      //~TradeMA();
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

 
Mobin9179 #:

OK, you are creating the objects in a local scope, they loose their validity on leaving the scope.

A function has its local scope, given by {}. - Move your Objects to global scope, or make your Objects behave like signletons (advanced, and unnecessary here)

   TradeMA MA_1(days1);
   TradeMA MA_2(days2);
   TradeMA MA_3(days3);
   //CRSIClass RSI_1(InpPeriodRSI);

void OnInit()
{   
};

void OnDeinit(const int reason)
  {
      Alert("OnDeinit Worked");
  };

void OnTick()
  {
  Print("ONTICK WORKING");
     
   //TradeMA MA_1(days1);
   //TradeMA MA_2(days2);
   //TradeMA MA_3(days3);
   
   MA_1.TradeMAOnTick();
   MA_2.TradeMAOnTick();
   MA_3.TradeMAOnTick();
   //RSI_1.OnCalculate();
   
[...]


If you change the signature of OnTick(), the terminal is not able to find your OnTick-Function.

void OnTick(TradeMA &MA_1 , TradeMA &MA_2 , TradeMA &MA_3)

This is a function with  the name OnTick(), but its signature does not match, therefore it wont be called.

 
Dominik Egert #:

OK, you are creating the objects in a local scope, they loose their validity on leaving the scope.

A function has its local scope, given by {}. - Move your Objects to global scope, or make your Objects behave like signletons (advanced, and unnecessary here)


If you change the signature of OnTick(), the terminal is not able to find your OnTick-Function.

This is a function with  the name OnTick(), but its signature does not match, therefore it wont be called.

Thank you so much