Time Based Pending Orders

 

Hi Guys, I need Help with this EA:


1. I want it to Open Two Pending Stop Orders at a Specific Time(Which can be Changed with Inputs)

2. If One of the Orders gets Filled, it must Delete the Other Pending Order


The Code Compiles without Errors as you can See attached screenshots, but:

1. It Opens Many Orders (More than the Two I intended it to Open)

2. Whenever I change the Time input it does not Open any O Until I Change it manually in the Code


Please Advice if I am Missing Something

Files:
 
Insert the code using the button Codeand attach with a button Attach file
 
#include<Trade\Trade.mqh>
CTrade trade;

double Ask,Bid;
input datetime Time=D'15:15:55';
input double LotSize=0.01;
input double TP=300;


void OnTick()

 {  double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
    double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
    double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
    
    // Convert the Time to Local PC Time
    datetime time=TimeLocal();
    
    // Creating a String for Time to Include Seconds
    string minutesAndSeconds=TimeToString(time,TIME_SECONDS);
    
    // If there are No Positions Open
    if((PositionsTotal()==0)&&(StringSubstr(minutesAndSeconds,0,8)==Time));
   
    // I want to Open Buy Stop
     { trade.BuyStop(LotSize,Ask+100*_Point,_Symbol,0,Ask+TP*_Point,ORDER_TIME_GTC,0,0);
       trade.SellStop(LotSize,Bid-100*_Point,_Symbol,0,Bid-TP*_Point,ORDER_TIME_GTC,0,0);
     }

   // If a One Position Opens
  if (Balance!=Equity) CancelOrder();
  
 }
   // Cancel All Pending Orders
   void CancelOrder()
   
  { for(int i=OrdersTotal()-1; i>=0; i--)
   { ulong OrderTicket= OrderGetTicket(i);
     trade.OrderDelete(OrderTicket);
   }
  }
 
844303 # :

You ask the terminal: how many POSITIONS?

PositionsTotal()

but when you are placing PENDING ORDERS!

 

Maybe I used the wrong Statement, but I wanted One Position at Any given Time and a Maximum of Two Pending Orders.

To my Surprise the EA can Open  Four Orders in 1 Second.

#include<Trade\Trade.mqh>
CTrade trade;

double Ask,Bid;
input datetime Time=D'15:15:55';
input double LotSize=0.01;
input double TP=300;


void OnTick()

 {  double Balance=AccountInfoDouble(ACCOUNT_BALANCE);
    double Equity=AccountInfoDouble(ACCOUNT_EQUITY);
    double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
    double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
    
    // Convert the Time to Local PC Time
    datetime time=TimeLocal();
    
    // Creating a String for Time to Include Seconds
    string minutesAndSeconds=TimeToString(time,TIME_SECONDS);
    
    // If there are No Positions Open
    if(StringSubstr(minutesAndSeconds,0,8)==Time);
   
    // I want to Open Buy Stop
     { trade.BuyStop(LotSize,Ask+100*_Point,_Symbol,0,Ask+TP*_Point,ORDER_TIME_GTC,0,0);
       trade.SellStop(LotSize,Bid-100*_Point,_Symbol,0,Bid-TP*_Point,ORDER_TIME_GTC,0,0);
     }

   // If a One Position Opens
  if (Balance!=Equity) CancelOrder();
  
 }
   // Cancel All Pending Orders
   void CancelOrder()
   
  { for(int i=OrdersTotal()-1; i>=0; i--)
   { ulong OrderTicket= OrderGetTicket(i);
     trade.OrderDelete(OrderTicket);
   }
  }

I even Tried it without 

PositionsTotal()

And it did not make any Different


#Anyway, Thank you for responding to my Situation Mr. Vladimir

 

use forloop and orderselect to make a function to count your trades

built-in function is not good enough for this

 
Pak Hong Poon #:

use forloop and orderselect to make a function to count your trades

built-in function is not good enough for this

I have started coding about 2 weeks ago.

I don't know how to do that Yet.

I will research and Implement.

Thank You 

 
Hello, did you manage to get it to work, I'm interested in doing the same. Could you tell me how to do it. Thanks.
 
  1. Nekrodamus #: did you manage to get it to work, I'm interested in doing the same. Could you tell me how to do it.

    If you had used this, you would have found this:

    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)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.


  2. 844303: 2. If One of the Orders gets Filled, it must Delete the Other Pending Order

    There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

      Don't worry about it unless you're scalping M1 or trading news.

    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

  3.      { trade.BuyStop(LotSize,Ask+100*_Point,_Symbol,0,Ask+TP*_Point,ORDER_TIME_GTC,0,0);
           trade.SellStop(LotSize,Bid-100*_Point,_Symbol,0,Bid-TP*_Point,ORDER_TIME_GTC,0,0);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 pip spreads) in EURCHF? - General - MQL5 programming forum (2022)

Reason: