Jump to content

[ Help Req] Please Help with this Pending Orders Code


Recommended Posts

I am trying to write a small pending orders code . it simply closes a pending order if the parent order is a win and it should open a pending order if the parent is a LOSS. The code below works but it goes into a LOOP after a few iterations. Any Help on the Bottom part of the code. Thank you in advance :cry:

 

int CheckOpenPendingOrders() {

for (int i = 0; i < OrdersTotal(); i++) {

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderSymbol() == Symbol() && OrderType() == OP_SELL || OrderType() == OP_BUY)

if (StringSubstr(OrderComment(), 0, 4) == "Open") return (OrderTicket());

}

}

return (0);

}

 

int CheckPendingOrders() {

for (int i = 0; i < OrdersTotal(); i++) {

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderSymbol() == Symbol() && OrderType() == OP_SELLSTOP || OrderType() == OP_BUYSTOP)

if (StringSubstr(OrderComment(), 0, 4) == "Pending") return (OrderTicket());

}

}

return (0);

}

 

// The Below Code does not seem to work it goes into a LOOP after after a few trades.

 

 

void ClosePendingOrders() {

double PendingOrderOpenPrice;

int ParentTicket;

int PendingTicket = CheckPendingOrders();

int OpenPendingOrderTicket = CheckOpenPendingOrders();

 

if (PendingTicket > 0 ) //Scan to see if there exists pendings Orders that needs to be cancelled

{

OrderSelect(PendingTicket, SELECT_BY_TICKET);

PendingOrderOpenPrice = OrderOpenPrice();

ParentTicket = OriginalOrderTicket(PendingTicket);

OrderSelect(ParentTicket, SELECT_BY_TICKET);

 

if (OrderCloseTime() > 0)

{

 

if (OrderProfit() <= 0.0 )return; // it should activate the Pending order ( into an open Sellstop or buystop) if the Parent order was a LOSS trade

// i.e the STOP LOSS was hit

// does not seem to work correctly it causes a LOOP. it is able to activate the pending order but

// then it starts to go into LOOP

 

OrderDelete(PendingTicket); // Deletes the Pending Order if the Parent order was a WIN

if (!(Outcome)) return;

return;

}

 

 

}

 

}

Link to comment
Share on other sites

Re: [ Help Req] Please Help with this Pending Orders Code

 

Hi progressapama,

 

I did some thorough reading on your code but was unable to find the cause of problem. One possible reason could be errors from the return values of the customized functions or the MT4's functions. Suggestions:

 

1. Print out the variables values such as ParentTicket, PendingTicket and compared with the expected values during the forward test.

 

2. Print out the system errors for MT4's functions such as OrderSelect using the recommended GetLastError() and ErrorDescription() functions from MT4 documentations. This can be found in

 

h**p://docs.mql4.com/constants/errors

 

Hope this information helps.

 

Regards,

Collin

Link to comment
Share on other sites

Re: [ Help Req] Please Help with this Pending Orders Code

 

Thank you so much i think i figured out where the problem was occuring .

 

Thank you :arrow: :peace: :shand:

 

 

Hi Progressapama, we are very appreciated if you can share this pending order EA, attached is the EA maybe similiar to it, but it couldn't close all pending order or opened orders once 1 of the opended position hit profit!

Link to comment
Share on other sites

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...