-
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. -
The stoploss parameter is a price, not a distance (in PIPs or points).
-
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 -
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.
-
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.
-
if(closePrice == emaValue) {
Doubles are rarely equal. Understand the links in:
The == operand. - MQL4 programming forum #2 (2013)
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.

- 2023.01.15
- www.mql5.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.