Skip to content

Data

HistoryManager

Source code in strategytester5\MetaTrader5\data.py
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
class HistoryManager:
    def __init__(self, mt5_instance: Optional[Any] = None,
                 broker_data_path: Optional[str] = config.DEFAULT_BROKER_DATA_PATH,
                 logger: Optional[logging.Logger] = None):

        """
        A class for managing historical market data for the simulation. It provides methods for synchronizing bars and ticks from the MetaTrader5 terminal, as well as loading them from locally stored parquet files.
        Args:
            mt5_instance (MetaTrader5) : Initialized MetaTrader5 instance to extract bars from
            broker_data_path (Optional |str): A directory where the synchronized bars and ticks will be stored as parquet files. Defaults to `strategytester5.config.DEFAULT_BROKER_DATA_PATH`
            logger (Optional[logging.Logger], optional): The logger to use. Defaults to None.
        """

        self.mt5_instance = mt5_instance
        self.broker_data_dir = broker_data_path
        self.logger = logger
        self._last_start_date: int
        self._last_end_date: int

        # self._mt5_lock = Lock()

    @staticmethod
    def bars_file_path(symbol: str, timeframe_str: str, year: int, month: int,
                       broker_data_dir: Optional[str] = config.DEFAULT_BROKER_DATA_PATH) -> Path:
        return Path(broker_data_dir) / symbol / "Bars" / timeframe_str / f"{year:04d}{month:02d}.parquet"

    @staticmethod
    def ticks_file_path(symbol: str, year: int, month: int,
                        broker_data_dir: Optional[str] = config.DEFAULT_BROKER_DATA_PATH) -> Path:
        return Path(broker_data_dir) / symbol / "Ticks" / f"{year:04d}{month:02d}.parquet"

    @staticmethod
    def month_start(month: int, year: int) -> datetime:
        return datetime(year=year, month=month, day=1, hour=0, minute=0, second=0, microsecond=0)

    @staticmethod
    def next_month(month: int, year: int) -> datetime:
        dt = HistoryManager.month_start(month, year)
        if dt.month == 12:
            return dt.replace(year=dt.year + 1, month=1)
        return dt.replace(month=dt.month + 1)

    @staticmethod
    def month_floor(dt: datetime) -> datetime:
        return dt.replace(day=1, hour=0, minute=0, second=0, microsecond=0)

    @staticmethod
    def next_month_dt(dt: datetime) -> datetime:
        dt = HistoryManager.month_floor(dt)
        if dt.month == 12:
            return dt.replace(year=dt.year + 1, month=1)
        return dt.replace(month=dt.month + 1)

    @staticmethod
    def iter_months_between(date_from: datetime, date_to: datetime) -> Iterable[tuple[int, int]]:
        cur = HistoryManager.month_floor(date_from)

        while cur <= date_to:
            yield cur.year, cur.month
            cur = HistoryManager.next_month_dt(cur)

    def _info_log(self, msg: str):
        if self.logger is None:
            print(msg)
            return

        self.logger.info(msg)

    def _error_log(self, msg: str):
        if self.logger is None:
            print(msg)
            return

        self.logger.error(msg)

    def _warning_log(self, msg: str):
        if self.logger is None:
            print(msg)
            return

        self.logger.warning(msg)

    def _critical_log(self, msg: str):
        if self.logger is None:
            print(msg)
            return

        self.logger.critical(msg)

    def synchronize_bars(self,
                         symbol: str,
                         timeframe: int,
                         month: int,
                         year: int,
                         ) -> Optional[pl.DataFrame]:
        """
        Extracts bars (rates) from the MetaTrader 5 terminal, stores them in a nearby location for simulator usage.

        Returns:
            Synchronized bars in a polars DataFrame object

        Args:
            timeframe (int) : A timeframe to extract bars from
            symbol (str) : An instrument in the terminal
            month (int) : The entire month to synchronize
            year (int) : A year which a specified `month` belongs
        """

        if self.mt5_instance is None:
            log = ("Cannot synchronize Bars from MetaTrader5, due to an invalid MetaTrader5 instance.\n"
                   "If the default MetaTrader5 wasn't installed initially, run `pip install strategytester5[mt5]`")

            self._critical_log(log)
            raise RuntimeError(log)

        start = self.month_start(month, year)
        end = self.next_month(month, year)

        # add it to the market watch
        if not self.mt5_instance.symbol_select(symbol, True):
            err = f"Failed to select or add {symbol} to the MarketWatch, mt5 error = {self.mt5_instance.last_error()}"
            self._error_log(err)
            return None

        self._info_log(f"Fetching bars from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}")

        # with self._mt5_lock:
        rates = self.mt5_instance.copy_rates_range(symbol, timeframe, start, end)
        if rates is None or len(rates) == 0:
            warn = f"No bars were received from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}"
            self._warning_log(warn)
            return None

        # rates dataframe
        df = pl.DataFrame(rates)

        file = self.bars_file_path(symbol=symbol, timeframe_str=MetaTrader5Constants.TIMEFRAME2STRING_MAP[timeframe],
                                   year=year, month=month,
                                   broker_data_dir=self.broker_data_dir)
        file.parent.mkdir(parents=True, exist_ok=True)
        df.write_parquet(file)

        return df

    def synchronize_ticks(
            self,
            symbol: str,
            month: int,
            year: int,
    ) -> Optional[pl.DataFrame]:
        """
        Extracts ticks from the MetaTrader 5 terminal, stores them in a nearby location for simulator usage.

        Returns:
            Synchronized ticks in a polars DataFrame object

        Args:
            symbol (str) : An instrument in the terminal
            month (int) : The entire month to synchronize
            year (int) : A year which a specified `month` belongs
        """

        if self.mt5_instance is None:
            log = ("Cannot synchronize ticks from MetaTrader5, due to an invalid MetaTrader5 instance.\n"
                   "If the default MetaTrader5 wasn't installed initially, run `pip install strategytester5[mt5]`")

            self._critical_log(log)
            raise RuntimeError(log)

        start = self.month_start(month, year)
        end = self.next_month(month, year)

        # add it to the market watch
        if not self.mt5_instance.symbol_select(symbol, True):
            err = f"Failed to select or add {symbol} to the MarketWatch, mt5 error = {self.mt5_instance.last_error()}"
            self._error_log(err)
            return None

        self._info_log(f"Fetching ticks from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}")

        # with self._mt5_lock:
        ticks = self.mt5_instance.copy_ticks_range(symbol, start, end, self.mt5_instance.COPY_TICKS_ALL)
        if ticks is None or len(ticks) == 0:
            warn = f"No ticks were received from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}"
            self._warning_log(warn)
            return None

        # rates dataframe
        df = pl.DataFrame(ticks)

        file = self.ticks_file_path(symbol, year=year, month=month, broker_data_dir=self.broker_data_dir)
        file.parent.mkdir(parents=True, exist_ok=True)
        df.write_parquet(file)

        return df

    def synchronize_all_timeframes(
            self,
            symbols: Iterable[str],
            date_from: datetime,
            date_to: datetime,
            max_workers: int = 4,
    ):

        """
        Synchronizes bars from all timeframes from the MetaTrader5 terminal for specified symbols and date range. This method is useful for pre-populating the local storage with bars from all timeframes, so the simulator can later load them without accessing the terminal.

        Args:
            symbols (Iterable[str]): A list of symbols to synchronize
            date_from (datetime): The start date for synchronization
            date_to (datetime): The end date for synchronization
            max_workers (int): The maximum number of worker threads to use

        """

        if date_from > date_to:
            self._warning_log("date_from must be <= date_to")
            return

        all_tfs = list(MetaTrader5Constants.STRING2TIMEFRAME_MAP.values())

        self._info_log("Synchronizing all timeframes...")

        tasks = []

        # Build all jobs (symbol x timeframe x year x month)
        for symbol in symbols:
            for tf in all_tfs:
                for year, month in self.iter_months_between(date_from, date_to):
                    tasks.append((symbol, tf, year, month))

        start = time.time()

        results = []

        with ThreadPoolExecutor(max_workers=max_workers) as executor:

            futures = [
                executor.submit(self.synchronize_bars, sym, tf, month, year)
                for sym, tf, year, month in tasks
            ]

            for fut in as_completed(futures):
                res = fut.result()
                results.append(res)

                # sym, tf, y, m, status = res

        elapsed = time.time() - start
        self._info_log(f"Finished synchronization in {elapsed:.2f}s")

    @staticmethod
    def copy_rates_range_from_parquet(
            symbol: str,
            timeframe: int,
            date_from: datetime,
            date_to: datetime,
            polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto",
            broker_data_dir: Optional[str] = config.DEFAULT_BROKER_DATA_PATH,
            logger: Optional[logging.Logger] = None
    ):
        """Copies bars (rates) for a specified symbol, timeframe and date range from locally stored parquet files. This method is used by the simulator to load bars without accessing the MetaTrader5 terminal.

        Args:
            symbol (str): An instrument in the terminal
            timeframe (int): A timeframe to extract bars from
            date_from (datetime): start date of the bars to copy
            date_to (datetime): end date of the bars to copy
            polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
            broker_data_dir (Optional[str], optional): The directory where broker data is stored. Defaults to config.DEFAULT_BROKER_DATA_PATH.
            logger (Optional[logging.Logger], optional): The logger to use. Defaults to None.

        Returns:
            Copied bars as a NumPy array or None in case of a failure
        """

        if date_from > date_to:
            _warning_log("date_from must be <= date_to", logger)
            return None

        timeframe_str = MetaTrader5Constants.TIMEFRAME2STRING_MAP[timeframe]

        files: list[str] = []
        for year, month in HistoryManager.iter_months_between(date_from, date_to):
            file = HistoryManager.bars_file_path(symbol=symbol, timeframe_str=timeframe_str, year=year, month=month,
                                                 broker_data_dir=broker_data_dir)
            if file.exists():
                files.append(str(file))

        if not files:
            _warning_log(
                f"No stored bar history found for {symbol} {timeframe_str} "
                f"between {date_from} and {date_to}", logger
            )
            return None

        t_from = int(date_from.timestamp())
        t_to = int(date_to.timestamp())

        try:
            df = (
                pl.scan_parquet(files)
                .filter(
                    (pl.col("time") >= t_from) &
                    (pl.col("time") <= t_to)
                )
                .sort("time")
                .select([
                    "time",
                    "open",
                    "high",
                    "low",
                    "close",
                    "tick_volume",
                    "spread",
                    "real_volume",
                ])
                .collect(engine=polars_collect_engine)
            )

            if df.is_empty():
                return np.empty(0, dtype=RATES_DTYPE)

            rows = list(
                zip(
                    df["time"].to_list(),
                    df["open"].to_list(),
                    df["high"].to_list(),
                    df["low"].to_list(),
                    df["close"].to_list(),
                    df["tick_volume"].to_list(),
                    df["spread"].to_list(),
                    df["real_volume"].to_list(),
                )
            )

            return np.array(rows, dtype=RATES_DTYPE)

        except Exception as e:
            _warning_log(
                f"Failed to copy stored rates for {symbol} {timeframe_str} "
                f"from {date_from} to {date_to}: {e}", logger
            )
            return None

    @staticmethod
    def _tick_flag_mask(flags: int) -> int:
        if flags == MetaTrader5Constants.COPY_TICKS_ALL:
            return (
                    MetaTrader5Constants.TICK_FLAG_BID
                    | MetaTrader5Constants.TICK_FLAG_ASK
                    | MetaTrader5Constants.TICK_FLAG_LAST
                    | MetaTrader5Constants.TICK_FLAG_VOLUME
                    | MetaTrader5Constants.TICK_FLAG_BUY
                    | MetaTrader5Constants.TICK_FLAG_SELL
            )

        mask = 0
        if flags & MetaTrader5Constants.COPY_TICKS_INFO:
            mask |= MetaTrader5Constants.TICK_FLAG_BID | MetaTrader5Constants.TICK_FLAG_ASK
        if flags & MetaTrader5Constants.COPY_TICKS_TRADE:
            mask |= MetaTrader5Constants.TICK_FLAG_LAST | MetaTrader5Constants.TICK_FLAG_VOLUME

        return mask

    @staticmethod
    def copy_ticks_range_from_parquet(
            symbol: str,
            date_from: datetime,
            date_to: Optional[datetime] = None,
            flags: int = MetaTrader5Constants.COPY_TICKS_ALL,
            limit: Optional[int] = None,
            polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto",
            broker_data_dir: Optional[str] = config.DEFAULT_BROKER_DATA_PATH,
            logger: Optional[logging.Logger] = None,
    ):

        """Copies ticks for a specified symbol, timeframe and date range from locally stored parquet files. This method is used by the simulator to load ticks without accessing the MetaTrader5 terminal.

        Args:
            symbol (str): An instrument in the terminal
            date_from (datetime): start date of the ticks to copy
            date_to (datetime): end date of the ticks to copy
            flags (int): A flag to define the type of the requested ticks. COPY_TICKS_INFO – ticks with Bid and/or Ask changes, COPY_TICKS_TRADE – ticks with changes in Last and Volume, COPY_TICKS_ALL – all ticks. For any type of request, the values of the previous tick are added to the remaining fields of the MqlTick structure.
            limit (int | optional): The maximum number of ticks to collect
            polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
            broker_data_dir (Optional[str], optional): The directory where broker data is stored. Defaults to config.DEFAULT_BROKER_DATA_PATH.
            logger (Optional[logging.Logger], optional): The logger to use. Defaults to None.

        Returns:
            Copied ticks as a NumPy array or None in case of a failure
        """

        if limit is None:
            if date_to is None:
                _warning_log("Either date_to or limit must be provided", logger)
                return None

            if date_from > date_to:
                _warning_log("date_from must be <= date_to", logger)
                return None

        # ---------------- determine months ----------------
        months_iter = (
            HistoryManager.iter_months_between(date_from, date_to)
            if limit is None
            else HistoryManager.iter_months_between(date_from, date_from + timedelta(days=365 * 20))
            # wide range fallback
        )

        files: list[str] = []
        for year, month in months_iter:
            file = HistoryManager.ticks_file_path(symbol, year, month, broker_data_dir)
            if file.exists():
                files.append(str(file))

        if not files:
            _warning_log(f"No stored tick history found for {symbol}", logger)
            return None

        t_from = int(date_from.timestamp())
        t_to = int(date_to.timestamp()) if date_to else None

        flag_mask = HistoryManager._tick_flag_mask(flags)

        try:
            lf = pl.scan_parquet(files)

            # ---------------- filtering ----------------
            lf = lf.filter(pl.col("time") >= t_from)

            if limit is None:
                lf = lf.filter(pl.col("time") <= t_to)

            lf = lf.filter((pl.col("flags") & flag_mask) != 0)

            # ---------------- sorting ----------------
            lf = lf.sort(["time", "time_msc"])

            # ---------------- limit mode ----------------
            if limit is not None:
                lf = lf.limit(limit)

            df = (
                lf.select([
                    "time",
                    "bid",
                    "ask",
                    "last",
                    "volume",
                    "time_msc",
                    "flags",
                    "volume_real",
                ])
                .collect(engine=polars_collect_engine)
            )

            if df.is_empty():
                return np.empty(0, dtype=TICKS_DTYPE)

            return np.array(
                list(zip(
                    df["time"],
                    df["bid"],
                    df["ask"],
                    df["last"],
                    df["volume"],
                    df["time_msc"],
                    df["flags"],
                    df["volume_real"],
                )),
                dtype=TICKS_DTYPE
            )

        except Exception as e:
            _warning_log(
                f"Failed to copy ticks for {symbol} from {date_from}: {e}",
                logger
            )
            return None

    def _ensure_ticks_month(self, symbol, year, month, sync: bool):
        file = self.ticks_file_path(symbol, year, month, self.broker_data_dir)

        if file.exists():
            return True

        if not sync:
            return False

        df = self.synchronize_ticks(symbol, month, year)
        return df is not None

    def _process_symbol_ticks(
            self,
            symbol: str,
            symbol_id: int,
            date_from: datetime,
            date_to: datetime,
            sync: bool,
            polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto"
    ) -> Optional[np.ndarray]:

        files = []

        for year, month in self.iter_months_between(date_from, date_to):

            if not self._ensure_ticks_month(symbol, year, month, sync):
                continue

            file = self.ticks_file_path(symbol, year, month, self.broker_data_dir)
            files.append(str(file))

        if not files:
            return None

        t_from = int(date_from.timestamp())
        t_to = int(date_to.timestamp())

        df = (
            pl.scan_parquet(files)
            .filter(
                (pl.col("time") >= t_from) &
                (pl.col("time") <= t_to)
            )
            .collect(engine=polars_collect_engine)
        )

        if df.is_empty():
            return None

        df = df.with_columns(
            pl.lit(symbol_id).alias("symbol_id")
        )

        return np.array(
            list(zip(
                df["time"].to_list(),
                df["bid"].to_list(),
                df["ask"].to_list(),
                df["last"].to_list(),
                df["volume"].to_list(),
                df["time_msc"].to_list(),
                df["flags"].to_list(),
                df["volume_real"].to_list(),
                df["symbol_id"].to_list(),
            )),
            dtype=MULTI_TICKS_DTYPE
        )

    def _ensure_bars_month(self, symbol: str, timeframe: int, year: int, month: int, sync: bool):
        file = self.bars_file_path(
            symbol,
            MetaTrader5Constants.TIMEFRAME2STRING_MAP[timeframe],
            year,
            month,
            self.broker_data_dir,
        )

        if file.exists():
            return True

        if not sync:
            return False

        df = self.synchronize_bars(symbol, timeframe, month, year)
        return df is not None

    def _process_symbol_bars(
            self,
            symbol: str,
            symbol_id: int,
            timeframe: int,
            date_from: datetime,
            date_to: datetime,
            sync: bool,
            polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto"
    ) -> Optional[np.ndarray]:

        tf_str = MetaTrader5Constants.TIMEFRAME2STRING_MAP[timeframe]

        files = []

        for year, month in self.iter_months_between(date_from, date_to):

            if not self._ensure_bars_month(symbol, timeframe, year, month, sync):
                continue

            file = self.bars_file_path(symbol, tf_str, year, month, self.broker_data_dir)
            files.append(str(file))

        if not files:
            return None

        t_from = int(date_from.timestamp())
        t_to = int(date_to.timestamp())

        df = (
            pl.scan_parquet(files)
            .filter(
                (pl.col("time") >= t_from) &
                (pl.col("time") <= t_to)
            )
            .collect(engine=polars_collect_engine)
        )

        if df.is_empty():
            return None

        df = df.with_columns(
            pl.lit(symbol_id).alias("symbol_id")
        )

        return np.array(
            list(zip(
                df["time"].to_list(),
                df["open"].to_list(),
                df["high"].to_list(),
                df["low"].to_list(),
                df["close"].to_list(),
                df["tick_volume"].to_list(),
                df["spread"].to_list(),
                df["real_volume"].to_list(),
                df["symbol_id"].to_list(),
            )),
            dtype=MULTI_RATES_DTYPE
        )

    def load_ticks_lazy_multi(
            self,
            symbols: list[str],
            date_from: datetime,
            date_to: datetime,
            sync: bool = True,
    ) -> pl.LazyFrame:

        symbol_to_id = {s: i for i, s in enumerate(symbols)}

        lazy_frames = []

        for symbol in symbols:
            files = []

            for year, month in self.iter_months_between(date_from, date_to):
                if not self._ensure_ticks_month(symbol, year, month, sync):
                    continue

                file = self.ticks_file_path(symbol, year, month, self.broker_data_dir)
                files.append(str(file))

            if not files:
                continue

            lf = (
                pl.scan_parquet(files)
                .filter(
                    (pl.col("time") >= int(date_from.timestamp())) &
                    (pl.col("time") <= int(date_to.timestamp()))
                )
                .with_columns(pl.lit(symbol_to_id[symbol]).alias("symbol_id"))
            )

            lazy_frames.append(lf)

        if not lazy_frames:
            err = "No tick data found"
            self.logger.critical(err)
            raise RuntimeError(err)

        return pl.concat(lazy_frames)

    def build_tick_stream(
            self,
            symbols: list[str],
            date_from: datetime,
            date_to: datetime,
            sync: bool = True,
            polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto"
    ) -> pl.DataFrame:

        """Builds a tick stream for specified symbols and date range. This method is used by the simulator to load ticks without accessing the MetaTrader5 terminal.

        Args:
            symbols (list[str]): A list of symbols to load ticks for
            date_from (datetime): start date of the ticks to load
            date_to (datetime): end date of the ticks to load
            sync (bool): whether to synchronize missing months from the terminal. Defaults to True.
            polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
        """

        lf = self.load_ticks_lazy_multi(symbols, date_from, date_to, sync)

        df = (
            lf
            .sort(["time", "time_msc"])  # global ordering
            .select([
                "time",
                "bid",
                "ask",
                "last",
                "volume",
                "time_msc",
                "flags",
                "volume_real",
                "symbol_id",
            ])
            .collect(engine=polars_collect_engine)  # âš¡ critical
        )

        return df

    def load_bars_lazy_multi(
            self,
            symbols: list[str],
            timeframe: int,
            date_from: datetime,
            date_to: datetime,
            sync: bool = True,
    ) -> pl.LazyFrame:

        symbol_to_id = {s: i for i, s in enumerate(symbols)}
        tf_str = MetaTrader5Constants.TIMEFRAME2STRING_MAP[timeframe]

        lazy_frames = []

        for symbol in symbols:
            files = []

            for year, month in self.iter_months_between(date_from, date_to):
                if not self._ensure_bars_month(symbol, timeframe, year, month, sync):
                    continue

                file = self.bars_file_path(symbol, tf_str, year, month, self.broker_data_dir)
                files.append(str(file))

            if not files:
                continue

            lf = (
                pl.scan_parquet(files)
                .filter(
                    (pl.col("time") >= int(date_from.timestamp())) &
                    (pl.col("time") <= int(date_to.timestamp()))
                )
                .with_columns(pl.lit(symbol_to_id[symbol]).alias("symbol_id"))
            )

            lazy_frames.append(lf)

        if not lazy_frames:
            err = "No bars data found"
            self.logger.critical(err)
            raise RuntimeError(err)

        return pl.concat(lazy_frames)

    def build_bar_stream(
            self,
            symbols: list[str],
            timeframe: int,
            date_from: datetime,
            date_to: datetime,
            sync: bool = True,
            polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto"
    ) -> pl.DataFrame:

        """Builds a bar stream for specified symbols and date range. This method is used by the simulator to load bars without accessing the MetaTrader5 terminal.

        Args:
            symbols (list[str]): A list of symbols to load bars for
            timeframe (int): The timeframe for the bars
            date_from (datetime): start date of the bars to load
            date_to (datetime): end date of the bars to load
            sync (bool): whether to synchronize missing months from the terminal. Defaults to True.
            polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
        """

        lf = self.load_bars_lazy_multi(symbols, timeframe, date_from, date_to, sync)

        df = (
            lf
            .sort(["time", "symbol_id"])  # bars don't need time_msc
            .select([
                "time",
                "open",
                "high",
                "low",
                "close",
                "tick_volume",
                "spread",
                "real_volume",
                "symbol_id",
            ])
            .collect(engine=polars_collect_engine)
        )

        return df

