Order Pending History

 

Hi

Ii would like to know if anyone can give me a hand with some code. Still new at coding but can get around most things just having trouble with history lookup. I have looked around and found some code that works but not in the way I want. All I want to do is to look up in order history the last open price for a cancelled pending order for that currency pair and be able to return that number. I want to be able to update lot size on other pending orders on the chart if there was a win or a lose but place it at the same place as the cancelled one.

Is there a way to get this code to stop once it finds the first pending order for that currency pair.

This would help me out a lot.

Thanks


double PriceHistoryB()
{
// retrieving info from trade history
int i,hstTotal=HistoryTotal();
int HistoryOrderType;
int HistoryOrderTicket;
int HistoryOrderCloseTime;
double HistoryOrderClosePrice;
int HistoryOrderOpenTime;
double HistoryOrderOpenPrice;
double HistoryOrderOpenProfit;
double HistoryOrderOpenStopLoss;
string OrderName;

for(i=0;i<hstTotal;i++)
{
//---- check selection result
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Access to history failed with error (",GetLastError(),")");
break;
}
HistoryOrderType = OrderType();

HistoryOrderCloseTime=OrderCloseTime();
HistoryOrderClosePrice=OrderClosePrice();
HistoryOrderOpenTime=OrderOpenTime();
HistoryOrderOpenPrice=OrderOpenPrice();
HistoryOrderOpenProfit=OrderProfit();
HistoryOrderOpenStopLoss=OrderStopLoss();
HistoryOrderTicket=OrderTicket();
OrderName=DoubleToStr(HistoryOrderTicket,0)+" "+TimeToStr(HistoryOrderOpenTime)+" "+DoubleToStr(HistoryOrderOpenPrice,4);

if (HistoryOrderType == OP_BUYSTOP)
{
if (OrderSymbol() == Symbol())
{
return(HistoryOrderOpenPrice);
}

}
}
}

 
 
for(i=OrdersHistoryTotal()-1;i>=0;i--){             //JJC: https://forum.mql4.com/44528
    if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
        if(OrderSymbol()==iSymbol
        && OrderMagicNumber()==iMagic
        && (OrderType()>=2 && OrderType()<=5)       //https://book.mql4.com/appendix/trading
        ){
            return(OrderOpenPrice());               //Returns Expected Open Price of Canceled Order
        }
    }
}
 

I'll recommend using print statements above the return. In this print statement test for

HistoryOrderCloseTime=OrderCloseTime();
HistoryOrderClosePrice=OrderClosePrice();
HistoryOrderOpenTime=OrderOpenTime();
HistoryOrderOpenPrice=OrderOpenPrice();
HistoryOrderOpenProfit=OrderProfit();
HistoryOrderOpenStopLoss=OrderStopLoss();
HistoryOrderTicket=OrderTicket();

See if all those returns valid values before assigning them to variables.

Order_Profit for Canceled Pending orders will always be 0.

If you need to base decisions on Order_Profit then you'll need to search for Non-Pending orders at some point.

 

Thank you so much for your fast response you are a life saver now I have something to work on :D

Reason: