I am new to mql4 programming. I want want to understand some fundamental. I have this testing EA whose code is displayed below. How do I stop it
from multiple entry. Like when the condition for buy is true, it enters buy on every tick, the same with sell.
My question is how to i stop it from entering several buy or sell as the case may be. I want it to enter single buy on a particular pair and when the
sell condition is true, it will close the buy and enter sell. Please help me out.
Thanks in advance.
//| Copyright 2019, MetaQuotes Software Corp. |
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//| Expert initialization function
//| Expert deinitialization function
void OnDeinit(const int reason)
//| Expert tick function
double f1 = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
double f2 = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0);
if(f2 > f1)
else if(f2 < f1)
You need to define a boolean flag:
example: bool buy=false;
if( buy condition && buy==false)
buy=true; ( here it becomes true)
or... you can use a for loop and count positions like: for( int i=0; i<OrdersTotal(); i++) and then check if (OrdersTotal()==0)
and other case for sell
if(conditional statement && buy==false)
sell=true // here it becomes true
Alert("OrderSend failed with error #",ErrorDescription(GetLastError()));
Alert("some order details");
for(int i=OrdersTotal()-1; i>=0; i--)
//filter orders by magic,symbol type
//if no open order is found set iBot = false
OrderSend does not return a boolean. Your test fails.
Your post is incoherent.
If you want to check if an OrderSend() was successful
Please note that my post was
"If you want to check if an OrderSend() was
Yours is about if the order is NOT successful so neither is more correct than the other.