The ea doesnt run, stay freeze.

To add comments, please log in or register
theforce
33
theforce  

Hi everyone, i am trying to run a EA conditioning to open just one trade per trading criteria.


trading criteria: 

1. If the Moving Average of 20 crossover to upside the 50 Moving Average, excute a OP_BUY, if there is an OP_SELL at that moment, close it.

2. If the Moving Average of 20 crossover to downside the 50 Moving Average, excute a OP_SELL, if there is an OP_BUY at that moment, close it.


I suspect the code is staying freeze, because of this:


for(int i=1; i>=OrdersTotal(); i++)
 {   
  if (OrderSelect(i-1,SELECT_BY_POS)==true)
  {
    total++;

  if(total <1 )
   
   {
    return;    
   }
   tip = OrderType();
  }
 }


This is the code: 


extern int period_ma1 = 50,
           period_ma2= 20;
          

bool open_b = false,
     open_s = false,
     close_b = false,
     close_s = false,
     ans = false; 
    
int ticket,
    total= 0,
    tip = -1;    
      
int start()

{

//Orders Control//

for(int i=1; i>=OrdersTotal(); i++)
 {   
  if (OrderSelect(i-1,SELECT_BY_POS)==true)
  {
    total++;

  if(total <1 )
   
   {
    return;    
   }
   tip = OrderType();
  }
 }


//trading criteria//

double MA1 = iMA(NULL,0,period_ma1,0,MODE_SMA, PRICE_CLOSE,0);

double MA11 = iMA(NULL,0,period_ma1,1,MODE_SMA, PRICE_CLOSE,0);

double MA2 = iMA(NULL,0,period_ma2,0,MODE_SMA, PRICE_CLOSE,0);

double MA22 = iMA(NULL,0,period_ma2,1,MODE_SMA, PRICE_CLOSE,0);


if( MA11 > MA22 && MA1 < MA2) 

 {

  open_b = True;

  close_s = True;

 }

if (MA11 < MA22 && MA1 > MA2)

 { 
 
  open_s = True;

  close_b = True;

 }
 
 //Closing trade criteria//

  while(true)                                    {      if( tip == 0 && close_b == true) {   ans = OrderClose(ticket,0.01, Bid,2);    } return; } while(true) { if( tip == 1 && close_s == True) {   ans = OrderClose(ticket,0.01, Ask,2); } return; } //opening trade criteria// while(true) { if(total == 0 && open_s == true) { ticket = OrderSend(NULL, OP_SELL,0.01,Bid,2,0,0); } return; } while(true) { if( total ==0 && open_b == true) { ticket = OrderSend(NULL, OP_BUY,0.01,Ask ,2,0,0); } return; } return; }
Sergey Golubev
Moderator
106721
Sergey Golubev  

I just hope someone will help (I am not a coder sorry).

If not so you may go to Freelance service for example.

------------------

Freelance


The forum

  1. What MQL5 developers think about the Freelance service 
  2. MQL5.com Freelance: Developers' Source of Income (Infographic) 
  3. Freelance, decompilation, the rules, examples of the decompiled code - the thread with examples
  4. Some new Freelance services (translation, consultation, converting) - the post.
  5. Freelance service new webdesign - the post

The articles

  1. How to implement traders' orders and make a profit in the MQL5 Freelance service 
  2. Freelance Jobs on MQL5.com - Developer's Favorite Place 
  3. MQL5.com Freelance: Developers' Source of Income (Infographic) 
  4. Do Traders Need Services From Developers?
  5. A Few Tips for First-Time Customers
============
whroeder1
17928
whroeder1  
  1. for(int i=1; i>=OrdersTotal(); i++)
     {   
      if (OrderSelect(i-1,SELECT_BY_POS)==true)
    If there are no open orders the loop doesn't run since 1>=0 is false. If there is one open order the loop runs forever 1>=1 is true and 2>=1 is true and 2147483647>=1 is also true.

  2. int total= 0;    
    int start(){  
       for(int i=1; i>=OrdersTotal(); i++) if (OrderSelect(i-1,SELECT_BY_POS)==true){
          total++;
          if(total <1 ){ return;    }
    When will that return ever execute?
  3. After the first time, when will total ever get reset to zero?
  4. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  5.  ans = OrderClose(ticket,0.01, Ask,2);
    ticket = OrderSend(NULL, OP_SELL,0.01,Bid,2,0,0);
    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  6. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

  7. if( MA11 > MA22 && MA1 < MA2) {
      open_b = True;
      close_s = True;
    }
    if( total ==0 && open_b == true)
    You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  8. What is the value of open_b if that if statement is not true?
To add comments, please log in or register