Help with my EA. - page 3

 
Alain Verleyen:
That's not exact. OrderClosePrice() returns the bid/ask and it's best practice to use it with OrderClose().
Thank you for your comment. I didn't know it before. The documentation doesn't describe it.
 
whroeder1:

  1. You can use OrderClosePrice() instead of Bid/Ask in MT4.
              www.mql5.com/en/forum/158890/page3#comment_3812636

Thank you for your comment and link. Maybe I'm wrong but years ago it didn't work. Has there been a change?

 
FernandoBorea:
 

Here's the coding:

I've edited your code a little bit. Just for study purpose ;-)

extern int MASlow   = 30;
extern int MAFast   = 25;
extern int MASignal = 10;
extern double Lots  = 0.01;
extern double StopLoss = 30; // StopLoss (0 = Cerrar en senal contraria).
extern double TakeProfit = 30; // TakeProfit (0 = Cerrar en senal contraria).
extern int MagicNumber = 1111;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {       
   double SlowMA = iMA(Symbol(),Period(),MASlow,0,MODE_LWMA,PRICE_CLOSE,1);
   double FastMA = iMA(Symbol(),Period(),MAFast,0,MODE_LWMA,PRICE_CLOSE,1);
   double SignalMA = iMA(Symbol(),Period(),MASignal,0,MODE_LWMA,PRICE_CLOSE,1);
   double PrevSlowMA = iMA(Symbol(),Period(),MASlow,0,MODE_LWMA,PRICE_CLOSE,2);
   double PrevFastMA = iMA(Symbol(),Period(),MAFast,0,MODE_LWMA,PRICE_CLOSE,2);
   double PrevSignalMA = iMA(Symbol(),Period(),MASignal,0,MODE_LWMA,PRICE_CLOSE,2);    
   static int ticket = 0;
//=====Revisar trades=====//    
   bool res;
   res = OrderSelect(ticket,SELECT_BY_TICKET);
   //res&=OrderCloseTime()>0;
//====Enviar trades====//        
   if(!res)
     {
      if (SignalMA > FastMA && SignalMA > SlowMA && !(PrevSignalMA > PrevFastMA && PrevSignalMA > PrevSlowMA))
        {
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,50,0.0,0.0,"RKSDTV",MagicNumber);
         if(ticket < 0)
           {
            Alert("Error enviando orden de compra");
            Alert("Codigo del error: ",GetLastError());
           }
        } 
      else if (SignalMA < FastMA && SignalMA < SlowMA && !(PrevSignalMA < PrevFastMA && PrevSignalMA < PrevSlowMA))
        {
         ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,50,0.0,0.0,"RKSDTV",MagicNumber);
         if(ticket < 0 )
           {
            Alert("Error enviando orden de venta");
            Alert("Codigo del error: ",GetLastError());
           }
        }
     }  
   else
     {
      if(OrderMagicNumber() == MagicNumber && OrderCloseTime() == 0)
        {
         if(OrderType() == OP_BUY)
           {
            if(SignalMA < FastMA && SignalMA < SlowMA)
              {
               if(!OrderClose(ticket,OrderLots(),OrderClosePrice(),50))
                 {
                  Alert("Error cerrando orden #",ticket);
                  Alert("Codigo del error: ",GetLastError());
                 }
               else ticket=0;
              }
           }
         else if(OrderType() == OP_SELL)
           {
            if(SignalMA > FastMA && SignalMA > SlowMA)
              {
               if(!OrderClose(ticket,OrderLots(),OrderClosePrice(),50))
                 {
                  Alert("Error cerrando orden #",ticket);
                  Alert("Codigo del error: ",GetLastError());
                 }
               else ticket=0;
              }
           }
        }     
     }
  }
 
whroeder1:
  1. Using Points means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Slippage defined in index points - Currency Pairs - Expert Advisors and Automated Trading - MQL5 programming forum

What a PIP is??? I know what a PIP mean within currencies but could you explain me what a PIP is within metals, indexes...? I always use TickSize and I'm satisfied with that. Or is there a risk with using TickSize?

For example Crude Oil from my broker:

Digits after decimal point:                 3
Point size in the quote currency:       0.001
Tick size in points:                            0.010

 
Petr Nosek:

OK, but you never use "Pips" in your code behind the block.

I moved the conversion stuff to the OnInit() block, this is a better way to code it?

 
Petr Nosek:

For example:

Thanks! I'll try that way!

 
Petr Nosek:
Is there any reason for using Pips? I don't think so.

I'm planning to make the previous Lots structure on SL and TP so it would have no problem when both are set to 0, maybe I'll use it to the slippage too but not sure yet how to properly structure it.

 
whroeder1:
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Static/global variables are initialized once, on program load, with a constant. Your variables are bogus and never change. Drop the static.

  3. You can use OrderClosePrice() instead of Bid/Ask in MT4.
              www.mql5.com/en/forum/158890/page3#comment_3812636

  4. 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.

  5. 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

  6. Using Points means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Slippage defined in index points - Currency Pairs - Expert Advisors and Automated Trading - MQL5 programming forum

Thanks!

How do I select a specific part of a comment to reply to it? As you did on each number:)

What do you suggest to do on the pip part?

EDIT: I read the code on the link you posted but didn't understood it, would you explain it?

 
Petr Nosek: I know what a PIP mean within currencies but could you explain me what a PIP is within metals, indexes...?

If you had bothered to read the second link provided in #20.6 you would see that my synthetic PIP starts with tick size.

 
Petr Nosek:

I've edited your code a little bit. Just for study purpose ;-)

Thanks! I'll edit on my own then compare it to that one! I think it could be a good way to learn from it:D

Reason: