Ontick inside void ontick()-manual trade is alrrady open

 

Hello everyone. I'm sorry if i ask questions so basic I'm new to this. But i just want to know it this is possible. I actually want , one ma cross, and then wait for another ma to cross then close the trade. Thank you everyone in advance.

void OnTick()
{
  If (ma100_1hr < ma50_1hr);

         Ontick()

            If (ma100_15m > ma50_15m);

             //i dont know the actual way of closing it but lets say close the trade
     
} 
Improperly formatted code removed by moderator.
 

Please use the CODE button (Alt-S) when inserting code in your posts.

Code button in editor

 
Progmt4: I'm sorry if i ask questions so basic I'm new to this. But i just want to know it this is possible. I actually want , one ma cross, and then wait for another ma to cross then close the trade.

No, don't ever do that! OnTick() is an event handler that is called by the terminal when a New Tick event occurs.

It should never be called directly by your code. Learn to correctly structure your code logic and handle the terminal's events properly.

 
Fernando Carreiro #:

No, don't ever do that! OnTick() is an event handler that is called by the terminal when a New Tick event occurs.

It should never be called directly by your code. Learn to correctly structure your code logic and handle the terminal's events properly.

Thanks
 
Progmt4: I actually want , one ma cross, and then wait for another ma to cross then close the trade.

Nothing is going to change inside the event handler. You must note your condition, return and wait for a new tick, and then check for the other condition.
          Multiple conditions for taking a trade. - Expert Advisors and Automated Trading - MQL5 programming forum (2023)

 
William Roeder #:

Nothing is going to change inside the event handler. You must note your condition, return and wait for a new tick, and then check for the other condition.
          Multiple conditions for taking a trade. - Expert Advisors and Automated Trading - MQL5 programming forum (2023)

Hi sir, i tried this one but, it did not work. can you help me with this. thanks you and i really appreciate it.


//curent chart, current period, 50 candles,no shift, simple, close price
double SlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,0);
      
//curent chart, current period, 50 candles,no shift, simple, close price
double LastSlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,1);
      
//curent chart, current period, 10 candles,no shift, simple, close price
double FastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,0);
      
//curent chart, current period, 10 candles,no shift, simple, close price
double LastFastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,1);
      
//curent chart, current period, 50 candles,no shift, simple, close price
double SlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,0);
      
//curent chart, current period, 50 candles,no shift, simple, close price
double LastSlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,1);
      
//curent chart, current period, 10 candles,no shift, simple, close price
double FastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,0);
      
//curent chart, current period, 10 candles,no shift, simple, close price
double LastFastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,1);


bool firstpart_buy = LastFastMovingAverage15 < LastSlowMovingAverage15
         && FastMovingAverage15 > SlowMovingAverage15 ;
         
bool firstpart_sell = LastFastMovingAverage15 > LastSlowMovingAverage15
         && FastMovingAverage15 < SlowMovingAverage15 ;
         
bool secondpart_buy =  LastFastMovingAverage > LastSlowMovingAverage 
         && FastMovingAverage < SlowMovingAverage ;
         
bool secondpart_sell = LastFastMovingAverage < LastSlowMovingAverage 
         && FastMovingAverage > SlowMovingAverage ; 



void OnTick()
  {
  
      enum State {idle, c1b,c1s,c2b,c2s};
      static State state =  idle;
    
      if (firstpart_buy == true)  
      {    
         if(state == idle ) state = c2b;
         
       }   
       
        
       
     
      if (firstpart_sell == true)  
      {
      
         if(state == idle) state = c2s;
         
      }
      
      
      if (secondpart_buy == true)
      {
      
         if(state == c2b) close_buy();
            
         if(state == idle) return;
            
      }
      
      
      if (secondpart_sell == true)
      {
         if(state == c2s) close_sell();
         
         if(state == idle) return;
      
      }
      
      
      
      
  }
  
  
  
 void close_buy()
 {
   for (int i= OrdersTotal(); i>=0; i--)
            {
               if (OrderSelect(i, SELECT_BY_POS)== true)
               
               //if (OrderSymbol() == Symbol())
               
               OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Green );
            }    
}


void close_sell()
{
   for (int c= OrdersTotal(); c>=0; c--)
            {
               if (OrderSelect(c, SELECT_BY_POS)== true)
               
               //if (OrderSymbol() == Symbol())
               
               OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Red );
            }
}
  

  
 
Progmt4 #:Hi sir, i tried this one but, it did not work. can you help me with this. thanks you and i really appreciate it.

Your entire first section should not be in the global scope. It should be inside the OnTick() event handler, or in some function called from within OnTick().

You were doing it correctly before, so why did you now decide to put it in the global scope?

EDIT: I suggest you take some to first learn the basics of programming, for example in a language like C/C++.

 
Thank you for your help. how about this one. really appreciate the help.  how about this. will the value of state changes in every  tick as it is now in local/static or will the code work. thanks, really appreciate the help.
void OnTick()
  {
  
      //curent chart, current period, 50 candles,no shift, simple, close price
      double SlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,0);
      
      //curent chart, current period, 50 candles,no shift, simple, close price
      double LastSlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,1);
            
      //curent chart, current period, 10 candles,no shift, simple, close price
      double FastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,0);
            
      //curent chart, current period, 10 candles,no shift, simple, close price
      double LastFastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,1);
            
      //curent chart, current period, 50 candles,no shift, simple, close price
      double SlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,0);
            
      //curent chart, current period, 50 candles,no shift, simple, close price
      double LastSlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,1);
            
      //curent chart, current period, 10 candles,no shift, simple, close price
      double FastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,0);
            
      //curent chart, current period, 10 candles,no shift, simple, close price
      double LastFastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,1);
      
      
      bool firstpart_buy = LastFastMovingAverage15 < LastSlowMovingAverage15
               && FastMovingAverage15 > SlowMovingAverage15 ;
               
      bool firstpart_sell = LastFastMovingAverage15 > LastSlowMovingAverage15
               && FastMovingAverage15 < SlowMovingAverage15 ;
               
      bool secondpart_buy =  LastFastMovingAverage > LastSlowMovingAverage 
               && FastMovingAverage < SlowMovingAverage ;
               
      bool secondpart_sell = LastFastMovingAverage < LastSlowMovingAverage 
               && FastMovingAverage > SlowMovingAverage ; 
  
      enum State {idle, c1b,c1s,c2b,c2s};
      static State state =  idle;
    
      if (firstpart_buy == true)  
      {    
         if(state == idle ) state = c2b;
        
         
       }   
       
        
      if (firstpart_sell == true)  
      {
      
         if(state == idle) state = c2s;
       
      }
      
      
      if (secondpart_buy == true)
      {
      
         if(state == c2b)
         {
            for (int i= OrdersTotal(); i>=0; i--)
                  {
                     if (OrderSelect(i, SELECT_BY_POS)== true)
                     
                     //if (OrderSymbol() == Symbol())
                     
                     OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Green );
                  }     
         }
            
         if(state == idle) return;
            
      }
      
      
      if (secondpart_sell == true)
      {
         if(state == c2s) 
         {
            for (int c= OrdersTotal(); c>=0; c--)
                  {
                     if (OrderSelect(c, SELECT_BY_POS)== true)
                     
                     //if (OrderSymbol() == Symbol())
                     
                     OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Green );
                  }     
         }
         
         
         if(state == idle) return;
      
      }
      
     
  }
 
Progmt4 #: Hi sir, i tried this one but, it did not work. can you help me with this. thanks you and i really appreciate it.
double FastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,0);
double LastFastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,1);
double SlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,0);
double LastSlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,1);
double FastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,0);
double LastFastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,1);
bool firstpart_buy = LastFastMovingAverage15 < LastSlowMovingAverage15 && FastMovingAverage15 > SlowMovingAverage15 ;
bool firstpart_sell = LastFastMovingAverage15 > LastSlowMovingAverage15 && FastMovingAverage15 < SlowMovingAverage15 ;
bool secondpart_buy =  LastFastMovingAverage > LastSlowMovingAverage  && FastMovingAverage < SlowMovingAverage ;         
bool secondpart_sell = LastFastMovingAverage < LastSlowMovingAverage  && FastMovingAverage > SlowMovingAverage ; 

Those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 (2013)

Reason: