Help with my EA. - page 2

 
Petr Nosek:

Because you don't check crossover but just the position of SignalMA. If you want to check crossover you should compare the previous position with the current position. 

Any suggestions? How can I make the EA differentiate between position of SIGNALMA and SIGNALMA crossover?
 
Petr Nosek:
OrderClosePrice() returns close price. If you close the order for example at 1.23000 the OrderClosePrice() will return 1.23000. If the order is still open the OrderClosePrice() returns 0.0 . Similar to OrderCloseTime()
I was told that ClosePrice if the order stills running will return the price that would close at if it's closed at that moment
 
Petr Nosek:

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

Yep,  I just noticed that I posted a draft version, later this day I'll update with the actual "final" code before modifying anything upon the suggestions as I want to learn more about why it's bad before making any changes so I can learn more:D
 
FernandoBorea:
 
Could you please explain me the difference between using (!res) and then else? What does " ! " Do?

! is a negation.

https://www.mql5.com/en/docs/basis/operators/if

Documentation on MQL5: Language Basics / Operators / Conditional Operator if-else
Documentation on MQL5: Language Basics / Operators / Conditional Operator if-else
  • www.mql5.com
If the expression is true, operator1 is executed and control is given to the operator that follows operator2 (operator2 is not executed). If the expression is false, operator2 is executed.
 
FernandoBorea:
I was told that ClosePrice if the order stills running will return the price that would close at if it's closed at that moment

You should want your money back :D

https://docs.mql4.com/trading/ordercloseprice

OrderClosePrice - Trade Functions - MQL4 Reference
OrderClosePrice - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderClosePrice - Trade Functions - MQL4 Reference
 
FernandoBorea:
Any suggestions? How can I make the EA differentiate between position of SIGNALMA and SIGNALMA crossover?

For example:

    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);
...
        if(SignalMA > FastMA && SignalMA > SlowMA && !(PrevSignalMA > PrevFastMA && PrevSignalMA > PrevSlowMA))
...
 
FernandoBorea:
What's the Magic Number for?

You should use Magic Number, because you can recognize by Magic Number what EA has opened the Order. Usually you don't want to close Orders opened by another EA.

if(OrderMagicNumber() == MagicNumber && OrderCloseTime() == 0)
 
FernandoBorea:
Yep,  I just noticed that I posted a draft version, later this day I'll update with the actual "final" code before modifying anything upon the suggestions as I want to learn more about why it's bad before making any changes so I can learn more:D
Is there any reason for using Pips? I don't think so.
 
Petr Nosek:
OrderClosePrice() returns close price. If you close the order for example at 1.23000 the OrderClosePrice() will return 1.23000. If the order is still open the OrderClosePrice() returns 0.0 . Similar to OrderCloseTime()
That's not exact. OrderClosePrice() returns the bid/ask and it's best practice to use it with OrderClose().
 
  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. FernandoBorea: 2- I can't get the EA to close the first order and then open another one.
        static double SlowMA = iMA(Symbol(),Period(),MASlow,0,MODE_LWMA,PRICE_CLOSE,1);
        static double FastMA = iMA(Symbol(),Period(),MAFast,0,MODE_LWMA,PRICE_CLOSE,1);
        static double SignalMA = iMA(Symbol(),Period(),MASignal,0,MODE_LWMA,PRICE_CLOSE,1);    
    Static/global variables are initialized once, on program load, with a constant. Your variables are bogus and never change. Drop the static.

  3. Petr Nosek: you can't close the order by using OrderClosePrice() (returned value is zero, because the order is still open). You have to use Ask or Bid.
    You can use OrderClosePrice() instead of Bid/Ask in MT4.
              www.mql5.com/en/forum/158890/page3#comment_3812636

  4. FernandoBorea: Could you please explain me the difference between using (!res) and then else? What does " ! " Do?
    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. FernandoBorea: What's the Magic Number for?
    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. FernandoBorea: Pips variable it's for 4 to 5 broker digits conversion.
    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