Download MetaTrader 5

Error while programming EA - Every tick makes a trade!

To add comments, please log in or register
Leo
86
Leo  

Hi everyone!

I am new of MQL4 code world. I come from a C++ background and I am trying to learn MQL4 language & conventions.

I am writing a simple Expert Advisor (my first of all!). It compiles but, when I test it, it ends with "every tick a trade". I attach code to better understand what I am trying to do:

//+------------------------------------------------------------------+

//|                                                MyFirstExpert.mq4 |

//|                                                  Leonardo Urbano |

//|                            http://investinmarkets.altervista.org |

//+------------------------------------------------------------------+


#property copyright "Leonardo Urbano"

#property link      "http://investinmarkets.altervista.org"

#property version   "1.00"

#property strict


sinput string Bar = "Bar Count Settings";

input int BarCount = 3;

int Ticket = 0;


//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  { 

     bool is_p = false;     

     if (Ticket == 0)

     {

        if (Close[1] > Close[2]) is_p = true;

     }

     

     else 

     {

        if (Close[1] < Close[2]) is_p = false;

     }

     

     if (is_p == true && Ticket == 0)

     {

        Ticket = OrderSend(_Symbol,OP_SELL,0.1,Bid,0,0,0,"Sell Order Custom",110,0,clrRed);

        Alert("Sell order opened due to match found.");

        Comment("Sell order opened #"+Ticket+".");

     }

     

     if (Ticket != 0 && is_p == false)

     {

        bool select = OrderSelect(Ticket,SELECT_BY_TICKET);

        bool close = OrderClose(Ticket,OrderLots(),Ask,0,clrGreen);

        if (close == true)

        {

           Alert("Sell order closed.");

           Comment("Sell order closed #"+Ticket+".");

           Ticket = 0;

        }

        else

        {

           Alert("Impossible to close order.");

        }

     }

  }

I want to simply compare last and second to last bars and, if are both positive then open a sell order (just this case for the moment). If opened, the next bar check if still positive, if not close the trade.

I am getting a trade for every tick!

Thank you in advance! 

whroeder1
15582
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. Your price check should not be dependent on whether a order is opened.
      if (Ticket == 0){
            if (Close[1] > Close[2]) is_p = true;
         } else {
            if (Close[1] < Close[2]) is_p = false;
         }
    Remove ticket and simplify.
    is_p = Close[1] > Close[2]);
    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. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
Leo
86
Leo  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Check your return codes.What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. Your price check should not be dependent on whether a order is opened.
    Remove ticket and simplify.
    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. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

I have already done what you suggested thanks! Sometimes abbreviations in MQL are not the same as C++ 14 ones so  I preferred to keep code verbose but "surely working". I was looking for code snippet tool while posting and I did not think to attach the code. Next time I will follow these rules. Many thanks! 
To add comments, please log in or register