__init__(mt5_instance=None, broker_data_path=config.DEFAULT_BROKER_DATA_PATH, logger=None)

A class for managing historical market data for the simulation. It provides methods for synchronizing bars and ticks from the MetaTrader5 terminal, as well as loading them from locally stored parquet files. Args: mt5_instance (MetaTrader5) : Initialized MetaTrader5 instance to extract bars from broker_data_path (Optional |str): A directory where the synchronized bars and ticks will be stored as parquet files. Defaults to strategytester5.config.DEFAULT_BROKER_DATA_PATH logger (Optional[logging.Logger], optional): The logger to use. Defaults to None.

Source code in strategytester5\MetaTrader5\data.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def __init__(self, mt5_instance: Optional[Any] = None,
             broker_data_path: Optional[str] = config.DEFAULT_BROKER_DATA_PATH,
             logger: Optional[logging.Logger] = None):

    """
    A class for managing historical market data for the simulation. It provides methods for synchronizing bars and ticks from the MetaTrader5 terminal, as well as loading them from locally stored parquet files.
    Args:
        mt5_instance (MetaTrader5) : Initialized MetaTrader5 instance to extract bars from
        broker_data_path (Optional |str): A directory where the synchronized bars and ticks will be stored as parquet files. Defaults to `strategytester5.config.DEFAULT_BROKER_DATA_PATH`
        logger (Optional[logging.Logger], optional): The logger to use. Defaults to None.
    """

    self.mt5_instance = mt5_instance
    self.broker_data_dir = broker_data_path
    self.logger = logger
    self._last_start_date: int
    self._last_end_date: int

build_bar_stream(symbols, timeframe, date_from, date_to, sync=True, polars_collect_engine='auto')

Builds a bar stream for specified symbols and date range. This method is used by the simulator to load bars without accessing the MetaTrader5 terminal.

Parameters:

Name Type Description Default
symbols list[str]

A list of symbols to load bars for

required
timeframe int

The timeframe for the bars

required
date_from datetime

start date of the bars to load

required
date_to datetime

end date of the bars to load

required
sync bool

whether to synchronize missing months from the terminal. Defaults to True.

True
polars_collect_engine Literal['auto', 'in-memory', 'streaming', 'gpu']

Polars collection engine to use. Defaults to "auto".

'auto'
Source code in strategytester5\MetaTrader5\data.py
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
def build_bar_stream(
        self,
        symbols: list[str],
        timeframe: int,
        date_from: datetime,
        date_to: datetime,
        sync: bool = True,
        polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto"
) -> pl.DataFrame:

    """Builds a bar stream for specified symbols and date range. This method is used by the simulator to load bars without accessing the MetaTrader5 terminal.

    Args:
        symbols (list[str]): A list of symbols to load bars for
        timeframe (int): The timeframe for the bars
        date_from (datetime): start date of the bars to load
        date_to (datetime): end date of the bars to load
        sync (bool): whether to synchronize missing months from the terminal. Defaults to True.
        polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
    """

    lf = self.load_bars_lazy_multi(symbols, timeframe, date_from, date_to, sync)

    df = (
        lf
        .sort(["time", "symbol_id"])  # bars don't need time_msc
        .select([
            "time",
            "open",
            "high",
            "low",
            "close",
            "tick_volume",
            "spread",
            "real_volume",
            "symbol_id",
        ])
        .collect(engine=polars_collect_engine)
    )

    return df

build_tick_stream(symbols, date_from, date_to, sync=True, polars_collect_engine='auto')

Builds a tick stream for specified symbols and date range. This method is used by the simulator to load ticks without accessing the MetaTrader5 terminal.

Parameters:

Name Type Description Default
symbols list[str]

A list of symbols to load ticks for

required
date_from datetime

start date of the ticks to load

required
date_to datetime

end date of the ticks to load

required
sync bool

whether to synchronize missing months from the terminal. Defaults to True.

True
polars_collect_engine Literal['auto', 'in-memory', 'streaming', 'gpu']

Polars collection engine to use. Defaults to "auto".

'auto'
Source code in strategytester5\MetaTrader5\data.py
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
def build_tick_stream(
        self,
        symbols: list[str],
        date_from: datetime,
        date_to: datetime,
        sync: bool = True,
        polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto"
) -> pl.DataFrame:

    """Builds a tick stream for specified symbols and date range. This method is used by the simulator to load ticks without accessing the MetaTrader5 terminal.

    Args:
        symbols (list[str]): A list of symbols to load ticks for
        date_from (datetime): start date of the ticks to load
        date_to (datetime): end date of the ticks to load
        sync (bool): whether to synchronize missing months from the terminal. Defaults to True.
        polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
    """

    lf = self.load_ticks_lazy_multi(symbols, date_from, date_to, sync)

    df = (
        lf
        .sort(["time", "time_msc"])  # global ordering
        .select([
            "time",
            "bid",
            "ask",
            "last",
            "volume",
            "time_msc",
            "flags",
            "volume_real",
            "symbol_id",
        ])
        .collect(engine=polars_collect_engine)  # âš¡ critical
    )

    return df

copy_rates_range_from_parquet(symbol, timeframe, date_from, date_to, polars_collect_engine='auto', broker_data_dir=config.DEFAULT_BROKER_DATA_PATH, logger=None) staticmethod

Copies bars (rates) for a specified symbol, timeframe and date range from locally stored parquet files. This method is used by the simulator to load bars without accessing the MetaTrader5 terminal.

Parameters:

Name Type Description Default
symbol str

An instrument in the terminal

required
timeframe int

A timeframe to extract bars from

required
date_from datetime

start date of the bars to copy

required
date_to datetime

end date of the bars to copy

required
polars_collect_engine Literal['auto', 'in-memory', 'streaming', 'gpu']

Polars collection engine to use. Defaults to "auto".

'auto'
broker_data_dir Optional[str]

The directory where broker data is stored. Defaults to config.DEFAULT_BROKER_DATA_PATH.

DEFAULT_BROKER_DATA_PATH
logger Optional[Logger]

The logger to use. Defaults to None.

None

Returns:

Type Description

Copied bars as a NumPy array or None in case of a failure

Source code in strategytester5\MetaTrader5\data.py
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
@staticmethod
def copy_rates_range_from_parquet(
        symbol: str,
        timeframe: int,
        date_from: datetime,
        date_to: datetime,
        polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto",
        broker_data_dir: Optional[str] = config.DEFAULT_BROKER_DATA_PATH,
        logger: Optional[logging.Logger] = None
):
    """Copies bars (rates) for a specified symbol, timeframe and date range from locally stored parquet files. This method is used by the simulator to load bars without accessing the MetaTrader5 terminal.

    Args:
        symbol (str): An instrument in the terminal
        timeframe (int): A timeframe to extract bars from
        date_from (datetime): start date of the bars to copy
        date_to (datetime): end date of the bars to copy
        polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
        broker_data_dir (Optional[str], optional): The directory where broker data is stored. Defaults to config.DEFAULT_BROKER_DATA_PATH.
        logger (Optional[logging.Logger], optional): The logger to use. Defaults to None.

    Returns:
        Copied bars as a NumPy array or None in case of a failure
    """

    if date_from > date_to:
        _warning_log("date_from must be <= date_to", logger)
        return None

    timeframe_str = MetaTrader5Constants.TIMEFRAME2STRING_MAP[timeframe]

    files: list[str] = []
    for year, month in HistoryManager.iter_months_between(date_from, date_to):
        file = HistoryManager.bars_file_path(symbol=symbol, timeframe_str=timeframe_str, year=year, month=month,
                                             broker_data_dir=broker_data_dir)
        if file.exists():
            files.append(str(file))

    if not files:
        _warning_log(
            f"No stored bar history found for {symbol} {timeframe_str} "
            f"between {date_from} and {date_to}", logger
        )
        return None

    t_from = int(date_from.timestamp())
    t_to = int(date_to.timestamp())

    try:
        df = (
            pl.scan_parquet(files)
            .filter(
                (pl.col("time") >= t_from) &
                (pl.col("time") <= t_to)
            )
            .sort("time")
            .select([
                "time",
                "open",
                "high",
                "low",
                "close",
                "tick_volume",
                "spread",
                "real_volume",
            ])
            .collect(engine=polars_collect_engine)
        )

        if df.is_empty():
            return np.empty(0, dtype=RATES_DTYPE)

        rows = list(
            zip(
                df["time"].to_list(),
                df["open"].to_list(),
                df["high"].to_list(),
                df["low"].to_list(),
                df["close"].to_list(),
                df["tick_volume"].to_list(),
                df["spread"].to_list(),
                df["real_volume"].to_list(),
            )
        )

        return np.array(rows, dtype=RATES_DTYPE)

    except Exception as e:
        _warning_log(
            f"Failed to copy stored rates for {symbol} {timeframe_str} "
            f"from {date_from} to {date_to}: {e}", logger
        )
        return None

copy_ticks_range_from_parquet(symbol, date_from, date_to=None, flags=MetaTrader5Constants.COPY_TICKS_ALL, limit=None, polars_collect_engine='auto', broker_data_dir=config.DEFAULT_BROKER_DATA_PATH, logger=None) staticmethod

Copies ticks for a specified symbol, timeframe and date range from locally stored parquet files. This method is used by the simulator to load ticks without accessing the MetaTrader5 terminal.

Parameters:

Name Type Description Default
symbol str

An instrument in the terminal

required
date_from datetime

start date of the ticks to copy

required
date_to datetime

end date of the ticks to copy

None
flags int

A flag to define the type of the requested ticks. COPY_TICKS_INFO – ticks with Bid and/or Ask changes, COPY_TICKS_TRADE – ticks with changes in Last and Volume, COPY_TICKS_ALL – all ticks. For any type of request, the values of the previous tick are added to the remaining fields of the MqlTick structure.

COPY_TICKS_ALL
limit int | optional

The maximum number of ticks to collect

None
polars_collect_engine Literal['auto', 'in-memory', 'streaming', 'gpu']

Polars collection engine to use. Defaults to "auto".

'auto'
broker_data_dir Optional[str]

The directory where broker data is stored. Defaults to config.DEFAULT_BROKER_DATA_PATH.

DEFAULT_BROKER_DATA_PATH
logger Optional[Logger]

The logger to use. Defaults to None.

None

Returns:

Type Description

Copied ticks as a NumPy array or None in case of a failure

Source code in strategytester5\MetaTrader5\data.py
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
@staticmethod
def copy_ticks_range_from_parquet(
        symbol: str,
        date_from: datetime,
        date_to: Optional[datetime] = None,
        flags: int = MetaTrader5Constants.COPY_TICKS_ALL,
        limit: Optional[int] = None,
        polars_collect_engine: Literal["auto", "in-memory", "streaming", "gpu"] = "auto",
        broker_data_dir: Optional[str] = config.DEFAULT_BROKER_DATA_PATH,
        logger: Optional[logging.Logger] = None,
):

    """Copies ticks for a specified symbol, timeframe and date range from locally stored parquet files. This method is used by the simulator to load ticks without accessing the MetaTrader5 terminal.

    Args:
        symbol (str): An instrument in the terminal
        date_from (datetime): start date of the ticks to copy
        date_to (datetime): end date of the ticks to copy
        flags (int): A flag to define the type of the requested ticks. COPY_TICKS_INFO – ticks with Bid and/or Ask changes, COPY_TICKS_TRADE – ticks with changes in Last and Volume, COPY_TICKS_ALL – all ticks. For any type of request, the values of the previous tick are added to the remaining fields of the MqlTick structure.
        limit (int | optional): The maximum number of ticks to collect
        polars_collect_engine (Literal["auto", "in-memory", "streaming", "gpu"], optional): Polars collection engine to use. Defaults to "auto".
        broker_data_dir (Optional[str], optional): The directory where broker data is stored. Defaults to config.DEFAULT_BROKER_DATA_PATH.
        logger (Optional[logging.Logger], optional): The logger to use. Defaults to None.

    Returns:
        Copied ticks as a NumPy array or None in case of a failure
    """

    if limit is None:
        if date_to is None:
            _warning_log("Either date_to or limit must be provided", logger)
            return None

        if date_from > date_to:
            _warning_log("date_from must be <= date_to", logger)
            return None

    # ---------------- determine months ----------------
    months_iter = (
        HistoryManager.iter_months_between(date_from, date_to)
        if limit is None
        else HistoryManager.iter_months_between(date_from, date_from + timedelta(days=365 * 20))
        # wide range fallback
    )

    files: list[str] = []
    for year, month in months_iter:
        file = HistoryManager.ticks_file_path(symbol, year, month, broker_data_dir)
        if file.exists():
            files.append(str(file))

    if not files:
        _warning_log(f"No stored tick history found for {symbol}", logger)
        return None

    t_from = int(date_from.timestamp())
    t_to = int(date_to.timestamp()) if date_to else None

    flag_mask = HistoryManager._tick_flag_mask(flags)

    try:
        lf = pl.scan_parquet(files)

        # ---------------- filtering ----------------
        lf = lf.filter(pl.col("time") >= t_from)

        if limit is None:
            lf = lf.filter(pl.col("time") <= t_to)

        lf = lf.filter((pl.col("flags") & flag_mask) != 0)

        # ---------------- sorting ----------------
        lf = lf.sort(["time", "time_msc"])

        # ---------------- limit mode ----------------
        if limit is not None:
            lf = lf.limit(limit)

        df = (
            lf.select([
                "time",
                "bid",
                "ask",
                "last",
                "volume",
                "time_msc",
                "flags",
                "volume_real",
            ])
            .collect(engine=polars_collect_engine)
        )

        if df.is_empty():
            return np.empty(0, dtype=TICKS_DTYPE)

        return np.array(
            list(zip(
                df["time"],
                df["bid"],
                df["ask"],
                df["last"],
                df["volume"],
                df["time_msc"],
                df["flags"],
                df["volume_real"],
            )),
            dtype=TICKS_DTYPE
        )

    except Exception as e:
        _warning_log(
            f"Failed to copy ticks for {symbol} from {date_from}: {e}",
            logger
        )
        return None

synchronize_all_timeframes(symbols, date_from, date_to, max_workers=4)

Synchronizes bars from all timeframes from the MetaTrader5 terminal for specified symbols and date range. This method is useful for pre-populating the local storage with bars from all timeframes, so the simulator can later load them without accessing the terminal.

Parameters:

Name Type Description Default
symbols Iterable[str]

A list of symbols to synchronize

required
date_from datetime

The start date for synchronization

required
date_to datetime

The end date for synchronization

required
max_workers int

The maximum number of worker threads to use

