Expert Advisor to control multiple indicators

 

Hi,

I'm trying to write an EA and I want to control multiple indicators like

ind 1, ind 2, ind 3, ind 4

if, ind 1 buy and ind 2 buy ind 3 sell ind 4 buy

it should be closed.

if ind 1 buy and ind 2 buy ind 3 buy ind 4 buy

it should open buy order

if ind 1 sell and ind 2 sell ind 3 sell ind 4 sell

it should open sell order,

in other words,

I want to open buy or sell order when each indicator become same.

Otherwise I want to close.

I prepared an if conditon but it doesnt work

if(use_HA1 == true){

ha1 = HA1Control();
buyDecision = (buyDecision && (ha1 == OP_BUY));
sellDecision = (sellDecision && (ha1 == OP_SELL));
closeDesicion = (closeDesicion && (ha1 == 5));
}
if(use_HA2 == true){
ha2 = HA2Control();
buyDecision = (buyDecision && (ha2 == OP_BUY));
sellDecision = (sellDecision && (ha2 == OP_SELL));
closeDesicion = (closeDesicion && (ha2 == 5));

}
if(use_Parabolic == true){
par = parabolicControl();
buyDecision = (buyDecision && (par == OP_BUY));
sellDecision = (sellDecision && (par == OP_SELL));
closeDesicion = (closeDesicion && (par == 5));
}
if(use_BBand == true){
bband = bBandControl();
buyDecision = (buyDecision && (bband == OP_BUY));
sellDecision = (sellDecision && (bband == OP_SELL));
closeDesicion = (closeDesicion && (bband == 5));
}
if(buyDecision == true)
{
nTicket = OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"buy order",Magic,0,Green);
}
else if(sellDecision == true)
{
nTicket = OrderSend(Symbol(),OP_SELL,1,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"sell order",Magic,0,Red);
}
else if(closeDesicion == true)
{
nTicket = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
nTicket = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red);
}

Each indicator works single with no problem. But when I start to combine, it cares just one indicator and it opens or closes according to just one of them.


Please Help

 

  1. Don't bother to paste ALL the code, make us guess what the datatypes are, what the functions return etc.
  2. if(use_HA1 == true){
    ha1 = HA1Control();
    buyDecision = (buyDecision && (ha1 == OP_BUY));
    sellDecision = (sellDecision && (ha1 == OP_SELL));
    closeDesicion = (closeDesicion && (ha1 == 5));
    }
    What is the initial value of buyDecision, sell and close?
  3. Simplify the code where possible.
    buyDecision = (buyDecision && (ha1 == OP_BUY));
    buyDecision &= (ha1 == OP_BUY);

  4. if(buyDecision == true)
    You would never say if( (2+2 == 4) == true) would you? if(buyDecision) or if (!buyDecision) is sufficient.
 
WHRoeder

Your item #4 is incorrect. You have specified a bitwise operation AND assignment (&=). There is no logical assignment version (&&=).

 

initial values of close sell buy are true.

And bitwise operations can only be applied int data type said the compiler when I wrote


buyDecision &= (ha1 == OP_BUY);


I added my expert advisor in the attachment. I think there is a little mistake in my code. I coulndt fix it for days. If you guys can help me to fix it, I would be happiest in the world. You can find the code in the attachment.


Best Regards,

Files:
eawith4ind.mq4  16 kb
 
The way you use order select is wrong, here's an example on how using OrderSelect() Loops and Closing or Deleting Orders
 
Each indicator works by ownself. When I select any of them true from expert properties. But they do not work as combine. I have no problem about close or open order in my code. (I think :))) )
I just want to combine 4 or 3 or 2 indicators to work together.
 
ihsany87:

I added my expert advisor in the attachment. I think there is a little mistake in my code. I coulndt fix it for days. If you guys can help me to fix it, I would be happiest in the world. You can find the code in the attachment.

You need to read this thread and implement what is described in it: What are Function return values ? How do I use them ?

nTicket is an int, OrderClose returns a bool . . . why are you trying to close the same order twice ?

   else if(closeDesicion == true)
   {
                nTicket = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
                nTicket = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red);
                flag = 0;
                commonStatus = "CLOSE COMMON";
   }
 
ihsany87:
Each indicator works by ownself. When I select any of them true from expert properties. But they do not work as combine. I have no problem about close or open order in my code. (I think :))) )
I just want to combine 4 or 3 or 2 indicators to work together.
You have major problems with your code . . . can it pick up from where it left off if MT4 crashes ? you only select by ticket, when your EA starts it doesn't have the ticket number so it can't close anything until it places a new order . . . you don't report any errors, you have zero debugging Print() statements, all your Comment statements are commented out . . . .
 
I'm newbie on writing mql4 therefore I wrote some codes as I see from somewhere. You are right nTicket is an int I shouldnt do this like that. But I just used it like that because compiler did not give any error and I thought it was okay. Secondly, I saw that there are two different types of close order with Ask and Bid and I thought that I should use both of them to close order. One of them is enough to close order ?
 

I'm sorry, but I don't understand what exactly do you expect from this code

if((OrdersTotal()==0) && (OrderType()==0))

if((OrdersTotal()==0)&& (OrderType()==1))

Can you please explain them to us ?

 
Then, I just thought that my code is working. Actually, there are also some errors in my each indicators' code. I tried to debug with Print() but I didnt do it. Can you tell me how print works during debug. Where can I see the print's output ?