MQL4 EA Quesion

 
delete
Moving Average - Trend Indicators - MetaTrader 5 Help
Moving Average - Trend Indicators - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2.   double val=0.0021; //21 pips away 
      if(wma-Bid ==val && OrdersTotal()==0)

    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 (2013)

  3. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select 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 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

 
Code was working fine, I just didn't know how to test properly. Thanks. 
 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 (2013)

  3. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select 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 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

What types should I use instead of a double then? Thanks
 
CodeBerries #:
What types should I use instead of a double then? Thanks

A double has a max precision of 

1.7976931348623158e+308


It indeed will rarely be equal if you see it that way; the way a computer does.

Solution: It is easy to set up a range using >= or <= conditions instead of == when dealing with doubles.

It should do the trick in most cases, but there may be a better way I'm not aware of.

 
Jeremie Courchesne #:

A double has a max precision of 

1.7976931348623158e+308


It indeed will rarely be equal if you see it that way; the way a computer does.

Solution: It is easy to set up a range using >= or <= conditions instead of == when dealing with doubles.

It should do the trick in most cases, but there may be a better way I'm not aware of.

right thanks, I just had another question, what is the best way to take profit at for example when the price is equal to the 200EMA on the 5 min or 15 min. Here is what I have but the position is closing as soon as it is opened. Thanks. 


double profitEMA = iMA(_Symbol, PERIOD_M5, 200, 0, MODE_EMA, PRICE_CLOSE, 0);

 int buyticket = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 3, NULL, Ask + profitEMA, NULL, 0, 0, Green);

int sellticket = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 3, NULL, Bid - profitEMA, NULL, 0, 0, Green);

 
CodeBerries #:

right thanks, I just had another question, what is the best way to take profit at for example when the price is equal to the 200EMA on the 5 min or 15 min. Here is what I have but the position is closing as soon as it is opened. Thanks. 


double profitEMA = iMA(_Symbol, PERIOD_M5, 200, 0, MODE_EMA, PRICE_CLOSE, 0);

 int buyticket = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 3, NULL, Ask + profitEMA, NULL, 0, 0, Green);

int sellticket = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 3, NULL, Bid - profitEMA, NULL, 0, 0, Green);

I personally never input a TP and SL in the OrderSend Function.
I'd rather have the function entering the trade, returning me a Ticket, and then calling OrderModify to add TP and SL, making sure Ordermodify returns true.

I would not input a "condition" as a TP, I would rather create a function that checks for the condition and, if it occurs, trigger an Orderclose, making sure it returns true.
You could also OrderModify your TP every time the MA Value changes.. But I think that would be less efficient.

Reason: