correcting issues in the code

 

Hello,


I'm not programmer and I'm having really hard time to correct issues in the code for simple EMA trading strategy.

If there is a good soul out there to help with errors I would be grateful.

Here is the code:

input int emaPeriod = 20;

input double positionSize = 20;  // Position size in lots

input int stopLossPips = 6;      // Stop loss in pips

int handle;


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

//| Expert initialization function                                   |

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

void OnInit()

{

   handle = iCustom(NULL, 0, "EMA", emaPeriod, 0);

}


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

//| Expert tick function                                             |

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

void OnTick()

{

   double emaValue = iCustom(NULL, 0, "EMA", emaPeriod, 0);

   double closePrice = iClose(NULL, 0, 0);

   double prevClosePrice = iClose(NULL, 0, 1);

   if (closePrice > emaValue && prevClosePrice < emaValue) {

       if (PositionSelect(handle) < 0) {

         OrderSend(Symbol(), OP_BUY, positionSize, Ask, stopLossPips, 0, 0, "Buy", handle, 0, Blue);

      }

   } else if (closePrice < emaValue && prevClosePrice > emaValue) {

       if (PositionSelect(handle) > 0) {

         OrderSend(Symbol(), OP_SELL, positionSize, Bid, stopLossPips, 0, 0, "Sell", handle, 0, Red);

      }

   }

   if(closePrice == emaValue) {

      OrderClose(handle);

   }

}


It's simple EMA strategy to enter a trade on candle crossing the EMA20 and close it when it touches back the EMA20 line (also it opens position in opposite side).

That's it. And it shows 8 errors and 3 warnings (errors: 'OP_BUY', 'Ask', 'OP_SELL', 'Bid', 'Orderclose'- undefined identifier; 'OrderSend'- wrong parameters count; 'handle'- some operator expected).

Warnings: if (PositionSelect(handle) < 0) { - implicit conversation from 'number' to 'string', expression is always false (same line of code); if (PositionSelect(handle) > 0) { -  implicit conversation from 'number' to 'string'.


Can someone help please and make this function because I have no idea what to do.


Thank you.

 
  1. OrderSend(Symbol(), OP_SELL, positionSize, Bid, stopLossPips, 0, 0, "Sell", handle, 0, Red);

    Why did you post your MT4 question in the 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? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. The stoploss parameter is a price, not a distance (in PIPs or points).

  3. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  4.  if (closePrice > emaValue && prevClosePrice < emaValue) {

    You have no test for a new bar. Therefor, your condition will try to open a new order every tick.

  5.    handle = iCustom(NULL, 0, "EMA", emaPeriod, 0);
    ⋮
           if (PositionSelect(handle) < 0) {

    Perhaps you should read the manual. MT5's PositionSelect does not take a handle. What do you think selecting a position by a EMA could possibly mean?
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    MT4 has no PositionSelect. Do not post code that will not even compile.

  6. if(closePrice == emaValue) {

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

 
psyho01:

You cannot mix MQL4 and MQL5 code.

The errors suggest that you are compiling in the MQL5 editor.

You cannot expect to create a working EA by copying and pasting random snippets of code.

Maybe you should try Freelance.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2023.01.15
  • www.mql5.com
The largest freelance service with MQL5 application developers
Reason: