Help with IF ELSE

 

Hey everyone.  I am very new to MQL and am just stuck with playing around with a very simple EA I'm trying to write.  It is just supposed to execute an order when the MA is below the Bid price.  In addition I am hoping that it doesn't let additional orders open if there is one already open.  

Here is what I have.  It's sloppy, but I have been stuck on this and I think I'm overlooking something.  Thanks in advance.


extern double  Lots = 1.0;

extern int     Slip = 10;

extern int     MA_PERIOD = 20;

extern int     StopLoss = 20;

extern int     TakeProfit = 50;

extern int     SleepTime = 5000;


void OnTick()

  {

  int ticket = 1;

if(OrderSelect(ticket,SELECT_BY_TICKET) == false)

   {

   double MA =iMA(Symbol(), Period(), MA_PERIOD, 0, 0, 0, 1);

   Sleep(SleepTime);

   Alert("MA" + string(MA));

   Alert("BID" + string(Bid));

   if(MA < Bid)

      {

      ticket = OrderSend(Symbol(), OP_BUY, Lots, Bid, Slip, Bid-StopLoss*Point, Bid+TakeProfit*Point, "Sent by Practice_1");

      if(ticket < 0)

         {

         Alert("Error executing order");

         }

         else

         {

         Alert("Ticket number is " + string(ticket));

         bool SelectTicket;

         SelectTicket = OrderSelect(ticket, SELECT_BY_TICKET);

         }  

      }

}  

  }


 
pjk1413:

Hey everyone.  I am very new to MQL and am just stuck with playing around with a very simple EA I'm trying to write.  It is just supposed to execute an order when the MA is below the Bid price.  In addition I am hoping that it doesn't let additional orders open if there is one already open.  

Hello. I think you should start from the very beginning. It is here: https://book.mql4.com

MQL4 Tutorial
MQL4 Tutorial
  • book.mql4.com
Nowadays, a personal computer became indispensable for everybody. The rapid development of Internet and performance of modern computers opened up new vistas in many fields of human activities. As early as ten years ago, the financial market trade was available only for banks and for a limited community of specialists. Today, anybody can join...
 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your (original) post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2.  int ticket = 1;
    if(OrderSelect(ticket,SELECT_BY_TICKET) == false)
    1. Only in the tester does ticket numbers start with one. Therefor this if will always be true.
    2. If you change ticket to static, then you must test if the ticket has closed.
    3. 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.

    Use a proper OrderSelect loop
    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
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
  3.  bool SelectTicket;
     SelectTicket = OrderSelect(ticket, SELECT_BY_TICKET);
    What do you think that does?

  4.  Sleep(SleepTime);
    1) Why are you sleeping? 2) After Sleep and between server calls the market will have changed. You must update your variables. To use any pre-defined variables and series arrays you must call RefreshRates
              RefreshRates - Timeseries and Indicators Access - MQL4 Reference

  5. extern int     StopLoss = 20;
    extern int     TakeProfit = 50;
          ticket = OrderSend(Symbol(), OP_BUY, Lots, Bid, Slip, Bid-StopLoss*Point, Bid+TakeProfit*Point
    You buy at the Ask and sell at the Bid.
    • Your buy order's TP/SL are triggered when the Bid reaches it. Not the Ask.
    • Your sell order's TP/SL will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 and MetaTrader 4 - MQL4 programming forum - Page 3
    • The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools -> Options {control-O} -> charts -> Show ask line.)

  6. Assuming a 5 digit broker, a 2.0 pip stop is very small. You can't move stops (or pending prices) closer to the market than the minimum (MODE_STOPLEVEL * _Point.)
              Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
              What is a TICK? - MQL4 and MetaTrader 4 - MQL4 programming forum

Reason: