Delete Pending Orders

 

Hi All,

I am a newbie in programming. 

I am trying to build an expert advisor based on Pinbar Candle.

I want the EA to open a Buy Stop Order at the high of a Pinbar which test the 50EMA and a Sell Stop Order at the low of a Pinbar which test the 50 EMA.

This part of code is working. 

However, I also want the EA to Close a pending order if it has not been filled and that price has closed below/above the Stop Loss Level.

Let me explain, if a Buy Stop Order is opened and before it is filled a candle has closed below the Stop Loss.

When I am running the EA the Pending Order is not deleted even when price closed below/above the Stop Loss Level .

Can someone please advise what is wrong with the attached code.

Files:
Pinbar_EA.mq4  5 kb
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
  1.    bool Pinbar;
        if (…)
           Pinbar=true;
      return(Pinbar);    

    What is the value returned if the if statement is false?

  2.    if (OrdersTotal() == 0) 

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum 2013.02.15
              PositionClose is not working - MQL5 programming forum 2020.02.21
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles 24 July 2006
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles Small>1 February 2011

  3.    int result=OrderDelete(OrderTicket());   
    

    You can not use any Trade Functions until you first select an order.

  4. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

 
William Roeder:
  1. What is the value returned if the if statement is false?

  2. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum 2013.02.15
              PositionClose is not working - MQL5 programming forum 2020.02.21
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles 24 July 2006
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles Small>1 February 2011

  3. You can not use any Trade Functions until you first select an order.

  4. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

Thanks for the comment. The resources provided is very helpful