4
Source code in strategytester5\MetaTrader5\data.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def synchronize_all_timeframes(
        self,
        symbols: Iterable[str],
        date_from: datetime,
        date_to: datetime,
        max_workers: int = 4,
):

    """
    Synchronizes bars from all timeframes from the MetaTrader5 terminal for specified symbols and date range. This method is useful for pre-populating the local storage with bars from all timeframes, so the simulator can later load them without accessing the terminal.

    Args:
        symbols (Iterable[str]): A list of symbols to synchronize
        date_from (datetime): The start date for synchronization
        date_to (datetime): The end date for synchronization
        max_workers (int): The maximum number of worker threads to use

    """

    if date_from > date_to:
        self._warning_log("date_from must be <= date_to")
        return

    all_tfs = list(MetaTrader5Constants.STRING2TIMEFRAME_MAP.values())

    self._info_log("Synchronizing all timeframes...")

    tasks = []

    # Build all jobs (symbol x timeframe x year x month)
    for symbol in symbols:
        for tf in all_tfs:
            for year, month in self.iter_months_between(date_from, date_to):
                tasks.append((symbol, tf, year, month))

    start = time.time()

    results = []

    with ThreadPoolExecutor(max_workers=max_workers) as executor:

        futures = [
            executor.submit(self.synchronize_bars, sym, tf, month, year)
            for sym, tf, year, month in tasks
        ]

        for fut in as_completed(futures):
            res = fut.result()
            results.append(res)

            # sym, tf, y, m, status = res

    elapsed = time.time() - start
    self._info_log(f"Finished synchronization in {elapsed:.2f}s")

synchronize_bars(symbol, timeframe, month, year)

Extracts bars (rates) from the MetaTrader 5 terminal, stores them in a nearby location for simulator usage.

Returns:

Type Description
Optional[DataFrame]

Synchronized bars in a polars DataFrame object

Parameters:

Name Type Description Default
timeframe (int)

A timeframe to extract bars from

required
symbol (str)

An instrument in the terminal

required
month (int)

The entire month to synchronize

required
year (int)

A year which a specified month belongs

required
Source code in strategytester5\MetaTrader5\data.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
def synchronize_bars(self,
                     symbol: str,
                     timeframe: int,
                     month: int,
                     year: int,
                     ) -> Optional[pl.DataFrame]:
    """
    Extracts bars (rates) from the MetaTrader 5 terminal, stores them in a nearby location for simulator usage.

    Returns:
        Synchronized bars in a polars DataFrame object

    Args:
        timeframe (int) : A timeframe to extract bars from
        symbol (str) : An instrument in the terminal
        month (int) : The entire month to synchronize
        year (int) : A year which a specified `month` belongs
    """

    if self.mt5_instance is None:
        log = ("Cannot synchronize Bars from MetaTrader5, due to an invalid MetaTrader5 instance.\n"
               "If the default MetaTrader5 wasn't installed initially, run `pip install strategytester5[mt5]`")

        self._critical_log(log)
        raise RuntimeError(log)

    start = self.month_start(month, year)
    end = self.next_month(month, year)

    # add it to the market watch
    if not self.mt5_instance.symbol_select(symbol, True):
        err = f"Failed to select or add {symbol} to the MarketWatch, mt5 error = {self.mt5_instance.last_error()}"
        self._error_log(err)
        return None

    self._info_log(f"Fetching bars from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}")

    # with self._mt5_lock:
    rates = self.mt5_instance.copy_rates_range(symbol, timeframe, start, end)
    if rates is None or len(rates) == 0:
        warn = f"No bars were received from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}"
        self._warning_log(warn)
        return None

    # rates dataframe
    df = pl.DataFrame(rates)

    file = self.bars_file_path(symbol=symbol, timeframe_str=MetaTrader5Constants.TIMEFRAME2STRING_MAP[timeframe],
                               year=year, month=month,
                               broker_data_dir=self.broker_data_dir)
    file.parent.mkdir(parents=True, exist_ok=True)
    df.write_parquet(file)

    return df

synchronize_ticks(symbol, month, year)

Extracts ticks from the MetaTrader 5 terminal, stores them in a nearby location for simulator usage.

Returns:

Type Description
Optional[DataFrame]

Synchronized ticks in a polars DataFrame object

Parameters:

Name Type Description Default
symbol (str)

An instrument in the terminal

required
month (int)

The entire month to synchronize

required
year (int)

A year which a specified month belongs

required
Source code in strategytester5\MetaTrader5\data.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def synchronize_ticks(
        self,
        symbol: str,
        month: int,
        year: int,
) -> Optional[pl.DataFrame]:
    """
    Extracts ticks from the MetaTrader 5 terminal, stores them in a nearby location for simulator usage.

    Returns:
        Synchronized ticks in a polars DataFrame object

    Args:
        symbol (str) : An instrument in the terminal
        month (int) : The entire month to synchronize
        year (int) : A year which a specified `month` belongs
    """

    if self.mt5_instance is None:
        log = ("Cannot synchronize ticks from MetaTrader5, due to an invalid MetaTrader5 instance.\n"
               "If the default MetaTrader5 wasn't installed initially, run `pip install strategytester5[mt5]`")

        self._critical_log(log)
        raise RuntimeError(log)

    start = self.month_start(month, year)
    end = self.next_month(month, year)

    # add it to the market watch
    if not self.mt5_instance.symbol_select(symbol, True):
        err = f"Failed to select or add {symbol} to the MarketWatch, mt5 error = {self.mt5_instance.last_error()}"
        self._error_log(err)
        return None

    self._info_log(f"Fetching ticks from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}")

    # with self._mt5_lock:
    ticks = self.mt5_instance.copy_ticks_range(symbol, start, end, self.mt5_instance.COPY_TICKS_ALL)
    if ticks is None or len(ticks) == 0:
        warn = f"No ticks were received from MetaTrader5 from {symbol} for: {year:04d}-{month:02d}"
        self._warning_log(warn)
        return None

    # rates dataframe
    df = pl.DataFrame(ticks)

    file = self.ticks_file_path(symbol, year=year, month=month, broker_data_dir=self.broker_data_dir)
    file.parent.mkdir(parents=True, exist_ok=True)
    df.write_parquet(file)

    return df