How to close all trades using EA at the end of each day?

I want to write a code (for an advisor) which closes all open trades at the end of the trading day. So for example if the market for US stocks closes at 16:30, all open APPLE orders must be sold at something like 16:29. I tried to look this up on the internet but I couldn’t find any specific answers, can anyone here help me out?

EA should consider :

  • current time & day of week [ TimeCurrent() && TimeToStruct() functions ]

  • get session trade times (or quote times ?) for the symbol SymbolInfoSessionTrade() and/or SymbolInfoSessionQuote() function ]

  • calculate the correct time for position closures ( one minute before market closing, is not a good idea )

  • close positions if in right time-window for the symbol. (loop through open positions and check the closing conditions, as above, for all open positions)

Thank you!! This should work just fine, but what I cant seem to understand is how you can create the SymbolInfoSessionTrade() function. When I look at the documentation I don’t understand what they mean with session index , and also what they mean with time of session beginning and time of session end , because isn’t that exactly the information you want to receive?

you don’t create that function, it’s ready to be used.

some parameters of that function are input parameters, some are output variables. (those having a & before variable name are output parameters)
output parameters are used to store some function results. (that’s how some functions are created.)

so, a return of true means the function got your variables updated successfully, and you can use the content of those two output variables.

session_index, probably is for those symbols having a few open/close times per weekday (just guessing, not sure)
e.g. : Sundays, 00:00:00 to 9:59:59 and 13:00:00 to 18:00:00 (two sessions per day, with 10 and 5 hours duration)