Jump to content

Error 0 when trade doesn't open


kata

Recommended Posts

I'm writing an EA which when it's trading logic is met is supposed to open 2 market orders using the same values. Sometimes both these orders open, and sometimes they dont. For a while I had errors where the second order wouldn't open, and I got an error code. I have fixed that error by using RefreshRates between sending order 1 and 2. However, now I seem to regularly be getting only the second order opening. First I didn't get any error at all, so I went back to the mql4 file and looked at it very carfully, and then moved a bracket around the Else" part of the statement, thinking that was the problem. Today I have come home and discvered the same thing has happened, but this time I have an error message. I looked up the 0 code, only to discover it meant I had no error.

 

So please, if there is no error, why isn't my trade opening??!! It's like it isn't even tryin to open it, but then other times they boh open fine, using exactly eht same lines of code!

 

Does anyone have any idea of why this might be?

Link to comment
Share on other sites

This is my trade opening logic. I curently always have trailing stop set to higher than 0.

{

static datetime Time.newBar;

bool newBar = (Time[0] > Time.newBar);

if (newBar)

{ // A new bar began.

Time.newBar = Time[0]; // Reset static.

//... <<<< Any code here is executed only when a new bar appears

if ((bflag==false) && (sflag==false)) //No current trades open

{

//Alert("Check each tick section");

 

//open buy trade

if (buy == true) //buy trade

{

if (TrailingStop == 0)

{

tickbuy1=OrderSend(Symbol(),0,Lots,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),NormalizeDouble(Ask+TakeProfit*Point,Digits),buycomment1,Magic_no,0,Lime);

//tickbuy2=OrderSend(Symbol(),0,Lots/2,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),NormalizeDouble(Ask+TakeProfit*Point,Digits),comment,Magic_no,0,Lime);

}

if (TrailingStop > 0)

{

tickbuy1=OrderSend(Symbol(),0,Lots/2,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),NormalizeDouble(Ask+TakeProfit*Point,Digits),buycomment1,Magic_no,0,Lime);

RefreshRates();

tickbuy2=OrderSend(Symbol(),0,Lots/2,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),0,buycomment2,Magic_no,0,Lime);

}

 

if (tickbuy1>0)

{

if(OrderSelect(tickbuy1,SELECT_BY_TICKET))

{

Print("BUY 1 order opened at: ", OrderOpenPrice());

bflag=true;

//Bbeflag = true;

}

}

else

{

Print("Error opening BUY 1 order : ",GetLastError());

}

 

if (TrailingStop > 0)

{

if (tickbuy2>0)

{

if(OrderSelect(tickbuy2,SELECT_BY_TICKET))

{

Print("BUY 2 order opened at: ", OrderOpenPrice());

bflag=true;

error2 = false;

//Bbeflag = true;

}

}

else

{

Print("Error opening BUY 2 order : ",GetLastError());

error2 = true;

}

 

}

return(0);

}

 

if (sell == true) //sell trade

{

if (TrailingStop == 0)

{

ticksell1=OrderSend(Symbol(),1,Lots,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),NormalizeDouble(Bid-TakeProfit*Point,Digits),sellcomment1,Magic_no,0,Red);

//ticksell2=OrderSend(Symbol(),1,Lots/2,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),NormalizeDouble(Bid-TakeProfit*Point,Digits),comment,Magic_no,0,Red);

}

if (TrailingStop > 0)

{

ticksell1=OrderSend(Symbol(),1,Lots/2,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),NormalizeDouble(Bid-TakeProfit*Point,Digits),sellcomment1,Magic_no,0,Red);

RefreshRates();

ticksell2=OrderSend(Symbol(),1,Lots/2,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),0,sellcomment2,Magic_no,0,Red);

}

 

if (ticksell1>0)

{

if(OrderSelect(ticksell1,SELECT_BY_TICKET))

{

Print("SELL 1 order opened at: ", OrderOpenPrice());

sflag=true;

//Sbeflag = true;

}

}

else

{

Print("Error opening SELL 1 order : ",GetLastError());

}

 

if (TrailingStop > 0)

{

if (ticksell2>0)

{

if(OrderSelect(ticksell2,SELECT_BY_TICKET))

{

Print("SELL 2 order opened at: ", OrderOpenPrice());

sflag=true;

error2 = false;

//Sbeflag = true;

}

}

else

{

Print("Error opening SELL 2 order : ",GetLastError());

error2 = true;

}

 

}

return(0);

}

}

else if ((bflag == true) && (sflag == true))

{

//Alert("Buy and Sell trades already opened.");

return(0);

}

else if ((bflag == true) && (sell == true)) // buy order is open but sell signal is given

 

// check if open orders are in profit

{

Alert("buy order is open but sell signal is given");

RefreshRates();

if (tickbuy1 > 0)

{

if(OrderSelect(tickbuy1,SELECT_BY_TICKET)==true)

{

stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);

if (OrderProfit() > NormalizeDouble(Ask+(stoplevel*Point),Digits))

{

OrderClose(tickbuy1,OrderLots(),Bid,3*Point,Blue);

}

}

}

RefreshRates();

if (tickbuy2 > 0)

{

if(OrderSelect(tickbuy2,SELECT_BY_TICKET)==true)

{

stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);

if (OrderProfit() > NormalizeDouble(Ask+(stoplevel*Point),Digits))

{

OrderClose(tickbuy2,OrderLots(),Bid,3*Point,Blue);

}

}

}

if (TrailingStop == 0)

{

ticksell1=OrderSend(Symbol(),1,Lots,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),NormalizeDouble(Bid-TakeProfit*Point,Digits),sellcomment1,Magic_no,0,Red);

//ticksell2=OrderSend(Symbol(),1,Lots/2,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),NormalizeDouble(Bid-TakeProfit*Point,Digits),comment,Magic_no,0,Red);

}

if (TrailingStop > 0)

{

ticksell1=OrderSend(Symbol(),1,Lots/2,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),NormalizeDouble(Bid-TakeProfit*Point,Digits),sellcomment1,Magic_no,0,Red);

RefreshRates();

ticksell2=OrderSend(Symbol(),1,Lots/2,NormalizeDouble(Bid,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Bid+StopLoss*Point,Digits),0,sellcomment2,Magic_no,0,Red);

}

 

if (ticksell1>0)

{

if(OrderSelect(ticksell1,SELECT_BY_TICKET))

{

Print("SELL 1 order opened at: ", OrderOpenPrice());

sflag=true;

//Sbeflag = true;

}

}

else

{

Print("Error opening SELL 1 order : ",GetLastError());

}

 

if (TrailingStop > 0)

{

if (ticksell2>0)

{

if(OrderSelect(ticksell2,SELECT_BY_TICKET))

{

Print("SELL 2 order opened at: ", OrderOpenPrice());

sflag=true;

error2 = false;

//Sbeflag = true;

}

}

else

{

Print("Error opening SELL 2 order : ",GetLastError());

error2 = true;

}

 

}

return(0);

}

Link to comment
Share on other sites

else if ((sflag == true) && (buy == true)) // sell order is open but buy signal is given

//Alert("sell order is open but buy signal is given");

// check if open orders are in profit

{

Alert("sell order is open but buy signal is given");

RefreshRates();

if (ticksell1 > 0)

{

if(OrderSelect(ticksell1,SELECT_BY_TICKET)==true)

{

stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);

if (OrderProfit() > NormalizeDouble(Bid+(stoplevel*Point),Digits))

{

OrderClose(ticksell1,OrderLots(),Ask,3*Point,Blue);

}

}

}

RefreshRates();

if (ticksell2 > 0)

{

if(OrderSelect(ticksell2,SELECT_BY_TICKET)==true)

{

stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);

if (OrderProfit() > NormalizeDouble(Bid+(stoplevel*Point),Digits))

{

OrderClose(ticksell2,OrderLots(),Ask,3*Point,Blue);

}

}

}

if (TrailingStop == 0)

{

RefreshRates();

tickbuy1=OrderSend(Symbol(),0,Lots,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),NormalizeDouble(Ask+TakeProfit*Point,Digits),buycomment1,Magic_no,0,Lime);

//tickbuy2=OrderSend(Symbol(),0,Lots/2,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),NormalizeDouble(Ask+TakeProfit*Point,Digits),comment,Magic_no,0,Lime);

}

if (TrailingStop > 0)

{

tickbuy1=OrderSend(Symbol(),0,Lots/2,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),NormalizeDouble(Ask+TakeProfit*Point,Digits),buycomment1,Magic_no,0,Lime);

RefreshRates();

tickbuy2=OrderSend(Symbol(),0,Lots/2,NormalizeDouble(Ask,Digits),NormalizeDouble(5*Point,Digits),NormalizeDouble(Ask-StopLoss*Point,Digits),0,buycomment2,Magic_no,0,Lime);

}

 

if (tickbuy1>0)

{

if(OrderSelect(tickbuy1,SELECT_BY_TICKET))

{

Print("BUY 1 order opened at: ", OrderOpenPrice());

bflag=true;

//Bbeflag = true;

}

}

else

{

Print("Error opening BUY 1 order : ",GetLastError());

}

 

if (TrailingStop > 0)

{

if (tickbuy2>0)

{

if(OrderSelect(tickbuy2,SELECT_BY_TICKET))

{

Print("BUY 2 order opened at: ", OrderOpenPrice());

bflag=true;

error2 = false;

//Bbeflag = true;

}

}

else

{

Print("Error opening BUY 2 order : ",GetLastError());

error2 = true;

}

 

}

return(0);

}

 

 

 

 

}

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...