Experts: MQL5 Programming for Traders – Source Codes from the Book. Part 6 - page 2

 
Stanislav Korotky #:
Orders, deals, positions are not related to timeframes anyhow. You either misunderstood something or your wording is incorrect.
Am sorry I guess my choice of word is confusing by "timeframe" I mean "date range". Say I want to select Deals/Orders transacted within a given date range like say Deals from 2025-10-01 00:00:00 to 2025-10-22 23:59:59. 
 
pauldic #:
Am sorry I guess my choice of word is confusing by "timeframe" I mean "date range". Say I want to select Deals/Orders transacted within a given date range like say Deals from 2025-10-01 00:00:00 to 2025-10-22 23:59:59. 

If you want to analyze a subrange of a trading history, when it's more efficient to request only this part of the history before the filtering, without affecting the filtering code itself:

input datetime SubrangeFrom = 0;
input datetime SubrangeTo = 0;

...

{
   HistorySelect(SubrangeFrom, SubrangeTo);
   // ... filter code goes here as is
}

If, for some reason, you want to select a (more narrow) subrange within the global range which you applied with HistorySelect, then you still can do it in the filtering code like so:

{
      // some of these go here

      // HistorySelect(0, LONG_MAX);
      // HistorySelectByPosition(PositionID);
      ...
      DealTuple deals[];
      if(SubrangeFrom != SubrangeTo && SubrangeFrom < SubrangeTo)
      {
         filter.let(DEAL_TIME, SubrangeFrom - 1, IS::GREATER).let(DEAL_TIME, SubrangeTo + 1, IS::LESS);
      }
      filter.let(DEAL_POSITION_ID, PositionID).select(deals, true);
      ...
}

The highlighted in yellow line sets 2 conditions for the datetime range [SubrangeFrom, SubrangeTo] using additional qualificators IS::GREATER and IS::LESS (by default, they are not specified in other calls to let(), and then IS::EQUAL is normally used for single value fields).

I know only one reason to apply the subfilter by date range - it is for orders' setup time (ORDER_TIME_SETUP), because HistorySelect is applied to another datetime property of orders - namely order execution time (ORDER_TIME_DONE). Also it might be interesting to filter a subrange of active orders (not in the history), if there are many of them.

You can look at the MQL5/Scripts/MQL5Book/p6/TradeHistoryPrint.mq5 example script, as a starting point.
 
Stanislav Korotky #:

If you want to analyze a subrange of a trading history, when it's more efficient to request only this part of the history before the filtering, without affecting the filtering code itself:

If, for some reason, you want to select a (more narrow) subrange within the global range which you applied with HistorySelect, then you still can do it in the filtering code like so:

The highlighted in yellow line sets 2 conditions for the datetime range [SubrangeFrom, SubrangeTo] using additional qualificators IS::GREATER and IS::LESS (by default, they are not specified in other calls to let(), and then IS::EQUAL is normally used for single value fields).

I know only one reason to apply the subfilter by date range - it is for orders' setup time (ORDER_TIME_SETUP), because HistorySelect is applied to another datetime property of orders - namely order execution time (ORDER_TIME_DONE). Also it might be interesting to filter a subrange of active orders (not in the history), if there are many of them.

You can look at the MQL5/Scripts/MQL5Book/p6/TradeHistoryPrint.mq5 example script, as a starting point.
@Stanislav Korotky Thanks once again... It is a great starting point for me and will start working with it