GreggBazin: I would like to close my trade at 21h45 every day,
- For buy orders, your code closes orders when the hour is between zero and 20 (inclusive) and the minute is less than 45.
- For sell orders, your code closes orders when the hour is between zero and 20 (inclusive) and the minute is more than 44.
- Move your time tests outside the for loop and test for what you say you want.
- No need to test the order type when you use OrderClosePrice.
- For non-FIFO
(non-US brokers), (or the EA only opens one order per symbol,) you can
simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 programming forum - You say you want to close "my trade" Do you mean all trades on the current chart — this is what your for loop attempts. It is not what your TotalOrdersCount
returns.
Thanks William !
I worked on my code and here is a new result, simplier:
input int MagicNumber=0101; input double Lots=1; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert start fonction | //+------------------------------------------------------------------+ int start() { if((Hour()==09)&&(Minute()==00)) { OrderSend(Symbol(), OP_BUY, Lots, Ask, 2, Ask - 100, Ask + 200, "test achat", MagicNumber, 0, Green); } if((Hour()==12)&&(Minute()==00)) { OrderSend(Symbol(), OP_SELL, Lots, Bid, 2, Bid + 100, Bid - 200, "test vente", MagicNumber, 0, Red); } for(int cnt=1;cnt<OrdersTotal();cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber ) { if(OrderType()==OP_BUY) { if((Hour()==20)&&(Minute()==59)) //here is the close buy rule { OrderClose(OrderTicket(),Lots,Bid,2,Red); } } } else { if((Hour()==20)&&(Minute()==59)) // here is the close sell rule { OrderClose(OrderTicket(),Lots,Ask,2,Red); } } } return(0); }
I still have 2 problems:
1/ We have the same position launch more than one tim. In ProRealTime, we have a function wich name is "DEFPARAM
CumulateOrders = false" and "IF not
longonmarket THEN..."
With these functions, if one trade is open, we can't open a second one, so no mutliple repetitions as I have with my code above...
2/ The position doesn't close at 20h59. I would like to avoid overnight and close the position at 20h59.
What do you think about that?
Any advice? Thanks again for your time
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi there,
I'm a newbie and I have a problem with a part of my code.
I would like to close my trade at 21h45 every day, even if the position is positive or negative.
Here is my portion of code. I don't understand why it's not working:
Thanks in advance for your help and your time !