Opening orders during high volatility

 

Hi folks!


I'm trying to build a hedge EA. Basically my EA opens a buy in one pair and a sell in a correlated pair. Basically the isdea is to have the same number of buys as sells. My problem though is that sometimes the EA opens orders of the same type an now I have 2 sells and no buys (or vice versa). I have no clue how this is happening. Below is the code I've made. Please go through it and tell me if you see anything wrong with the logic. Bottomline is: everytime a position is opened, a counter position should be opened in the other pair. I very much appreciate your help!


//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
int OpenOrders()
{
int ticket;
if(RabbitTopDistance() > MinDistance)
{

ticket = OrderSend(Turtle,OP_BUY,LotSize,Ask,3,0,0,PairNumber(),MagicNo,0,Blue);
if(ticket<0){Print("OrderSend failed with error #",GetLastError());}
else {
ticket = OrderSend(Rabbit,OP_SELL,LotSize,MarketInfo(Rabbit,MODE_BID),3,0,0,PairNumber(),MagicNo,0,Red);
if(ticket<0){Print("OrderSend failed with error #",GetLastError()); ReOpen1();}
}
}

if(TurtleTopDistance() > MinDistance)
{
ticket = OrderSend(Rabbit,OP_BUY,LotSize,MarketInfo(Rabbit,MODE_ASK),3,0,0,PairNumber(),MagicNo,0,Blue);
if(ticket<0){Print("OrderSend failed with error #",GetLastError());}
else {
ticket = OrderSend(Turtle,OP_SELL,LotSize,Bid,3,0,0,PairNumber(),MagicNo,0,Red);
if(ticket<0){Print("OrderSend failed with error #",GetLastError()); ReOpen3();}
}
}
return(ticket);
}

void ReOpen1() {int ticket; ticket = OrderSend(Rabbit,OP_SELL,LotSize,MarketInfo(Rabbit,MODE_BID),3,0,0,PairNumber(),MagicNo,0,Red) ;if(ticket<0)ReOpen1() ;return;}
void ReOpen2() {int ticket; ticket = OrderSend(Turtle,OP_BUY,LotSize,Ask,3,0,0,PairNumber(),MagicNo,0,Blue) ;if(ticket<0)ReOpen2() ;return;}
void ReOpen3() {int ticket; ticket = OrderSend(Turtle,OP_SELL,LotSize,Bid,3,0,0,PairNumber(),MagicNo,0,Red) ;if(ticket<0)ReOpen3() ;return;}
void ReOpen4() {int ticket; ticket = OrderSend(Rabbit,OP_BUY,LotSize,MarketInfo(Rabbit,MODE_ASK),3,0,0,PairNumber(),MagicNo,0,Blue) ;if(ticket<0)ReOpen4() ;return;}

 
heyarn:

Hi folks!



if(RabbitTopDistance() > MinDistance)
{
ticket = OrderSend(Turtle,OP_BUY,LotSize,Ask,3,0,0,PairNumber(),MagicNo,0,Blue);


(when the OrderSend statement is executed, the first block of the conditional-if is satisfied (assuming the order went through, therefore, the second block of the conditional-if is not called that is why the second order is not sent.) You need to re-arrange your code a bit.


if(ticket<0){Print("OrderSend failed with error #",GetLastError());}



else {
ticket = OrderSend(Rabbit,OP_SELL,LotSize,MarketInfo(Rabbit,MODE_BID),3,0,0,PairNumber(),MagicNo,0,Red);
if(ticket<0){Print("OrderSend failed with error #",GetLastError()); ReOpen1();}
}

Cheers
 

It sounds like you are doing what a lot of people are going to be doing. You sound like you want to create a Global variable that you can use to uniquely identify the direction of each chart. So, you may set one to be called "chart1" with the value of 0 for a buy or 1 for a sell, then set the second variable to the other number and call it chart2, personally, I think I would use the symbol name in it so that you can uniquely identify programmatically the variable that goes with the chart.

GlobalVariable set manually:

eurusd = 0;

eurgbp = 1;

in the code:

if (GlobalVariableGet("eurusd") == 0)

{

// Take a trade to buy

}

if (GlobalVariableGet("eurgbp") == 1)

{

// Take a trade to sell

}

Reason: