11"""Module for retrieving and managing historical price data from MetaTrader 5.
2+
23Provides the Rates class for accessing historical price information.
34"""
45
56from __future__ import annotations
67
7- from typing import Union
8-
98import MetaTrader5 as Mt5
109
1110
1211class Rates :
1312 """Represents historical price data for a financial instrument."""
1413
1514 def __init__ (self , symbol : str , time_frame : int , start_position : int , count : int ) -> None :
16- """Initialize a Rates object.
15+ """Initializes a Rates object.
1716
1817 Args:
1918 symbol (str): The financial instrument symbol.
@@ -24,15 +23,14 @@ def __init__(self, symbol: str, time_frame: int, start_position: int, count: int
2423 Returns:
2524 None
2625 """
27- self ._symbol = symbol
28- self ._time_frame = time_frame
29- self ._start_position = start_position
30- self ._count = count
26+
27+ def _raise_value_error (msg : str ) -> None :
28+ raise ValueError (msg )
3129
3230 try :
3331 rates = Mt5 .copy_rates_from_pos (symbol , time_frame , start_position , count )
3432 if rates is None :
35- raise ValueError (f"Failed to retrieve rates for { symbol } " )
33+ _raise_value_error (f"Failed to retrieve rates for { symbol } " )
3634
3735 self ._time = [rate [0 ] for rate in rates ]
3836 self ._open = [rate [1 ] for rate in rates ]
@@ -42,11 +40,11 @@ def __init__(self, symbol: str, time_frame: int, start_position: int, count: int
4240 self ._tick_volume = [rate [5 ] for rate in rates ]
4341 self ._spread = [rate [6 ] for rate in rates ]
4442 self ._real_volume = [rate [7 ] for rate in rates ]
45- except Exception as e :
46- raise ValueError (f"Failed to create Rates object for symbol { symbol } . Error: { e } " )
43+ except Mt5 . Error as e :
44+ raise ValueError (f"Failed to create Rates object for symbol { symbol } " ) from e
4745
4846 @property
49- def time (self ) -> list [Union [ int , float ] ]:
47+ def time (self ) -> list [int | float ]:
5048 """List of timestamps."""
5149 return self ._time
5250
0 commit comments