def __post_init__ ( self ) -> None :
- """Normalize accepted timeframe aliases to the stored integer value."""
- if not isinstance ( self . timeframe , int ):
- object . __setattr__ ( self , "timeframe" , parse_timeframe ( self . timeframe ))
+ def __post_init__ ( self ) -> None :
+ """Normalize accepted timeframe aliases to the stored integer value."""
+ if not isinstance ( self . timeframe , int ):
+ object . __setattr__ ( self , "timeframe" , parse_timeframe ( self . timeframe ))
@@ -725,49 +733,51 @@ by an explicit table (for example a custom SQLite view).
Source code in mt5cli/history.py
- def append_dataframe (
- conn : sqlite3 . Connection ,
- frame : pd . DataFrame ,
- table_name : str ,
- if_exists : IfExists ,
-) -> bool :
- """Append a DataFrame to SQLite when it has a schema.
-
- Returns:
- True if a table was written, False if the frame had no columns.
- """
- if len ( frame . columns ) == 0 :
- logger . warning ( "Skipping %s : dataset returned no columns" , table_name )
- return False
- frame . to_sql ( # type: ignore[reportUnknownMemberType]
- table_name ,
- conn ,
- if_exists = if_exists . value ,
- index = False ,
- chunksize = 50_000 ,
- )
- return True
+ def append_dataframe (
+ conn : sqlite3 . Connection ,
+ frame : pd . DataFrame ,
+ table_name : str ,
+ if_exists : IfExists ,
+) -> bool :
+ """Append a DataFrame to SQLite when it has a schema.
+
+ Returns:
+ True if a table was written, False if the frame had no columns.
+ """
+ if len ( frame . columns ) == 0 :
+ logger . warning ( "Skipping %s : dataset returned no columns" , table_name )
+ return False
+ writable = _canonicalize_sqlite_time_columns ( frame )
+ writable . to_sql ( # type: ignore[reportUnknownMemberType]
+ table_name ,
+ conn ,
+ if_exists = if_exists . value ,
+ index = False ,
+ chunksize = 50_000 ,
+ )
+ return True
@@ -796,33 +806,33 @@ by an explicit table (for example a custom SQLite view).
Source code in mt5cli/history.py
- def augment_written_columns_from_sqlite (
- conn : sqlite3 . Connection ,
- datasets : set [ Dataset ],
- written_columns : dict [ Dataset , set [ str ]],
-) -> None :
- """Add existing table columns to the written column map."""
- for dataset in datasets :
- columns = get_table_columns ( conn , dataset . table_name )
- if not columns :
- continue
- if dataset in written_columns :
- written_columns [ dataset ] . update ( columns )
- else :
- written_columns [ dataset ] = columns
+ def augment_written_columns_from_sqlite (
+ conn : sqlite3 . Connection ,
+ datasets : set [ Dataset ],
+ written_columns : dict [ Dataset , set [ str ]],
+) -> None :
+ """Add existing table columns to the written column map."""
+ for dataset in datasets :
+ columns = get_table_columns ( conn , dataset . table_name )
+ if not columns :
+ continue
+ if dataset in written_columns :
+ written_columns [ dataset ] . update ( columns )
+ else :
+ written_columns [ dataset ] = columns
@@ -988,14 +998,7 @@ with symbol=None for each timeframe instead of raising.
Source code in mt5cli/history.py
- 561
-562
-563
-564
-565
-566
-567
-568
+ def build_rate_targets (
- symbols : Sequence [ str ],
- timeframes : Sequence [ int | str ],
- * ,
- allow_missing_symbol : bool = False ,
-) -> list [ RateTarget ]:
- """Build rate targets for every symbol and timeframe combination.
-
- Args:
- symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``.
- timeframes: MT5 timeframes as integers or names (for example ``M1``).
- allow_missing_symbol: When True and ``symbols`` is empty, build targets
- with ``symbol=None`` for each timeframe instead of raising.
-
- Returns:
- Targets in row-major order: every timeframe for the first symbol, then
- every timeframe for the next symbol, and so on.
-
- Raises:
- ValueError: If ``timeframes`` is empty, or ``symbols`` is empty and
- ``allow_missing_symbol`` is False.
- """
- if not timeframes :
- msg = "At least one timeframe is required."
- raise ValueError ( msg )
- if not symbols :
- if not allow_missing_symbol :
- msg = "At least one symbol is required."
- raise ValueError ( msg )
- return [ RateTarget ( symbol = None , timeframe = tf ) for tf in timeframes ]
- return [
- RateTarget ( symbol = symbol , timeframe = tf )
- for symbol in symbols
- for tf in timeframes
- ]
+595
+596
+597
+598
+599
+600
+601
+602 def build_rate_targets (
+ symbols : Sequence [ str ],
+ timeframes : Sequence [ int | str ],
+ * ,
+ allow_missing_symbol : bool = False ,
+) -> list [ RateTarget ]:
+ """Build rate targets for every symbol and timeframe combination.
+
+ Args:
+ symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``.
+ timeframes: MT5 timeframes as integers or names (for example ``M1``).
+ allow_missing_symbol: When True and ``symbols`` is empty, build targets
+ with ``symbol=None`` for each timeframe instead of raising.
+
+ Returns:
+ Targets in row-major order: every timeframe for the first symbol, then
+ every timeframe for the next symbol, and so on.
+
+ Raises:
+ ValueError: If ``timeframes`` is empty, or ``symbols`` is empty and
+ ``allow_missing_symbol`` is False.
+ """
+ if not timeframes :
+ msg = "At least one timeframe is required."
+ raise ValueError ( msg )
+ if not symbols :
+ if not allow_missing_symbol :
+ msg = "At least one symbol is required."
+ raise ValueError ( msg )
+ return [ RateTarget ( symbol = None , timeframe = tf ) for tf in timeframes ]
+ return [
+ RateTarget ( symbol = symbol , timeframe = tf )
+ for symbol in symbols
+ for tf in timeframes
+ ]
@@ -1090,14 +1100,7 @@ a symbol such as EURUSD_M1 cannot collide with EURUSD
Source code in mt5cli/history.py
- 133
-134
-135
-136
-137
-138
-139
-140
+ def build_rate_view_name (
- * ,
- symbol : str ,
- granularity : str ,
- granularity_count : int ,
- timeframe : int ,
-) -> str :
- """Return a collision-free offline optimize view name.
-
- View names always include the timeframe integer after a ``__`` separator so
- a symbol such as ``EURUSD_M1`` cannot collide with ``EURUSD`` at timeframe
- ``M1``.
- """
- if granularity_count == 1 :
- return f "rate_ { symbol } __ { timeframe } "
- return f "rate_ { symbol } __ { granularity } _ { timeframe } "
+148
+149
+150
+151
+152
+153
+154
+155 def build_rate_view_name (
+ * ,
+ symbol : str ,
+ granularity : str ,
+ granularity_count : int ,
+ timeframe : int ,
+) -> str :
+ """Return a collision-free offline optimize view name.
+
+ View names always include the timeframe integer after a ``__`` separator so
+ a symbol such as ``EURUSD_M1`` cannot collide with ``EURUSD`` at timeframe
+ ``M1``.
+ """
+ if granularity_count == 1 :
+ return f "rate_ { symbol } __ { timeframe } "
+ return f "rate_ { symbol } __ { granularity } _ { timeframe } "
@@ -1170,41 +1180,41 @@ a symbol such as EURUSD_M1 cannot collide with EURUSD
Source code in mt5cli/history.py
- def create_cash_events_view (
- conn : sqlite3 . Connection ,
- deals_columns : set [ str ],
-) -> bool :
- """Create the cash_events SQLite view derived from history_deals.
-
- Returns:
- True if the view was created, False if required columns are missing.
- """
- if "type" not in deals_columns :
- logger . warning ( "Skipping cash_events view: history_deals.type is missing" )
- return False
- conn . execute ( "DROP VIEW IF EXISTS cash_events" )
- conn . execute (
- "CREATE VIEW cash_events AS" # noqa: S608
- f " SELECT * FROM history_deals WHERE type NOT IN { _TRADE_DEAL_TYPES_SQL } " ,
- )
- return True
+ def create_cash_events_view (
+ conn : sqlite3 . Connection ,
+ deals_columns : set [ str ],
+) -> bool :
+ """Create the cash_events SQLite view derived from history_deals.
+
+ Returns:
+ True if the view was created, False if required columns are missing.
+ """
+ if "type" not in deals_columns :
+ logger . warning ( "Skipping cash_events view: history_deals.type is missing" )
+ return False
+ conn . execute ( "DROP VIEW IF EXISTS cash_events" )
+ conn . execute (
+ "CREATE VIEW cash_events AS" # noqa: S608
+ f " SELECT * FROM history_deals WHERE type NOT IN { _TRADE_DEAL_TYPES_SQL } " ,
+ )
+ return True
@@ -1232,51 +1242,51 @@ a symbol such as EURUSD_M1 cannot collide with EURUSD
Source code in mt5cli/history.py
- def create_history_indexes (
- conn : sqlite3 . Connection ,
- written_columns : dict [ Dataset , set [ str ]],
-) -> None :
- """Create useful indexes for collected history tables when present."""
- if { "symbol" , "timeframe" , "time" } . issubset (
- written_columns . get ( Dataset . rates , set ()),
- ):
- conn . execute (
- "CREATE INDEX IF NOT EXISTS idx_rates_symbol_timeframe_time"
- " ON rates(symbol, timeframe, time)" ,
- )
- if { "symbol" , "time" } . issubset ( written_columns . get ( Dataset . ticks , set ())):
- conn . execute (
- "CREATE INDEX IF NOT EXISTS idx_ticks_symbol_time ON ticks(symbol, time)" ,
- )
- if { "position_id" , "symbol" } . issubset (
- written_columns . get ( Dataset . history_deals , set ()),
- ):
- conn . execute (
- "CREATE INDEX IF NOT EXISTS idx_history_deals_position_symbol"
- " ON history_deals(position_id, symbol)" ,
- )
+ def create_history_indexes (
+ conn : sqlite3 . Connection ,
+ written_columns : dict [ Dataset , set [ str ]],
+) -> None :
+ """Create useful indexes for collected history tables when present."""
+ if { "symbol" , "timeframe" , "time" } . issubset (
+ written_columns . get ( Dataset . rates , set ()),
+ ):
+ conn . execute (
+ "CREATE INDEX IF NOT EXISTS idx_rates_symbol_timeframe_time"
+ " ON rates(symbol, timeframe, time)" ,
+ )
+ if { "symbol" , "time" } . issubset ( written_columns . get ( Dataset . ticks , set ())):
+ conn . execute (
+ "CREATE INDEX IF NOT EXISTS idx_ticks_symbol_time ON ticks(symbol, time)" ,
+ )
+ if { "position_id" , "symbol" } . issubset (
+ written_columns . get ( Dataset . history_deals , set ()),
+ ):
+ conn . execute (
+ "CREATE INDEX IF NOT EXISTS idx_history_deals_position_symbol"
+ " ON history_deals(position_id, symbol)" ,
+ )
@@ -1326,99 +1336,99 @@ a symbol such as EURUSD_M1 cannot collide with EURUSD
Source code in mt5cli/history.py
- def create_positions_reconstructed_view (
- conn : sqlite3 . Connection ,
- deals_columns : set [ str ],
-) -> bool :
- """Create the positions_reconstructed SQLite view derived from history_deals.
-
- Returns:
- True if the view was created, False if required columns are missing.
- """
- if not _POSITIONS_VIEW_REQUIRED_COLUMNS . issubset ( deals_columns ):
- missing = ", " . join ( sorted ( _POSITIONS_VIEW_REQUIRED_COLUMNS - deals_columns ))
- logger . warning (
- "Skipping positions_reconstructed view: history_deals missing columns: %s " ,
- missing ,
- )
- return False
- conn . execute ( "DROP VIEW IF EXISTS positions_reconstructed" )
- conn . execute (
- "CREATE VIEW positions_reconstructed AS" # noqa: S608
- " SELECT"
- " position_id,"
- " symbol,"
- " MIN(CASE WHEN entry = 0 THEN time END) AS open_time,"
- " MAX(CASE WHEN entry IN (1, 2, 3) THEN time END) AS close_time,"
- " MIN(CASE WHEN entry = 0 THEN type END) AS direction,"
- " SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) AS volume_open,"
- " SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) AS volume_close,"
- " SUM(CASE WHEN entry = 2 THEN volume ELSE 0 END) AS volume_reversal,"
- " CASE"
- " WHEN SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) > 0"
- " THEN SUM(CASE WHEN entry = 0 THEN price * volume ELSE 0 END)"
- " / SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END)"
- " END AS open_price,"
- " CASE"
- " WHEN SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) > 0"
- " THEN SUM(CASE WHEN entry IN (1, 2, 3) THEN price * volume ELSE 0 END)"
- " / SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END)"
- " END AS close_price,"
- " SUM(profit) AS total_profit,"
- " SUM(CASE WHEN entry = 2 THEN 1 ELSE 0 END) AS reversal_count,"
- " COUNT(*) AS deals_count"
- " FROM history_deals"
- f " WHERE type IN { _TRADE_DEAL_TYPES_SQL } AND position_id != 0"
- " GROUP BY position_id, symbol"
- " HAVING SUM(CASE WHEN entry IN (1, 2, 3) THEN 1 ELSE 0 END) > 0" ,
- )
- return True
+ def create_positions_reconstructed_view (
+ conn : sqlite3 . Connection ,
+ deals_columns : set [ str ],
+) -> bool :
+ """Create the positions_reconstructed SQLite view derived from history_deals.
+
+ Returns:
+ True if the view was created, False if required columns are missing.
+ """
+ if not _POSITIONS_VIEW_REQUIRED_COLUMNS . issubset ( deals_columns ):
+ missing = ", " . join ( sorted ( _POSITIONS_VIEW_REQUIRED_COLUMNS - deals_columns ))
+ logger . warning (
+ "Skipping positions_reconstructed view: history_deals missing columns: %s " ,
+ missing ,
+ )
+ return False
+ conn . execute ( "DROP VIEW IF EXISTS positions_reconstructed" )
+ conn . execute (
+ "CREATE VIEW positions_reconstructed AS" # noqa: S608
+ " SELECT"
+ " position_id,"
+ " symbol,"
+ " MIN(CASE WHEN entry = 0 THEN time END) AS open_time,"
+ " MAX(CASE WHEN entry IN (1, 2, 3) THEN time END) AS close_time,"
+ " MIN(CASE WHEN entry = 0 THEN type END) AS direction,"
+ " SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) AS volume_open,"
+ " SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) AS volume_close,"
+ " SUM(CASE WHEN entry = 2 THEN volume ELSE 0 END) AS volume_reversal,"
+ " CASE"
+ " WHEN SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) > 0"
+ " THEN SUM(CASE WHEN entry = 0 THEN price * volume ELSE 0 END)"
+ " / SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END)"
+ " END AS open_price,"
+ " CASE"
+ " WHEN SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) > 0"
+ " THEN SUM(CASE WHEN entry IN (1, 2, 3) THEN price * volume ELSE 0 END)"
+ " / SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END)"
+ " END AS close_price,"
+ " SUM(profit) AS total_profit,"
+ " SUM(CASE WHEN entry = 2 THEN 1 ELSE 0 END) AS reversal_count,"
+ " COUNT(*) AS deals_count"
+ " FROM history_deals"
+ f " WHERE type IN { _TRADE_DEAL_TYPES_SQL } AND position_id != 0"
+ " GROUP BY position_id, symbol"
+ " HAVING SUM(CASE WHEN entry IN (1, 2, 3) THEN 1 ELSE 0 END) > 0" ,
+ )
+ return True
@@ -1443,67 +1453,67 @@ a symbol such as EURUSD_M1 cannot collide with EURUSD
Source code in mt5cli/history.py
- def create_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None :
- """Create rate compatibility views from the normalized rates table."""
- columns = get_table_columns ( conn , Dataset . rates . table_name )
- if not { "symbol" , "timeframe" , "time" } . issubset ( columns ):
- return
- drop_rate_compatibility_views ( conn )
- select_columns = sorted ( columns - { "symbol" , "timeframe" })
- quoted_columns = ", " . join ( f '" { column } "' for column in select_columns )
- rows = conn . execute (
- "SELECT DISTINCT symbol, timeframe FROM rates ORDER BY symbol, timeframe" ,
- ) . fetchall ()
- timeframes_by_symbol : dict [ str , list [ int ]] = {}
- for symbol , timeframe in rows :
- timeframes_by_symbol . setdefault ( str ( symbol ), []) . append ( int ( timeframe ))
- for symbol , timeframes in timeframes_by_symbol . items ():
- for timeframe in timeframes :
- granularity = resolve_granularity_name ( timeframe )
- view_name = build_rate_view_name (
- symbol = symbol ,
- granularity = granularity ,
- granularity_count = len ( timeframes ),
- timeframe = timeframe ,
- )
- quoted_view_name = quote_sqlite_identifier ( view_name )
- escaped_symbol = symbol . replace ( "'" , "''" )
- conn . execute (
- f "CREATE VIEW { quoted_view_name } AS" # noqa: S608
- f " SELECT { quoted_columns } FROM rates"
- f " WHERE symbol = ' { escaped_symbol } '"
- f " AND timeframe = { timeframe } " ,
- )
+ def create_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None :
+ """Create rate compatibility views from the normalized rates table."""
+ columns = get_table_columns ( conn , Dataset . rates . table_name )
+ if not { "symbol" , "timeframe" , "time" } . issubset ( columns ):
+ return
+ drop_rate_compatibility_views ( conn )
+ select_columns = sorted ( columns - { "symbol" , "timeframe" })
+ quoted_columns = ", " . join ( f '" { column } "' for column in select_columns )
+ rows = conn . execute (
+ "SELECT DISTINCT symbol, timeframe FROM rates ORDER BY symbol, timeframe" ,
+ ) . fetchall ()
+ timeframes_by_symbol : dict [ str , list [ int ]] = {}
+ for symbol , timeframe in rows :
+ timeframes_by_symbol . setdefault ( str ( symbol ), []) . append ( int ( timeframe ))
+ for symbol , timeframes in timeframes_by_symbol . items ():
+ for timeframe in timeframes :
+ granularity = resolve_granularity_name ( timeframe )
+ view_name = build_rate_view_name (
+ symbol = symbol ,
+ granularity = granularity ,
+ granularity_count = len ( timeframes ),
+ timeframe = timeframe ,
+ )
+ quoted_view_name = quote_sqlite_identifier ( view_name )
+ escaped_symbol = symbol . replace ( "'" , "''" )
+ conn . execute (
+ f "CREATE VIEW { quoted_view_name } AS" # noqa: S608
+ f " SELECT { quoted_columns } FROM rates"
+ f " WHERE symbol = ' { escaped_symbol } '"
+ f " AND timeframe = { timeframe } " ,
+ )
@@ -1542,97 +1552,97 @@ unscoped deduplication pass instead.
Source code in mt5cli/history.py
- def deduplicate_history_tables (
- conn : sqlite3 . Connection ,
- written_columns : dict [ Dataset , set [ str ]],
- written_tables : set [ Dataset ],
- dedup_scopes : Mapping [ Dataset , Sequence [ DedupScope ]] | None = None ,
-) -> None :
- """Deduplicate appended history tables by stable identifiers.
-
- Scopes whose required columns are not present in the written table are
- skipped. If all scopes for a dataset are skipped, the table receives one
- unscoped deduplication pass instead.
- """
- cursor = conn . cursor ()
- for dataset in written_tables :
- columns = written_columns . get ( dataset , set ())
- table = dataset . table_name
- keys = next (
- (
- candidate
- for candidate in _HISTORY_DEDUP_KEYS [ dataset ]
- if set ( candidate ) . issubset ( columns )
- ),
- None ,
- )
- if keys is None :
- logger . warning (
- "Skipping %s deduplication: no supported key columns" ,
- table ,
- )
- continue
- raw_scopes : Sequence [ DedupScope ] = (
- dedup_scopes . get ( dataset , ()) if dedup_scopes else ()
- )
- scopes = [ scope for scope in raw_scopes if scope . required_columns <= columns ]
- if scopes :
- for scope in scopes :
- drop_duplicates_in_table (
- cursor ,
- table ,
- list ( keys ),
- keep = "last" ,
- scope_where = scope . where ,
- scope_params = scope . params ,
- )
- continue
- drop_duplicates_in_table ( cursor , table , list ( keys ), keep = "last" )
+ def deduplicate_history_tables (
+ conn : sqlite3 . Connection ,
+ written_columns : dict [ Dataset , set [ str ]],
+ written_tables : set [ Dataset ],
+ dedup_scopes : Mapping [ Dataset , Sequence [ DedupScope ]] | None = None ,
+) -> None :
+ """Deduplicate appended history tables by stable identifiers.
+
+ Scopes whose required columns are not present in the written table are
+ skipped. If all scopes for a dataset are skipped, the table receives one
+ unscoped deduplication pass instead.
+ """
+ cursor = conn . cursor ()
+ for dataset in written_tables :
+ columns = written_columns . get ( dataset , set ())
+ table = dataset . table_name
+ keys = next (
+ (
+ candidate
+ for candidate in _HISTORY_DEDUP_KEYS [ dataset ]
+ if set ( candidate ) . issubset ( columns )
+ ),
+ None ,
+ )
+ if keys is None :
+ logger . warning (
+ "Skipping %s deduplication: no supported key columns" ,
+ table ,
+ )
+ continue
+ raw_scopes : Sequence [ DedupScope ] = (
+ dedup_scopes . get ( dataset , ()) if dedup_scopes else ()
+ )
+ scopes = [ scope for scope in raw_scopes if scope . required_columns <= columns ]
+ if scopes :
+ for scope in scopes :
+ drop_duplicates_in_table (
+ cursor ,
+ table ,
+ list ( keys ),
+ keep = "last" ,
+ scope_where = scope . where ,
+ scope_params = scope . params ,
+ )
+ continue
+ drop_duplicates_in_table ( cursor , table , list ( keys ), keep = "last" )
@@ -1688,73 +1698,85 @@ unscoped deduplication pass instead.
Source code in mt5cli/history.py
- def drop_duplicates_in_table (
- cursor : sqlite3 . Cursor ,
- table : str ,
- ids : list [ str ],
- * ,
- keep : Literal [ "first" , "last" ] = "last" ,
- scope_where : str | None = None ,
- scope_params : tuple [ object , ... ] = (),
-) -> None :
- """Remove duplicate rows, keeping the first or last ROWID per key group.
-
- Raises:
- ValueError: If the table or column names are invalid.
- """
- if not table . isidentifier ():
- msg = f "Invalid table name: { table } "
- raise ValueError ( msg )
- if invalid := { column for column in ids if not column . isidentifier ()}:
- msg = f "Invalid column names: { ', ' . join ( sorted ( invalid )) } "
- raise ValueError ( msg )
- ids_csv = ", " . join ( f '" { column } "' for column in ids )
- rowid_selector = "MIN" if keep == "first" else "MAX"
- if scope_where :
- delete_sql = (
- f "DELETE FROM { table } WHERE { scope_where } AND ROWID NOT IN" # noqa: S608
- f " (SELECT { rowid_selector } (ROWID) FROM { table } WHERE { scope_where } "
- f " GROUP BY { ids_csv } )"
- )
- cursor . execute ( delete_sql , scope_params + scope_params )
- return
- cursor . execute (
- f "DELETE FROM { table } WHERE ROWID NOT IN" # noqa: S608
- f " (SELECT { rowid_selector } (ROWID) FROM { table } GROUP BY { ids_csv } )" ,
- )
+ def drop_duplicates_in_table (
+ cursor : sqlite3 . Cursor ,
+ table : str ,
+ ids : list [ str ],
+ * ,
+ keep : Literal [ "first" , "last" ] = "last" ,
+ scope_where : str | None = None ,
+ scope_params : tuple [ object , ... ] = (),
+) -> None :
+ """Remove duplicate rows, keeping the first or last ROWID per key group.
+
+ Raises:
+ ValueError: If the table or column names are invalid.
+ """
+ if not table . isidentifier ():
+ msg = f "Invalid table name: { table } "
+ raise ValueError ( msg )
+ if invalid := { column for column in ids if not column . isidentifier ()}:
+ msg = f "Invalid column names: { ', ' . join ( sorted ( invalid )) } "
+ raise ValueError ( msg )
+ ids_csv = ", " . join ( _sqlite_dedup_key_expression ( column ) for column in ids )
+ rowid_selector = "MIN" if keep == "first" else "MAX"
+ prepared_scope_params = tuple (
+ _require_serialized_sqlite_timestamp ( value )
+ if isinstance ( value , datetime )
+ else value
+ for value in scope_params
+ )
+ if scope_where :
+ delete_sql = (
+ f "DELETE FROM { table } WHERE { scope_where } AND ROWID NOT IN" # noqa: S608
+ f " (SELECT { rowid_selector } (ROWID) FROM { table } WHERE { scope_where } "
+ f " GROUP BY { ids_csv } )"
+ )
+ cursor . execute ( delete_sql , prepared_scope_params + prepared_scope_params )
+ return
+ cursor . execute (
+ f "DELETE FROM { table } WHERE ROWID NOT IN" # noqa: S608
+ f " (SELECT { rowid_selector } (ROWID) FROM { table } GROUP BY { ids_csv } )" ,
+ )
@@ -1846,35 +1868,35 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- 116
-117
-118
-119
-120
-121
-122
-123
+ def drop_forming_rate_bar ( df_rate : pd . DataFrame ) -> pd . DataFrame :
- """Return closed bars from chronologically ordered MT5 rate data.
-
- MetaTrader 5 ``copy_rates_from_pos(start_pos=0)`` includes the still-forming
- current bar as the last row. Slice it off so downstream logic only sees
- completed bars. Empty frames and single-row frames return empty results.
-
- Args:
- df_rate: Rate data ordered oldest-to-newest with the forming bar last.
+130
+131
+132
+133
+134
+135
+136
+137 def drop_forming_rate_bar ( df_rate : pd . DataFrame ) -> pd . DataFrame :
+ """Return closed bars from chronologically ordered MT5 rate data.
- Returns:
- A new DataFrame with all rows except the last. Index and columns are
- preserved. The input frame is not modified.
- """
- return df_rate . iloc [: - 1 ] . copy ()
+ MetaTrader 5 ``copy_rates_from_pos(start_pos=0)`` includes the still-forming
+ current bar as the last row. Slice it off so downstream logic only sees
+ completed bars. Empty frames and single-row frames return empty results.
+
+ Args:
+ df_rate: Rate data ordered oldest-to-newest with the forming bar last.
+
+ Returns:
+ A new DataFrame with all rows except the last. Index and columns are
+ preserved. The input frame is not modified.
+ """
+ return df_rate . iloc [: - 1 ] . copy ()
@@ -1899,21 +1921,21 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- def drop_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None :
- """Drop all mt5cli-managed ``rate_*`` compatibility views."""
- rows = conn . execute (
- "SELECT name FROM sqlite_master WHERE type = 'view' AND name GLOB 'rate_*'" ,
- ) . fetchall ()
- for ( view_name ,) in rows :
- quoted_view_name = quote_sqlite_identifier ( str ( view_name ))
- conn . execute ( f "DROP VIEW IF EXISTS { quoted_view_name } " )
+ def drop_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None :
+ """Drop all mt5cli-managed ``rate_*`` compatibility views."""
+ rows = conn . execute (
+ "SELECT name FROM sqlite_master WHERE type = 'view' AND name GLOB 'rate_*'" ,
+ ) . fetchall ()
+ for ( view_name ,) in rows :
+ quoted_view_name = quote_sqlite_identifier ( str ( view_name ))
+ conn . execute ( f "DROP VIEW IF EXISTS { quoted_view_name } " )
@@ -1976,61 +1998,61 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- def filter_incremental_history_deals_frame (
- frame : pd . DataFrame ,
- symbols : Sequence [ str ],
- start_by_symbol : dict [ str , datetime ],
- account_event_start : datetime ,
-) -> pd . DataFrame :
- """Filter incrementally fetched history_deals by symbol and event start times.
-
- Returns:
- Rows for selected symbols at or after each symbol start, plus account
- events at or after ``account_event_start``.
- """
- if frame . empty :
- return frame . copy ()
- parsed_times = _frame_parsed_times ( frame )
- time_valid = parsed_times . notna ()
- account_event_mask = _history_deals_account_event_mask ( frame )
- account_keep = account_event_mask & ( parsed_times >= account_event_start )
- trade_keep = pd . Series ( data = False , index = frame . index )
- if "symbol" in frame . columns :
- for symbol in symbols :
- trade_keep |= (
- ( frame [ "symbol" ] == symbol )
- & ( parsed_times >= start_by_symbol [ symbol ])
- & ~ account_event_mask
- )
- keep = ( account_keep | trade_keep ) & time_valid
- return frame . loc [ keep ] . copy ()
+ def filter_incremental_history_deals_frame (
+ frame : pd . DataFrame ,
+ symbols : Sequence [ str ],
+ start_by_symbol : dict [ str , datetime ],
+ account_event_start : datetime ,
+) -> pd . DataFrame :
+ """Filter incrementally fetched history_deals by symbol and event start times.
+
+ Returns:
+ Rows for selected symbols at or after each symbol start, plus account
+ events at or after ``account_event_start``.
+ """
+ if frame . empty :
+ return frame . copy ()
+ parsed_times = _frame_parsed_times ( frame )
+ time_valid = parsed_times . notna ()
+ account_event_mask = _history_deals_account_event_mask ( frame )
+ account_keep = account_event_mask & ( parsed_times >= account_event_start )
+ trade_keep = pd . Series ( data = False , index = frame . index )
+ if "symbol" in frame . columns :
+ for symbol in symbols :
+ trade_keep |= (
+ ( frame [ "symbol" ] == symbol )
+ & ( parsed_times >= start_by_symbol [ symbol ])
+ & ~ account_event_mask
+ )
+ keep = ( account_keep | trade_keep ) & time_valid
+ return frame . loc [ keep ] . copy ()
@@ -2083,41 +2105,41 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- def filter_trade_history_frame (
- frame : pd . DataFrame ,
- symbols : Sequence [ str ],
- * ,
- include_account_events : bool ,
-) -> pd . DataFrame :
- """Filter trade history rows to selected symbols and account events.
-
- Returns:
- Filtered history rows.
- """
- if "symbol" not in frame . columns :
- return frame
- symbol_mask = frame [ "symbol" ] . isin ( symbols )
- if not include_account_events :
- return frame . loc [ symbol_mask ] . copy ()
- account_event_mask = _history_deals_account_event_mask ( frame )
- return frame . loc [ symbol_mask | account_event_mask ] . copy ()
+ def filter_trade_history_frame (
+ frame : pd . DataFrame ,
+ symbols : Sequence [ str ],
+ * ,
+ include_account_events : bool ,
+) -> pd . DataFrame :
+ """Filter trade history rows to selected symbols and account events.
+
+ Returns:
+ Filtered history rows.
+ """
+ if "symbol" not in frame . columns :
+ return frame
+ symbol_mask = frame [ "symbol" ] . isin ( symbols )
+ if not include_account_events :
+ return frame . loc [ symbol_mask ] . copy ()
+ account_event_mask = _history_deals_account_event_mask ( frame )
+ return frame . loc [ symbol_mask | account_event_mask ] . copy ()
@@ -2144,47 +2166,49 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- def get_history_deals_account_event_start_datetime (
- conn : sqlite3 . Connection ,
- * ,
- fallback_start : datetime ,
-) -> datetime :
- """Return the next update start for account-level history_deals rows."""
- table = Dataset . history_deals . table_name
- columns = get_table_columns ( conn , table )
- if "time" not in columns :
- return fallback_start
- if "type" in columns :
- where_clause = f "type NOT IN { _TRADE_DEAL_TYPES_SQL } "
- elif "symbol" in columns :
- where_clause = "symbol IS NULL OR symbol = ''"
- else :
- return fallback_start
- row = conn . execute (
- f "SELECT MAX(time) FROM { table } WHERE { where_clause } " , # noqa: S608
- ) . fetchone ()
- parsed = parse_sqlite_timestamp ( row [ 0 ] if row else None )
- return parsed if parsed is not None else fallback_start
+ def get_history_deals_account_event_start_datetime (
+ conn : sqlite3 . Connection ,
+ * ,
+ fallback_start : datetime ,
+) -> datetime :
+ """Return the next update start for account-level history_deals rows."""
+ table = Dataset . history_deals . table_name
+ columns = get_table_columns ( conn , table )
+ if "time" not in columns :
+ return fallback_start
+ if "type" in columns :
+ where_clause = f "type NOT IN { _TRADE_DEAL_TYPES_SQL } "
+ elif "symbol" in columns :
+ where_clause = "symbol IS NULL OR symbol = ''"
+ else :
+ return fallback_start
+ parsed = _load_latest_parseable_time (
+ conn ,
+ table ,
+ where_clause = where_clause ,
+ )
+ return parsed if parsed is not None else fallback_start
@@ -2216,41 +2240,41 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- def get_incremental_start_datetime (
- conn : sqlite3 . Connection ,
- dataset : Dataset ,
- * ,
- symbol : str ,
- timeframe : int | None ,
- fallback_start : datetime ,
-) -> datetime :
- """Return the next update start datetime from existing MAX(time)."""
- timeframes = [ timeframe ] if timeframe is not None else None
- starts = load_incremental_start_datetimes (
- conn ,
- dataset ,
- symbols = [ symbol ],
- timeframes = timeframes ,
- fallback_start = fallback_start ,
- )
- return starts [ symbol , timeframe ]
+ def get_incremental_start_datetime (
+ conn : sqlite3 . Connection ,
+ dataset : Dataset ,
+ * ,
+ symbol : str ,
+ timeframe : int | None ,
+ fallback_start : datetime ,
+) -> datetime :
+ """Return the next update start datetime from existing MAX(time)."""
+ timeframes = [ timeframe ] if timeframe is not None else None
+ starts = load_incremental_start_datetimes (
+ conn ,
+ dataset ,
+ symbols = [ symbol ],
+ timeframes = timeframes ,
+ fallback_start = fallback_start ,
+ )
+ return starts [ symbol , timeframe ]
@@ -2275,15 +2299,15 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- def get_table_columns ( conn : sqlite3 . Connection , table : str ) -> set [ str ]:
- """Return existing SQLite columns for a table."""
- quoted_table = quote_sqlite_identifier ( table )
- rows = conn . execute ( f "PRAGMA table_info( { quoted_table } )" ) . fetchall ()
- return { str ( row [ 1 ]) for row in rows }
+ def get_table_columns ( conn : sqlite3 . Connection , table : str ) -> set [ str ]:
+ """Return existing SQLite columns for a table."""
+ quoted_table = quote_sqlite_identifier ( table )
+ rows = conn . execute ( f "PRAGMA table_info( { quoted_table } )" ) . fetchall ()
+ return { str ( row [ 1 ]) for row in rows }
@@ -2315,153 +2339,99 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- def load_incremental_start_datetimes (
- conn : sqlite3 . Connection ,
- dataset : Dataset ,
- * ,
- symbols : Sequence [ str ],
- timeframes : Sequence [ int ] | None = None ,
- fallback_start : datetime ,
-) -> dict [ tuple [ str , int | None ], datetime ]:
- """Return next update start datetimes keyed by symbol and optional timeframe."""
- table = dataset . table_name
- columns = get_table_columns ( conn , table )
- if dataset is Dataset . rates and columns :
- _validate_rates_schema ( columns )
-
- if "time" not in columns :
- if dataset is Dataset . rates and timeframes is not None :
- return {
- ( symbol , timeframe ): fallback_start
- for symbol in symbols
- for timeframe in timeframes
- }
- return {( symbol , None ): fallback_start for symbol in symbols }
-
- parsed_by_key : dict [ tuple [ str , int | None ], datetime ] = {}
- if (
- dataset is Dataset . rates
- and timeframes is not None
- and { "symbol" , "timeframe" } . issubset ( columns )
- ):
- symbol_placeholders = ", " . join ( "?" for _ in symbols )
- timeframe_placeholders = ", " . join ( "?" for _ in timeframes )
- grouped_rates_query = (
- "SELECT symbol, timeframe, MAX(time) FROM " # noqa: S608
- f " { table } WHERE symbol IN ( { symbol_placeholders } )"
- f " AND timeframe IN ( { timeframe_placeholders } )"
- " GROUP BY symbol, timeframe"
- )
- rows = conn . execute (
- grouped_rates_query ,
- [ * symbols , * timeframes ],
- ) . fetchall ()
- for row_symbol , row_timeframe , max_time in rows :
- parsed = parse_sqlite_timestamp ( max_time )
- if parsed is not None :
- parsed_by_key [ str ( row_symbol ), int ( row_timeframe )] = parsed
- return {
- ( symbol , timeframe ): parsed_by_key . get (
- ( symbol , timeframe ),
- fallback_start ,
- )
- for symbol in symbols
- for timeframe in timeframes
- }
-
- if "symbol" in columns :
- symbol_placeholders = ", " . join ( "?" for _ in symbols )
- rows = conn . execute (
- f "SELECT symbol, MAX(time) FROM { table } " # noqa: S608
- f " WHERE symbol IN ( { symbol_placeholders } ) GROUP BY symbol" ,
- list ( symbols ),
- ) . fetchall ()
- for row_symbol , max_time in rows :
- parsed = parse_sqlite_timestamp ( max_time )
- if parsed is not None :
- parsed_by_key [ str ( row_symbol ), None ] = parsed
- return {
- ( symbol , None ): parsed_by_key . get (( symbol , None ), fallback_start )
- for symbol in symbols
- }
-
- row = conn . execute ( f "SELECT MAX(time) FROM { table } " ) . fetchone () # noqa: S608
- parsed = parse_sqlite_timestamp ( row [ 0 ] if row else None )
- shared_start = parsed if parsed is not None else fallback_start
- return {( symbol , None ): shared_start for symbol in symbols }
+ def load_incremental_start_datetimes (
+ conn : sqlite3 . Connection ,
+ dataset : Dataset ,
+ * ,
+ symbols : Sequence [ str ],
+ timeframes : Sequence [ int ] | None = None ,
+ fallback_start : datetime ,
+) -> dict [ tuple [ str , int | None ], datetime ]:
+ """Return next update start datetimes keyed by symbol and optional timeframe."""
+ table = dataset . table_name
+ columns = get_table_columns ( conn , table )
+ if dataset is Dataset . rates and columns :
+ _validate_rates_schema ( columns )
+
+ if "time" not in columns :
+ if dataset is Dataset . rates and timeframes is not None :
+ return {
+ ( symbol , timeframe ): fallback_start
+ for symbol in symbols
+ for timeframe in timeframes
+ }
+ return {( symbol , None ): fallback_start for symbol in symbols }
+
+ if (
+ dataset is Dataset . rates
+ and timeframes is not None
+ and { "symbol" , "timeframe" } . issubset ( columns )
+ ):
+ return _load_grouped_rate_start_datetimes (
+ conn ,
+ table ,
+ symbols = symbols ,
+ timeframes = timeframes ,
+ fallback_start = fallback_start ,
+ )
+
+ if "symbol" in columns :
+ return _load_symbol_start_datetimes (
+ conn ,
+ table ,
+ symbols = symbols ,
+ fallback_start = fallback_start ,
+ )
+
+ parsed = _load_latest_parseable_time ( conn , table )
+ shared_start = parsed if parsed is not None else fallback_start
+ return {( symbol , None ): shared_start for symbol in symbols }
@@ -2586,14 +2556,7 @@ completed bars. Empty frames and single-row frames return empty results.
Source code in mt5cli/history.py
- 306
-307
-308
-309
-310
-311
-312
-313
+ 313
314
315
316
@@ -2607,28 +2570,35 @@ completed bars. Empty frames and single-row frames return empty results.
324
325
326
-327 def load_rate_data (
- conn_or_path : SqliteConnOrPath ,
- table : str ,
- count : int | None = None ,
-) -> pd . DataFrame :
- """Load rate-like data from a SQLite database path or connection.
-
- Args:
- conn_or_path: SQLite database path or open connection.
- table: Source table or view name.
- count: Optional number of most recent rows to load.
-
- Returns:
- DataFrame indexed by ascending ``time``.
-
- """
- conn , should_close = _open_existing_sqlite_database ( conn_or_path )
- try :
- return load_rate_data_from_connection ( conn , table , count = count )
- finally :
- if should_close :
- conn . close ()
+327
+328
+329
+330
+331
+332
+333
+334 def load_rate_data (
+ conn_or_path : SqliteConnOrPath ,
+ table : str ,
+ count : int | None = None ,
+) -> pd . DataFrame :
+ """Load rate-like data from a SQLite database path or connection.
+
+ Args:
+ conn_or_path: SQLite database path or open connection.
+ table: Source table or view name.
+ count: Optional number of most recent rows to load.
+
+ Returns:
+ DataFrame indexed by ascending ``time``.
+
+ """
+ conn , should_close = _open_existing_sqlite_database ( conn_or_path )
+ try :
+ return load_rate_data_from_connection ( conn , table , count = count )
+ finally :
+ if should_close :
+ conn . close ()
@@ -2767,14 +2737,7 @@ or view contains no rows.
Source code in mt5cli/history.py
- 260
-261
-262
-263
-264
-265
-266
-267
+ def load_rate_data_from_connection (
- connection : sqlite3 . Connection ,
- table : str ,
- count : int | None = None ,
-) -> pd . DataFrame :
- """Load rate-like data from a SQLite table or view.
-
- Args:
- connection: Open SQLite connection.
- table: Source table or view name.
- count: Optional number of most recent rows to load.
-
- Returns:
- DataFrame indexed by ascending ``time``.
-
- Raises:
- ValueError: If inputs, schema, timestamps are invalid, or the table
- or view contains no rows.
- """
- table_name = _validate_rate_load_request ( table , count )
- columns = get_table_columns ( connection , table_name )
- _ensure_rate_columns ( columns , table_name )
- quoted_table = quote_sqlite_identifier ( table_name )
- if count is None :
- frame = cast (
- "pd.DataFrame" ,
- pd . read_sql_query ( # type: ignore[reportUnknownMemberType]
- f "SELECT * FROM { quoted_table } ORDER BY time ASC" , # noqa: S608
- connection ,
- ),
- )
- else :
- frame = cast (
- "pd.DataFrame" ,
- pd . read_sql_query ( # type: ignore[reportUnknownMemberType]
- f "SELECT * FROM { quoted_table } ORDER BY time DESC LIMIT ?" , # noqa: S608
- connection ,
- params = ( count ,),
- ),
- )
- if frame . empty :
- msg = f "SQLite table or view { table_name !r} contains no rows."
- raise ValueError ( msg )
- return _parse_rate_time_index ( frame , table_name )
+303
+304
+305
+306
+307
+308
+309
+310 def load_rate_data_from_connection (
+ connection : sqlite3 . Connection ,
+ table : str ,
+ count : int | None = None ,
+) -> pd . DataFrame :
+ """Load rate-like data from a SQLite table or view.
+
+ Args:
+ connection: Open SQLite connection.
+ table: Source table or view name.
+ count: Optional number of most recent rows to load.
+
+ Returns:
+ DataFrame indexed by ascending ``time``.
+
+ Raises:
+ ValueError: If inputs, schema, timestamps are invalid, or the table
+ or view contains no rows.
+ """
+ table_name = _validate_rate_load_request ( table , count )
+ columns = get_table_columns ( connection , table_name )
+ _ensure_rate_columns ( columns , table_name )
+ quoted_table = quote_sqlite_identifier ( table_name )
+ if count is None :
+ frame = cast (
+ "pd.DataFrame" ,
+ pd . read_sql_query ( # type: ignore[reportUnknownMemberType]
+ f "SELECT * FROM { quoted_table } ORDER BY time ASC" , # noqa: S608
+ connection ,
+ ),
+ )
+ else :
+ frame = cast (
+ "pd.DataFrame" ,
+ pd . read_sql_query ( # type: ignore[reportUnknownMemberType]
+ f "SELECT * FROM { quoted_table } ORDER BY time DESC LIMIT ?" , # noqa: S608
+ connection ,
+ params = ( count ,),
+ ),
+ )
+ if frame . empty :
+ msg = f "SQLite table or view { table_name !r} contains no rows."
+ raise ValueError ( msg )
+ return _parse_rate_time_index ( frame , table_name )
@@ -3066,14 +3036,7 @@ with symbol=None for each granularity instead of raising.
Source code in mt5cli/history.py
- 795
-796
-797
-798
-799
-800
-801
-802
+ def load_rate_series_by_granularity (
- conn_or_path : SqliteConnOrPath ,
- symbols : Sequence [ str ],
- granularities : Sequence [ int | str ],
- count : int ,
- * ,
- explicit_tables : Sequence [ str ] | None = None ,
- allow_missing_symbol : bool = False ,
-) -> dict [ tuple [ str | None , str ], pd . DataFrame ]:
- """Load rate series keyed by symbol and string granularity name.
-
- Builds targets with :func:`build_rate_targets` and loads them with
- :func:`load_rate_series_from_sqlite`, then rekeys the result by granularity
- name (for example ``M1``) instead of the integer timeframe to reduce
- downstream boilerplate.
-
- Args:
- conn_or_path: SQLite database path or open connection.
- symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``.
- granularities: MT5 timeframes as integers or names (for example ``M1``).
- count: Number of most recent rows to load per series.
- explicit_tables: Optional explicit table or view names matching the
- built targets in row-major order. Required when symbols are omitted.
- allow_missing_symbol: When True and ``symbols`` is empty, build targets
- with ``symbol=None`` for each granularity instead of raising.
-
- Returns:
- Mapping keyed by ``(symbol | None, granularity_name)`` to each rate
- DataFrame. Propagates ``ValueError`` (via :func:`build_rate_targets` and
- :func:`load_rate_series_from_sqlite`) when inputs are empty or invalid,
- table resolution fails, or duplicate targets are present.
- """
- targets = build_rate_targets (
- symbols ,
- granularities ,
- allow_missing_symbol = allow_missing_symbol ,
- )
- series = load_rate_series_from_sqlite (
- conn_or_path ,
- targets ,
- count ,
- explicit_tables = explicit_tables ,
- )
- return {
- ( symbol , resolve_granularity_name ( timeframe )): frame
- for ( symbol , timeframe ), frame in series . items ()
- }
+841
+842
+843
+844
+845
+846
+847
+848 def load_rate_series_by_granularity (
+ conn_or_path : SqliteConnOrPath ,
+ symbols : Sequence [ str ],
+ granularities : Sequence [ int | str ],
+ count : int ,
+ * ,
+ explicit_tables : Sequence [ str ] | None = None ,
+ allow_missing_symbol : bool = False ,
+) -> dict [ tuple [ str | None , str ], pd . DataFrame ]:
+ """Load rate series keyed by symbol and string granularity name.
+
+ Builds targets with :func:`build_rate_targets` and loads them with
+ :func:`load_rate_series_from_sqlite`, then rekeys the result by granularity
+ name (for example ``M1``) instead of the integer timeframe to reduce
+ downstream boilerplate.
+
+ Args:
+ conn_or_path: SQLite database path or open connection.
+ symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``.
+ granularities: MT5 timeframes as integers or names (for example ``M1``).
+ count: Number of most recent rows to load per series.
+ explicit_tables: Optional explicit table or view names matching the
+ built targets in row-major order. Required when symbols are omitted.
+ allow_missing_symbol: When True and ``symbols`` is empty, build targets
+ with ``symbol=None`` for each granularity instead of raising.
+
+ Returns:
+ Mapping keyed by ``(symbol | None, granularity_name)`` to each rate
+ DataFrame. Propagates ``ValueError`` (via :func:`build_rate_targets` and
+ :func:`load_rate_series_from_sqlite`) when inputs are empty or invalid,
+ table resolution fails, or duplicate targets are present.
+ """
+ targets = build_rate_targets (
+ symbols ,
+ granularities ,
+ allow_missing_symbol = allow_missing_symbol ,
+ )
+ series = load_rate_series_from_sqlite (
+ conn_or_path ,
+ targets ,
+ count ,
+ explicit_tables = explicit_tables ,
+ )
+ return {
+ ( symbol , resolve_granularity_name ( timeframe )): frame
+ for ( symbol , timeframe ), frame in series . items ()
+ }
@@ -3412,14 +3382,7 @@ fails.
Source code in mt5cli/history.py
- 715
-716
-717
-718
-719
-720
-721
-722
+ def load_rate_series_from_sqlite (
- conn_or_path : SqliteConnOrPath ,
- targets : Sequence [ RateTarget ] | None = None ,
- count : int | None = None ,
- explicit_tables : Sequence [ str ] | None = None ,
- * ,
- table : str | None = None ,
-) -> dict [ tuple [ str | None , int ], pd . DataFrame ] | pd . DataFrame :
- """Load one table/view or multiple rate series from a SQLite database.
-
- Args:
- conn_or_path: SQLite database path or open connection.
- targets: Rate targets to load. Each ``(symbol, timeframe_int)`` pair must
- be unique. Omit when loading a single explicit ``table``.
- count: Optional number of most recent rows to load per series.
- explicit_tables: Optional explicit table or view names matching targets.
- When omitted, managed ``rate_*`` compatibility views must already
- exist in the database.
- table: Optional single table or view name to load directly.
-
- Returns:
- A DataFrame when ``table`` is provided, otherwise a mapping keyed by
- ``(symbol, timeframe_int)`` to each rate DataFrame.
-
- Raises:
- ValueError: If ``count`` is not positive, targets are empty, duplicate
- ``(symbol, timeframe_int)`` pairs are present, or table resolution
- fails.
- """
- if table is not None :
- return load_rate_data ( conn_or_path , table , count = count )
- if count is None or count <= 0 :
- msg = "count must be positive."
- raise ValueError ( msg )
- if targets is None :
- msg = "targets are required when table is not provided."
- raise ValueError ( msg )
- target_list = list ( targets )
- if not target_list :
- msg = "At least one rate target is required."
+792
+793
+794
+795
+796
+797
+798
+799 def load_rate_series_from_sqlite (
+ conn_or_path : SqliteConnOrPath ,
+ targets : Sequence [ RateTarget ] | None = None ,
+ count : int | None = None ,
+ explicit_tables : Sequence [ str ] | None = None ,
+ * ,
+ table : str | None = None ,
+) -> dict [ tuple [ str | None , int ], pd . DataFrame ] | pd . DataFrame :
+ """Load one table/view or multiple rate series from a SQLite database.
+
+ Args:
+ conn_or_path: SQLite database path or open connection.
+ targets: Rate targets to load. Each ``(symbol, timeframe_int)`` pair must
+ be unique. Omit when loading a single explicit ``table``.
+ count: Optional number of most recent rows to load per series.
+ explicit_tables: Optional explicit table or view names matching targets.
+ When omitted, managed ``rate_*`` compatibility views must already
+ exist in the database.
+ table: Optional single table or view name to load directly.
+
+ Returns:
+ A DataFrame when ``table`` is provided, otherwise a mapping keyed by
+ ``(symbol, timeframe_int)`` to each rate DataFrame.
+
+ Raises:
+ ValueError: If ``count`` is not positive, targets are empty, duplicate
+ ``(symbol, timeframe_int)`` pairs are present, or table resolution
+ fails.
+ """
+ if table is not None :
+ return load_rate_data ( conn_or_path , table , count = count )
+ if count is None or count <= 0 :
+ msg = "count must be positive."
raise ValueError ( msg )
- if explicit_tables is None and any ( target . symbol is None for target in target_list ):
- msg = (
- "Cannot resolve a rate table for a target without a symbol; "
- "provide explicit_tables."
- )
- raise ValueError ( msg )
- seen_keys : set [ tuple [ str | None , int ]] = set ()
- for target in target_list :
- key = ( target . symbol , target . timeframe_int )
- if key in seen_keys :
- symbol_repr = repr ( target . symbol )
- msg = f "Duplicate rate target: ( { symbol_repr } , { target . timeframe_int } )"
- raise ValueError ( msg )
- seen_keys . add ( key )
- tables = (
- resolve_rate_tables ( None , target_list , explicit_tables )
- if explicit_tables is not None
- else None
- )
- conn , should_close = _open_existing_sqlite_database ( conn_or_path )
- try :
- resolved_tables = tables or resolve_rate_tables (
- conn ,
- target_list ,
- require_existing = True ,
- )
- return {
- ( target . symbol , target . timeframe_int ): load_rate_data_from_connection (
- conn ,
- table ,
- count = count ,
- )
- for target , table in zip ( target_list , resolved_tables , strict = True )
- }
- finally :
- if should_close :
- conn . close ()
+ if targets is None :
+ msg = "targets are required when table is not provided."
+ raise ValueError ( msg )
+ target_list = list ( targets )
+ if not target_list :
+ msg = "At least one rate target is required."
+ raise ValueError ( msg )
+ if explicit_tables is None and any ( target . symbol is None for target in target_list ):
+ msg = (
+ "Cannot resolve a rate table for a target without a symbol; "
+ "provide explicit_tables."
+ )
+ raise ValueError ( msg )
+ seen_keys : set [ tuple [ str | None , int ]] = set ()
+ for target in target_list :
+ key = ( target . symbol , target . timeframe_int )
+ if key in seen_keys :
+ symbol_repr = repr ( target . symbol )
+ msg = f "Duplicate rate target: ( { symbol_repr } , { target . timeframe_int } )"
+ raise ValueError ( msg )
+ seen_keys . add ( key )
+ tables = (
+ resolve_rate_tables ( None , target_list , explicit_tables )
+ if explicit_tables is not None
+ else None
+ )
+ conn , should_close = _open_existing_sqlite_database ( conn_or_path )
+ try :
+ resolved_tables = tables or resolve_rate_tables (
+ conn ,
+ target_list ,
+ require_existing = True ,
+ )
+ return {
+ ( target . symbol , target . timeframe_int ): load_rate_data_from_connection (
+ conn ,
+ table ,
+ count = count ,
+ )
+ for target , table in zip ( target_list , resolved_tables , strict = True )
+ }
+ finally :
+ if should_close :
+ conn . close ()
@@ -3614,14 +3584,7 @@ fails.
Source code in mt5cli/history.py
- 862
-863
-864
-865
-866
-867
-868
-869
+ def parse_sqlite_timestamp ( value : object ) -> datetime | None :
- """Parse a SQLite history timestamp value.
-
- Returns:
- Parsed timezone-aware datetime, or None when parsing fails.
- """
- if value is None :
- return None
- if isinstance ( value , datetime ):
- return value if value . tzinfo is not None else value . replace ( tzinfo = UTC )
- if isinstance ( value , int | float ):
- return datetime . fromtimestamp ( float ( value ), tz = UTC )
- if isinstance ( value , str ):
- return _parse_string_sqlite_timestamp ( value )
- logger . warning ( "Ignoring unsupported history timestamp type: %s " , type ( value ))
- return None
+877
+878
+879
+880
+881
+882
+883
+884 def parse_sqlite_timestamp ( value : object ) -> datetime | None :
+ """Parse a SQLite history timestamp value.
+
+ Returns:
+ Parsed timezone-aware datetime, or None when parsing fails.
+ """
+ if value is None :
+ return None
+ if isinstance ( value , datetime ):
+ return value if value . tzinfo is not None else value . replace ( tzinfo = UTC )
+ if isinstance ( value , int | float ):
+ return datetime . fromtimestamp ( float ( value ), tz = UTC )
+ if isinstance ( value , str ):
+ return _parse_string_sqlite_timestamp ( value )
+ logger . warning ( "Ignoring unsupported history timestamp type: %s " , type ( value ))
+ return None
@@ -3669,11 +3639,11 @@ fails.
Source code in mt5cli/history.py
- def quote_sqlite_identifier ( identifier : str ) -> str :
- """Return a safely quoted SQLite identifier using double quotes."""
- return '"' + identifier . replace ( '"' , '""' ) + '"'
+ def quote_sqlite_identifier ( identifier : str ) -> str :
+ """Return a safely quoted SQLite identifier using double quotes."""
+ return '"' + identifier . replace ( '"' , '""' ) + '"'
@@ -3702,27 +3672,27 @@ fails.
Source code in mt5cli/history.py
- def record_written_columns (
- written_columns : dict [ Dataset , set [ str ]],
- dataset : Dataset ,
- frame : pd . DataFrame ,
-) -> None :
- """Remember columns for datasets written during collection."""
- columns = set ( frame . columns )
- if dataset in written_columns :
- written_columns [ dataset ] . update ( columns )
- else :
- written_columns [ dataset ] = columns
+ def record_written_columns (
+ written_columns : dict [ Dataset , set [ str ]],
+ dataset : Dataset ,
+ frame : pd . DataFrame ,
+) -> None :
+ """Remember columns for datasets written during collection."""
+ columns = set ( frame . columns )
+ if dataset in written_columns :
+ written_columns [ dataset ] . update ( columns )
+ else :
+ written_columns [ dataset ] = columns
@@ -3747,19 +3717,19 @@ fails.
Source code in mt5cli/history.py
- def resolve_granularity_name ( timeframe : int ) -> str :
- """Return a granularity name for a timeframe integer when known."""
- try :
- name = _get_timeframe_name ( timeframe )
- except ValueError :
- return str ( timeframe )
- return name . removeprefix ( "TIMEFRAME_" )
+ def resolve_granularity_name ( timeframe : int ) -> str :
+ """Return a granularity name for a timeframe integer when known."""
+ try :
+ name = _get_timeframe_name ( timeframe )
+ except ValueError :
+ return str ( timeframe )
+ return name . removeprefix ( "TIMEFRAME_" )
@@ -3829,27 +3799,27 @@ fails.
Source code in mt5cli/history.py
- 66
-67
-68
-69
-70
-71
-72
-73
+ def resolve_history_datasets ( datasets : set [ Dataset ] | None ) -> set [ Dataset ]:
- """Resolve configured history datasets.
-
- Returns:
- ``DEFAULT_HISTORY_DATASETS`` (rates, history-orders, history-deals)
- when ``datasets`` is None, otherwise the configured selection (which
- may be empty or explicitly include ``Dataset.ticks``).
- """
- if datasets is None :
- return set ( DEFAULT_HISTORY_DATASETS )
- return set ( datasets )
+76
+77
+78
+79
+80
+81
+82
+83 def resolve_history_datasets ( datasets : set [ Dataset ] | None ) -> set [ Dataset ]:
+ """Resolve configured history datasets.
+
+ Returns:
+ ``DEFAULT_HISTORY_DATASETS`` (rates, history-orders, history-deals)
+ when ``datasets`` is None, otherwise the configured selection (which
+ may be empty or explicitly include ``Dataset.ticks``).
+ """
+ if datasets is None :
+ return set ( DEFAULT_HISTORY_DATASETS )
+ return set ( datasets )
@@ -3897,19 +3867,19 @@ fails.
Source code in mt5cli/history.py
- def resolve_history_tick_flags ( flags : int | str ) -> int :
- """Resolve tick copy flags from an integer or name.
-
- Returns:
- Integer tick flag value.
- """
- return parse_tick_flags ( flags )
+ def resolve_history_tick_flags ( flags : int | str ) -> int :
+ """Resolve tick copy flags from an integer or name.
+
+ Returns:
+ Integer tick flag value.
+ """
+ return parse_tick_flags ( flags )
@@ -3959,39 +3929,39 @@ fails.
Source code in mt5cli/history.py
- def resolve_history_timeframes (
- timeframes : Sequence [ int | str ] | None ,
-) -> list [ int ]:
- """Resolve rate timeframes, deduplicating aliases for the same integer.
-
- Returns:
- Ordered list of unique timeframe integers.
- """
- raw = timeframes if timeframes is not None else DEFAULT_HISTORY_TIMEFRAMES
- seen : set [ int ] = set ()
- resolved : list [ int ] = []
- for value in raw :
- tf = parse_timeframe ( value )
- if tf not in seen :
- seen . add ( tf )
- resolved . append ( tf )
- return resolved
+ def resolve_history_timeframes (
+ timeframes : Sequence [ int | str ] | None ,
+) -> list [ int ]:
+ """Resolve rate timeframes, deduplicating aliases for the same integer.
+
+ Returns:
+ Ordered list of unique timeframe integers.
+ """
+ raw = timeframes if timeframes is not None else DEFAULT_HISTORY_TIMEFRAMES
+ seen : set [ int ] = set ()
+ resolved : list [ int ] = []
+ for value in raw :
+ tf = parse_timeframe ( value )
+ if tf not in seen :
+ seen . add ( tf )
+ resolved . append ( tf )
+ return resolved
@@ -4067,14 +4037,7 @@ view names.
Source code in mt5cli/history.py
- 151
-152
-153
-154
-155
-156
-157
-158
+ def resolve_rate_table_name ( symbol : str , granularity : str ) -> str :
- """Return the canonical normalized SQLite rate table name.
-
- The normalized history table stores all symbols and timeframes in
- ``rates``; use :func:`resolve_rate_view_name` for per-symbol compatibility
- view names.
-
- Returns:
- Canonical normalized rates table name.
+168
+169
+170
+171
+172
+173
+174
+175 def resolve_rate_table_name ( symbol : str , granularity : str ) -> str :
+ """Return the canonical normalized SQLite rate table name.
- Raises:
- ValueError: If ``symbol`` or ``granularity`` is invalid.
- """
- parse_timeframe ( granularity )
- if not symbol . strip ():
- msg = "symbol must not be empty."
- raise ValueError ( msg )
- return Dataset . rates . table_name
+ The normalized history table stores all symbols and timeframes in
+ ``rates``; use :func:`resolve_rate_view_name` for per-symbol compatibility
+ view names.
+
+ Returns:
+ Canonical normalized rates table name.
+
+ Raises:
+ ValueError: If ``symbol`` or ``granularity`` is invalid.
+ """
+ parse_timeframe ( granularity )
+ if not symbol . strip ():
+ msg = "symbol must not be empty."
+ raise ValueError ( msg )
+ return Dataset . rates . table_name
@@ -4285,14 +4255,7 @@ database or a managed view is missing.
Source code in mt5cli/history.py
- 598
-599
-600
-601
-602
-603
-604
-605
+ def resolve_rate_tables (
- conn_or_path : SqliteConnOrPath | None ,
- targets : Sequence [ RateTarget ],
- explicit_tables : Sequence [ str ] | None = None ,
- * ,
- require_existing : bool = False ,
-) -> list [ str ]:
- """Resolve SQLite table or view names for rate targets.
-
- Args:
- conn_or_path: SQLite database path or open connection. May be None when
- ``explicit_tables`` is provided, or when ``require_existing`` is
- False and deterministic default view names are sufficient.
- targets: Rate targets to resolve.
- explicit_tables: Optional explicit table or view names. When provided,
- they are used as-is and must match the number of targets.
- require_existing: When True, require the database and managed views to
- exist for each symbol target. Ignored when ``explicit_tables`` is
- provided.
-
- Returns:
- Table or view names aligned with ``targets``.
-
- Raises:
- ValueError: If ``targets`` is empty, ``explicit_tables`` length does not
- match the target count, a target without a symbol is resolved
- without an explicit table, or ``require_existing`` is True and the
- database or a managed view is missing.
- """
- target_list = list ( targets )
- if not target_list :
- msg = "At least one rate target is required."
- raise ValueError ( msg )
- if explicit_tables is not None :
- tables = list ( explicit_tables )
- if len ( tables ) != len ( target_list ):
- msg = (
- f "Expected { len ( target_list ) } explicit table(s) "
- f "to match the targets, got { len ( tables ) } ."
- )
- raise ValueError ( msg )
- return tables
- if any ( target . symbol is None for target in target_list ):
- msg = (
- "Cannot resolve a rate table for a target without a symbol; "
- "provide explicit_tables."
- )
- raise ValueError ( msg )
- conn , should_close = _open_history_connection ( conn_or_path )
- try :
- if conn is None :
- if require_existing :
- path = (
- conn_or_path
- if isinstance ( conn_or_path , ( Path , str ))
- else "database"
- )
- msg = f "SQLite database not found: { path } "
- raise ValueError ( msg )
- timeframe_counts = None
- existing_views : set [ str ] = set ()
- else :
- timeframe_counts = _load_rates_timeframe_counts ( conn )
- existing_views = _load_existing_rate_views ( conn )
- resolved : list [ str ] = []
- for target in target_list :
- symbol = cast ( "str" , target . symbol )
- timeframe = target . timeframe_int
- resolved . append (
- _resolve_rate_view_name_from_context (
- symbol = symbol ,
- timeframe = timeframe ,
- granularity_name = resolve_granularity_name ( timeframe ),
- timeframe_counts = timeframe_counts ,
- existing_views = existing_views ,
- require_existing = require_existing ,
- ),
- )
- return resolved
- finally :
- if should_close and conn is not None :
- conn . close ()
+679
+680
+681
+682
+683
+684
+685
+686 def resolve_rate_tables (
+ conn_or_path : SqliteConnOrPath | None ,
+ targets : Sequence [ RateTarget ],
+ explicit_tables : Sequence [ str ] | None = None ,
+ * ,
+ require_existing : bool = False ,
+) -> list [ str ]:
+ """Resolve SQLite table or view names for rate targets.
+
+ Args:
+ conn_or_path: SQLite database path or open connection. May be None when
+ ``explicit_tables`` is provided, or when ``require_existing`` is
+ False and deterministic default view names are sufficient.
+ targets: Rate targets to resolve.
+ explicit_tables: Optional explicit table or view names. When provided,
+ they are used as-is and must match the number of targets.
+ require_existing: When True, require the database and managed views to
+ exist for each symbol target. Ignored when ``explicit_tables`` is
+ provided.
+
+ Returns:
+ Table or view names aligned with ``targets``.
+
+ Raises:
+ ValueError: If ``targets`` is empty, ``explicit_tables`` length does not
+ match the target count, a target without a symbol is resolved
+ without an explicit table, or ``require_existing`` is True and the
+ database or a managed view is missing.
+ """
+ target_list = list ( targets )
+ if not target_list :
+ msg = "At least one rate target is required."
+ raise ValueError ( msg )
+ if explicit_tables is not None :
+ tables = list ( explicit_tables )
+ if len ( tables ) != len ( target_list ):
+ msg = (
+ f "Expected { len ( target_list ) } explicit table(s) "
+ f "to match the targets, got { len ( tables ) } ."
+ )
+ raise ValueError ( msg )
+ return tables
+ if any ( target . symbol is None for target in target_list ):
+ msg = (
+ "Cannot resolve a rate table for a target without a symbol; "
+ "provide explicit_tables."
+ )
+ raise ValueError ( msg )
+ conn , should_close = _open_history_connection ( conn_or_path )
+ try :
+ if conn is None :
+ if require_existing :
+ path = (
+ conn_or_path
+ if isinstance ( conn_or_path , ( Path , str ))
+ else "database"
+ )
+ msg = f "SQLite database not found: { path } "
+ raise ValueError ( msg )
+ timeframe_counts = None
+ existing_views : set [ str ] = set ()
+ else :
+ timeframe_counts = _load_rates_timeframe_counts ( conn )
+ existing_views = _load_existing_rate_views ( conn )
+ resolved : list [ str ] = []
+ for target in target_list :
+ symbol = cast ( "str" , target . symbol )
+ timeframe = target . timeframe_int
+ resolved . append (
+ _resolve_rate_view_name_from_context (
+ symbol = symbol ,
+ timeframe = timeframe ,
+ granularity_name = resolve_granularity_name ( timeframe ),
+ timeframe_counts = timeframe_counts ,
+ existing_views = existing_views ,
+ require_existing = require_existing ,
+ ),
+ )
+ return resolved
+ finally :
+ if should_close and conn is not None :
+ conn . close ()
@@ -4615,14 +4585,7 @@ default view name is returned without creating a database file.
Source code in mt5cli/history.py
- 426
-427
-428
-429
-430
-431
-432
-433
+ 433
434
435
436
@@ -4667,59 +4630,66 @@ default view name is returned without creating a database file.
475
476
477
-478 def resolve_rate_view_name (
- conn_or_path : SqliteConnOrPath | None ,
- symbol : str ,
- granularity : str ,
- * ,
- require_existing : bool = False ,
-) -> str :
- """Resolve the mt5cli-managed rate compatibility view name.
-
- Args:
- conn_or_path: SQLite database path or open connection. When None or a
- non-existing path and ``require_existing`` is False, the deterministic
- default view name is returned without creating a database file.
- symbol: Symbol stored in the normalized ``rates`` table.
- granularity: Timeframe name (for example ``M1``) or integer string.
- require_existing: When True, require the database and a managed view to exist.
-
- Returns:
- View name such as ``rate_EURUSD__1`` or ``rate_EURUSD__M1_1``.
-
- Raises:
- ValueError: If ``require_existing`` is True and the database or view is missing.
- """
- timeframe = parse_timeframe ( granularity )
- granularity_name = resolve_granularity_name ( timeframe )
- conn , should_close = _open_history_connection ( conn_or_path )
- try :
- if conn is None :
- if require_existing :
- path = (
- conn_or_path
- if isinstance ( conn_or_path , ( Path , str ))
- else "database"
- )
- msg = f "SQLite database not found: { path } "
- raise ValueError ( msg )
- return build_rate_view_name (
- symbol = symbol ,
- granularity = granularity_name ,
- granularity_count = 1 ,
- timeframe = timeframe ,
- )
- return _resolve_rate_view_name_from_context (
- symbol = symbol ,
- timeframe = timeframe ,
- granularity_name = granularity_name ,
- timeframe_counts = _load_rates_timeframe_counts ( conn ),
- existing_views = _load_existing_rate_views ( conn ),
- require_existing = require_existing ,
- )
- finally :
- if should_close and conn is not None :
- conn . close ()
+478
+479
+480
+481
+482
+483
+484
+485 def resolve_rate_view_name (
+ conn_or_path : SqliteConnOrPath | None ,
+ symbol : str ,
+ granularity : str ,
+ * ,
+ require_existing : bool = False ,
+) -> str :
+ """Resolve the mt5cli-managed rate compatibility view name.
+
+ Args:
+ conn_or_path: SQLite database path or open connection. When None or a
+ non-existing path and ``require_existing`` is False, the deterministic
+ default view name is returned without creating a database file.
+ symbol: Symbol stored in the normalized ``rates`` table.
+ granularity: Timeframe name (for example ``M1``) or integer string.
+ require_existing: When True, require the database and a managed view to exist.
+
+ Returns:
+ View name such as ``rate_EURUSD__1`` or ``rate_EURUSD__M1_1``.
+
+ Raises:
+ ValueError: If ``require_existing`` is True and the database or view is missing.
+ """
+ timeframe = parse_timeframe ( granularity )
+ granularity_name = resolve_granularity_name ( timeframe )
+ conn , should_close = _open_history_connection ( conn_or_path )
+ try :
+ if conn is None :
+ if require_existing :
+ path = (
+ conn_or_path
+ if isinstance ( conn_or_path , ( Path , str ))
+ else "database"
+ )
+ msg = f "SQLite database not found: { path } "
+ raise ValueError ( msg )
+ return build_rate_view_name (
+ symbol = symbol ,
+ granularity = granularity_name ,
+ granularity_count = 1 ,
+ timeframe = timeframe ,
+ )
+ return _resolve_rate_view_name_from_context (
+ symbol = symbol ,
+ timeframe = timeframe ,
+ granularity_name = granularity_name ,
+ timeframe_counts = _load_rates_timeframe_counts ( conn ),
+ existing_views = _load_existing_rate_views ( conn ),
+ require_existing = require_existing ,
+ )
+ finally :
+ if should_close and conn is not None :
+ conn . close ()
@@ -4874,14 +4844,7 @@ default view names are returned without creating a database file.
Source code in mt5cli/history.py
- 481
-482
-483
-484
-485
-486
-487
-488
+ 488
489
490
491
@@ -4927,60 +4890,67 @@ default view names are returned without creating a database file.
531
532
533
-534 def resolve_rate_view_names (
- conn_or_path : SqliteConnOrPath | None ,
- symbols : Sequence [ str ],
- granularities : Sequence [ str ],
- * ,
- require_existing : bool = False ,
-) -> list [ str ]:
- """Resolve rate compatibility view names for symbol and granularity pairs.
-
- Args:
- conn_or_path: SQLite database path or open connection. When None or a
- non-existing path and ``require_existing`` is False, deterministic
- default view names are returned without creating a database file.
- symbols: Symbols stored in the normalized ``rates`` table.
- granularities: Timeframe names (for example ``M1``) or integer strings.
- require_existing: When True, require the database and managed views to exist.
-
- Returns:
- View names in row-major order: every ``granularity`` for the first
- symbol, then every granularity for the next symbol, and so on.
- """
- conn , should_close = _open_history_connection ( conn_or_path )
- try :
- if conn is None :
- return [
- resolve_rate_view_name (
- conn_or_path ,
- symbol ,
- granularity ,
- require_existing = require_existing ,
- )
- for symbol in symbols
- for granularity in granularities
- ]
- timeframe_counts = _load_rates_timeframe_counts ( conn )
- existing_views = _load_existing_rate_views ( conn )
- resolved : list [ str ] = []
- for symbol in symbols :
- for granularity in granularities :
- timeframe = parse_timeframe ( granularity )
- resolved . append (
- _resolve_rate_view_name_from_context (
- symbol = symbol ,
- timeframe = timeframe ,
- granularity_name = resolve_granularity_name ( timeframe ),
- timeframe_counts = timeframe_counts ,
- existing_views = existing_views ,
- require_existing = require_existing ,
- ),
- )
- return resolved
- finally :
- if should_close and conn is not None :
- conn . close ()
+534
+535
+536
+537
+538
+539
+540
+541 def resolve_rate_view_names (
+ conn_or_path : SqliteConnOrPath | None ,
+ symbols : Sequence [ str ],
+ granularities : Sequence [ str ],
+ * ,
+ require_existing : bool = False ,
+) -> list [ str ]:
+ """Resolve rate compatibility view names for symbol and granularity pairs.
+
+ Args:
+ conn_or_path: SQLite database path or open connection. When None or a
+ non-existing path and ``require_existing`` is False, deterministic
+ default view names are returned without creating a database file.
+ symbols: Symbols stored in the normalized ``rates`` table.
+ granularities: Timeframe names (for example ``M1``) or integer strings.
+ require_existing: When True, require the database and managed views to exist.
+
+ Returns:
+ View names in row-major order: every ``granularity`` for the first
+ symbol, then every granularity for the next symbol, and so on.
+ """
+ conn , should_close = _open_history_connection ( conn_or_path )
+ try :
+ if conn is None :
+ return [
+ resolve_rate_view_name (
+ conn_or_path ,
+ symbol ,
+ granularity ,
+ require_existing = require_existing ,
+ )
+ for symbol in symbols
+ for granularity in granularities
+ ]
+ timeframe_counts = _load_rates_timeframe_counts ( conn )
+ existing_views = _load_existing_rate_views ( conn )
+ resolved : list [ str ] = []
+ for symbol in symbols :
+ for granularity in granularities :
+ timeframe = parse_timeframe ( granularity )
+ resolved . append (
+ _resolve_rate_view_name_from_context (
+ symbol = symbol ,
+ timeframe = timeframe ,
+ granularity_name = resolve_granularity_name ( timeframe ),
+ timeframe_counts = timeframe_counts ,
+ existing_views = existing_views ,
+ require_existing = require_existing ,
+ ),
+ )
+ return resolved
+ finally :
+ if should_close and conn is not None :
+ conn . close ()
@@ -5038,135 +5008,135 @@ default view names are returned without creating a database file.
Source code in mt5cli/history.py
- def write_collected_datasets (
- conn : sqlite3 . Connection ,
- client : Mt5DataClient ,
- symbols : Sequence [ str ],
- datasets : set [ Dataset ],
- timeframe : int ,
- flags : int ,
- date_from : datetime ,
- date_to : datetime ,
- if_exists : IfExists ,
-) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]:
- """Collect selected datasets and stream each symbol frame into SQLite.
-
- Returns:
- Written datasets and their columns.
- """
- written_columns : dict [ Dataset , set [ str ]] = {}
- written_tables : set [ Dataset ] = set ()
- if Dataset . rates in datasets and write_rates_dataset (
- conn ,
- client ,
- symbols ,
- timeframe ,
- date_from ,
- date_to ,
- if_exists ,
- written_columns ,
- ):
- written_tables . add ( Dataset . rates )
- if Dataset . ticks in datasets and write_ticks_dataset (
- conn ,
- client ,
- symbols ,
- flags ,
- date_from ,
- date_to ,
- if_exists ,
- written_columns ,
- ):
- written_tables . add ( Dataset . ticks )
- if Dataset . history_orders in datasets and write_history_dataset (
- conn ,
- client . history_orders_get_as_df ,
- Dataset . history_orders ,
- symbols ,
- date_from ,
- date_to ,
- if_exists ,
- written_columns ,
- include_account_events = False ,
- ):
- written_tables . add ( Dataset . history_orders )
- if Dataset . history_deals in datasets and write_history_dataset (
- conn ,
- client . history_deals_get_as_df ,
- Dataset . history_deals ,
- symbols ,
- date_from ,
- date_to ,
- if_exists ,
- written_columns ,
- include_account_events = False ,
- ):
- written_tables . add ( Dataset . history_deals )
- return written_tables , written_columns
+ def write_collected_datasets (
+ conn : sqlite3 . Connection ,
+ client : Mt5DataClient ,
+ symbols : Sequence [ str ],
+ datasets : set [ Dataset ],
+ timeframe : int ,
+ flags : int ,
+ date_from : datetime ,
+ date_to : datetime ,
+ if_exists : IfExists ,
+) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]:
+ """Collect selected datasets and stream each symbol frame into SQLite.
+
+ Returns:
+ Written datasets and their columns.
+ """
+ written_columns : dict [ Dataset , set [ str ]] = {}
+ written_tables : set [ Dataset ] = set ()
+ if Dataset . rates in datasets and write_rates_dataset (
+ conn ,
+ client ,
+ symbols ,
+ timeframe ,
+ date_from ,
+ date_to ,
+ if_exists ,
+ written_columns ,
+ ):
+ written_tables . add ( Dataset . rates )
+ if Dataset . ticks in datasets and write_ticks_dataset (
+ conn ,
+ client ,
+ symbols ,
+ flags ,
+ date_from ,
+ date_to ,
+ if_exists ,
+ written_columns ,
+ ):
+ written_tables . add ( Dataset . ticks )
+ if Dataset . history_orders in datasets and write_history_dataset (
+ conn ,
+ client . history_orders_get_as_df ,
+ Dataset . history_orders ,
+ symbols ,
+ date_from ,
+ date_to ,
+ if_exists ,
+ written_columns ,
+ include_account_events = False ,
+ ):
+ written_tables . add ( Dataset . history_orders )
+ if Dataset . history_deals in datasets and write_history_dataset (
+ conn ,
+ client . history_deals_get_as_df ,
+ Dataset . history_deals ,
+ symbols ,
+ date_from ,
+ date_to ,
+ if_exists ,
+ written_columns ,
+ include_account_events = False ,
+ ):
+ written_tables . add ( Dataset . history_deals )
+ return written_tables , written_columns
@@ -5225,101 +5195,101 @@ default view names are returned without creating a database file.
Source code in mt5cli/history.py
- def write_history_dataset (
- conn : sqlite3 . Connection ,
- fetch : Callable [ ... , pd . DataFrame ],
- dataset : Dataset ,
- symbols : Sequence [ str ],
- date_from : datetime ,
- date_to : datetime ,
- if_exists : IfExists ,
- written_columns : dict [ Dataset , set [ str ]],
- * ,
- include_account_events : bool = False ,
-) -> bool :
- """Stream a history dataset into SQLite.
-
- Returns:
- True if the target table was written.
- """
- table_exists = False
- if include_account_events :
- frame = filter_trade_history_frame (
- fetch ( date_from = date_from , date_to = date_to ),
- symbols ,
- include_account_events = True ,
- )
- return write_streamed_frame (
- conn ,
- frame ,
- dataset ,
- table_exists ,
- if_exists ,
- written_columns ,
- )
-
- def _fetch_history_frame ( sym : str ) -> pd . DataFrame :
- return filter_trade_history_frame (
- fetch ( date_from = date_from , date_to = date_to , symbol = sym ),
- [ sym ],
- include_account_events = False ,
- )
-
- return _stream_symbol_frames (
- conn ,
- symbols ,
- dataset ,
- if_exists ,
- written_columns ,
- _fetch_history_frame ,
- )
+ def write_history_dataset (
+ conn : sqlite3 . Connection ,
+ fetch : Callable [ ... , pd . DataFrame ],
+ dataset : Dataset ,
+ symbols : Sequence [ str ],
+ date_from : datetime ,
+ date_to : datetime ,
+ if_exists : IfExists ,
+ written_columns : dict [ Dataset , set [ str ]],
+ * ,
+ include_account_events : bool = False ,
+) -> bool :
+ """Stream a history dataset into SQLite.
+
+ Returns:
+ True if the target table was written.
+ """
+ table_exists = False
+ if include_account_events :
+ frame = filter_trade_history_frame (
+ fetch ( date_from = date_from , date_to = date_to ),
+ symbols ,
+ include_account_events = True ,
+ )
+ return write_streamed_frame (
+ conn ,
+ frame ,
+ dataset ,
+ table_exists ,
+ if_exists ,
+ written_columns ,
+ )
+
+ def _fetch_history_frame ( sym : str ) -> pd . DataFrame :
+ return filter_trade_history_frame (
+ fetch ( date_from = date_from , date_to = date_to , symbol = sym ),
+ [ sym ],
+ include_account_events = False ,
+ )
+
+ return _stream_symbol_frames (
+ conn ,
+ symbols ,
+ dataset ,
+ if_exists ,
+ written_columns ,
+ _fetch_history_frame ,
+ )
@@ -5381,167 +5351,167 @@ default view names are returned without creating a database file.
Source code in mt5cli/history.py
- def write_incremental_datasets ( # noqa: PLR0913
- conn : sqlite3 . Connection ,
- client : Mt5DataClient ,
- symbols : Sequence [ str ],
- selected_datasets : set [ Dataset ],
- resolved_timeframes : list [ int ],
- resolved_tick_flags : int ,
- fallback_start : datetime ,
- end_date : datetime ,
- * ,
- deduplicate : bool ,
- create_rate_views : bool ,
- with_views : bool ,
- include_account_events : bool ,
-) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]:
- """Append selected datasets incrementally and refresh indexes and views.
-
- Returns:
- Written datasets and their columns.
- """
- written_columns : dict [ Dataset , set [ str ]] = {}
- written_tables : set [ Dataset ] = set ()
- dedup_scopes : dict [ Dataset , list [ DedupScope ]] = {}
- if Dataset . rates in selected_datasets :
- _write_incremental_rates (
- conn ,
- client ,
- symbols ,
- resolved_timeframes ,
- fallback_start ,
- end_date ,
- written_columns ,
- written_tables ,
- dedup_scopes ,
- )
- if Dataset . ticks in selected_datasets :
- _write_incremental_ticks (
- conn ,
- client ,
- symbols ,
- resolved_tick_flags ,
- fallback_start ,
- end_date ,
- written_columns ,
- written_tables ,
- dedup_scopes ,
- )
- if Dataset . history_orders in selected_datasets :
- _write_incremental_history_orders (
- conn ,
- client ,
- symbols ,
- fallback_start ,
- end_date ,
- written_columns ,
- written_tables ,
- dedup_scopes ,
- )
- if Dataset . history_deals in selected_datasets :
- _write_incremental_history_deals (
- conn ,
- client ,
- symbols ,
- fallback_start ,
- end_date ,
- written_columns ,
- written_tables ,
- dedup_scopes ,
- include_account_events = include_account_events ,
- )
- _finalize_incremental_writes (
- conn ,
- selected_datasets ,
- written_columns ,
- written_tables ,
- dedup_scopes ,
- deduplicate = deduplicate ,
- create_rate_views = create_rate_views ,
- with_views = with_views ,
- )
- return written_tables , written_columns
+ def write_incremental_datasets ( # noqa: PLR0913
+ conn : sqlite3 . Connection ,
+ client : Mt5DataClient ,
+ symbols : Sequence [ str ],
+ selected_datasets : set [ Dataset ],
+ resolved_timeframes : list [ int ],
+ resolved_tick_flags : int ,
+ fallback_start : datetime ,
+ end_date : datetime ,
+ * ,
+ deduplicate : bool ,
+ create_rate_views : bool ,
+ with_views : bool ,
+ include_account_events : bool ,
+) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]:
+ """Append selected datasets incrementally and refresh indexes and views.
+
+ Returns:
+ Written datasets and their columns.
+ """
+ written_columns : dict [ Dataset , set [ str ]] = {}
+ written_tables : set [ Dataset ] = set ()
+ dedup_scopes : dict [ Dataset , list [ DedupScope ]] = {}
+ if Dataset . rates in selected_datasets :
+ _write_incremental_rates (
+ conn ,
+ client ,
+ symbols ,
+ resolved_timeframes ,
+ fallback_start ,
+ end_date ,
+ written_columns ,
+ written_tables ,
+ dedup_scopes ,
+ )
+ if Dataset . ticks in selected_datasets :
+ _write_incremental_ticks (
+ conn ,
+ client ,
+ symbols ,
+ resolved_tick_flags ,
+ fallback_start ,
+ end_date ,
+ written_columns ,
+ written_tables ,
+ dedup_scopes ,
+ )
+ if Dataset . history_orders in selected_datasets :
+ _write_incremental_history_orders (
+ conn ,
+ client ,
+ symbols ,
+ fallback_start ,
+ end_date ,
+ written_columns ,
+ written_tables ,
+ dedup_scopes ,
+ )
+ if Dataset . history_deals in selected_datasets :
+ _write_incremental_history_deals (
+ conn ,
+ client ,
+ symbols ,
+ fallback_start ,
+ end_date ,
+ written_columns ,
+ written_tables ,
+ dedup_scopes ,
+ include_account_events = include_account_events ,
+ )
+ _finalize_incremental_writes (
+ conn ,
+ selected_datasets ,
+ written_columns ,
+ written_tables ,
+ dedup_scopes ,
+ deduplicate = deduplicate ,
+ create_rate_views = create_rate_views ,
+ with_views = with_views ,
+ )
+ return written_tables , written_columns
@@ -5598,77 +5568,77 @@ default view names are returned without creating a database file.
Source code in mt5cli/history.py
- def write_rates_dataset (
- conn : sqlite3 . Connection ,
- client : Mt5DataClient ,
- symbols : Sequence [ str ],
- timeframe : int ,
- date_from : datetime ,
- date_to : datetime ,
- if_exists : IfExists ,
- written_columns : dict [ Dataset , set [ str ]],
-) -> bool :
- """Stream rates frames into SQLite.
-
- Returns:
- True if the rates table was written.
- """
-
- def _fetch_rates_frame ( sym : str ) -> pd . DataFrame :
- frame = client . copy_rates_range_as_df (
- symbol = sym ,
- timeframe = timeframe ,
- date_from = date_from ,
- date_to = date_to ,
- ) . drop ( columns = [ "symbol" , "timeframe" ], errors = "ignore" )
- if len ( frame . columns ) != 0 :
- frame . insert ( 0 , "symbol" , sym )
- frame . insert ( 1 , "timeframe" , timeframe )
- return frame
-
- return _stream_symbol_frames (
- conn ,
- symbols ,
- Dataset . rates ,
- if_exists ,
- written_columns ,
- _fetch_rates_frame ,
- )
+ def write_rates_dataset (
+ conn : sqlite3 . Connection ,
+ client : Mt5DataClient ,
+ symbols : Sequence [ str ],
+ timeframe : int ,
+ date_from : datetime ,
+ date_to : datetime ,
+ if_exists : IfExists ,
+ written_columns : dict [ Dataset , set [ str ]],
+) -> bool :
+ """Stream rates frames into SQLite.
+
+ Returns:
+ True if the rates table was written.
+ """
+
+ def _fetch_rates_frame ( sym : str ) -> pd . DataFrame :
+ frame = client . copy_rates_range_as_df (
+ symbol = sym ,
+ timeframe = timeframe ,
+ date_from = date_from ,
+ date_to = date_to ,
+ ) . drop ( columns = [ "symbol" , "timeframe" ], errors = "ignore" )
+ if len ( frame . columns ) != 0 :
+ frame . insert ( 0 , "symbol" , sym )
+ frame . insert ( 1 , "timeframe" , timeframe )
+ return frame
+
+ return _stream_symbol_frames (
+ conn ,
+ symbols ,
+ Dataset . rates ,
+ if_exists ,
+ written_columns ,
+ _fetch_rates_frame ,
+ )
@@ -5723,41 +5693,41 @@ default view names are returned without creating a database file.
Source code in mt5cli/history.py
- def write_streamed_frame (
- conn : sqlite3 . Connection ,
- frame : pd . DataFrame ,
- dataset : Dataset ,
- table_exists : bool ,
- if_exists : IfExists ,
- written_columns : dict [ Dataset , set [ str ]],
-) -> bool :
- """Write one streamed dataset frame and track table state.
-
- Returns:
- True if the dataset table exists after this write attempt.
- """
- write_mode = IfExists . APPEND if table_exists else if_exists
- if append_dataframe ( conn , frame , dataset . table_name , write_mode ):
- record_written_columns ( written_columns , dataset , frame )
- return True
- return table_exists
+ def write_streamed_frame (
+ conn : sqlite3 . Connection ,
+ frame : pd . DataFrame ,
+ dataset : Dataset ,
+ table_exists : bool ,
+ if_exists : IfExists ,
+ written_columns : dict [ Dataset , set [ str ]],
+) -> bool :
+ """Write one streamed dataset frame and track table state.
+
+ Returns:
+ True if the dataset table exists after this write attempt.
+ """
+ write_mode = IfExists . APPEND if table_exists else if_exists
+ if append_dataframe ( conn , frame , dataset . table_name , write_mode ):
+ record_written_columns ( written_columns , dataset , frame )
+ return True
+ return table_exists
@@ -5814,75 +5784,75 @@ default view names are returned without creating a database file.
Source code in mt5cli/history.py
- def write_ticks_dataset (
- conn : sqlite3 . Connection ,
- client : Mt5DataClient ,
- symbols : Sequence [ str ],
- flags : int ,
- date_from : datetime ,
- date_to : datetime ,
- if_exists : IfExists ,
- written_columns : dict [ Dataset , set [ str ]],
-) -> bool :
- """Stream ticks frames into SQLite.
-
- Returns:
- True if the ticks table was written.
- """
-
- def _fetch_ticks_frame ( sym : str ) -> pd . DataFrame :
- frame = client . copy_ticks_range_as_df (
- symbol = sym ,
- date_from = date_from ,
- date_to = date_to ,
- flags = flags ,
- ) . drop ( columns = [ "symbol" ], errors = "ignore" )
- if len ( frame . columns ) != 0 :
- frame . insert ( 0 , "symbol" , sym )
- return frame
-
- return _stream_symbol_frames (
- conn ,
- symbols ,
- Dataset . ticks ,
- if_exists ,
- written_columns ,
- _fetch_ticks_frame ,
- )
+ def write_ticks_dataset (
+ conn : sqlite3 . Connection ,
+ client : Mt5DataClient ,
+ symbols : Sequence [ str ],
+ flags : int ,
+ date_from : datetime ,
+ date_to : datetime ,
+ if_exists : IfExists ,
+ written_columns : dict [ Dataset , set [ str ]],
+) -> bool :
+ """Stream ticks frames into SQLite.
+
+ Returns:
+ True if the ticks table was written.
+ """
+
+ def _fetch_ticks_frame ( sym : str ) -> pd . DataFrame :
+ frame = client . copy_ticks_range_as_df (
+ symbol = sym ,
+ date_from = date_from ,
+ date_to = date_to ,
+ flags = flags ,
+ ) . drop ( columns = [ "symbol" ], errors = "ignore" )
+ if len ( frame . columns ) != 0 :
+ frame . insert ( 0 , "symbol" , sym )
+ return frame
+
+ return _stream_symbol_frames (
+ conn ,
+ symbols ,
+ Dataset . ticks ,
+ if_exists ,
+ written_columns ,
+ _fetch_ticks_frame ,
+ )
diff --git a/api/index.html b/api/index.html
index 8bdee0b..c262fb6 100644
--- a/api/index.html
+++ b/api/index.html
@@ -82,6 +82,14 @@
History Collection (SQLite)
+
+ Telemetry
+
+
+
+ Grafana
+
+
Utils
@@ -195,6 +203,14 @@ responsibilities.
SQLite schema, incremental writes, dedup, and rate views
+Telemetry
+OpenTelemetry metrics setup, meters, and emitted metric names
+
+
+Grafana
+Grafana-ready SQLite schema, views, snapshots, and published copies
+
+
CLI
Typer commands that delegate to the Python API
diff --git a/api/public-contract/index.html b/api/public-contract/index.html
index c334019..27b36fc 100644
--- a/api/public-contract/index.html
+++ b/api/public-contract/index.html
@@ -82,6 +82,14 @@
History Collection (SQLite)
+
+ Telemetry
+
+
+
+ Grafana
+
+
Utils
diff --git a/api/schemas/index.html b/api/schemas/index.html
index 4a8d093..3e0e56c 100644
--- a/api/schemas/index.html
+++ b/api/schemas/index.html
@@ -82,6 +82,14 @@
History Collection (SQLite)
+
+ Telemetry
+
+
+
+ Grafana
+
+
Utils
diff --git a/api/sdk/index.html b/api/sdk/index.html
index ca3d674..b5d11e4 100644
--- a/api/sdk/index.html
+++ b/api/sdk/index.html
@@ -82,6 +82,14 @@
History Collection (SQLite)
+
+ Telemetry
+
+
+
+ Grafana
+
+
Utils
diff --git a/api/telemetry/index.html b/api/telemetry/index.html
new file mode 100644
index 0000000..f893af3
--- /dev/null
+++ b/api/telemetry/index.html
@@ -0,0 +1,673 @@
+
+
+
+
+
+
+
+
+
+
+ Telemetry - mt5cli API Documentation
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Telemetry
+
+
+
+
+
+
+
+ mt5cli.telemetry
+
+
+
+
+
+
+
Optional OpenTelemetry metrics for MT5 history and snapshot observability.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ logger
+
+
+
+ module-attribute
+
+
+
+
logger = getLogger ( __name__ )
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
configure_metrics ( meter : Any ) -> None
+
+
+
+
+
Configure MT5 metrics using the provided meter.
+
+
+
Parameters:
+
+
+
+ Name
+ Type
+ Description
+ Default
+
+
+
+
+
+ meter
+
+
+ Any
+
+
+
+
An OpenTelemetry Meter or duck-typed compatible object.
+
+
+
+ required
+
+
+
+
+
+
+
+ Source code in mt5cli/telemetry.py
+ def configure_metrics ( meter : Any ) -> None : # noqa: ANN401
+ """Configure MT5 metrics using the provided meter.
+
+ Args:
+ meter: An OpenTelemetry ``Meter`` or duck-typed compatible object.
+ """
+ _metrics . configure ( meter )
+
+
+
+
+
+
+
+
+
+
+ enable_otel_metrics
+
+
+
+
enable_otel_metrics (
+ service_name : str = "mt5cli" ,
+ readers : list [ Any ] | None = None ,
+) -> None
+
+
+
+
+
Enable OTel metrics by wiring up an SDK MeterProvider pipeline.
+
Requires the otel optional dependency group:
+pip install "mt5cli[otel]".
+
+
+
Parameters:
+
+
+
+ Name
+ Type
+ Description
+ Default
+
+
+
+
+
+ service_name
+
+
+ str
+
+
+
+
OTel meter/service name used for the Resource and
+the meter itself.
+
+
+
+ 'mt5cli'
+
+
+
+
+ readers
+
+
+ list [Any ] | None
+
+
+
+
Optional list of metric readers. When None (the default),
+a :class:~opentelemetry.sdk.metrics.export.PeriodicExportingMetricReader
+backed by an OTLP HTTP exporter is created automatically
+(reads the endpoint from OTEL_EXPORTER_OTLP_ENDPOINT).
+Pass a custom list (e.g. InMemoryMetricReader for tests)
+to override.
+
+
+
+ None
+
+
+
+
+
+
+
Raises:
+
+
+
+ Type
+ Description
+
+
+
+
+
+ ImportError
+
+
+
+
If opentelemetry-api is not installed, or if
+readers is None and
+opentelemetry-exporter-otlp-proto-http is not installed.
+
+
+
+
+
+
+
+
+ Source code in mt5cli/telemetry.py
+ def enable_otel_metrics (
+ service_name : str = "mt5cli" ,
+ readers : list [ Any ] | None = None ,
+) -> None :
+ """Enable OTel metrics by wiring up an SDK ``MeterProvider`` pipeline.
+
+ Requires the ``otel`` optional dependency group:
+ ``pip install "mt5cli[otel]"``.
+
+ Args:
+ service_name: OTel meter/service name used for the ``Resource`` and
+ the meter itself.
+ readers: Optional list of metric readers. When *None* (the default),
+ a :class:`~opentelemetry.sdk.metrics.export.PeriodicExportingMetricReader`
+ backed by an OTLP HTTP exporter is created automatically
+ (reads the endpoint from ``OTEL_EXPORTER_OTLP_ENDPOINT``).
+ Pass a custom list (e.g. ``InMemoryMetricReader`` for tests)
+ to override.
+
+ Raises:
+ ImportError: If ``opentelemetry-api`` is not installed, or if
+ ``readers`` is *None* and
+ ``opentelemetry-exporter-otlp-proto-http`` is not installed.
+ """
+ if not _OTEL_AVAILABLE :
+ msg = (
+ "opentelemetry-api is not installed. "
+ 'Install it with: pip install "mt5cli[otel]"'
+ )
+ raise ImportError ( msg )
+ if readers is None :
+ if _OtelOTLPExporter is None :
+ msg = (
+ "opentelemetry-exporter-otlp-proto-http is required for the "
+ "default OTLP export pipeline. "
+ 'Install it with: pip install "mt5cli[otel]" or pass a '
+ "custom readers list."
+ )
+ raise ImportError ( msg )
+ readers = [ _OtelPeriodicReader ( _OtelOTLPExporter ())] # type: ignore[misc]
+ resource = _OtelResource . create ({ "service.name" : service_name }) # type: ignore[union-attr]
+ provider = _OtelMeterProvider ( resource = resource , metric_readers = readers ) # type: ignore[misc]
+ _otel_metrics_mod . set_meter_provider ( provider ) # type: ignore[union-attr]
+ meter = provider . get_meter ( service_name )
+ configure_metrics ( meter )
+
+
+
+
+
+
+
+
+
+
+ get_metrics
+
+
+
+
get_metrics () -> _Mt5Metrics
+
+
+
+
+
Return the global :class:_Mt5Metrics instance.
+
+
+
Returns:
+
+
+
+ Type
+ Description
+
+
+
+
+
+ _Mt5Metrics
+
+
+
+
The global metric registry (no-op until :func:configure_metrics is
+
+
+
+
+
+ _Mt5Metrics
+
+
+
+
+
+
+
+
+
+
+ Source code in mt5cli/telemetry.py
+ def get_metrics () -> _Mt5Metrics :
+ """Return the global :class:`_Mt5Metrics` instance.
+
+ Returns:
+ The global metric registry (no-op until :func:`configure_metrics` is
+ called).
+ """
+ return _metrics
+
+
+
+
+
+
+
+
+
+
+
+
+
Enabling OpenTelemetry metrics
+
Install the optional exporter dependencies with:
+
+
Then enable the default OTLP HTTP pipeline:
+
from mt5cli.telemetry import enable_otel_metrics
+
+enable_otel_metrics ( service_name = "mt5cli" )
+
+
When readers=None, enable_otel_metrics() builds a
+PeriodicExportingMetricReader backed by the OTLP HTTP exporter and reads the
+endpoint from OTEL_EXPORTER_OTLP_ENDPOINT.
+
If your application already owns an OpenTelemetry Meter, wire mt5cli into it
+directly with configure_metrics(meter).
+
Emitted metric names
+
enable_otel_metrics() / configure_metrics() register these instruments:
+
+mt5_history_update_duration_seconds
+mt5_history_update_rows_total
+mt5_history_update_failures_total
+mt5_snapshot_update_duration_seconds
+mt5_snapshot_update_failures_total
+mt5_account_balance
+mt5_account_equity
+mt5_account_margin
+mt5_account_margin_free
+mt5_account_margin_level
+mt5_position_profit
+mt5_position_volume
+mt5_terminal_connected
+mt5_terminal_trade_allowed
+mt5_terminal_trade_expert
+mt5_last_successful_update_timestamp
+
+
The history metrics use a dataset attribute. Account and position gauges add
+labels such as login, server, and symbol where applicable.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
From here you can search these documents. Enter your search terms below.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Keys
+ Action
+
+
+
+
+ ?
+ Open this help
+
+
+ n
+ Next page
+
+
+ p
+ Previous page
+
+
+ s
+ Search
+
+
+
+
+
+
+
+
+
+
+
diff --git a/api/trading/index.html b/api/trading/index.html
index b634dae..8d80cf9 100644
--- a/api/trading/index.html
+++ b/api/trading/index.html
@@ -82,6 +82,14 @@
History Collection (SQLite)
+
+ Telemetry
+
+
+
+ Grafana
+
+
Utils
diff --git a/api/utils/index.html b/api/utils/index.html
index 82dd740..c788167 100644
--- a/api/utils/index.html
+++ b/api/utils/index.html
@@ -82,6 +82,14 @@
History Collection (SQLite)
+
+ Telemetry
+
+
+
+ Grafana
+
+
Utils
@@ -96,7 +104,7 @@
-
+
Previous
diff --git a/index.html b/index.html
index e4ff45f..5e4d5f6 100644
--- a/index.html
+++ b/index.html
@@ -82,6 +82,14 @@
History Collection (SQLite)
+
+ Telemetry
+
+
+
+ Grafana
+
+
Utils
@@ -668,5 +676,5 @@ applications should prefer dedicated closing helpers or their own risk controls.
diff --git a/objects.inv b/objects.inv
index 07efacfd37358dcebd59e6882d192fbef7806315..b74237e0fa43e246e846ae08b52cc995137db29a 100644
GIT binary patch
delta 4025
zcmV;q4@U5w9-<(SkALMR7RUGVDY`1X9jB71Y-Z=QuM=0hZI69>QoW*NiEAsZR}19F
zeEI>zMMC0n4zf3$*yjH`NaAsT1JwMtyedyl^Sm7ze%I0bL-+dcyvi8+@au=HD}QFi
z_n&{N$7JIFt)&0_hyKIvh2{e<+vbjEd|;AMWIWRd-e*PGJbxIB{&}S`xG($PK2;@c
z_%buO`mF(tKhBa*gsgukoBy`eP}3#EmmNzpk+_8K`o8U#Ao`NiPUfE(*7Pe&XwllH
zp}9wFsB(!zF#96@*~YT7>OR
z_WQbViazje;OA{$XP}pDrMF18yZ?OM-R+^@-hWDNf%X@lK9lpy(?9N@tfgXs`?5Q~
zJ%yLbg}{Oxd*`w_*e!{_Qe<6cjQUgMRm&)8JK9VwH>MW5V2(iGp&OuY)PL3$*LQ+8PT-U%IyEM1G`0pE+G97jY%Cpbvd^
z$bXuQFf}>@zB~P!H@&8Z__Xe+e6EA%Yfr_48;$6ktB2R;bSjlst*zE>nO~YwXfbRle3LrOQ17OHK$8|BAXx96iNy
z(qo*bTnU5lmy##3N1Y$-bzlH^OY1jr^clSU1;CV(5i)t4FUmOYVFv8UA_ua2C4Zh4
z5yZFUU_+~x6mLu+KnB;97iVqMM@)Gd7O16}9Aqf{ndW-GDfNc)afyHyF6S2Biuk)U
zK%Q-J+=#<>Kv>agzZs7|2{18JhVbdF39uFMd6obRb7{apz!8NQS;zy1JiaT$$V>_{
zKr%*(!v-@N;5QcatE2TS=4ZnI=zk9utuvvaevs@QhBVRCsP&uh
zdE5XCb1BHc+Cj%z8TBz!EX_jbCHeh-0Nxh+jksKM0RAXjfoF+g%ZOImC5Vu$sz&G8
zo^|lla_^F3Oc-1o6NiE6Ki>n|iWN%t#ma7>-G9jYSMng-
zuA$2Ml4Gm@V4w^f+Wxrk$~Ky}P(I`9q~y8ek78)d`;sPPAqJdfq!AU6>(0y`DVsmp
z1}KgW
zup*TSenQ|KfXP7Lc510ZKAMVSxzBn;0E}C2@VZKCXNS>B^=gRxdjOLGCd!mFJ3bzI
zUE8gn@Fsq4)eg?1?LN8`?ubQ<*cYIPVE6(N5eR3*AymI#^a62(yMOnH>vAbmy_so#
zi*dTZ)ndhYrJ90nMT?T%SCmMP%eM>`hfO_#;vD~gVTlr(kK-QF^<|D}V;o`wK2V-N
zx?B{1cj4E@@yZKHZj(`1KxRajLSIk~Q=GLh>l!1!#Y=#4O?DnAH{pZMb*!*er`&>1
zF=f6*@(_Eew$R6ZAb%~}OSOeol_L>sYbWfuvcKv&^zW?-u77v*HjY67V_|Z&U(DO>Bb
zCuh5}uQ%lL?%UlWU~DYuQyh@~c`k6k&QHHy?#Z7Qcl+zxZ{+NBe|opu-z~mCI6pSR
z#HgVlBH8D1oCCLJ*L=@(Oz$M6MrRrZEr#w+6eYTJK7-F-RC45*)yypVqO?8zF_gWW
zhMSrmJmvY3_J0&1-4>MU*p7jCag$0o;-V#z%lNAp0~BVOM*%+mOl!>-Y>?SRu2nqfOxnI*jKi8y;#kZPJ&V)8<%|#}ELf
zo)k%p?3h6Tq-)}vtqUQvdy;3OzoAc5&SXkd8s+Fa`+uD+0A%u8Whd4!-VbU9KRsLO
z1?6qsi2?k5S;=`#pC=ch6|yC&%6$H$UT-c4l5;(VB?meqCj}Fnaso2m=>1I7&H)e8
z*ZDcbD+7nJlA&B>BfhJwiozvZjMfsWwT8-3$n;rLK+;FHvvc%?RlOpkLlu}oj=|^8
zy&2{o34g@(Rht!o+$7;t8B{I&7BQJe3%TcoU@+nMm*JJN1X_Ae;T%GQ1cBuWe-FA4
zd7$G1s3i#T-RlndD8W^bSl(tbFW9v*66YROE9{S<<>sNwLKOJ&P*Q9=tH)yt-vJF_
zZS_P6V1W*TF^$zYWMCUH!G$dxNPmn}koKr#1An*S^-Ppa`ABul+z@P5WPwx7{J7qe
z`mIOIqQE3E=H14o%#3ryTe2cNODbMzk<{Cdh%tl}mkCTK6P}fek^CloJrUC09Ak|6
zLTW;LG3Oc!bewzl1AWuUF10~V%Shu)BYGph2Ve|zRhFww{xVEeB|*R)Vgo)l3uc~G
zpMP)@Sz&U@f>4%7WpgYSr3SSA2RV4f3o-`vU}81(Xa*f0S7}{Sj{wnXY7eeiN1@MV
zQ&4~@N)}5iVxM@@4;fO=b@4xbMZ
zmq!nah$Dh0M_}ZRy>&oH`qvAc8W9x(k7|H&)*H!y`tZ377`g9|1|TCT_p##}q5%J?
z2`=d}l-HRicHSdedNQAFi}XJ}gnyacP1Ki7UJV7c7sj>$#lKH-1xhXz7h6!O6&=rf
zBX!%fylu*S3r{qN_gVg(Jhnr>C1b5R0&D|-i5yggel}nUTifbhgz*|YqK
zkT-cI{n6#e(mR$DbEAUh&B}QMGKw_e_sMpoVeky)T`BXH_BkbHpaobBvwtKOEy0J5
zZ1i+V;^&`*RLH%KX+ZDZMKBKIYDM=Qz7@y6AuR?NF{M_0tv$k
zFV6S~E;FGxEs&KZz-0s+#|5$&q`Az80`7mcgO_0u`<9V+-tH$AE)$kKxu^CSZG(
z{H0a~64kQFaIO|{T2cAe>3`|XZ!D#SOc}}~4WS|X6~ZsrM{t=5ZD;^lSpr-}A{!n+
zb{q_ssqmf%kfk-kGK8MhNa$UiVO6e#_(_hmd5!RK6kY{t@*{P=168}C@n?`Vk#!{6
zgboIf!4_QBp=5LX+b`!xIC8RQ!~9Odlum5ZP`FyV)C+thbRm?pSbu;*TpNs<3-{Ju
zgws$kt`25sg$No7)&;03Uwr^Z=V6T*h-ECY=`36T9by|k+%sPCK{#8qc_J#x=CQ31
zt=f!GXI&?Kx2LQc!2Jz=nly1{6gL@~GXJS3O>hHsTQm+iQS$>D(B8}S*=*dautA!T
z1KdlNnM^R}et4ZlRi+U1
zy)TEJCLn=%v%zblYTz}|Z)(a}$YdT%R4K|nVWVu>OeMB6^-@oDn^=6NZQ!%?W_K?|NM1
z6ir-S<40`*7~x|KvTp!zN
z87BaCRK_9iw)PU(S&7S`o<(F8SIxz?23W)FenooZ>FB_;ta}URLf`>;CrTkuOT0AT
zpUMrQZCMbSmUlm5X$(>p!ux$Aprt{4c>$S+VW-T<5yfg|Q;#}6F4B3fDvoHD+nqqk
zgWGGNT7T#M*qGS0yEUe{`W7p?xH`YaYW_OCy}G!9=FfeVG+c9%v!?`8Cx5fH
zNzsYU`Nu$cigY|;;$J|i^JeU@#CHdFOyxeK5HhMlnDS2cgk}C~o5Q-`{meKi;jRD7
fD<33!+eTP8n_3
zrw;&fK!U`h8t~hLW5RU
z6-{NCNa##IW#2TkP2$lr`INj%A8DDyVW(QnylFGq>K1TESyyoAld@_}@iBKCPnu@|N_8g*NEHxMWlQwG3hK;2?;Z0oLBe_?K##k7#;a)=|M
zCuvBhdVe?;W!6rznC#zJ*|b)?91F;3sK_O;th275AAUXc#GiioXq4HUpY6B%%Wqq<
z|9-oj4O7CAC`R{1{WL3YqJ)wyhbSv$C~D|058D^fl`{xHmo*4i_3?^6(E>uyWevi5
zC;NTfI7RPx)A5VCEfdhqHp&~MyX}APx4S*`*?$|w4bcAb%U5!7b@uxX%9<-?xNqBw
zyEAyJoCyrrX>`u3gV~d~Uy7t@v{84dw5S;+bwjJM=f=FnHo3)so$aYsT@<26CxcrA
zRf)d2@4jT^NUC1~=+#*DhKY=FS+9d`xHGiv!I~NaM_-z{wDhGoZ&d
zIe#QoLYSJJ0q@;#r%6VWf+01veBGfU@uG7=Pt#41K#V?I`NUuw_3_wG3Iff)OlE_Y
zuW0*&a`I5uKcI@d%FC`ChBav2#Xkw_N+E(adv*JXc-ZL=l!DPJ!w8^PfPLB3lO>0?
zF3|$nv7kh4QXMH&K~wSmlbAO{%sD5Xi+^=aIule$!yao6<@b&uVmO#62+zSSrI$f3
zTTH1%qfby3la)a|{a%21kb^=!PN3vTGV9{Z!>^ElS$lGq7Ysi1<^yyTme5
z%!3|%oN^)b!e4S8#qM=(w9|nB;0>+Y#K~uHju!w^R)){yDPELt-ogx+!6FN?d4DC2
z9udShy10F{dVq_-w7;^Zo5F-;Q
z$N)(g$qp;b3;?&WpkEz)&q97S41a)rV8M6BH`Vu&?UQ^H9(0=lcC3t`N!5`?n(DP~
z6E2S%U|}i+8CW{#I4iwAWQxUE@ZBW0{|~^MVz&{SYYxDlWDD@5P-q#^C_4uck`zVn
zJeyGm$6M}PatsN*PJq4Q3(1_R`bEmzbDm{g-ZOCM3IDk+&{Ql?!&t2BW`Bbl`S41P
z!tLscq|G_T3IGPGz@ctW3$JXYc?0D#zAQ?fbN(o%#=OmGL={57NkuC00`g%nGiS>B
zS7rm#|LuCSf;w8UuBD{osjW>1<&(!rs(El03aMn&x5)oTZHu|F5Eeygl2*KPaxu>x
zJwZTtUampNF^U|D5;tQm(;{eCtIgnt$G(Ic!&rA&=x
zy8F%gaDlDI3iEOmg04l6k~|cYNRP|43>JosBZA_bet}_$7Mu3-9@4aVis@r4Vhuh}
zo<7=K6o7Z()`s!Q3yJQNQCL7_#E?Q?P#sg4H8X3QBfrH>fO2(pj+E>4LF+n}->Ow^
zz{i|2*CM%(Jy#p((|v?QibBlk>Cv*>1bv%{o_qeN2Q-dqF`&v9Ea_Rx>8-
zu4_7#w~}HLzBGeoU9(HYY<51Ld9OjMq%)h8Os^xMv@QMF<$tZ5*~>h-Dw6X2NLz}K
zuFG??WOyTP+^7;`yLh|h%HU$z5``J3X28pzXpJez3c0uljzH{w7CEQVqq}Ku6aI*J
zdB#MuYvZ+pq^9+X1u|(GT4jzrrf`jt?pUgsn3!vGTAiA5zpBX8l_H6OBzzR$DW}h_
z0DuoM%=6NOJ%1AVL}hS9qT(p0;kb7OSR7;=3Z*AnG2V76C>UQYb%WBnY{Z=4AunX8
zM8dH3%VbYf1Ea^M-oEVIE77Jv&(fv8(g-qVaD{v(gdw-aYmI!@OCz+=G_&K}Yf2%LaKV-neI|ZWEak
zY>S14xhK^E`?IULzUVv?4L(2Q6x$8Tk*3Y&LpvJ6>f(tKKu8{f(VbQA(X*8pZzvlF
z8XiV0Xn$~cWCyq6_KcKO^@#PG>=0~KWP?-Pd>`SBebzl@)?lI-(_v#(W%{VlnyLuL
zkBVEGMUD17qEA}GRRS}}_*V%+B)3V|NQAUE#b{%$MRecptEt8SouX}Speul6Q)~1X
zIPMoH&|7yM0BxWtl2mQ$m5FJp2m)>qYw)RC(0{SQ;Y^D_OxY^)LRlo0O|e|G8c@U^
za@2woqzw*%u0>=}2gE)u%Gx4>0MR1y2G=a}PA9<*6kyDY!xG|^Mw$y&tXDv=LY>nF
z6n?$F`Rkf|+kaMpRr2NL>i*j`<^jLQksv3w+^)jR^-*#7yo>UF=@o(tNH+A
z2Y=}chrtNW0|2I}34o*%j|u5>MqX)n-r=vNa|UuU(ElA-yC-pdD%r)k57MeOc5eMT
zcK&7=`@u?};&MvcrxZ+I>42sY_-D~~p=Et!7$Ff3&Qm{TV?<+G5<(@PeBE(?YRxK*
z)UR8vSfqXe9=K!km8B5^#4nEJji5HLZ+|?f5ALY~bBv8rV3m6>R2;&wUlfCJrOv?^
z-rbHEM07Qv`-p;|(7sqOz`40NNbv3@2dLLIYX6S#uspauJdE(}701XeyJ>)s_^0RF
zCms|7H)lgR%dI0oy?-Y*M()~{4P->sKD1#w2ypL{wn^v7d)6|c6W-9$%{1B;7=M3U
z2p!#x)aO-NbQv`l#?}GFpHFfHO0MK)Yf!2e?N50kWnI<0uJUvZPjrY6N&164)?K@%
zVzoN-X9a+X8Wh=ZZO}IAVs%Z^QpP4JppmsQqx_MOH@T+aq05Ig>`{!YjY^Z%3)=xm
zFB%BHjkW_FgJUXhE1A}`O)1e6Eq}mjm?1G}5k7QaYnM$DKJmz>Lhf#g13GsrfpHjD
zE4b6>tvK#2P(Ems{~R}=iJ!wnPFvz;Dv^^DNS2g;amGcknenAjfvn5{HY4CPE09Gm
z%`pGYdvE+1`mm=RW@v%%
z8uB()s3}*21?C`OjcJHsjCBu;=e!d`3$31rTJC?W3q;Q_A(Tne$dO9Sf;(`ZjgQwv
zhG=k6nV3M#Fnhzz7)(*``+uS)Dg&T?kPAggpVPcT8c_pW%n6hS=5O6jgvC%W%L`)4
z3em=fWSPY>rB15(xG3xVj!Rf+|KJKvw@IFoyy$aR^oHt5lmN_m=w2roOEHAu6!k+(
zqY;c2#mZXrIrr=C#c*zVnY^qetgZv&w80?W?Rz%f1IQ7L*QOU{Xw9Hcy7QQ
zo%EtjnHL&YcQ;~g4AK_-d+7q8xj}q;0hxzk$I8eV#Uipf%sM{K$~iv#7tqYN5`c<(
zx2{38*1cyTv43snSV&{;0LKU|yUkph<(>*y`OVqw?e_W{X=QV9c6o)>+-+~K&c1Ac
zvgMZ-VdXdH+u!bgM;h5|@9u8yu%gTBiyN%wud}=B%WG);>>p-?Yc?r+iZHeLo7Gi}
zP7KaJJIZ6E{r2sD1EtpQO!_6RFG_|~_T#yrDugL#uq?+fbAJTUuk(KG5GN(PcAqEX
df&|a5_6sL*a+J<{JV;3DKHqwx{68$~ndc2CRP_J=
diff --git a/search/search_index.json b/search/search_index.json
index ce9c632..ae46cb9 100644
--- a/search/search_index.json
+++ b/search/search_index.json
@@ -1 +1 @@
-{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"mt5cli \u00b6 Generic MT5 data and execution infrastructure for Python applications. Overview \u00b6 mt5cli provides a stable MT5Client Python API, standardized dataset schemas, storage helpers, and a CLI for exporting MetaTrader 5 data. It is built on top of pdmt5 , a pandas-based data handler for MetaTrader 5. Architecture \u00b6 pdmt5 \u2014 canonical MT5 client, DataFrame/trading primitives, and MT5 constant parsing ( TIMEFRAME_* , COPY_TICKS_* , order types). mt5cli \u2014 public MT5Client API, schema contracts, storage helpers, CLI commands, and SQLite history collection built on pdmt5. mt5api \u2014 sibling HTTP adapter for remote MT5 access; not a dependency of mt5cli. Features \u00b6 Multi-format export : CSV, JSON, Parquet, and SQLite3 output formats Auto-detection : Format detection from file extensions Comprehensive data access : Rates, ticks, account info, symbols, orders, positions, and trading history Flexible timeframes : Named timeframes (M1, H1, D1, etc.) and numeric values Connection management : Optional credentials, server, and timeout configuration SQLite rate loading : Load mt5cli-managed rate tables/views for offline workflows Installation \u00b6 pip install mt5cli Parquet export is not included by default. To enable it, install the parquet extra: pip install \"mt5cli[parquet]\" Python API for downstream packages \u00b6 Import MT5Client for generic MT5 data access, schema normalization, and optional order primitives. from datetime import UTC , datetime from pathlib import Path from mt5cli import ( MT5Client , build_config , collect_history , mt5_session , ) from mt5cli.history import load_rate_data , resolve_rate_view_name from mt5cli.schemas import DataKind , normalize_dataframe from mt5cli.sdk import minimum_margins , recent_ticks from mt5cli.utils import Dataset , export_dataframe # Persistent session for multiple calls with mt5_session ( build_config ( login = 12345 , server = \"Broker-Demo\" )) as client : rates = client . copy_rates_range ( \"EURUSD\" , timeframe = \"H1\" , date_from = \"2024-01-01\" , date_to = \"2024-02-01\" , ) positions = client . positions () check = client . order_check ({ \"action\" : 1 , \"symbol\" : \"EURUSD\" , \"volume\" : 0.1 }) # Normalize MT5 frames to the public schema contract before storage closed_rates = normalize_dataframe ( rates , DataKind . rates , symbol = \"EURUSD\" , timeframe = \"H1\" ) export_dataframe ( closed_rates , Path ( \"rates.csv\" ), \"csv\" ) # Offline rate loading from mt5cli-managed SQLite history view = resolve_rate_view_name ( Path ( \"history.db\" ), \"EURUSD\" , \"M1\" , require_existing = True ) offline_rates = load_rate_data ( Path ( \"history.db\" ), view , count = 1000 ) # One-off helpers still work without instantiating a client ticks = recent_ticks ( \"EURUSD\" , seconds = 300 ) margins = minimum_margins ( \"EURUSD\" ) collect_history ( Path ( \"history.db\" ), symbols = [ \"EURUSD\" , \"GBPUSD\" ], date_from = datetime ( 2024 , 1 , 1 , tzinfo = UTC ), date_to = datetime ( 2024 , 2 , 1 , tzinfo = UTC ), datasets = { Dataset . rates , Dataset . history_deals }, ) Schema contracts live in mt5cli.schemas ( DataKind , validate_schema , normalize_dataframe ). Export and storage helpers are in mt5cli.utils ( Dataset , export_dataframe ) and mt5cli.history . MT5Client.order_send() is a live execution primitive: it can place real trades on the connected account. mt5cli does not implement strategy logic, signal generation, backtesting, or optimization \u2014 downstream applications must gate live execution explicitly (the CLI requires --yes for order-send ). MT5Client.mt5_summary() returns structured nested Python values. Use MT5Client.mt5_summary_as_df() when you need a one-row DataFrame for export. Quick Start \u00b6 # Export account information to CSV mt5cli -o account.csv account-info # Export EURUSD M1 rates to Parquet mt5cli -o rates.parquet rates-from --symbol EURUSD --timeframe M1 \\ --date-from 2024 -01-01 --count 1000 # Export ticks to JSON mt5cli -o ticks.json ticks-from --symbol EURUSD \\ --date-from 2024 -01-01 --count 500 --flags ALL # Export symbols to SQLite3 with custom table name mt5cli -o data.db --table symbols symbols --group \"*USD*\" # Export with connection credentials mt5cli --login 12345 --password mypass --server MyBroker-Demo \\ -o positions.csv positions Commands \u00b6 Rates \u00b6 Command Description rates-from Export rates from a start date rates-from-pos Export rates from a start position latest-rates Export latest rates rates-range Export rates for a date range Ticks \u00b6 Command Description ticks-from Export ticks from a start date ticks-range Export ticks for a date range ticks-recent Export ticks from a trailing window Information \u00b6 Command Description account-info Export account information terminal-info Export terminal information version Export MetaTrader 5 version information last-error Export the last error information symbols Export symbol list symbol-info Export symbol details symbol-info-tick Export the last tick for a symbol minimum-margins Export minimum-volume margin summary market-book Export market depth (order book) Trading State \u00b6 Command Description orders Export active orders positions Export open positions history-orders Export historical orders history-deals Export historical deals recent-history-deals Export historical deals from a trailing window mt5-summary Export terminal/account status summary order-check Check funds sufficiency for a trade request (read-only, no --yes ) Execution (live / mutating) \u00b6 These commands send requests to the live trade server and can place or close real trades. Both require --yes for live execution. Command Description order-send Send a raw trade request directly to MT5 ( --yes required; expert path \u2014 no extra validation) close-positions Close open positions by --symbol or --ticket ( --yes required for live; --dry-run to preview) Use order-check (Trading State) to validate funds before running order-send --yes . close-positions is the safer high-level alternative that builds correct close requests automatically. order-send is the expert raw path \u2014 downstream applications should prefer dedicated closing helpers or their own risk controls. Bulk Collection \u00b6 Command Description collect-history Collect rates, history-orders, and history-deals (ticks opt-in via --dataset ticks ) for one or more symbols into a single SQLite database (optional cash-event/position views) mt5cli -o history.db collect-history \\ --symbol EURUSD --symbol GBPUSD \\ --date-from 2024 -01-01 --date-to 2024 -02-01 \\ --dataset rates --dataset history-deals \\ --timeframe M1 --flags ALL --if-exists append --with-views collect-history options: Option Default Description --symbol/-s required Symbol to collect (repeat for multiple). --date-from required Start date in ISO 8601. --date-to required End date in ISO 8601. --dataset rates, history-orders, history-deals Repeatable: rates , ticks , history-orders , history-deals . Ticks are opt-in: pass --dataset ticks to include them. --timeframe M1 Rates timeframe; recorded in a timeframe column on the rates table. --flags ALL Tick copy flags forwarded to copy_ticks_range . --if-exists fail append , replace , or fail when a target table already exists. --with-views off Add cash_events and positions_reconstructed views (requires the history-deals dataset). History orders and deals are fetched per symbol and concatenated, so the symbol filter is applied consistently across all datasets. The cash_events view is derived from symbol-filtered history_deals , so account-level cash events with empty or non-matching symbols may be excluded. The positions_reconstructed view excludes positions with no closing deal, uses volume-weighted open/close prices, and reports reversal deals ( DEAL_ENTRY_INOUT ) via volume_reversal / reversal_count . See the History schema diagram for a sample ER layout of the resulting database. Global Options \u00b6 Option Description -o, --output Output file path (required) -f, --format Output format (auto-detected from extension if omitted) --table Table name for SQLite3 output (default: \"data\") --login Trading account login --password Trading account password --server Trading server name --path Path to MetaTrader5 terminal EXE file --timeout Connection timeout in milliseconds --log-level Logging level (DEBUG, INFO, WARNING, ERROR) Requirements \u00b6 Python 3.11+ Windows OS (MetaTrader 5 requirement) MetaTrader 5 platform API Reference \u00b6 Browse the API documentation for detailed module information: CLI Module - CLI application with data export and execution commands SDK Module - Programmatic read-only data collection API Utils Module - Constants, parameter types, parsers, and export utilities Development \u00b6 This project follows strict code quality standards: Type hints required (strict mode) Comprehensive linting with Ruff Test coverage tracking Google-style docstrings License \u00b6 MIT License - see LICENSE file for details.","title":"Home"},{"location":"#mt5cli","text":"Generic MT5 data and execution infrastructure for Python applications.","title":"mt5cli"},{"location":"#overview","text":"mt5cli provides a stable MT5Client Python API, standardized dataset schemas, storage helpers, and a CLI for exporting MetaTrader 5 data. It is built on top of pdmt5 , a pandas-based data handler for MetaTrader 5.","title":"Overview"},{"location":"#architecture","text":"pdmt5 \u2014 canonical MT5 client, DataFrame/trading primitives, and MT5 constant parsing ( TIMEFRAME_* , COPY_TICKS_* , order types). mt5cli \u2014 public MT5Client API, schema contracts, storage helpers, CLI commands, and SQLite history collection built on pdmt5. mt5api \u2014 sibling HTTP adapter for remote MT5 access; not a dependency of mt5cli.","title":"Architecture"},{"location":"#features","text":"Multi-format export : CSV, JSON, Parquet, and SQLite3 output formats Auto-detection : Format detection from file extensions Comprehensive data access : Rates, ticks, account info, symbols, orders, positions, and trading history Flexible timeframes : Named timeframes (M1, H1, D1, etc.) and numeric values Connection management : Optional credentials, server, and timeout configuration SQLite rate loading : Load mt5cli-managed rate tables/views for offline workflows","title":"Features"},{"location":"#installation","text":"pip install mt5cli Parquet export is not included by default. To enable it, install the parquet extra: pip install \"mt5cli[parquet]\"","title":"Installation"},{"location":"#python-api-for-downstream-packages","text":"Import MT5Client for generic MT5 data access, schema normalization, and optional order primitives. from datetime import UTC , datetime from pathlib import Path from mt5cli import ( MT5Client , build_config , collect_history , mt5_session , ) from mt5cli.history import load_rate_data , resolve_rate_view_name from mt5cli.schemas import DataKind , normalize_dataframe from mt5cli.sdk import minimum_margins , recent_ticks from mt5cli.utils import Dataset , export_dataframe # Persistent session for multiple calls with mt5_session ( build_config ( login = 12345 , server = \"Broker-Demo\" )) as client : rates = client . copy_rates_range ( \"EURUSD\" , timeframe = \"H1\" , date_from = \"2024-01-01\" , date_to = \"2024-02-01\" , ) positions = client . positions () check = client . order_check ({ \"action\" : 1 , \"symbol\" : \"EURUSD\" , \"volume\" : 0.1 }) # Normalize MT5 frames to the public schema contract before storage closed_rates = normalize_dataframe ( rates , DataKind . rates , symbol = \"EURUSD\" , timeframe = \"H1\" ) export_dataframe ( closed_rates , Path ( \"rates.csv\" ), \"csv\" ) # Offline rate loading from mt5cli-managed SQLite history view = resolve_rate_view_name ( Path ( \"history.db\" ), \"EURUSD\" , \"M1\" , require_existing = True ) offline_rates = load_rate_data ( Path ( \"history.db\" ), view , count = 1000 ) # One-off helpers still work without instantiating a client ticks = recent_ticks ( \"EURUSD\" , seconds = 300 ) margins = minimum_margins ( \"EURUSD\" ) collect_history ( Path ( \"history.db\" ), symbols = [ \"EURUSD\" , \"GBPUSD\" ], date_from = datetime ( 2024 , 1 , 1 , tzinfo = UTC ), date_to = datetime ( 2024 , 2 , 1 , tzinfo = UTC ), datasets = { Dataset . rates , Dataset . history_deals }, ) Schema contracts live in mt5cli.schemas ( DataKind , validate_schema , normalize_dataframe ). Export and storage helpers are in mt5cli.utils ( Dataset , export_dataframe ) and mt5cli.history . MT5Client.order_send() is a live execution primitive: it can place real trades on the connected account. mt5cli does not implement strategy logic, signal generation, backtesting, or optimization \u2014 downstream applications must gate live execution explicitly (the CLI requires --yes for order-send ). MT5Client.mt5_summary() returns structured nested Python values. Use MT5Client.mt5_summary_as_df() when you need a one-row DataFrame for export.","title":"Python API for downstream packages"},{"location":"#quick-start","text":"# Export account information to CSV mt5cli -o account.csv account-info # Export EURUSD M1 rates to Parquet mt5cli -o rates.parquet rates-from --symbol EURUSD --timeframe M1 \\ --date-from 2024 -01-01 --count 1000 # Export ticks to JSON mt5cli -o ticks.json ticks-from --symbol EURUSD \\ --date-from 2024 -01-01 --count 500 --flags ALL # Export symbols to SQLite3 with custom table name mt5cli -o data.db --table symbols symbols --group \"*USD*\" # Export with connection credentials mt5cli --login 12345 --password mypass --server MyBroker-Demo \\ -o positions.csv positions","title":"Quick Start"},{"location":"#commands","text":"","title":"Commands"},{"location":"#rates","text":"Command Description rates-from Export rates from a start date rates-from-pos Export rates from a start position latest-rates Export latest rates rates-range Export rates for a date range","title":"Rates"},{"location":"#ticks","text":"Command Description ticks-from Export ticks from a start date ticks-range Export ticks for a date range ticks-recent Export ticks from a trailing window","title":"Ticks"},{"location":"#information","text":"Command Description account-info Export account information terminal-info Export terminal information version Export MetaTrader 5 version information last-error Export the last error information symbols Export symbol list symbol-info Export symbol details symbol-info-tick Export the last tick for a symbol minimum-margins Export minimum-volume margin summary market-book Export market depth (order book)","title":"Information"},{"location":"#trading-state","text":"Command Description orders Export active orders positions Export open positions history-orders Export historical orders history-deals Export historical deals recent-history-deals Export historical deals from a trailing window mt5-summary Export terminal/account status summary order-check Check funds sufficiency for a trade request (read-only, no --yes )","title":"Trading State"},{"location":"#execution-live-mutating","text":"These commands send requests to the live trade server and can place or close real trades. Both require --yes for live execution. Command Description order-send Send a raw trade request directly to MT5 ( --yes required; expert path \u2014 no extra validation) close-positions Close open positions by --symbol or --ticket ( --yes required for live; --dry-run to preview) Use order-check (Trading State) to validate funds before running order-send --yes . close-positions is the safer high-level alternative that builds correct close requests automatically. order-send is the expert raw path \u2014 downstream applications should prefer dedicated closing helpers or their own risk controls.","title":"Execution (live / mutating)"},{"location":"#bulk-collection","text":"Command Description collect-history Collect rates, history-orders, and history-deals (ticks opt-in via --dataset ticks ) for one or more symbols into a single SQLite database (optional cash-event/position views) mt5cli -o history.db collect-history \\ --symbol EURUSD --symbol GBPUSD \\ --date-from 2024 -01-01 --date-to 2024 -02-01 \\ --dataset rates --dataset history-deals \\ --timeframe M1 --flags ALL --if-exists append --with-views collect-history options: Option Default Description --symbol/-s required Symbol to collect (repeat for multiple). --date-from required Start date in ISO 8601. --date-to required End date in ISO 8601. --dataset rates, history-orders, history-deals Repeatable: rates , ticks , history-orders , history-deals . Ticks are opt-in: pass --dataset ticks to include them. --timeframe M1 Rates timeframe; recorded in a timeframe column on the rates table. --flags ALL Tick copy flags forwarded to copy_ticks_range . --if-exists fail append , replace , or fail when a target table already exists. --with-views off Add cash_events and positions_reconstructed views (requires the history-deals dataset). History orders and deals are fetched per symbol and concatenated, so the symbol filter is applied consistently across all datasets. The cash_events view is derived from symbol-filtered history_deals , so account-level cash events with empty or non-matching symbols may be excluded. The positions_reconstructed view excludes positions with no closing deal, uses volume-weighted open/close prices, and reports reversal deals ( DEAL_ENTRY_INOUT ) via volume_reversal / reversal_count . See the History schema diagram for a sample ER layout of the resulting database.","title":"Bulk Collection"},{"location":"#global-options","text":"Option Description -o, --output Output file path (required) -f, --format Output format (auto-detected from extension if omitted) --table Table name for SQLite3 output (default: \"data\") --login Trading account login --password Trading account password --server Trading server name --path Path to MetaTrader5 terminal EXE file --timeout Connection timeout in milliseconds --log-level Logging level (DEBUG, INFO, WARNING, ERROR)","title":"Global Options"},{"location":"#requirements","text":"Python 3.11+ Windows OS (MetaTrader 5 requirement) MetaTrader 5 platform","title":"Requirements"},{"location":"#api-reference","text":"Browse the API documentation for detailed module information: CLI Module - CLI application with data export and execution commands SDK Module - Programmatic read-only data collection API Utils Module - Constants, parameter types, parsers, and export utilities","title":"API Reference"},{"location":"#development","text":"This project follows strict code quality standards: Type hints required (strict mode) Comprehensive linting with Ruff Test coverage tracking Google-style docstrings","title":"Development"},{"location":"#license","text":"MIT License - see LICENSE file for details.","title":"License"},{"location":"api/","text":"API Reference \u00b6 This section documents the mt5cli public Python API and CLI modules. Start with the Public API Contract for the stable downstream SDK surface, CLI boundary, internal modules, and out-of-scope strategy responsibilities. Public API layers \u00b6 Module Purpose Public API Contract Stable downstream SDK exports, CLI boundary, and out-of-scope items Client MT5Client session abstraction for data access and order primitives Schemas Canonical DataFrame contracts and normalization helpers Converters Symbol, timeframe, timezone, and date-range utilities Exceptions Stable mt5cli exception types and MT5 error normalization SDK Module-level fetch helpers, multi-account collectors, incremental history Trading Trading-capable sessions and operational helpers History Collection (SQLite) SQLite schema, incremental writes, dedup, and rate views CLI Typer commands that delegate to the Python API Utils Parsing helpers and Click parameter types Architecture overview \u00b6 flowchart TD App[\"Downstream application\"] --> Client[\"MT5Client\"] CLI[\"mt5cli CLI\"] --> Client Client --> SDK[\"sdk / pdmt5\"] Client --> Schemas[\"schemas\"] History[\"history SQLite\"] --> Utils[\"utils export\"] SDK --> PDMT5[\"pdmt5.Mt5DataClient\"] Downstream packages should depend on the package root exports documented in the Public API Contract ( MT5Client , collect_history , load_rate_series_from_sqlite , etc.) rather than private modules. Lower-level helpers are accessible directly from their owning modules. MT5Client.order_send() is a live execution primitive that can place real trades. mt5cli exposes minimal execution helpers only; strategy logic, signals, backtests, and optimization remain out of scope and must be implemented downstream with explicit execution gating. Quick start \u00b6 from mt5cli import MT5Client , build_config , mt5_session with mt5_session ( build_config ( login = 12345 )) as client : rates = client . copy_rates_range ( \"EURUSD\" , \"H1\" , \"2024-01-01\" , \"2024-02-01\" ) positions = client . positions () mt5cli -o account.csv account-info mt5cli -o rates.parquet rates-range --symbol EURUSD --timeframe H1 \\ --date-from 2024 -01-01 --date-to 2024 -02-01 See individual module pages for detailed usage examples.","title":"Overview"},{"location":"api/#api-reference","text":"This section documents the mt5cli public Python API and CLI modules. Start with the Public API Contract for the stable downstream SDK surface, CLI boundary, internal modules, and out-of-scope strategy responsibilities.","title":"API Reference"},{"location":"api/#public-api-layers","text":"Module Purpose Public API Contract Stable downstream SDK exports, CLI boundary, and out-of-scope items Client MT5Client session abstraction for data access and order primitives Schemas Canonical DataFrame contracts and normalization helpers Converters Symbol, timeframe, timezone, and date-range utilities Exceptions Stable mt5cli exception types and MT5 error normalization SDK Module-level fetch helpers, multi-account collectors, incremental history Trading Trading-capable sessions and operational helpers History Collection (SQLite) SQLite schema, incremental writes, dedup, and rate views CLI Typer commands that delegate to the Python API Utils Parsing helpers and Click parameter types","title":"Public API layers"},{"location":"api/#architecture-overview","text":"flowchart TD App[\"Downstream application\"] --> Client[\"MT5Client\"] CLI[\"mt5cli CLI\"] --> Client Client --> SDK[\"sdk / pdmt5\"] Client --> Schemas[\"schemas\"] History[\"history SQLite\"] --> Utils[\"utils export\"] SDK --> PDMT5[\"pdmt5.Mt5DataClient\"] Downstream packages should depend on the package root exports documented in the Public API Contract ( MT5Client , collect_history , load_rate_series_from_sqlite , etc.) rather than private modules. Lower-level helpers are accessible directly from their owning modules. MT5Client.order_send() is a live execution primitive that can place real trades. mt5cli exposes minimal execution helpers only; strategy logic, signals, backtests, and optimization remain out of scope and must be implemented downstream with explicit execution gating.","title":"Architecture overview"},{"location":"api/#quick-start","text":"from mt5cli import MT5Client , build_config , mt5_session with mt5_session ( build_config ( login = 12345 )) as client : rates = client . copy_rates_range ( \"EURUSD\" , \"H1\" , \"2024-01-01\" , \"2024-02-01\" ) positions = client . positions () mt5cli -o account.csv account-info mt5cli -o rates.parquet rates-range --symbol EURUSD --timeframe H1 \\ --date-from 2024 -01-01 --date-to 2024 -02-01 See individual module pages for detailed usage examples.","title":"Quick start"},{"location":"api/cli/","text":"CLI Module \u00b6 mt5cli.cli \u00b6 Command-line interface for MetaTrader 5 data and execution utilities. app module-attribute \u00b6 app = Typer ( name = \"mt5cli\" , help = \"MT5 data and execution utilities \u2014 read market data, inspect account state, and send trade requests. Data commands write to CSV, JSON, Parquet, or SQLite3. Execution commands (order-send, close-positions) require --yes for live mutations.\" , ) logger module-attribute \u00b6 logger = getLogger ( __name__ ) account_info \u00b6 account_info ( ctx : Context ) -> None Export account information. Source code in mt5cli/cli.py 385 386 387 388 @app . command ( rich_help_panel = \"Data / Export\" ) def account_info ( ctx : typer . Context ) -> None : \"\"\"Export account information.\"\"\" _export_command ( ctx , lambda client : client . account_info ()) close_positions \u00b6 close_positions ( ctx : Context , symbol : Annotated [ list [ str ] | None , Option ( \"--symbol\" , \"-s\" , help = \"Symbol to close (repeat for multiple symbols).\" , ), ] = None , ticket : Annotated [ list [ int ] | None , Option ( \"--ticket\" , \"-t\" , help = \"Position ticket to close (repeat for multiple tickets).\" , ), ] = None , dry_run : Annotated [ bool , Option ( \"--dry-run\" , help = \"Preview close orders without executing them.\" , ), ] = False , yes : Annotated [ bool , Option ( \"--yes\" , help = \"Confirm live position closing.\" ), ] = False , ) -> None Close open positions by symbol or ticket. Delegates to :func: mt5cli.trading.close_open_positions . At least one --symbol or --ticket must be provided to avoid accidentally closing all positions. Use --dry-run to preview without executing; --yes is required for live execution. order-send is the expert raw-request path. close-positions is the safer high-level helper that builds correct close requests automatically. Raises: Type Description BadParameter If neither --symbol nor --ticket is given, or if --yes is missing for a live (non-dry-run) run. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Execution\" ) def close_positions ( ctx : typer . Context , symbol : Annotated [ list [ str ] | None , typer . Option ( \"--symbol\" , \"-s\" , help = \"Symbol to close (repeat for multiple symbols).\" , ), ] = None , ticket : Annotated [ list [ int ] | None , typer . Option ( \"--ticket\" , \"-t\" , help = \"Position ticket to close (repeat for multiple tickets).\" , ), ] = None , dry_run : Annotated [ bool , typer . Option ( \"--dry-run\" , help = \"Preview close orders without executing them.\" ), ] = False , yes : Annotated [ bool , typer . Option ( \"--yes\" , help = \"Confirm live position closing.\" ), ] = False , ) -> None : \"\"\"Close open positions by symbol or ticket. Delegates to :func:`mt5cli.trading.close_open_positions`. At least one ``--symbol`` or ``--ticket`` must be provided to avoid accidentally closing all positions. Use ``--dry-run`` to preview without executing; ``--yes`` is required for live execution. ``order-send`` is the expert raw-request path. ``close-positions`` is the safer high-level helper that builds correct close requests automatically. Raises: typer.BadParameter: If neither ``--symbol`` nor ``--ticket`` is given, or if ``--yes`` is missing for a live (non-dry-run) run. \"\"\" if not symbol and not ticket : msg = \"Provide at least one --symbol or --ticket to close positions.\" raise typer . BadParameter ( msg ) if not dry_run and not yes : msg = \"Pass --yes to close live positions.\" raise typer . BadParameter ( msg , param_hint = \"--yes\" ) export_ctx = _get_export_context ( ctx ) client = create_trading_client ( config = export_ctx . config ) try : results = close_open_positions ( client , symbols = list ( symbol ) if symbol else None , tickets = list ( ticket ) if ticket else None , dry_run = dry_run , ) finally : client . shutdown () df = _execution_results_to_df ( results ) _execute_export ( ctx , lambda : df ) collect_history \u00b6 collect_history ( ctx : Context , symbol : Annotated [ list [ str ], Option ( \"--symbol\" , \"-s\" , help = \"Symbol to collect (repeat for multiple symbols).\" , ), ], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], dataset : Annotated [ list [ Dataset ] | None , Option ( \"--dataset\" , help = \"Dataset to include (repeat for multiple). Defaults to rates, history-orders, history-deals. Ticks are opt-in: pass --dataset ticks to include them.\" , ), ] = None , timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Rates timeframe (e.g., M1, H1, D1).\" , ), ] = 1 , flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick copy flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , if_exists : Annotated [ IfExists , Option ( \"--if-exists\" , help = \"Behavior when a target table already exists.\" , ), ] = FAIL , with_views : Annotated [ bool , Option ( \"--with-views\" , help = \"Add cash_events and positions_reconstructed SQLite views derived from history_deals.\" , ), ] = False , ) -> None Collect historical datasets into a single SQLite database. Tables written depend on --dataset : rates , history_orders , history_deals by default. ticks are opt-in: pass --dataset ticks to include them (tick data grows the database quickly). History datasets are fetched per symbol and concatenated. Rates rows carry the requested timeframe so appended runs at different timeframes remain distinguishable. With --with-views (requires the history-deals dataset), optional views cash_events and positions_reconstructed are derived from history_deals when the required columns are present. Raises: Type Description BadParameter If the output format is not SQLite3. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Collection\" ) def collect_history ( ctx : typer . Context , symbol : Annotated [ list [ str ], typer . Option ( \"--symbol\" , \"-s\" , help = \"Symbol to collect (repeat for multiple symbols).\" , ), ], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], dataset : Annotated [ list [ Dataset ] | None , typer . Option ( \"--dataset\" , help = ( \"Dataset to include (repeat for multiple).\" \" Defaults to rates, history-orders, history-deals.\" \" Ticks are opt-in: pass --dataset ticks to include them.\" ), ), ] = None , timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Rates timeframe (e.g., M1, H1, D1).\" , ), ] = 1 , flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick copy flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , # pyright: ignore[reportArgumentType] if_exists : Annotated [ IfExists , typer . Option ( \"--if-exists\" , help = \"Behavior when a target table already exists.\" , ), ] = IfExists . FAIL , with_views : Annotated [ bool , typer . Option ( \"--with-views\" , help = ( \"Add cash_events and positions_reconstructed SQLite views\" \" derived from history_deals.\" ), ), ] = False , ) -> None : \"\"\"Collect historical datasets into a single SQLite database. Tables written depend on ``--dataset``: ``rates``, ``history_orders``, ``history_deals`` by default. ``ticks`` are opt-in: pass ``--dataset ticks`` to include them (tick data grows the database quickly). History datasets are fetched per symbol and concatenated. Rates rows carry the requested ``timeframe`` so appended runs at different timeframes remain distinguishable. With ``--with-views`` (requires the ``history-deals`` dataset), optional views ``cash_events`` and ``positions_reconstructed`` are derived from ``history_deals`` when the required columns are present. Raises: typer.BadParameter: If the output format is not SQLite3. \"\"\" export_ctx = _get_export_context ( ctx ) if export_ctx . output_format != \"sqlite3\" : msg = ( \"collect-history requires SQLite3 output.\" \" Use a .db/.sqlite/.sqlite3 extension or --format sqlite3.\" ) raise typer . BadParameter ( msg ) datasets = set ( dataset ) if dataset is not None else None sdk . collect_history ( output = export_ctx . output , symbols = symbol , date_from = date_from , date_to = date_to , datasets = datasets , timeframe = timeframe , flags = flags , if_exists = if_exists , with_views = with_views , config = export_ctx . config , ) grafana_schema \u00b6 grafana_schema ( ctx : Context , publish_copy : Annotated [ Path | None , Option ( \"--publish-copy\" , help = \"Publish a Grafana-ready SQLite copy to this path after schema creation.\" , ), ] = None , ) -> None Create or refresh Grafana-ready views and indexes in a SQLite database. Idempotent \u2014 safe to run repeatedly on the same database. Requires SQLite output. Does not connect to MetaTrader 5. Raises: Type Description BadParameter If the output format is not SQLite3. Source code in mt5cli/cli.py 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 @app . command ( rich_help_panel = \"Collection\" ) def grafana_schema ( ctx : typer . Context , publish_copy : Annotated [ Path | None , typer . Option ( \"--publish-copy\" , help = ( \"Publish a Grafana-ready SQLite copy to this path\" \" after schema creation.\" ), ), ] = None , ) -> None : \"\"\"Create or refresh Grafana-ready views and indexes in a SQLite database. Idempotent \u2014 safe to run repeatedly on the same database. Requires SQLite output. Does not connect to MetaTrader 5. Raises: typer.BadParameter: If the output format is not SQLite3. \"\"\" import sqlite3 as _sqlite3 # noqa: PLC0415 from .grafana import ( # noqa: PLC0415 create_snapshot_tables , ensure_grafana_schema , publish_grafana_copy , ) export_ctx = _get_export_context ( ctx ) if export_ctx . output_format != \"sqlite3\" : msg = ( \"grafana-schema requires SQLite3 output.\" \" Use a .db/.sqlite/.sqlite3 extension or --format sqlite3.\" ) raise typer . BadParameter ( msg ) with _sqlite3 . connect ( export_ctx . output ) as conn : conn . execute ( \"PRAGMA journal_mode=WAL\" ) conn . execute ( \"PRAGMA synchronous=NORMAL\" ) create_snapshot_tables ( conn ) ensure_grafana_schema ( conn ) logger . info ( \"Grafana schema applied to %s \" , export_ctx . output ) if publish_copy is not None : publish_grafana_copy ( export_ctx . output , publish_copy ) logger . info ( \"Grafana copy published to %s \" , publish_copy ) history_deals \u00b6 history_deals ( ctx : Context , date_from : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Order ticket.\" ) ] = None , position : Annotated [ int | None , Option ( help = \"Position ticket.\" ) ] = None , ) -> None Export historical deals. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def history_deals ( ctx : typer . Context , date_from : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Order ticket.\" )] = None , position : Annotated [ int | None , typer . Option ( help = \"Position ticket.\" )] = None , ) -> None : \"\"\"Export historical deals.\"\"\" _export_command ( ctx , lambda client : client . history_deals ( date_from = date_from , date_to = date_to , group = group , symbol = symbol , ticket = ticket , position = position , ), ) history_orders \u00b6 history_orders ( ctx : Context , date_from : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Order ticket.\" ) ] = None , position : Annotated [ int | None , Option ( help = \"Position ticket.\" ) ] = None , ) -> None Export historical orders. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def history_orders ( ctx : typer . Context , date_from : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Order ticket.\" )] = None , position : Annotated [ int | None , typer . Option ( help = \"Position ticket.\" )] = None , ) -> None : \"\"\"Export historical orders.\"\"\" _export_command ( ctx , lambda client : client . history_orders ( date_from = date_from , date_to = date_to , group = group , symbol = symbol , ticket = ticket , position = position , ), ) last_error \u00b6 last_error ( ctx : Context ) -> None Export the last error information. Source code in mt5cli/cli.py 550 551 552 553 @app . command ( rich_help_panel = \"Data / Export\" ) def last_error ( ctx : typer . Context ) -> None : \"\"\"Export the last error information.\"\"\" _export_command ( ctx , lambda client : client . last_error ()) latest_rates \u00b6 latest_rates ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" ), ], count : Annotated [ int , Option ( help = \"Number of records.\" ) ], start_pos : Annotated [ int , Option ( help = \"Start position (0 = current bar).\" ), ] = 0 , ) -> None Export latest rates from a start position. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def latest_rates ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" , ), ], count : Annotated [ int , typer . Option ( help = \"Number of records.\" )], start_pos : Annotated [ int , typer . Option ( help = \"Start position (0 = current bar).\" ), ] = 0 , ) -> None : \"\"\"Export latest rates from a start position.\"\"\" _export_command ( ctx , lambda client : client . latest_rates ( symbol , timeframe , count , start_pos = start_pos , ), ) main \u00b6 main () -> None Run the mt5cli CLI. Source code in mt5cli/cli.py 930 931 932 def main () -> None : \"\"\"Run the mt5cli CLI.\"\"\" app () market_book \u00b6 market_book ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export market depth (order book) for a symbol. Source code in mt5cli/cli.py 565 566 567 568 569 570 571 @app . command ( rich_help_panel = \"Data / Export\" ) def market_book ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export market depth (order book) for a symbol.\"\"\" _export_command ( ctx , lambda client : client . market_book ( symbol )) minimum_margins \u00b6 minimum_margins ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export minimum-volume buy and sell margin requirements. Source code in mt5cli/cli.py 418 419 420 421 422 423 424 @app . command ( rich_help_panel = \"Data / Export\" ) def minimum_margins ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export minimum-volume buy and sell margin requirements.\"\"\" _export_command ( ctx , lambda client : client . minimum_margins ( symbol )) mt5_summary \u00b6 mt5_summary ( ctx : Context ) -> None Export a compact terminal/account status summary. Source code in mt5cli/cli.py 538 539 540 541 @app . command ( rich_help_panel = \"Data / Export\" ) def mt5_summary ( ctx : typer . Context ) -> None : \"\"\"Export a compact terminal/account status summary.\"\"\" _export_command ( ctx , lambda client : client . mt5_summary_as_df ()) order_check \u00b6 order_check ( ctx : Context , request : Annotated [ dict [ str , Any ], Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP , ), ], ) -> None Check funds sufficiency for a trading operation. Source code in mt5cli/cli.py 574 575 576 577 578 579 580 581 582 583 @app . command ( rich_help_panel = \"Data / Export\" ) def order_check ( ctx : typer . Context , request : Annotated [ dict [ str , Any ], typer . Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP ), ], ) -> None : \"\"\"Check funds sufficiency for a trading operation.\"\"\" _export_command ( ctx , lambda client : client . order_check ( request )) order_send \u00b6 order_send ( ctx : Context , request : Annotated [ dict [ str , Any ], Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP , ), ], yes : Annotated [ bool , Option ( \"--yes\" , help = \"Confirm the live trade request.\" ), ] = False , ) -> None Send a raw trade request to the trade server (expert path, live execution). Passes the request JSON directly to MT5 order_send . This is the low-level expert path \u2014 it places real trades on the connected account with no additional validation beyond what MT5 itself performs. Use order-check first to validate funds sufficiency. Prefer close-positions for closing open positions. --yes is required. Raises: Type Description BadParameter If --yes is not provided. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Execution\" ) def order_send ( ctx : typer . Context , request : Annotated [ dict [ str , Any ], typer . Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP ), ], yes : Annotated [ bool , typer . Option ( \"--yes\" , help = \"Confirm the live trade request.\" ), ] = False , ) -> None : \"\"\"Send a raw trade request to the trade server (expert path, live execution). Passes the request JSON directly to MT5 ``order_send``. This is the low-level expert path \u2014 it places real trades on the connected account with no additional validation beyond what MT5 itself performs. Use ``order-check`` first to validate funds sufficiency. Prefer ``close-positions`` for closing open positions. ``--yes`` is required. Raises: typer.BadParameter: If --yes is not provided. \"\"\" if not yes : msg = \"Pass --yes to send a live trade request.\" raise typer . BadParameter ( msg , param_hint = \"--yes\" ) _export_command ( ctx , lambda client : client . order_send ( request )) orders \u00b6 orders ( ctx : Context , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Ticket filter.\" ) ] = None , ) -> None Export active orders. Source code in mt5cli/cli.py 427 428 429 430 431 432 433 434 435 436 437 438 @app . command ( rich_help_panel = \"Data / Export\" ) def orders ( ctx : typer . Context , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Ticket filter.\" )] = None , ) -> None : \"\"\"Export active orders.\"\"\" _export_command ( ctx , lambda client : client . orders ( symbol = symbol , group = group , ticket = ticket ), ) positions \u00b6 positions ( ctx : Context , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Ticket filter.\" ) ] = None , ) -> None Export open positions. Source code in mt5cli/cli.py 441 442 443 444 445 446 447 448 449 450 451 452 @app . command ( rich_help_panel = \"Data / Export\" ) def positions ( ctx : typer . Context , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Ticket filter.\" )] = None , ) -> None : \"\"\"Export open positions.\"\"\" _export_command ( ctx , lambda client : client . positions ( symbol = symbol , group = group , ticket = ticket ), ) rates_from \u00b6 rates_from ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe (e.g., M1, H1, D1, or integer).\" , ), ], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date in ISO 8601 format.\" , ), ], count : Annotated [ int , Option ( help = \"Number of records.\" ) ], ) -> None Export rates from a start date. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def rates_from ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe (e.g., M1, H1, D1, or integer).\" , ), ], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date in ISO 8601 format.\" , ), ], count : Annotated [ int , typer . Option ( help = \"Number of records.\" )], ) -> None : \"\"\"Export rates from a start date.\"\"\" _export_command ( ctx , lambda client : client . copy_rates_from ( symbol , timeframe , date_from , count ), ) rates_from_pos \u00b6 rates_from_pos ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" ), ], start_pos : Annotated [ int , Option ( help = \"Start position (0 = current bar).\" ), ], count : Annotated [ int , Option ( help = \"Number of records.\" ) ], ) -> None Export rates from a start position. Source code in mt5cli/cli.py 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 @app . command ( rich_help_panel = \"Data / Export\" ) def rates_from_pos ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" , ), ], start_pos : Annotated [ int , typer . Option ( help = \"Start position (0 = current bar).\" )], count : Annotated [ int , typer . Option ( help = \"Number of records.\" )], ) -> None : \"\"\"Export rates from a start position.\"\"\" _export_command ( ctx , lambda client : client . copy_rates_from_pos ( symbol , timeframe , start_pos , count , ), ) rates_range \u00b6 rates_range ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" ), ], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], ) -> None Export rates for a date range. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def rates_range ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" , ), ], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], ) -> None : \"\"\"Export rates for a date range.\"\"\" _export_command ( ctx , lambda client : client . copy_rates_range ( symbol , timeframe , date_from , date_to ), ) recent_history_deals \u00b6 recent_history_deals ( ctx : Context , hours : Annotated [ float , Option ( help = \"Lookback window in hours.\" ) ], date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" , ), ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , ) -> None Export historical deals from a recent trailing window. Source code in mt5cli/cli.py 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 @app . command ( rich_help_panel = \"Data / Export\" ) def recent_history_deals ( ctx : typer . Context , hours : Annotated [ float , typer . Option ( help = \"Lookback window in hours.\" )], date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" ), ] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , ) -> None : \"\"\"Export historical deals from a recent trailing window.\"\"\" _export_command ( ctx , lambda client : client . recent_history_deals ( hours , date_to = date_to , group = group , symbol = symbol , ), ) snapshot \u00b6 snapshot ( ctx : Context , symbol : Annotated [ list [ str ] | None , Option ( \"--symbol\" , \"-s\" , help = \"Symbol filter for positions/orders (repeat for multiple).\" , ), ] = None , with_account : Annotated [ bool , Option ( \"--with-account/--no-account\" , help = \"Snapshot account info.\" , ), ] = True , with_positions : Annotated [ bool , Option ( \"--with-positions/--no-positions\" , help = \"Snapshot open positions.\" , ), ] = True , with_orders : Annotated [ bool , Option ( \"--with-orders/--no-orders\" , help = \"Snapshot active orders.\" , ), ] = True , with_terminal : Annotated [ bool , Option ( \"--with-terminal/--no-terminal\" , help = \"Snapshot terminal info.\" , ), ] = True , with_grafana_schema : Annotated [ bool , Option ( \"--with-grafana-schema/--no-grafana-schema\" , help = \"Ensure Grafana views and indexes exist.\" , ), ] = False , publish_copy : Annotated [ Path | None , Option ( \"--publish-copy\" , help = \"Publish a Grafana-ready SQLite copy to this path after snapshot.\" , ), ] = None , ) -> None Snapshot current account, position, order, and terminal state into SQLite. Appends a timestamped snapshot row for each data type. Never places orders or modifies trading state. Raises: Type Description BadParameter If the output format is not SQLite3. Source code in mt5cli/cli.py 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 @app . command ( rich_help_panel = \"Collection\" ) def snapshot ( ctx : typer . Context , symbol : Annotated [ list [ str ] | None , typer . Option ( \"--symbol\" , \"-s\" , help = \"Symbol filter for positions/orders (repeat for multiple).\" , ), ] = None , with_account : Annotated [ bool , typer . Option ( \"--with-account/--no-account\" , help = \"Snapshot account info.\" ), ] = True , with_positions : Annotated [ bool , typer . Option ( \"--with-positions/--no-positions\" , help = \"Snapshot open positions.\" ), ] = True , with_orders : Annotated [ bool , typer . Option ( \"--with-orders/--no-orders\" , help = \"Snapshot active orders.\" ), ] = True , with_terminal : Annotated [ bool , typer . Option ( \"--with-terminal/--no-terminal\" , help = \"Snapshot terminal info.\" ), ] = True , with_grafana_schema : Annotated [ bool , typer . Option ( \"--with-grafana-schema/--no-grafana-schema\" , help = \"Ensure Grafana views and indexes exist.\" , ), ] = False , publish_copy : Annotated [ Path | None , typer . Option ( \"--publish-copy\" , help = ( \"Publish a Grafana-ready SQLite copy to this path after snapshot.\" ), ), ] = None , ) -> None : \"\"\"Snapshot current account, position, order, and terminal state into SQLite. Appends a timestamped snapshot row for each data type. Never places orders or modifies trading state. Raises: typer.BadParameter: If the output format is not SQLite3. \"\"\" export_ctx = _get_export_context ( ctx ) if export_ctx . output_format != \"sqlite3\" : msg = ( \"snapshot requires SQLite3 output.\" \" Use a .db/.sqlite/.sqlite3 extension or --format sqlite3.\" ) raise typer . BadParameter ( msg ) sdk . update_observability_with_config ( output = export_ctx . output , config = export_ctx . config , symbols = list ( symbol ) if symbol else None , include_account = with_account , include_positions = with_positions , include_orders = with_orders , include_terminal = with_terminal , with_grafana_schema = with_grafana_schema , ) logger . info ( \"Snapshot written to %s \" , export_ctx . output ) if publish_copy is not None : from .grafana import publish_grafana_copy # noqa: PLC0415 publish_grafana_copy ( export_ctx . output , publish_copy ) logger . info ( \"Grafana copy published to %s \" , publish_copy ) symbol_info \u00b6 symbol_info ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export symbol details. Source code in mt5cli/cli.py 409 410 411 412 413 414 415 @app . command ( rich_help_panel = \"Data / Export\" ) def symbol_info ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export symbol details.\"\"\" _export_command ( ctx , lambda client : client . symbol_info ( symbol )) symbol_info_tick \u00b6 symbol_info_tick ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export the last tick for a symbol. Source code in mt5cli/cli.py 556 557 558 559 560 561 562 @app . command ( rich_help_panel = \"Data / Export\" ) def symbol_info_tick ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export the last tick for a symbol.\"\"\" _export_command ( ctx , lambda client : client . symbol_info_tick ( symbol )) symbols \u00b6 symbols ( ctx : Context , group : Annotated [ str | None , Option ( help = \"Symbol group filter (e.g., *USD*).\" ), ] = None , ) -> None Export symbol list. Source code in mt5cli/cli.py 397 398 399 400 401 402 403 404 405 406 @app . command ( rich_help_panel = \"Data / Export\" ) def symbols ( ctx : typer . Context , group : Annotated [ str | None , typer . Option ( help = \"Symbol group filter (e.g., *USD*).\" ), ] = None , ) -> None : \"\"\"Export symbol list.\"\"\" _export_command ( ctx , lambda client : client . symbols ( group = group )) terminal_info \u00b6 terminal_info ( ctx : Context ) -> None Export terminal information. Source code in mt5cli/cli.py 391 392 393 394 @app . command ( rich_help_panel = \"Data / Export\" ) def terminal_info ( ctx : typer . Context ) -> None : \"\"\"Export terminal information.\"\"\" _export_command ( ctx , lambda client : client . terminal_info ()) ticks_from \u00b6 ticks_from ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], count : Annotated [ int , Option ( help = \"Number of ticks.\" )], flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ], ) -> None Export ticks from a start date. Source code in mt5cli/cli.py 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 @app . command ( rich_help_panel = \"Data / Export\" ) def ticks_from ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], count : Annotated [ int , typer . Option ( help = \"Number of ticks.\" )], flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ], ) -> None : \"\"\"Export ticks from a start date.\"\"\" _export_command ( ctx , lambda client : client . copy_ticks_from ( symbol , date_from , count , flags ), ) ticks_range \u00b6 ticks_range ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags.\" ), ], ) -> None Export ticks for a date range. Source code in mt5cli/cli.py 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 @app . command ( rich_help_panel = \"Data / Export\" ) def ticks_range ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags.\" ), ], ) -> None : \"\"\"Export ticks for a date range.\"\"\" _export_command ( ctx , lambda client : client . copy_ticks_range ( symbol , date_from , date_to , flags ), ) ticks_recent \u00b6 ticks_recent ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], seconds : Annotated [ float , Option ( help = \"Lookback window in seconds.\" ) ], date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" , ), ] = None , count : Annotated [ int , Option ( help = \"Maximum number of ticks to return.\" ), ] = 10000 , flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , ) -> None Export ticks from a recent time window. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def ticks_recent ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], seconds : Annotated [ float , typer . Option ( help = \"Lookback window in seconds.\" ), ], date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" ), ] = None , count : Annotated [ int , typer . Option ( help = \"Maximum number of ticks to return.\" ), ] = 10000 , flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , # pyright: ignore[reportArgumentType] ) -> None : \"\"\"Export ticks from a recent time window.\"\"\" _export_command ( ctx , lambda client : client . recent_ticks ( symbol , seconds , date_to = date_to , count = count , flags = flags , ), ) version \u00b6 version ( ctx : Context ) -> None Export MetaTrader5 version information. Source code in mt5cli/cli.py 544 545 546 547 @app . command ( rich_help_panel = \"Data / Export\" ) def version ( ctx : typer . Context ) -> None : \"\"\"Export MetaTrader5 version information.\"\"\" _export_command ( ctx , lambda client : client . version ())","title":"CLI"},{"location":"api/cli/#cli-module","text":"","title":"CLI Module"},{"location":"api/cli/#mt5cli.cli","text":"Command-line interface for MetaTrader 5 data and execution utilities.","title":"cli"},{"location":"api/cli/#mt5cli.cli.app","text":"app = Typer ( name = \"mt5cli\" , help = \"MT5 data and execution utilities \u2014 read market data, inspect account state, and send trade requests. Data commands write to CSV, JSON, Parquet, or SQLite3. Execution commands (order-send, close-positions) require --yes for live mutations.\" , )","title":"app"},{"location":"api/cli/#mt5cli.cli.logger","text":"logger = getLogger ( __name__ )","title":"logger"},{"location":"api/cli/#mt5cli.cli.account_info","text":"account_info ( ctx : Context ) -> None Export account information. Source code in mt5cli/cli.py 385 386 387 388 @app . command ( rich_help_panel = \"Data / Export\" ) def account_info ( ctx : typer . Context ) -> None : \"\"\"Export account information.\"\"\" _export_command ( ctx , lambda client : client . account_info ())","title":"account_info"},{"location":"api/cli/#mt5cli.cli.close_positions","text":"close_positions ( ctx : Context , symbol : Annotated [ list [ str ] | None , Option ( \"--symbol\" , \"-s\" , help = \"Symbol to close (repeat for multiple symbols).\" , ), ] = None , ticket : Annotated [ list [ int ] | None , Option ( \"--ticket\" , \"-t\" , help = \"Position ticket to close (repeat for multiple tickets).\" , ), ] = None , dry_run : Annotated [ bool , Option ( \"--dry-run\" , help = \"Preview close orders without executing them.\" , ), ] = False , yes : Annotated [ bool , Option ( \"--yes\" , help = \"Confirm live position closing.\" ), ] = False , ) -> None Close open positions by symbol or ticket. Delegates to :func: mt5cli.trading.close_open_positions . At least one --symbol or --ticket must be provided to avoid accidentally closing all positions. Use --dry-run to preview without executing; --yes is required for live execution. order-send is the expert raw-request path. close-positions is the safer high-level helper that builds correct close requests automatically. Raises: Type Description BadParameter If neither --symbol nor --ticket is given, or if --yes is missing for a live (non-dry-run) run. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Execution\" ) def close_positions ( ctx : typer . Context , symbol : Annotated [ list [ str ] | None , typer . Option ( \"--symbol\" , \"-s\" , help = \"Symbol to close (repeat for multiple symbols).\" , ), ] = None , ticket : Annotated [ list [ int ] | None , typer . Option ( \"--ticket\" , \"-t\" , help = \"Position ticket to close (repeat for multiple tickets).\" , ), ] = None , dry_run : Annotated [ bool , typer . Option ( \"--dry-run\" , help = \"Preview close orders without executing them.\" ), ] = False , yes : Annotated [ bool , typer . Option ( \"--yes\" , help = \"Confirm live position closing.\" ), ] = False , ) -> None : \"\"\"Close open positions by symbol or ticket. Delegates to :func:`mt5cli.trading.close_open_positions`. At least one ``--symbol`` or ``--ticket`` must be provided to avoid accidentally closing all positions. Use ``--dry-run`` to preview without executing; ``--yes`` is required for live execution. ``order-send`` is the expert raw-request path. ``close-positions`` is the safer high-level helper that builds correct close requests automatically. Raises: typer.BadParameter: If neither ``--symbol`` nor ``--ticket`` is given, or if ``--yes`` is missing for a live (non-dry-run) run. \"\"\" if not symbol and not ticket : msg = \"Provide at least one --symbol or --ticket to close positions.\" raise typer . BadParameter ( msg ) if not dry_run and not yes : msg = \"Pass --yes to close live positions.\" raise typer . BadParameter ( msg , param_hint = \"--yes\" ) export_ctx = _get_export_context ( ctx ) client = create_trading_client ( config = export_ctx . config ) try : results = close_open_positions ( client , symbols = list ( symbol ) if symbol else None , tickets = list ( ticket ) if ticket else None , dry_run = dry_run , ) finally : client . shutdown () df = _execution_results_to_df ( results ) _execute_export ( ctx , lambda : df )","title":"close_positions"},{"location":"api/cli/#mt5cli.cli.collect_history","text":"collect_history ( ctx : Context , symbol : Annotated [ list [ str ], Option ( \"--symbol\" , \"-s\" , help = \"Symbol to collect (repeat for multiple symbols).\" , ), ], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], dataset : Annotated [ list [ Dataset ] | None , Option ( \"--dataset\" , help = \"Dataset to include (repeat for multiple). Defaults to rates, history-orders, history-deals. Ticks are opt-in: pass --dataset ticks to include them.\" , ), ] = None , timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Rates timeframe (e.g., M1, H1, D1).\" , ), ] = 1 , flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick copy flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , if_exists : Annotated [ IfExists , Option ( \"--if-exists\" , help = \"Behavior when a target table already exists.\" , ), ] = FAIL , with_views : Annotated [ bool , Option ( \"--with-views\" , help = \"Add cash_events and positions_reconstructed SQLite views derived from history_deals.\" , ), ] = False , ) -> None Collect historical datasets into a single SQLite database. Tables written depend on --dataset : rates , history_orders , history_deals by default. ticks are opt-in: pass --dataset ticks to include them (tick data grows the database quickly). History datasets are fetched per symbol and concatenated. Rates rows carry the requested timeframe so appended runs at different timeframes remain distinguishable. With --with-views (requires the history-deals dataset), optional views cash_events and positions_reconstructed are derived from history_deals when the required columns are present. Raises: Type Description BadParameter If the output format is not SQLite3. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Collection\" ) def collect_history ( ctx : typer . Context , symbol : Annotated [ list [ str ], typer . Option ( \"--symbol\" , \"-s\" , help = \"Symbol to collect (repeat for multiple symbols).\" , ), ], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], dataset : Annotated [ list [ Dataset ] | None , typer . Option ( \"--dataset\" , help = ( \"Dataset to include (repeat for multiple).\" \" Defaults to rates, history-orders, history-deals.\" \" Ticks are opt-in: pass --dataset ticks to include them.\" ), ), ] = None , timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Rates timeframe (e.g., M1, H1, D1).\" , ), ] = 1 , flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick copy flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , # pyright: ignore[reportArgumentType] if_exists : Annotated [ IfExists , typer . Option ( \"--if-exists\" , help = \"Behavior when a target table already exists.\" , ), ] = IfExists . FAIL , with_views : Annotated [ bool , typer . Option ( \"--with-views\" , help = ( \"Add cash_events and positions_reconstructed SQLite views\" \" derived from history_deals.\" ), ), ] = False , ) -> None : \"\"\"Collect historical datasets into a single SQLite database. Tables written depend on ``--dataset``: ``rates``, ``history_orders``, ``history_deals`` by default. ``ticks`` are opt-in: pass ``--dataset ticks`` to include them (tick data grows the database quickly). History datasets are fetched per symbol and concatenated. Rates rows carry the requested ``timeframe`` so appended runs at different timeframes remain distinguishable. With ``--with-views`` (requires the ``history-deals`` dataset), optional views ``cash_events`` and ``positions_reconstructed`` are derived from ``history_deals`` when the required columns are present. Raises: typer.BadParameter: If the output format is not SQLite3. \"\"\" export_ctx = _get_export_context ( ctx ) if export_ctx . output_format != \"sqlite3\" : msg = ( \"collect-history requires SQLite3 output.\" \" Use a .db/.sqlite/.sqlite3 extension or --format sqlite3.\" ) raise typer . BadParameter ( msg ) datasets = set ( dataset ) if dataset is not None else None sdk . collect_history ( output = export_ctx . output , symbols = symbol , date_from = date_from , date_to = date_to , datasets = datasets , timeframe = timeframe , flags = flags , if_exists = if_exists , with_views = with_views , config = export_ctx . config , )","title":"collect_history"},{"location":"api/cli/#mt5cli.cli.grafana_schema","text":"grafana_schema ( ctx : Context , publish_copy : Annotated [ Path | None , Option ( \"--publish-copy\" , help = \"Publish a Grafana-ready SQLite copy to this path after schema creation.\" , ), ] = None , ) -> None Create or refresh Grafana-ready views and indexes in a SQLite database. Idempotent \u2014 safe to run repeatedly on the same database. Requires SQLite output. Does not connect to MetaTrader 5. Raises: Type Description BadParameter If the output format is not SQLite3. Source code in mt5cli/cli.py 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 @app . command ( rich_help_panel = \"Collection\" ) def grafana_schema ( ctx : typer . Context , publish_copy : Annotated [ Path | None , typer . Option ( \"--publish-copy\" , help = ( \"Publish a Grafana-ready SQLite copy to this path\" \" after schema creation.\" ), ), ] = None , ) -> None : \"\"\"Create or refresh Grafana-ready views and indexes in a SQLite database. Idempotent \u2014 safe to run repeatedly on the same database. Requires SQLite output. Does not connect to MetaTrader 5. Raises: typer.BadParameter: If the output format is not SQLite3. \"\"\" import sqlite3 as _sqlite3 # noqa: PLC0415 from .grafana import ( # noqa: PLC0415 create_snapshot_tables , ensure_grafana_schema , publish_grafana_copy , ) export_ctx = _get_export_context ( ctx ) if export_ctx . output_format != \"sqlite3\" : msg = ( \"grafana-schema requires SQLite3 output.\" \" Use a .db/.sqlite/.sqlite3 extension or --format sqlite3.\" ) raise typer . BadParameter ( msg ) with _sqlite3 . connect ( export_ctx . output ) as conn : conn . execute ( \"PRAGMA journal_mode=WAL\" ) conn . execute ( \"PRAGMA synchronous=NORMAL\" ) create_snapshot_tables ( conn ) ensure_grafana_schema ( conn ) logger . info ( \"Grafana schema applied to %s \" , export_ctx . output ) if publish_copy is not None : publish_grafana_copy ( export_ctx . output , publish_copy ) logger . info ( \"Grafana copy published to %s \" , publish_copy )","title":"grafana_schema"},{"location":"api/cli/#mt5cli.cli.history_deals","text":"history_deals ( ctx : Context , date_from : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Order ticket.\" ) ] = None , position : Annotated [ int | None , Option ( help = \"Position ticket.\" ) ] = None , ) -> None Export historical deals. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def history_deals ( ctx : typer . Context , date_from : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Order ticket.\" )] = None , position : Annotated [ int | None , typer . Option ( help = \"Position ticket.\" )] = None , ) -> None : \"\"\"Export historical deals.\"\"\" _export_command ( ctx , lambda client : client . history_deals ( date_from = date_from , date_to = date_to , group = group , symbol = symbol , ticket = ticket , position = position , ), )","title":"history_deals"},{"location":"api/cli/#mt5cli.cli.history_orders","text":"history_orders ( ctx : Context , date_from : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Order ticket.\" ) ] = None , position : Annotated [ int | None , Option ( help = \"Position ticket.\" ) ] = None , ) -> None Export historical orders. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def history_orders ( ctx : typer . Context , date_from : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ] = None , date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Order ticket.\" )] = None , position : Annotated [ int | None , typer . Option ( help = \"Position ticket.\" )] = None , ) -> None : \"\"\"Export historical orders.\"\"\" _export_command ( ctx , lambda client : client . history_orders ( date_from = date_from , date_to = date_to , group = group , symbol = symbol , ticket = ticket , position = position , ), )","title":"history_orders"},{"location":"api/cli/#mt5cli.cli.last_error","text":"last_error ( ctx : Context ) -> None Export the last error information. Source code in mt5cli/cli.py 550 551 552 553 @app . command ( rich_help_panel = \"Data / Export\" ) def last_error ( ctx : typer . Context ) -> None : \"\"\"Export the last error information.\"\"\" _export_command ( ctx , lambda client : client . last_error ())","title":"last_error"},{"location":"api/cli/#mt5cli.cli.latest_rates","text":"latest_rates ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" ), ], count : Annotated [ int , Option ( help = \"Number of records.\" ) ], start_pos : Annotated [ int , Option ( help = \"Start position (0 = current bar).\" ), ] = 0 , ) -> None Export latest rates from a start position. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def latest_rates ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" , ), ], count : Annotated [ int , typer . Option ( help = \"Number of records.\" )], start_pos : Annotated [ int , typer . Option ( help = \"Start position (0 = current bar).\" ), ] = 0 , ) -> None : \"\"\"Export latest rates from a start position.\"\"\" _export_command ( ctx , lambda client : client . latest_rates ( symbol , timeframe , count , start_pos = start_pos , ), )","title":"latest_rates"},{"location":"api/cli/#mt5cli.cli.main","text":"main () -> None Run the mt5cli CLI. Source code in mt5cli/cli.py 930 931 932 def main () -> None : \"\"\"Run the mt5cli CLI.\"\"\" app ()","title":"main"},{"location":"api/cli/#mt5cli.cli.market_book","text":"market_book ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export market depth (order book) for a symbol. Source code in mt5cli/cli.py 565 566 567 568 569 570 571 @app . command ( rich_help_panel = \"Data / Export\" ) def market_book ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export market depth (order book) for a symbol.\"\"\" _export_command ( ctx , lambda client : client . market_book ( symbol ))","title":"market_book"},{"location":"api/cli/#mt5cli.cli.minimum_margins","text":"minimum_margins ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export minimum-volume buy and sell margin requirements. Source code in mt5cli/cli.py 418 419 420 421 422 423 424 @app . command ( rich_help_panel = \"Data / Export\" ) def minimum_margins ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export minimum-volume buy and sell margin requirements.\"\"\" _export_command ( ctx , lambda client : client . minimum_margins ( symbol ))","title":"minimum_margins"},{"location":"api/cli/#mt5cli.cli.mt5_summary","text":"mt5_summary ( ctx : Context ) -> None Export a compact terminal/account status summary. Source code in mt5cli/cli.py 538 539 540 541 @app . command ( rich_help_panel = \"Data / Export\" ) def mt5_summary ( ctx : typer . Context ) -> None : \"\"\"Export a compact terminal/account status summary.\"\"\" _export_command ( ctx , lambda client : client . mt5_summary_as_df ())","title":"mt5_summary"},{"location":"api/cli/#mt5cli.cli.order_check","text":"order_check ( ctx : Context , request : Annotated [ dict [ str , Any ], Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP , ), ], ) -> None Check funds sufficiency for a trading operation. Source code in mt5cli/cli.py 574 575 576 577 578 579 580 581 582 583 @app . command ( rich_help_panel = \"Data / Export\" ) def order_check ( ctx : typer . Context , request : Annotated [ dict [ str , Any ], typer . Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP ), ], ) -> None : \"\"\"Check funds sufficiency for a trading operation.\"\"\" _export_command ( ctx , lambda client : client . order_check ( request ))","title":"order_check"},{"location":"api/cli/#mt5cli.cli.order_send","text":"order_send ( ctx : Context , request : Annotated [ dict [ str , Any ], Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP , ), ], yes : Annotated [ bool , Option ( \"--yes\" , help = \"Confirm the live trade request.\" ), ] = False , ) -> None Send a raw trade request to the trade server (expert path, live execution). Passes the request JSON directly to MT5 order_send . This is the low-level expert path \u2014 it places real trades on the connected account with no additional validation beyond what MT5 itself performs. Use order-check first to validate funds sufficiency. Prefer close-positions for closing open positions. --yes is required. Raises: Type Description BadParameter If --yes is not provided. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Execution\" ) def order_send ( ctx : typer . Context , request : Annotated [ dict [ str , Any ], typer . Option ( click_type = REQUEST_TYPE , help = _REQUEST_OPTION_HELP ), ], yes : Annotated [ bool , typer . Option ( \"--yes\" , help = \"Confirm the live trade request.\" ), ] = False , ) -> None : \"\"\"Send a raw trade request to the trade server (expert path, live execution). Passes the request JSON directly to MT5 ``order_send``. This is the low-level expert path \u2014 it places real trades on the connected account with no additional validation beyond what MT5 itself performs. Use ``order-check`` first to validate funds sufficiency. Prefer ``close-positions`` for closing open positions. ``--yes`` is required. Raises: typer.BadParameter: If --yes is not provided. \"\"\" if not yes : msg = \"Pass --yes to send a live trade request.\" raise typer . BadParameter ( msg , param_hint = \"--yes\" ) _export_command ( ctx , lambda client : client . order_send ( request ))","title":"order_send"},{"location":"api/cli/#mt5cli.cli.orders","text":"orders ( ctx : Context , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Ticket filter.\" ) ] = None , ) -> None Export active orders. Source code in mt5cli/cli.py 427 428 429 430 431 432 433 434 435 436 437 438 @app . command ( rich_help_panel = \"Data / Export\" ) def orders ( ctx : typer . Context , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Ticket filter.\" )] = None , ) -> None : \"\"\"Export active orders.\"\"\" _export_command ( ctx , lambda client : client . orders ( symbol = symbol , group = group , ticket = ticket ), )","title":"orders"},{"location":"api/cli/#mt5cli.cli.positions","text":"positions ( ctx : Context , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , ticket : Annotated [ int | None , Option ( help = \"Ticket filter.\" ) ] = None , ) -> None Export open positions. Source code in mt5cli/cli.py 441 442 443 444 445 446 447 448 449 450 451 452 @app . command ( rich_help_panel = \"Data / Export\" ) def positions ( ctx : typer . Context , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , ticket : Annotated [ int | None , typer . Option ( help = \"Ticket filter.\" )] = None , ) -> None : \"\"\"Export open positions.\"\"\" _export_command ( ctx , lambda client : client . positions ( symbol = symbol , group = group , ticket = ticket ), )","title":"positions"},{"location":"api/cli/#mt5cli.cli.rates_from","text":"rates_from ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe (e.g., M1, H1, D1, or integer).\" , ), ], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date in ISO 8601 format.\" , ), ], count : Annotated [ int , Option ( help = \"Number of records.\" ) ], ) -> None Export rates from a start date. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def rates_from ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe (e.g., M1, H1, D1, or integer).\" , ), ], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date in ISO 8601 format.\" , ), ], count : Annotated [ int , typer . Option ( help = \"Number of records.\" )], ) -> None : \"\"\"Export rates from a start date.\"\"\" _export_command ( ctx , lambda client : client . copy_rates_from ( symbol , timeframe , date_from , count ), )","title":"rates_from"},{"location":"api/cli/#mt5cli.cli.rates_from_pos","text":"rates_from_pos ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" ), ], start_pos : Annotated [ int , Option ( help = \"Start position (0 = current bar).\" ), ], count : Annotated [ int , Option ( help = \"Number of records.\" ) ], ) -> None Export rates from a start position. Source code in mt5cli/cli.py 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 @app . command ( rich_help_panel = \"Data / Export\" ) def rates_from_pos ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" , ), ], start_pos : Annotated [ int , typer . Option ( help = \"Start position (0 = current bar).\" )], count : Annotated [ int , typer . Option ( help = \"Number of records.\" )], ) -> None : \"\"\"Export rates from a start position.\"\"\" _export_command ( ctx , lambda client : client . copy_rates_from_pos ( symbol , timeframe , start_pos , count , ), )","title":"rates_from_pos"},{"location":"api/cli/#mt5cli.cli.rates_range","text":"rates_range ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" ), ], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], ) -> None Export rates for a date range. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def rates_range ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], timeframe : Annotated [ int , typer . Option ( click_type = TIMEFRAME_TYPE , help = \"Timeframe.\" , ), ], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], ) -> None : \"\"\"Export rates for a date range.\"\"\" _export_command ( ctx , lambda client : client . copy_rates_range ( symbol , timeframe , date_from , date_to ), )","title":"rates_range"},{"location":"api/cli/#mt5cli.cli.recent_history_deals","text":"recent_history_deals ( ctx : Context , hours : Annotated [ float , Option ( help = \"Lookback window in hours.\" ) ], date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" , ), ] = None , group : Annotated [ str | None , Option ( help = \"Group filter.\" ) ] = None , symbol : Annotated [ str | None , Option ( help = \"Symbol filter.\" ) ] = None , ) -> None Export historical deals from a recent trailing window. Source code in mt5cli/cli.py 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 @app . command ( rich_help_panel = \"Data / Export\" ) def recent_history_deals ( ctx : typer . Context , hours : Annotated [ float , typer . Option ( help = \"Lookback window in hours.\" )], date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" ), ] = None , group : Annotated [ str | None , typer . Option ( help = \"Group filter.\" )] = None , symbol : Annotated [ str | None , typer . Option ( help = \"Symbol filter.\" )] = None , ) -> None : \"\"\"Export historical deals from a recent trailing window.\"\"\" _export_command ( ctx , lambda client : client . recent_history_deals ( hours , date_to = date_to , group = group , symbol = symbol , ), )","title":"recent_history_deals"},{"location":"api/cli/#mt5cli.cli.snapshot","text":"snapshot ( ctx : Context , symbol : Annotated [ list [ str ] | None , Option ( \"--symbol\" , \"-s\" , help = \"Symbol filter for positions/orders (repeat for multiple).\" , ), ] = None , with_account : Annotated [ bool , Option ( \"--with-account/--no-account\" , help = \"Snapshot account info.\" , ), ] = True , with_positions : Annotated [ bool , Option ( \"--with-positions/--no-positions\" , help = \"Snapshot open positions.\" , ), ] = True , with_orders : Annotated [ bool , Option ( \"--with-orders/--no-orders\" , help = \"Snapshot active orders.\" , ), ] = True , with_terminal : Annotated [ bool , Option ( \"--with-terminal/--no-terminal\" , help = \"Snapshot terminal info.\" , ), ] = True , with_grafana_schema : Annotated [ bool , Option ( \"--with-grafana-schema/--no-grafana-schema\" , help = \"Ensure Grafana views and indexes exist.\" , ), ] = False , publish_copy : Annotated [ Path | None , Option ( \"--publish-copy\" , help = \"Publish a Grafana-ready SQLite copy to this path after snapshot.\" , ), ] = None , ) -> None Snapshot current account, position, order, and terminal state into SQLite. Appends a timestamped snapshot row for each data type. Never places orders or modifies trading state. Raises: Type Description BadParameter If the output format is not SQLite3. Source code in mt5cli/cli.py 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 @app . command ( rich_help_panel = \"Collection\" ) def snapshot ( ctx : typer . Context , symbol : Annotated [ list [ str ] | None , typer . Option ( \"--symbol\" , \"-s\" , help = \"Symbol filter for positions/orders (repeat for multiple).\" , ), ] = None , with_account : Annotated [ bool , typer . Option ( \"--with-account/--no-account\" , help = \"Snapshot account info.\" ), ] = True , with_positions : Annotated [ bool , typer . Option ( \"--with-positions/--no-positions\" , help = \"Snapshot open positions.\" ), ] = True , with_orders : Annotated [ bool , typer . Option ( \"--with-orders/--no-orders\" , help = \"Snapshot active orders.\" ), ] = True , with_terminal : Annotated [ bool , typer . Option ( \"--with-terminal/--no-terminal\" , help = \"Snapshot terminal info.\" ), ] = True , with_grafana_schema : Annotated [ bool , typer . Option ( \"--with-grafana-schema/--no-grafana-schema\" , help = \"Ensure Grafana views and indexes exist.\" , ), ] = False , publish_copy : Annotated [ Path | None , typer . Option ( \"--publish-copy\" , help = ( \"Publish a Grafana-ready SQLite copy to this path after snapshot.\" ), ), ] = None , ) -> None : \"\"\"Snapshot current account, position, order, and terminal state into SQLite. Appends a timestamped snapshot row for each data type. Never places orders or modifies trading state. Raises: typer.BadParameter: If the output format is not SQLite3. \"\"\" export_ctx = _get_export_context ( ctx ) if export_ctx . output_format != \"sqlite3\" : msg = ( \"snapshot requires SQLite3 output.\" \" Use a .db/.sqlite/.sqlite3 extension or --format sqlite3.\" ) raise typer . BadParameter ( msg ) sdk . update_observability_with_config ( output = export_ctx . output , config = export_ctx . config , symbols = list ( symbol ) if symbol else None , include_account = with_account , include_positions = with_positions , include_orders = with_orders , include_terminal = with_terminal , with_grafana_schema = with_grafana_schema , ) logger . info ( \"Snapshot written to %s \" , export_ctx . output ) if publish_copy is not None : from .grafana import publish_grafana_copy # noqa: PLC0415 publish_grafana_copy ( export_ctx . output , publish_copy ) logger . info ( \"Grafana copy published to %s \" , publish_copy )","title":"snapshot"},{"location":"api/cli/#mt5cli.cli.symbol_info","text":"symbol_info ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export symbol details. Source code in mt5cli/cli.py 409 410 411 412 413 414 415 @app . command ( rich_help_panel = \"Data / Export\" ) def symbol_info ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export symbol details.\"\"\" _export_command ( ctx , lambda client : client . symbol_info ( symbol ))","title":"symbol_info"},{"location":"api/cli/#mt5cli.cli.symbol_info_tick","text":"symbol_info_tick ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], ) -> None Export the last tick for a symbol. Source code in mt5cli/cli.py 556 557 558 559 560 561 562 @app . command ( rich_help_panel = \"Data / Export\" ) def symbol_info_tick ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], ) -> None : \"\"\"Export the last tick for a symbol.\"\"\" _export_command ( ctx , lambda client : client . symbol_info_tick ( symbol ))","title":"symbol_info_tick"},{"location":"api/cli/#mt5cli.cli.symbols","text":"symbols ( ctx : Context , group : Annotated [ str | None , Option ( help = \"Symbol group filter (e.g., *USD*).\" ), ] = None , ) -> None Export symbol list. Source code in mt5cli/cli.py 397 398 399 400 401 402 403 404 405 406 @app . command ( rich_help_panel = \"Data / Export\" ) def symbols ( ctx : typer . Context , group : Annotated [ str | None , typer . Option ( help = \"Symbol group filter (e.g., *USD*).\" ), ] = None , ) -> None : \"\"\"Export symbol list.\"\"\" _export_command ( ctx , lambda client : client . symbols ( group = group ))","title":"symbols"},{"location":"api/cli/#mt5cli.cli.terminal_info","text":"terminal_info ( ctx : Context ) -> None Export terminal information. Source code in mt5cli/cli.py 391 392 393 394 @app . command ( rich_help_panel = \"Data / Export\" ) def terminal_info ( ctx : typer . Context ) -> None : \"\"\"Export terminal information.\"\"\" _export_command ( ctx , lambda client : client . terminal_info ())","title":"terminal_info"},{"location":"api/cli/#mt5cli.cli.ticks_from","text":"ticks_from ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], count : Annotated [ int , Option ( help = \"Number of ticks.\" )], flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ], ) -> None Export ticks from a start date. Source code in mt5cli/cli.py 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 @app . command ( rich_help_panel = \"Data / Export\" ) def ticks_from ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], count : Annotated [ int , typer . Option ( help = \"Number of ticks.\" )], flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ], ) -> None : \"\"\"Export ticks from a start date.\"\"\" _export_command ( ctx , lambda client : client . copy_ticks_from ( symbol , date_from , count , flags ), )","title":"ticks_from"},{"location":"api/cli/#mt5cli.cli.ticks_range","text":"ticks_range ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags.\" ), ], ) -> None Export ticks for a date range. Source code in mt5cli/cli.py 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 @app . command ( rich_help_panel = \"Data / Export\" ) def ticks_range ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], date_from : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"Start date.\" ), ], date_to : Annotated [ datetime , typer . Option ( click_type = DATETIME_TYPE , help = \"End date.\" ), ], flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags.\" ), ], ) -> None : \"\"\"Export ticks for a date range.\"\"\" _export_command ( ctx , lambda client : client . copy_ticks_range ( symbol , date_from , date_to , flags ), )","title":"ticks_range"},{"location":"api/cli/#mt5cli.cli.ticks_recent","text":"ticks_recent ( ctx : Context , symbol : Annotated [ str , Option ( help = \"Symbol name.\" )], seconds : Annotated [ float , Option ( help = \"Lookback window in seconds.\" ) ], date_to : Annotated [ datetime | None , Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" , ), ] = None , count : Annotated [ int , Option ( help = \"Maximum number of ticks to return.\" ), ] = 10000 , flags : Annotated [ int , Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , ) -> None Export ticks from a recent time window. Source code in mt5cli/cli.py 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 @app . command ( rich_help_panel = \"Data / Export\" ) def ticks_recent ( ctx : typer . Context , symbol : Annotated [ str , typer . Option ( help = \"Symbol name.\" )], seconds : Annotated [ float , typer . Option ( help = \"Lookback window in seconds.\" ), ], date_to : Annotated [ datetime | None , typer . Option ( click_type = DATETIME_TYPE , help = \"Window end date.\" ), ] = None , count : Annotated [ int , typer . Option ( help = \"Maximum number of ticks to return.\" ), ] = 10000 , flags : Annotated [ int , typer . Option ( click_type = TICK_FLAGS_TYPE , help = \"Tick flags (ALL, INFO, TRADE, or integer).\" , ), ] = \"ALL\" , # pyright: ignore[reportArgumentType] ) -> None : \"\"\"Export ticks from a recent time window.\"\"\" _export_command ( ctx , lambda client : client . recent_ticks ( symbol , seconds , date_to = date_to , count = count , flags = flags , ), )","title":"ticks_recent"},{"location":"api/cli/#mt5cli.cli.version","text":"version ( ctx : Context ) -> None Export MetaTrader5 version information. Source code in mt5cli/cli.py 544 545 546 547 @app . command ( rich_help_panel = \"Data / Export\" ) def version ( ctx : typer . Context ) -> None : \"\"\"Export MetaTrader5 version information.\"\"\" _export_command ( ctx , lambda client : client . version ())","title":"version"},{"location":"api/client/","text":"Client \u00b6 mt5cli.client \u00b6 Stable public client abstraction for MT5 data and execution operations. __all__ module-attribute \u00b6 __all__ = [ 'MT5Client' , 'build_config' , 'mt5_session' ] MT5Client \u00b6 MT5Client ( * , path : str | None = None , login : int | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , retry_count : int = 3 , config : Mt5Config | None = None , client : Mt5DataClient | None = None , ) Bases: Mt5CliClient Public client for generic MT5 data access and order primitives. Extends the read-only SDK client with optional order check/send helpers and exposes the same connection lifecycle as :func: mt5_session . mt5cli intentionally exposes minimal execution primitives only. Trading decisions, signals, strategies, backtests, and optimization remain the responsibility of downstream applications. Source code in mt5cli/sdk.py 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 def __init__ ( self , * , path : str | None = None , login : int | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , retry_count : int = 3 , config : Mt5Config | None = None , client : Mt5DataClient | None = None , ) -> None : \"\"\"Initialize the SDK client. Args: path: Path to MetaTrader5 terminal EXE file. login: Trading account login. password: Trading account password. server: Trading server name. timeout: Connection timeout in milliseconds. retry_count: Number of MT5 initialization retries for sessions opened by this client. config: Optional pre-built ``Mt5Config`` (overrides other args). client: Optional already-connected ``Mt5DataClient``. Injected clients are reused as-is and are not initialized or shut down. \"\"\" self . _config = config or build_config ( path = path , login = login , password = password , server = server , timeout = timeout , ) self . _retry_count = retry_count self . _client = client self . _owns_client = client is None from_connected_client classmethod \u00b6 from_connected_client ( client : Mt5DataClient ) -> Self Bind to an already-connected Mt5DataClient without owning it. Returns: Type Description Self Client wrapper bound to the injected connection. Source code in mt5cli/client.py 63 64 65 66 67 68 69 70 @classmethod def from_connected_client ( cls , client : Mt5DataClient ) -> Self : \"\"\"Bind to an already-connected ``Mt5DataClient`` without owning it. Returns: Client wrapper bound to the injected connection. \"\"\" return cls ( client = client ) order_check \u00b6 order_check ( request : dict [ str , Any ]) -> DataFrame Check funds sufficiency for a trade request. Parameters: Name Type Description Default request dict [ str , Any ] MT5 order request dictionary. required Returns: Type Description DataFrame One-row DataFrame with the order-check result. Source code in mt5cli/client.py 34 35 36 37 38 39 40 41 42 43 def order_check ( self , request : dict [ str , Any ]) -> pd . DataFrame : \"\"\"Check funds sufficiency for a trade request. Args: request: MT5 order request dictionary. Returns: One-row DataFrame with the order-check result. \"\"\" return self . _fetch ( lambda client : client . order_check_as_df ( request = request )) order_send \u00b6 order_send ( request : dict [ str , Any ]) -> DataFrame Send a live trade request to the MT5 trade server. Warning This is a live execution primitive. A successful call can place, modify, or close real trades on the connected account. Downstream applications must gate usage explicitly (for example behind manual confirmation or application-specific risk controls). mt5cli does not implement strategy logic, signal generation, or trade sizing. Parameters: Name Type Description Default request dict [ str , Any ] MT5 order request dictionary. required Returns: Type Description DataFrame One-row DataFrame with the order-send result. Source code in mt5cli/client.py 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 def order_send ( self , request : dict [ str , Any ]) -> pd . DataFrame : \"\"\"Send a live trade request to the MT5 trade server. Warning: This is a live execution primitive. A successful call can place, modify, or close real trades on the connected account. Downstream applications must gate usage explicitly (for example behind manual confirmation or application-specific risk controls). mt5cli does not implement strategy logic, signal generation, or trade sizing. Args: request: MT5 order request dictionary. Returns: One-row DataFrame with the order-send result. \"\"\" return self . _fetch ( lambda client : client . order_send_as_df ( request = request )) build_config \u00b6 build_config ( * , path : str | None = None , login : int | str | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , allow_whole_dollar_env : bool = False , ) -> Mt5Config Build an Mt5Config from optional connection parameters. Parameters: Name Type Description Default path str | None Optional terminal executable path. None login int | str | None Optional trading account login. Integers are preserved. String values are coerced: empty or whitespace-only strings become None ; numeric strings such as \"12345\" are converted to int ; non-numeric strings raise ValueError . When allow_whole_dollar_env=True , $ENV_NAME and ${ENV_NAME} placeholders are expanded before coercion. None password str | None Optional trading account password. None server str | None Optional trading server name. None timeout int | None Optional connection timeout in milliseconds. None allow_whole_dollar_env bool When True , string parameters that are exactly $ENV_NAME are expanded from the environment. Applies to path , login , password , and server . Default False preserves existing behavior. False Returns: Type Description Mt5Config Configured Mt5Config instance. Source code in mt5cli/sdk.py 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 def build_config ( * , path : str | None = None , login : int | str | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , allow_whole_dollar_env : bool = False , ) -> Mt5Config : \"\"\"Build an ``Mt5Config`` from optional connection parameters. Args: path: Optional terminal executable path. login: Optional trading account login. Integers are preserved. String values are coerced: empty or whitespace-only strings become ``None``; numeric strings such as ``\"12345\"`` are converted to ``int``; non-numeric strings raise ``ValueError``. When ``allow_whole_dollar_env=True``, ``$ENV_NAME`` and ``${ENV_NAME}`` placeholders are expanded before coercion. password: Optional trading account password. server: Optional trading server name. timeout: Optional connection timeout in milliseconds. allow_whole_dollar_env: When ``True``, string parameters that are exactly ``$ENV_NAME`` are expanded from the environment. Applies to ``path``, ``login``, ``password``, and ``server``. Default ``False`` preserves existing behavior. Returns: Configured ``Mt5Config`` instance. \"\"\" if allow_whole_dollar_env : if path is not None : path = substitute_env_placeholders ( path , allow_whole_dollar_env = True ) if isinstance ( login , str ): login = substitute_env_placeholders ( login , allow_whole_dollar_env = True ) if password is not None : password = substitute_env_placeholders ( password , allow_whole_dollar_env = True ) if server is not None : server = substitute_env_placeholders ( server , allow_whole_dollar_env = True ) return Mt5Config ( path = path , login = _coerce_login ( login ), password = password , server = server , timeout = timeout , ) mt5_session \u00b6 mt5_session ( config : Mt5Config | None = None , ) -> Iterator [ MT5Client ] Open an MT5 terminal session and yield a connected :class: MT5Client . Parameters: Name Type Description Default config Mt5Config | None MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. None Yields: Name Type Description Connected MT5Client class: MT5Client bound to the session. Source code in mt5cli/client.py 73 74 75 76 77 78 79 80 81 82 83 84 85 86 @contextmanager def mt5_session ( config : Mt5Config | None = None ) -> Iterator [ MT5Client ]: \"\"\"Open an MT5 terminal session and yield a connected :class:`MT5Client`. Args: config: MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. Yields: Connected :class:`MT5Client` bound to the session. \"\"\" mt5_config = config or build_config () with connected_client ( mt5_config ) as client : yield MT5Client . from_connected_client ( client )","title":"Client"},{"location":"api/client/#client","text":"","title":"Client"},{"location":"api/client/#mt5cli.client","text":"Stable public client abstraction for MT5 data and execution operations.","title":"client"},{"location":"api/client/#mt5cli.client.__all__","text":"__all__ = [ 'MT5Client' , 'build_config' , 'mt5_session' ]","title":"__all__"},{"location":"api/client/#mt5cli.client.MT5Client","text":"MT5Client ( * , path : str | None = None , login : int | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , retry_count : int = 3 , config : Mt5Config | None = None , client : Mt5DataClient | None = None , ) Bases: Mt5CliClient Public client for generic MT5 data access and order primitives. Extends the read-only SDK client with optional order check/send helpers and exposes the same connection lifecycle as :func: mt5_session . mt5cli intentionally exposes minimal execution primitives only. Trading decisions, signals, strategies, backtests, and optimization remain the responsibility of downstream applications. Source code in mt5cli/sdk.py 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 def __init__ ( self , * , path : str | None = None , login : int | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , retry_count : int = 3 , config : Mt5Config | None = None , client : Mt5DataClient | None = None , ) -> None : \"\"\"Initialize the SDK client. Args: path: Path to MetaTrader5 terminal EXE file. login: Trading account login. password: Trading account password. server: Trading server name. timeout: Connection timeout in milliseconds. retry_count: Number of MT5 initialization retries for sessions opened by this client. config: Optional pre-built ``Mt5Config`` (overrides other args). client: Optional already-connected ``Mt5DataClient``. Injected clients are reused as-is and are not initialized or shut down. \"\"\" self . _config = config or build_config ( path = path , login = login , password = password , server = server , timeout = timeout , ) self . _retry_count = retry_count self . _client = client self . _owns_client = client is None","title":"MT5Client"},{"location":"api/client/#mt5cli.client.MT5Client.from_connected_client","text":"from_connected_client ( client : Mt5DataClient ) -> Self Bind to an already-connected Mt5DataClient without owning it. Returns: Type Description Self Client wrapper bound to the injected connection. Source code in mt5cli/client.py 63 64 65 66 67 68 69 70 @classmethod def from_connected_client ( cls , client : Mt5DataClient ) -> Self : \"\"\"Bind to an already-connected ``Mt5DataClient`` without owning it. Returns: Client wrapper bound to the injected connection. \"\"\" return cls ( client = client )","title":"from_connected_client"},{"location":"api/client/#mt5cli.client.MT5Client.order_check","text":"order_check ( request : dict [ str , Any ]) -> DataFrame Check funds sufficiency for a trade request. Parameters: Name Type Description Default request dict [ str , Any ] MT5 order request dictionary. required Returns: Type Description DataFrame One-row DataFrame with the order-check result. Source code in mt5cli/client.py 34 35 36 37 38 39 40 41 42 43 def order_check ( self , request : dict [ str , Any ]) -> pd . DataFrame : \"\"\"Check funds sufficiency for a trade request. Args: request: MT5 order request dictionary. Returns: One-row DataFrame with the order-check result. \"\"\" return self . _fetch ( lambda client : client . order_check_as_df ( request = request ))","title":"order_check"},{"location":"api/client/#mt5cli.client.MT5Client.order_send","text":"order_send ( request : dict [ str , Any ]) -> DataFrame Send a live trade request to the MT5 trade server. Warning This is a live execution primitive. A successful call can place, modify, or close real trades on the connected account. Downstream applications must gate usage explicitly (for example behind manual confirmation or application-specific risk controls). mt5cli does not implement strategy logic, signal generation, or trade sizing. Parameters: Name Type Description Default request dict [ str , Any ] MT5 order request dictionary. required Returns: Type Description DataFrame One-row DataFrame with the order-send result. Source code in mt5cli/client.py 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 def order_send ( self , request : dict [ str , Any ]) -> pd . DataFrame : \"\"\"Send a live trade request to the MT5 trade server. Warning: This is a live execution primitive. A successful call can place, modify, or close real trades on the connected account. Downstream applications must gate usage explicitly (for example behind manual confirmation or application-specific risk controls). mt5cli does not implement strategy logic, signal generation, or trade sizing. Args: request: MT5 order request dictionary. Returns: One-row DataFrame with the order-send result. \"\"\" return self . _fetch ( lambda client : client . order_send_as_df ( request = request ))","title":"order_send"},{"location":"api/client/#mt5cli.client.build_config","text":"build_config ( * , path : str | None = None , login : int | str | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , allow_whole_dollar_env : bool = False , ) -> Mt5Config Build an Mt5Config from optional connection parameters. Parameters: Name Type Description Default path str | None Optional terminal executable path. None login int | str | None Optional trading account login. Integers are preserved. String values are coerced: empty or whitespace-only strings become None ; numeric strings such as \"12345\" are converted to int ; non-numeric strings raise ValueError . When allow_whole_dollar_env=True , $ENV_NAME and ${ENV_NAME} placeholders are expanded before coercion. None password str | None Optional trading account password. None server str | None Optional trading server name. None timeout int | None Optional connection timeout in milliseconds. None allow_whole_dollar_env bool When True , string parameters that are exactly $ENV_NAME are expanded from the environment. Applies to path , login , password , and server . Default False preserves existing behavior. False Returns: Type Description Mt5Config Configured Mt5Config instance. Source code in mt5cli/sdk.py 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 def build_config ( * , path : str | None = None , login : int | str | None = None , password : str | None = None , server : str | None = None , timeout : int | None = None , allow_whole_dollar_env : bool = False , ) -> Mt5Config : \"\"\"Build an ``Mt5Config`` from optional connection parameters. Args: path: Optional terminal executable path. login: Optional trading account login. Integers are preserved. String values are coerced: empty or whitespace-only strings become ``None``; numeric strings such as ``\"12345\"`` are converted to ``int``; non-numeric strings raise ``ValueError``. When ``allow_whole_dollar_env=True``, ``$ENV_NAME`` and ``${ENV_NAME}`` placeholders are expanded before coercion. password: Optional trading account password. server: Optional trading server name. timeout: Optional connection timeout in milliseconds. allow_whole_dollar_env: When ``True``, string parameters that are exactly ``$ENV_NAME`` are expanded from the environment. Applies to ``path``, ``login``, ``password``, and ``server``. Default ``False`` preserves existing behavior. Returns: Configured ``Mt5Config`` instance. \"\"\" if allow_whole_dollar_env : if path is not None : path = substitute_env_placeholders ( path , allow_whole_dollar_env = True ) if isinstance ( login , str ): login = substitute_env_placeholders ( login , allow_whole_dollar_env = True ) if password is not None : password = substitute_env_placeholders ( password , allow_whole_dollar_env = True ) if server is not None : server = substitute_env_placeholders ( server , allow_whole_dollar_env = True ) return Mt5Config ( path = path , login = _coerce_login ( login ), password = password , server = server , timeout = timeout , )","title":"build_config"},{"location":"api/client/#mt5cli.client.mt5_session","text":"mt5_session ( config : Mt5Config | None = None , ) -> Iterator [ MT5Client ] Open an MT5 terminal session and yield a connected :class: MT5Client . Parameters: Name Type Description Default config Mt5Config | None MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. None Yields: Name Type Description Connected MT5Client class: MT5Client bound to the session. Source code in mt5cli/client.py 73 74 75 76 77 78 79 80 81 82 83 84 85 86 @contextmanager def mt5_session ( config : Mt5Config | None = None ) -> Iterator [ MT5Client ]: \"\"\"Open an MT5 terminal session and yield a connected :class:`MT5Client`. Args: config: MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. Yields: Connected :class:`MT5Client` bound to the session. \"\"\" mt5_config = config or build_config () with connected_client ( mt5_config ) as client : yield MT5Client . from_connected_client ( client )","title":"mt5_session"},{"location":"api/converters/","text":"Converters \u00b6 mt5cli.converters \u00b6 Shared conversion helpers for MT5 symbols, timeframes, and date ranges. __all__ module-attribute \u00b6 __all__ = [ \"ensure_utc\" , \"granularity_name\" , \"normalize_symbol\" , \"normalize_symbols\" , \"parse_date_range\" , \"parse_datetime\" , \"parse_tick_flags\" , \"parse_timeframe\" , \"recent_window\" , ] ensure_utc \u00b6 ensure_utc ( value : datetime | str ) -> datetime Return a timezone-aware UTC datetime. Parameters: Name Type Description Default value datetime | str Datetime instance or ISO 8601 string. required Returns: Type Description datetime UTC-aware datetime. Source code in mt5cli/converters.py 69 70 71 72 73 74 75 76 77 78 79 80 81 82 def ensure_utc ( value : datetime | str ) -> datetime : \"\"\"Return a timezone-aware UTC datetime. Args: value: Datetime instance or ISO 8601 string. Returns: UTC-aware datetime. \"\"\" if isinstance ( value , str ): return parse_datetime ( value ) if value . tzinfo is None : return value . replace ( tzinfo = UTC ) return value . astimezone ( UTC ) granularity_name \u00b6 granularity_name ( timeframe : int | str ) -> str Return a short granularity label for a timeframe integer or name. Parameters: Name Type Description Default timeframe int | str MT5 timeframe as integer or name (for example M1 ). required Returns: Type Description str Short name such as M1 or the stringified integer when unknown. Source code in mt5cli/converters.py 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 def granularity_name ( timeframe : int | str ) -> str : \"\"\"Return a short granularity label for a timeframe integer or name. Args: timeframe: MT5 timeframe as integer or name (for example ``M1``). Returns: Short name such as ``M1`` or the stringified integer when unknown. \"\"\" tf = parse_timeframe ( timeframe ) try : name = _get_timeframe_name ( tf ) except ValueError : return str ( tf ) return name . removeprefix ( \"TIMEFRAME_\" ) normalize_symbol \u00b6 normalize_symbol ( symbol : str ) -> str Normalize a broker symbol name for MT5 API calls. Strips surrounding whitespace while preserving broker-specific casing and suffixes (for example XAUUSDm , US500.cash , or EURUSD.r ). Parameters: Name Type Description Default symbol str Raw symbol name. required Returns: Type Description str Normalized symbol string. Raises: Type Description ValueError If the symbol is empty after normalization. Source code in mt5cli/converters.py 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 def normalize_symbol ( symbol : str ) -> str : \"\"\"Normalize a broker symbol name for MT5 API calls. Strips surrounding whitespace while preserving broker-specific casing and suffixes (for example ``XAUUSDm``, ``US500.cash``, or ``EURUSD.r``). Args: symbol: Raw symbol name. Returns: Normalized symbol string. Raises: ValueError: If the symbol is empty after normalization. \"\"\" normalized = symbol . strip () if not normalized : msg = \"Symbol must not be empty.\" raise ValueError ( msg ) return normalized normalize_symbols \u00b6 normalize_symbols ( symbols : Sequence [ str ]) -> list [ str ] Normalize a sequence of broker symbol names. Parameters: Name Type Description Default symbols Sequence [ str ] Raw symbol names. required Returns: Type Description list [ str ] List of normalized, de-duplicated symbols preserving first-seen order. Source code in mt5cli/converters.py 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 def normalize_symbols ( symbols : Sequence [ str ]) -> list [ str ]: \"\"\"Normalize a sequence of broker symbol names. Args: symbols: Raw symbol names. Returns: List of normalized, de-duplicated symbols preserving first-seen order. \"\"\" seen : set [ str ] = set () resolved : list [ str ] = [] for symbol in symbols : normalized = normalize_symbol ( symbol ) if normalized not in seen : seen . add ( normalized ) resolved . append ( normalized ) return resolved parse_date_range \u00b6 parse_date_range ( date_from : datetime | str , date_to : datetime | str ) -> tuple [ datetime , datetime ] Parse and validate an inclusive UTC date range. Parameters: Name Type Description Default date_from datetime | str Range start as datetime or ISO 8601 string. required date_to datetime | str Range end as datetime or ISO 8601 string. required Returns: Type Description tuple [ datetime , datetime ] Tuple of UTC-aware (start, end) datetimes. Raises: Type Description ValueError If date_from is after date_to . Source code in mt5cli/converters.py 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 def parse_date_range ( date_from : datetime | str , date_to : datetime | str , ) -> tuple [ datetime , datetime ]: \"\"\"Parse and validate an inclusive UTC date range. Args: date_from: Range start as datetime or ISO 8601 string. date_to: Range end as datetime or ISO 8601 string. Returns: Tuple of UTC-aware ``(start, end)`` datetimes. Raises: ValueError: If ``date_from`` is after ``date_to``. \"\"\" start = ensure_utc ( date_from ) end = ensure_utc ( date_to ) if start > end : msg = ( f \"date_from ( { start . isoformat () } ) must not be after \" f \"date_to ( { end . isoformat () } ).\" ) raise ValueError ( msg ) return start , end parse_datetime \u00b6 parse_datetime ( value : str ) -> datetime Parse an ISO 8601 datetime string to a timezone-aware datetime. Parameters: Name Type Description Default value str ISO 8601 datetime string (e.g., '2024-01-01' or '2024-01-01T12:00:00+00:00'). required Returns: Type Description datetime Parsed datetime with UTC timezone if no timezone is specified. Raises: Type Description ValueError If the string cannot be parsed. Source code in mt5cli/utils.py 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 def parse_datetime ( value : str ) -> datetime : \"\"\"Parse an ISO 8601 datetime string to a timezone-aware datetime. Args: value: ISO 8601 datetime string (e.g., '2024-01-01' or '2024-01-01T12:00:00+00:00'). Returns: Parsed datetime with UTC timezone if no timezone is specified. Raises: ValueError: If the string cannot be parsed. \"\"\" try : dt = datetime . fromisoformat ( value ) except ValueError : msg = f \"Invalid datetime format: ' { value } '. Use ISO 8601 format.\" raise ValueError ( msg ) from None if dt . tzinfo is None : dt = dt . replace ( tzinfo = UTC ) return dt parse_tick_flags \u00b6 parse_tick_flags ( value : object ) -> int Parse tick flags string or integer value. Parameters: Name Type Description Default value object Tick flag name (ALL, INFO, TRADE, COPY_TICKS_*) or integer value. required Returns: Type Description int Integer tick flag value compatible with MetaTrader 5 COPY_TICKS_* . Raises: Type Description ValueError If the flag is invalid. Source code in mt5cli/utils.py 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 def parse_tick_flags ( value : object ) -> int : \"\"\"Parse tick flags string or integer value. Args: value: Tick flag name (ALL, INFO, TRADE, COPY_TICKS_*) or integer value. Returns: Integer tick flag value compatible with MetaTrader 5 ``COPY_TICKS_*``. Raises: ValueError: If the flag is invalid. \"\"\" try : return _parse_copy_ticks ( value ) except ValueError : display = value if isinstance ( value , str ) else repr ( value ) valid = \", \" . join ( _TICK_FLAG_NAMES ) msg = ( f \"Invalid tick flags: ' { display } '. \" f \"Use one of: { valid } , or a supported integer.\" ) raise ValueError ( msg ) from None parse_timeframe \u00b6 parse_timeframe ( value : object ) -> int Parse a timeframe string or integer value. Parameters: Name Type Description Default value object Timeframe name (e.g., 'M1', 'H1', 'D1') or integer value. required Returns: Type Description int Integer timeframe value. Raises: Type Description ValueError If the timeframe is invalid. Source code in mt5cli/utils.py 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 def parse_timeframe ( value : object ) -> int : \"\"\"Parse a timeframe string or integer value. Args: value: Timeframe name (e.g., 'M1', 'H1', 'D1') or integer value. Returns: Integer timeframe value. Raises: ValueError: If the timeframe is invalid. \"\"\" try : return _parse_timeframe ( value ) except ValueError : display = value if isinstance ( value , str ) else repr ( value ) valid = \", \" . join ( TIMEFRAME_NAMES ) msg = ( f \"Invalid timeframe: ' { display } '. \" f \"Use one of: { valid } , or a supported integer.\" ) raise ValueError ( msg ) from None recent_window \u00b6 recent_window ( * , hours : float | None = None , seconds : float | None = None , date_to : datetime | str | None = None , ) -> tuple [ datetime , datetime ] Build a trailing UTC window ending at date_to or now. Exactly one of hours or seconds must be provided. Parameters: Name Type Description Default hours float | None Trailing window length in hours. None seconds float | None Trailing window length in seconds. None date_to datetime | str | None Window end. Defaults to current UTC time. None Returns: Type Description tuple [ datetime , datetime ] Tuple of UTC-aware (start, end) datetimes. Raises: Type Description ValueError If neither or both window lengths are provided, or if a length is not positive. Source code in mt5cli/converters.py 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 def recent_window ( * , hours : float | None = None , seconds : float | None = None , date_to : datetime | str | None = None , ) -> tuple [ datetime , datetime ]: \"\"\"Build a trailing UTC window ending at ``date_to`` or now. Exactly one of ``hours`` or ``seconds`` must be provided. Args: hours: Trailing window length in hours. seconds: Trailing window length in seconds. date_to: Window end. Defaults to current UTC time. Returns: Tuple of UTC-aware ``(start, end)`` datetimes. Raises: ValueError: If neither or both window lengths are provided, or if a length is not positive. \"\"\" if ( hours is None ) == ( seconds is None ): msg = \"Provide exactly one of hours or seconds.\" raise ValueError ( msg ) if hours is not None : length = timedelta ( hours = hours ) else : length = timedelta ( seconds = seconds if seconds is not None else 0 ) if length . total_seconds () <= 0 : msg = \"Window length must be positive.\" raise ValueError ( msg ) end = ensure_utc ( date_to ) if date_to is not None else datetime . now ( UTC ) return end - length , end","title":"Converters"},{"location":"api/converters/#converters","text":"","title":"Converters"},{"location":"api/converters/#mt5cli.converters","text":"Shared conversion helpers for MT5 symbols, timeframes, and date ranges.","title":"converters"},{"location":"api/converters/#mt5cli.converters.__all__","text":"__all__ = [ \"ensure_utc\" , \"granularity_name\" , \"normalize_symbol\" , \"normalize_symbols\" , \"parse_date_range\" , \"parse_datetime\" , \"parse_tick_flags\" , \"parse_timeframe\" , \"recent_window\" , ]","title":"__all__"},{"location":"api/converters/#mt5cli.converters.ensure_utc","text":"ensure_utc ( value : datetime | str ) -> datetime Return a timezone-aware UTC datetime. Parameters: Name Type Description Default value datetime | str Datetime instance or ISO 8601 string. required Returns: Type Description datetime UTC-aware datetime. Source code in mt5cli/converters.py 69 70 71 72 73 74 75 76 77 78 79 80 81 82 def ensure_utc ( value : datetime | str ) -> datetime : \"\"\"Return a timezone-aware UTC datetime. Args: value: Datetime instance or ISO 8601 string. Returns: UTC-aware datetime. \"\"\" if isinstance ( value , str ): return parse_datetime ( value ) if value . tzinfo is None : return value . replace ( tzinfo = UTC ) return value . astimezone ( UTC )","title":"ensure_utc"},{"location":"api/converters/#mt5cli.converters.granularity_name","text":"granularity_name ( timeframe : int | str ) -> str Return a short granularity label for a timeframe integer or name. Parameters: Name Type Description Default timeframe int | str MT5 timeframe as integer or name (for example M1 ). required Returns: Type Description str Short name such as M1 or the stringified integer when unknown. Source code in mt5cli/converters.py 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 def granularity_name ( timeframe : int | str ) -> str : \"\"\"Return a short granularity label for a timeframe integer or name. Args: timeframe: MT5 timeframe as integer or name (for example ``M1``). Returns: Short name such as ``M1`` or the stringified integer when unknown. \"\"\" tf = parse_timeframe ( timeframe ) try : name = _get_timeframe_name ( tf ) except ValueError : return str ( tf ) return name . removeprefix ( \"TIMEFRAME_\" )","title":"granularity_name"},{"location":"api/converters/#mt5cli.converters.normalize_symbol","text":"normalize_symbol ( symbol : str ) -> str Normalize a broker symbol name for MT5 API calls. Strips surrounding whitespace while preserving broker-specific casing and suffixes (for example XAUUSDm , US500.cash , or EURUSD.r ). Parameters: Name Type Description Default symbol str Raw symbol name. required Returns: Type Description str Normalized symbol string. Raises: Type Description ValueError If the symbol is empty after normalization. Source code in mt5cli/converters.py 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 def normalize_symbol ( symbol : str ) -> str : \"\"\"Normalize a broker symbol name for MT5 API calls. Strips surrounding whitespace while preserving broker-specific casing and suffixes (for example ``XAUUSDm``, ``US500.cash``, or ``EURUSD.r``). Args: symbol: Raw symbol name. Returns: Normalized symbol string. Raises: ValueError: If the symbol is empty after normalization. \"\"\" normalized = symbol . strip () if not normalized : msg = \"Symbol must not be empty.\" raise ValueError ( msg ) return normalized","title":"normalize_symbol"},{"location":"api/converters/#mt5cli.converters.normalize_symbols","text":"normalize_symbols ( symbols : Sequence [ str ]) -> list [ str ] Normalize a sequence of broker symbol names. Parameters: Name Type Description Default symbols Sequence [ str ] Raw symbol names. required Returns: Type Description list [ str ] List of normalized, de-duplicated symbols preserving first-seen order. Source code in mt5cli/converters.py 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 def normalize_symbols ( symbols : Sequence [ str ]) -> list [ str ]: \"\"\"Normalize a sequence of broker symbol names. Args: symbols: Raw symbol names. Returns: List of normalized, de-duplicated symbols preserving first-seen order. \"\"\" seen : set [ str ] = set () resolved : list [ str ] = [] for symbol in symbols : normalized = normalize_symbol ( symbol ) if normalized not in seen : seen . add ( normalized ) resolved . append ( normalized ) return resolved","title":"normalize_symbols"},{"location":"api/converters/#mt5cli.converters.parse_date_range","text":"parse_date_range ( date_from : datetime | str , date_to : datetime | str ) -> tuple [ datetime , datetime ] Parse and validate an inclusive UTC date range. Parameters: Name Type Description Default date_from datetime | str Range start as datetime or ISO 8601 string. required date_to datetime | str Range end as datetime or ISO 8601 string. required Returns: Type Description tuple [ datetime , datetime ] Tuple of UTC-aware (start, end) datetimes. Raises: Type Description ValueError If date_from is after date_to . Source code in mt5cli/converters.py 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 def parse_date_range ( date_from : datetime | str , date_to : datetime | str , ) -> tuple [ datetime , datetime ]: \"\"\"Parse and validate an inclusive UTC date range. Args: date_from: Range start as datetime or ISO 8601 string. date_to: Range end as datetime or ISO 8601 string. Returns: Tuple of UTC-aware ``(start, end)`` datetimes. Raises: ValueError: If ``date_from`` is after ``date_to``. \"\"\" start = ensure_utc ( date_from ) end = ensure_utc ( date_to ) if start > end : msg = ( f \"date_from ( { start . isoformat () } ) must not be after \" f \"date_to ( { end . isoformat () } ).\" ) raise ValueError ( msg ) return start , end","title":"parse_date_range"},{"location":"api/converters/#mt5cli.converters.parse_datetime","text":"parse_datetime ( value : str ) -> datetime Parse an ISO 8601 datetime string to a timezone-aware datetime. Parameters: Name Type Description Default value str ISO 8601 datetime string (e.g., '2024-01-01' or '2024-01-01T12:00:00+00:00'). required Returns: Type Description datetime Parsed datetime with UTC timezone if no timezone is specified. Raises: Type Description ValueError If the string cannot be parsed. Source code in mt5cli/utils.py 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 def parse_datetime ( value : str ) -> datetime : \"\"\"Parse an ISO 8601 datetime string to a timezone-aware datetime. Args: value: ISO 8601 datetime string (e.g., '2024-01-01' or '2024-01-01T12:00:00+00:00'). Returns: Parsed datetime with UTC timezone if no timezone is specified. Raises: ValueError: If the string cannot be parsed. \"\"\" try : dt = datetime . fromisoformat ( value ) except ValueError : msg = f \"Invalid datetime format: ' { value } '. Use ISO 8601 format.\" raise ValueError ( msg ) from None if dt . tzinfo is None : dt = dt . replace ( tzinfo = UTC ) return dt","title":"parse_datetime"},{"location":"api/converters/#mt5cli.converters.parse_tick_flags","text":"parse_tick_flags ( value : object ) -> int Parse tick flags string or integer value. Parameters: Name Type Description Default value object Tick flag name (ALL, INFO, TRADE, COPY_TICKS_*) or integer value. required Returns: Type Description int Integer tick flag value compatible with MetaTrader 5 COPY_TICKS_* . Raises: Type Description ValueError If the flag is invalid. Source code in mt5cli/utils.py 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 def parse_tick_flags ( value : object ) -> int : \"\"\"Parse tick flags string or integer value. Args: value: Tick flag name (ALL, INFO, TRADE, COPY_TICKS_*) or integer value. Returns: Integer tick flag value compatible with MetaTrader 5 ``COPY_TICKS_*``. Raises: ValueError: If the flag is invalid. \"\"\" try : return _parse_copy_ticks ( value ) except ValueError : display = value if isinstance ( value , str ) else repr ( value ) valid = \", \" . join ( _TICK_FLAG_NAMES ) msg = ( f \"Invalid tick flags: ' { display } '. \" f \"Use one of: { valid } , or a supported integer.\" ) raise ValueError ( msg ) from None","title":"parse_tick_flags"},{"location":"api/converters/#mt5cli.converters.parse_timeframe","text":"parse_timeframe ( value : object ) -> int Parse a timeframe string or integer value. Parameters: Name Type Description Default value object Timeframe name (e.g., 'M1', 'H1', 'D1') or integer value. required Returns: Type Description int Integer timeframe value. Raises: Type Description ValueError If the timeframe is invalid. Source code in mt5cli/utils.py 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 def parse_timeframe ( value : object ) -> int : \"\"\"Parse a timeframe string or integer value. Args: value: Timeframe name (e.g., 'M1', 'H1', 'D1') or integer value. Returns: Integer timeframe value. Raises: ValueError: If the timeframe is invalid. \"\"\" try : return _parse_timeframe ( value ) except ValueError : display = value if isinstance ( value , str ) else repr ( value ) valid = \", \" . join ( TIMEFRAME_NAMES ) msg = ( f \"Invalid timeframe: ' { display } '. \" f \"Use one of: { valid } , or a supported integer.\" ) raise ValueError ( msg ) from None","title":"parse_timeframe"},{"location":"api/converters/#mt5cli.converters.recent_window","text":"recent_window ( * , hours : float | None = None , seconds : float | None = None , date_to : datetime | str | None = None , ) -> tuple [ datetime , datetime ] Build a trailing UTC window ending at date_to or now. Exactly one of hours or seconds must be provided. Parameters: Name Type Description Default hours float | None Trailing window length in hours. None seconds float | None Trailing window length in seconds. None date_to datetime | str | None Window end. Defaults to current UTC time. None Returns: Type Description tuple [ datetime , datetime ] Tuple of UTC-aware (start, end) datetimes. Raises: Type Description ValueError If neither or both window lengths are provided, or if a length is not positive. Source code in mt5cli/converters.py 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 def recent_window ( * , hours : float | None = None , seconds : float | None = None , date_to : datetime | str | None = None , ) -> tuple [ datetime , datetime ]: \"\"\"Build a trailing UTC window ending at ``date_to`` or now. Exactly one of ``hours`` or ``seconds`` must be provided. Args: hours: Trailing window length in hours. seconds: Trailing window length in seconds. date_to: Window end. Defaults to current UTC time. Returns: Tuple of UTC-aware ``(start, end)`` datetimes. Raises: ValueError: If neither or both window lengths are provided, or if a length is not positive. \"\"\" if ( hours is None ) == ( seconds is None ): msg = \"Provide exactly one of hours or seconds.\" raise ValueError ( msg ) if hours is not None : length = timedelta ( hours = hours ) else : length = timedelta ( seconds = seconds if seconds is not None else 0 ) if length . total_seconds () <= 0 : msg = \"Window length must be positive.\" raise ValueError ( msg ) end = ensure_utc ( date_to ) if date_to is not None else datetime . now ( UTC ) return end - length , end","title":"recent_window"},{"location":"api/exceptions/","text":"Exceptions \u00b6 mt5cli.exceptions \u00b6 Normalized exception types for MT5 and mt5cli operations. T module-attribute \u00b6 T = TypeVar ( 'T' ) __all__ module-attribute \u00b6 __all__ = [ \"Mt5CliError\" , \"Mt5ConnectionError\" , \"Mt5OperationError\" , \"Mt5SchemaError\" , \"call_with_normalized_errors\" , \"is_recoverable_mt5_error\" , \"normalize_mt5_exception\" , ] Mt5CliError \u00b6 Bases: Exception Base exception for mt5cli public API errors. Mt5ConnectionError \u00b6 Bases: Mt5CliError Raised when MT5 initialization, login, or shutdown fails. Mt5OperationError \u00b6 Bases: Mt5CliError Raised when an MT5 data or trading operation fails. Mt5SchemaError \u00b6 Bases: Mt5CliError Raised when a DataFrame does not match an expected dataset schema. call_with_normalized_errors \u00b6 call_with_normalized_errors ( fn : Callable [[], T ]) -> T Run fn and map recoverable MT5 errors to mt5cli types. Parameters: Name Type Description Default fn Callable [[], T ] Callable performing MT5 work. required Returns: Type Description T Value returned by fn . Source code in mt5cli/exceptions.py 82 83 84 85 86 87 88 89 90 91 92 93 94 95 def call_with_normalized_errors ( fn : Callable [[], T ]) -> T : \"\"\"Run ``fn`` and map recoverable MT5 errors to mt5cli types. Args: fn: Callable performing MT5 work. Returns: Value returned by ``fn``. \"\"\" try : return fn () except _RECOVERABLE_MT5_ERRORS as exc : normalized = normalize_mt5_exception ( exc ) raise normalized from exc is_recoverable_mt5_error \u00b6 is_recoverable_mt5_error ( exc : BaseException ) -> bool Return whether an exception is a transient MT5 failure worth retrying. Parameters: Name Type Description Default exc BaseException Exception raised by MT5 or pdmt5. required Returns: Type Description bool True for Mt5RuntimeError and Mt5TradingError (if available). Source code in mt5cli/exceptions.py 51 52 53 54 55 56 57 58 59 60 def is_recoverable_mt5_error ( exc : BaseException ) -> bool : \"\"\"Return whether an exception is a transient MT5 failure worth retrying. Args: exc: Exception raised by MT5 or pdmt5. Returns: True for ``Mt5RuntimeError`` and ``Mt5TradingError`` (if available). \"\"\" return isinstance ( exc , _RECOVERABLE_MT5_ERRORS ) normalize_mt5_exception \u00b6 normalize_mt5_exception ( exc : BaseException ) -> Mt5CliError Map pdmt5/MT5 exceptions to stable mt5cli exception types. Parameters: Name Type Description Default exc BaseException Original exception from MT5 or pdmt5. required Returns: Type Description Mt5CliError Mt5ConnectionError for runtime failures, Mt5OperationError for Mt5CliError trading failures, or the original exception when it is not recognized. Source code in mt5cli/exceptions.py 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 def normalize_mt5_exception ( exc : BaseException ) -> Mt5CliError : \"\"\"Map pdmt5/MT5 exceptions to stable mt5cli exception types. Args: exc: Original exception from MT5 or pdmt5. Returns: ``Mt5ConnectionError`` for runtime failures, ``Mt5OperationError`` for trading failures, or the original exception when it is not recognized. \"\"\" if Mt5TradingError is not None and isinstance ( exc , Mt5TradingError ): return Mt5OperationError ( str ( exc )) if isinstance ( exc , Mt5RuntimeError ): return Mt5ConnectionError ( str ( exc )) if isinstance ( exc , Mt5CliError ): return exc return Mt5CliError ( str ( exc ))","title":"Exceptions"},{"location":"api/exceptions/#exceptions","text":"","title":"Exceptions"},{"location":"api/exceptions/#mt5cli.exceptions","text":"Normalized exception types for MT5 and mt5cli operations.","title":"exceptions"},{"location":"api/exceptions/#mt5cli.exceptions.T","text":"T = TypeVar ( 'T' )","title":"T"},{"location":"api/exceptions/#mt5cli.exceptions.__all__","text":"__all__ = [ \"Mt5CliError\" , \"Mt5ConnectionError\" , \"Mt5OperationError\" , \"Mt5SchemaError\" , \"call_with_normalized_errors\" , \"is_recoverable_mt5_error\" , \"normalize_mt5_exception\" , ]","title":"__all__"},{"location":"api/exceptions/#mt5cli.exceptions.Mt5CliError","text":"Bases: Exception Base exception for mt5cli public API errors.","title":"Mt5CliError"},{"location":"api/exceptions/#mt5cli.exceptions.Mt5ConnectionError","text":"Bases: Mt5CliError Raised when MT5 initialization, login, or shutdown fails.","title":"Mt5ConnectionError"},{"location":"api/exceptions/#mt5cli.exceptions.Mt5OperationError","text":"Bases: Mt5CliError Raised when an MT5 data or trading operation fails.","title":"Mt5OperationError"},{"location":"api/exceptions/#mt5cli.exceptions.Mt5SchemaError","text":"Bases: Mt5CliError Raised when a DataFrame does not match an expected dataset schema.","title":"Mt5SchemaError"},{"location":"api/exceptions/#mt5cli.exceptions.call_with_normalized_errors","text":"call_with_normalized_errors ( fn : Callable [[], T ]) -> T Run fn and map recoverable MT5 errors to mt5cli types. Parameters: Name Type Description Default fn Callable [[], T ] Callable performing MT5 work. required Returns: Type Description T Value returned by fn . Source code in mt5cli/exceptions.py 82 83 84 85 86 87 88 89 90 91 92 93 94 95 def call_with_normalized_errors ( fn : Callable [[], T ]) -> T : \"\"\"Run ``fn`` and map recoverable MT5 errors to mt5cli types. Args: fn: Callable performing MT5 work. Returns: Value returned by ``fn``. \"\"\" try : return fn () except _RECOVERABLE_MT5_ERRORS as exc : normalized = normalize_mt5_exception ( exc ) raise normalized from exc","title":"call_with_normalized_errors"},{"location":"api/exceptions/#mt5cli.exceptions.is_recoverable_mt5_error","text":"is_recoverable_mt5_error ( exc : BaseException ) -> bool Return whether an exception is a transient MT5 failure worth retrying. Parameters: Name Type Description Default exc BaseException Exception raised by MT5 or pdmt5. required Returns: Type Description bool True for Mt5RuntimeError and Mt5TradingError (if available). Source code in mt5cli/exceptions.py 51 52 53 54 55 56 57 58 59 60 def is_recoverable_mt5_error ( exc : BaseException ) -> bool : \"\"\"Return whether an exception is a transient MT5 failure worth retrying. Args: exc: Exception raised by MT5 or pdmt5. Returns: True for ``Mt5RuntimeError`` and ``Mt5TradingError`` (if available). \"\"\" return isinstance ( exc , _RECOVERABLE_MT5_ERRORS )","title":"is_recoverable_mt5_error"},{"location":"api/exceptions/#mt5cli.exceptions.normalize_mt5_exception","text":"normalize_mt5_exception ( exc : BaseException ) -> Mt5CliError Map pdmt5/MT5 exceptions to stable mt5cli exception types. Parameters: Name Type Description Default exc BaseException Original exception from MT5 or pdmt5. required Returns: Type Description Mt5CliError Mt5ConnectionError for runtime failures, Mt5OperationError for Mt5CliError trading failures, or the original exception when it is not recognized. Source code in mt5cli/exceptions.py 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 def normalize_mt5_exception ( exc : BaseException ) -> Mt5CliError : \"\"\"Map pdmt5/MT5 exceptions to stable mt5cli exception types. Args: exc: Original exception from MT5 or pdmt5. Returns: ``Mt5ConnectionError`` for runtime failures, ``Mt5OperationError`` for trading failures, or the original exception when it is not recognized. \"\"\" if Mt5TradingError is not None and isinstance ( exc , Mt5TradingError ): return Mt5OperationError ( str ( exc )) if isinstance ( exc , Mt5RuntimeError ): return Mt5ConnectionError ( str ( exc )) if isinstance ( exc , Mt5CliError ): return exc return Mt5CliError ( str ( exc ))","title":"normalize_mt5_exception"},{"location":"api/history/","text":"History Collection (SQLite) \u00b6 mt5cli.history \u00b6 SQLite storage helpers for the collect-history incremental data pipeline. DEFAULT_HISTORY_DATASETS module-attribute \u00b6 DEFAULT_HISTORY_DATASETS : frozenset [ Dataset ] = frozenset ({ rates , history_orders , history_deals , }) DEFAULT_HISTORY_TIMEFRAMES module-attribute \u00b6 DEFAULT_HISTORY_TIMEFRAMES : tuple [ str , ... ] = ( TIMEFRAME_NAMES ) SqliteConnOrPath module-attribute \u00b6 SqliteConnOrPath = Connection | Path | str logger module-attribute \u00b6 logger = getLogger ( __name__ ) DedupScope dataclass \u00b6 DedupScope ( where : str , params : tuple [ object , ... ], required_columns : frozenset [ str ], ) Scoped deduplication predicate and the columns it references. Attributes: Name Type Description where str SQL predicate appended to the duplicate-removal query. params tuple [ object , ...] Parameters bound to the scope predicate. required_columns frozenset [ str ] Columns that must be present in the written table for the scope to run. params instance-attribute \u00b6 params : tuple [ object , ... ] required_columns instance-attribute \u00b6 required_columns : frozenset [ str ] where instance-attribute \u00b6 where : str RateTarget dataclass \u00b6 RateTarget ( symbol : str | None , timeframe : int | str ) A single rate series identified by symbol and timeframe. Attributes: Name Type Description symbol str | None MT5 symbol name, or None when the rate series is addressed only by an explicit table (for example a custom SQLite view). timeframe int | str MT5 timeframe as an integer or name (for example M1 ). symbol instance-attribute \u00b6 symbol : str | None timeframe instance-attribute \u00b6 timeframe : int | str timeframe_int property \u00b6 timeframe_int : int Return the timeframe as its integer MT5 value. __post_init__ \u00b6 __post_init__ () -> None Normalize accepted timeframe aliases to the stored integer value. Source code in mt5cli/history.py 550 551 552 553 def __post_init__ ( self ) -> None : \"\"\"Normalize accepted timeframe aliases to the stored integer value.\"\"\" if not isinstance ( self . timeframe , int ): object . __setattr__ ( self , \"timeframe\" , parse_timeframe ( self . timeframe )) append_dataframe \u00b6 append_dataframe ( conn : Connection , frame : DataFrame , table_name : str , if_exists : IfExists , ) -> bool Append a DataFrame to SQLite when it has a schema. Returns: Type Description bool True if a table was written, False if the frame had no columns. Source code in mt5cli/history.py 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 def append_dataframe ( conn : sqlite3 . Connection , frame : pd . DataFrame , table_name : str , if_exists : IfExists , ) -> bool : \"\"\"Append a DataFrame to SQLite when it has a schema. Returns: True if a table was written, False if the frame had no columns. \"\"\" if len ( frame . columns ) == 0 : logger . warning ( \"Skipping %s : dataset returned no columns\" , table_name ) return False frame . to_sql ( # type: ignore[reportUnknownMemberType] table_name , conn , if_exists = if_exists . value , index = False , chunksize = 50_000 , ) return True augment_written_columns_from_sqlite \u00b6 augment_written_columns_from_sqlite ( conn : Connection , datasets : set [ Dataset ], written_columns : dict [ Dataset , set [ str ]], ) -> None Add existing table columns to the written column map. Source code in mt5cli/history.py 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 def augment_written_columns_from_sqlite ( conn : sqlite3 . Connection , datasets : set [ Dataset ], written_columns : dict [ Dataset , set [ str ]], ) -> None : \"\"\"Add existing table columns to the written column map.\"\"\" for dataset in datasets : columns = get_table_columns ( conn , dataset . table_name ) if not columns : continue if dataset in written_columns : written_columns [ dataset ] . update ( columns ) else : written_columns [ dataset ] = columns build_rate_targets \u00b6 build_rate_targets ( symbols : Sequence [ str ], timeframes : Sequence [ int | str ], * , allow_missing_symbol : bool = False , ) -> list [ RateTarget ] Build rate targets for every symbol and timeframe combination. Parameters: Name Type Description Default symbols Sequence [ str ] MT5 symbol names. May be empty when allow_missing_symbol . required timeframes Sequence [ int | str ] MT5 timeframes as integers or names (for example M1 ). required allow_missing_symbol bool When True and symbols is empty, build targets with symbol=None for each timeframe instead of raising. False Returns: Type Description list [ RateTarget ] Targets in row-major order: every timeframe for the first symbol, then list [ RateTarget ] every timeframe for the next symbol, and so on. Raises: Type Description ValueError If timeframes is empty, or symbols is empty and allow_missing_symbol is False. Source code in mt5cli/history.py 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 def build_rate_targets ( symbols : Sequence [ str ], timeframes : Sequence [ int | str ], * , allow_missing_symbol : bool = False , ) -> list [ RateTarget ]: \"\"\"Build rate targets for every symbol and timeframe combination. Args: symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``. timeframes: MT5 timeframes as integers or names (for example ``M1``). allow_missing_symbol: When True and ``symbols`` is empty, build targets with ``symbol=None`` for each timeframe instead of raising. Returns: Targets in row-major order: every timeframe for the first symbol, then every timeframe for the next symbol, and so on. Raises: ValueError: If ``timeframes`` is empty, or ``symbols`` is empty and ``allow_missing_symbol`` is False. \"\"\" if not timeframes : msg = \"At least one timeframe is required.\" raise ValueError ( msg ) if not symbols : if not allow_missing_symbol : msg = \"At least one symbol is required.\" raise ValueError ( msg ) return [ RateTarget ( symbol = None , timeframe = tf ) for tf in timeframes ] return [ RateTarget ( symbol = symbol , timeframe = tf ) for symbol in symbols for tf in timeframes ] build_rate_view_name \u00b6 build_rate_view_name ( * , symbol : str , granularity : str , granularity_count : int , timeframe : int , ) -> str Return a collision-free offline optimize view name. View names always include the timeframe integer after a __ separator so a symbol such as EURUSD_M1 cannot collide with EURUSD at timeframe M1 . Source code in mt5cli/history.py 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 def build_rate_view_name ( * , symbol : str , granularity : str , granularity_count : int , timeframe : int , ) -> str : \"\"\"Return a collision-free offline optimize view name. View names always include the timeframe integer after a ``__`` separator so a symbol such as ``EURUSD_M1`` cannot collide with ``EURUSD`` at timeframe ``M1``. \"\"\" if granularity_count == 1 : return f \"rate_ { symbol } __ { timeframe } \" return f \"rate_ { symbol } __ { granularity } _ { timeframe } \" create_cash_events_view \u00b6 create_cash_events_view ( conn : Connection , deals_columns : set [ str ] ) -> bool Create the cash_events SQLite view derived from history_deals. Returns: Type Description bool True if the view was created, False if required columns are missing. Source code in mt5cli/history.py 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 def create_cash_events_view ( conn : sqlite3 . Connection , deals_columns : set [ str ], ) -> bool : \"\"\"Create the cash_events SQLite view derived from history_deals. Returns: True if the view was created, False if required columns are missing. \"\"\" if \"type\" not in deals_columns : logger . warning ( \"Skipping cash_events view: history_deals.type is missing\" ) return False conn . execute ( \"DROP VIEW IF EXISTS cash_events\" ) conn . execute ( \"CREATE VIEW cash_events AS\" # noqa: S608 f \" SELECT * FROM history_deals WHERE type NOT IN { _TRADE_DEAL_TYPES_SQL } \" , ) return True create_history_indexes \u00b6 create_history_indexes ( conn : Connection , written_columns : dict [ Dataset , set [ str ]], ) -> None Create useful indexes for collected history tables when present. Source code in mt5cli/history.py 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 def create_history_indexes ( conn : sqlite3 . Connection , written_columns : dict [ Dataset , set [ str ]], ) -> None : \"\"\"Create useful indexes for collected history tables when present.\"\"\" if { \"symbol\" , \"timeframe\" , \"time\" } . issubset ( written_columns . get ( Dataset . rates , set ()), ): conn . execute ( \"CREATE INDEX IF NOT EXISTS idx_rates_symbol_timeframe_time\" \" ON rates(symbol, timeframe, time)\" , ) if { \"symbol\" , \"time\" } . issubset ( written_columns . get ( Dataset . ticks , set ())): conn . execute ( \"CREATE INDEX IF NOT EXISTS idx_ticks_symbol_time ON ticks(symbol, time)\" , ) if { \"position_id\" , \"symbol\" } . issubset ( written_columns . get ( Dataset . history_deals , set ()), ): conn . execute ( \"CREATE INDEX IF NOT EXISTS idx_history_deals_position_symbol\" \" ON history_deals(position_id, symbol)\" , ) create_positions_reconstructed_view \u00b6 create_positions_reconstructed_view ( conn : Connection , deals_columns : set [ str ] ) -> bool Create the positions_reconstructed SQLite view derived from history_deals. Returns: Type Description bool True if the view was created, False if required columns are missing. Source code in mt5cli/history.py 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 def create_positions_reconstructed_view ( conn : sqlite3 . Connection , deals_columns : set [ str ], ) -> bool : \"\"\"Create the positions_reconstructed SQLite view derived from history_deals. Returns: True if the view was created, False if required columns are missing. \"\"\" if not _POSITIONS_VIEW_REQUIRED_COLUMNS . issubset ( deals_columns ): missing = \", \" . join ( sorted ( _POSITIONS_VIEW_REQUIRED_COLUMNS - deals_columns )) logger . warning ( \"Skipping positions_reconstructed view: history_deals missing columns: %s \" , missing , ) return False conn . execute ( \"DROP VIEW IF EXISTS positions_reconstructed\" ) conn . execute ( \"CREATE VIEW positions_reconstructed AS\" # noqa: S608 \" SELECT\" \" position_id,\" \" symbol,\" \" MIN(CASE WHEN entry = 0 THEN time END) AS open_time,\" \" MAX(CASE WHEN entry IN (1, 2, 3) THEN time END) AS close_time,\" \" MIN(CASE WHEN entry = 0 THEN type END) AS direction,\" \" SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) AS volume_open,\" \" SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) AS volume_close,\" \" SUM(CASE WHEN entry = 2 THEN volume ELSE 0 END) AS volume_reversal,\" \" CASE\" \" WHEN SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) > 0\" \" THEN SUM(CASE WHEN entry = 0 THEN price * volume ELSE 0 END)\" \" / SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END)\" \" END AS open_price,\" \" CASE\" \" WHEN SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) > 0\" \" THEN SUM(CASE WHEN entry IN (1, 2, 3) THEN price * volume ELSE 0 END)\" \" / SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END)\" \" END AS close_price,\" \" SUM(profit) AS total_profit,\" \" SUM(CASE WHEN entry = 2 THEN 1 ELSE 0 END) AS reversal_count,\" \" COUNT(*) AS deals_count\" \" FROM history_deals\" f \" WHERE type IN { _TRADE_DEAL_TYPES_SQL } AND position_id != 0\" \" GROUP BY position_id, symbol\" \" HAVING SUM(CASE WHEN entry IN (1, 2, 3) THEN 1 ELSE 0 END) > 0\" , ) return True create_rate_compatibility_views \u00b6 create_rate_compatibility_views ( conn : Connection ) -> None Create rate compatibility views from the normalized rates table. Source code in mt5cli/history.py 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 def create_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None : \"\"\"Create rate compatibility views from the normalized rates table.\"\"\" columns = get_table_columns ( conn , Dataset . rates . table_name ) if not { \"symbol\" , \"timeframe\" , \"time\" } . issubset ( columns ): return drop_rate_compatibility_views ( conn ) select_columns = sorted ( columns - { \"symbol\" , \"timeframe\" }) quoted_columns = \", \" . join ( f '\" { column } \"' for column in select_columns ) rows = conn . execute ( \"SELECT DISTINCT symbol, timeframe FROM rates ORDER BY symbol, timeframe\" , ) . fetchall () timeframes_by_symbol : dict [ str , list [ int ]] = {} for symbol , timeframe in rows : timeframes_by_symbol . setdefault ( str ( symbol ), []) . append ( int ( timeframe )) for symbol , timeframes in timeframes_by_symbol . items (): for timeframe in timeframes : granularity = resolve_granularity_name ( timeframe ) view_name = build_rate_view_name ( symbol = symbol , granularity = granularity , granularity_count = len ( timeframes ), timeframe = timeframe , ) quoted_view_name = quote_sqlite_identifier ( view_name ) escaped_symbol = symbol . replace ( \"'\" , \"''\" ) conn . execute ( f \"CREATE VIEW { quoted_view_name } AS\" # noqa: S608 f \" SELECT { quoted_columns } FROM rates\" f \" WHERE symbol = ' { escaped_symbol } '\" f \" AND timeframe = { timeframe } \" , ) deduplicate_history_tables \u00b6 deduplicate_history_tables ( conn : Connection , written_columns : dict [ Dataset , set [ str ]], written_tables : set [ Dataset ], dedup_scopes : Mapping [ Dataset , Sequence [ DedupScope ]] | None = None , ) -> None Deduplicate appended history tables by stable identifiers. Scopes whose required columns are not present in the written table are skipped. If all scopes for a dataset are skipped, the table receives one unscoped deduplication pass instead. Source code in mt5cli/history.py 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 def deduplicate_history_tables ( conn : sqlite3 . Connection , written_columns : dict [ Dataset , set [ str ]], written_tables : set [ Dataset ], dedup_scopes : Mapping [ Dataset , Sequence [ DedupScope ]] | None = None , ) -> None : \"\"\"Deduplicate appended history tables by stable identifiers. Scopes whose required columns are not present in the written table are skipped. If all scopes for a dataset are skipped, the table receives one unscoped deduplication pass instead. \"\"\" cursor = conn . cursor () for dataset in written_tables : columns = written_columns . get ( dataset , set ()) table = dataset . table_name keys = next ( ( candidate for candidate in _HISTORY_DEDUP_KEYS [ dataset ] if set ( candidate ) . issubset ( columns ) ), None , ) if keys is None : logger . warning ( \"Skipping %s deduplication: no supported key columns\" , table , ) continue raw_scopes : Sequence [ DedupScope ] = ( dedup_scopes . get ( dataset , ()) if dedup_scopes else () ) scopes = [ scope for scope in raw_scopes if scope . required_columns <= columns ] if scopes : for scope in scopes : drop_duplicates_in_table ( cursor , table , list ( keys ), keep = \"last\" , scope_where = scope . where , scope_params = scope . params , ) continue drop_duplicates_in_table ( cursor , table , list ( keys ), keep = \"last\" ) drop_duplicates_in_table \u00b6 drop_duplicates_in_table ( cursor : Cursor , table : str , ids : list [ str ], * , keep : Literal [ \"first\" , \"last\" ] = \"last\" , scope_where : str | None = None , scope_params : tuple [ object , ... ] = (), ) -> None Remove duplicate rows, keeping the first or last ROWID per key group. Raises: Type Description ValueError If the table or column names are invalid. Source code in mt5cli/history.py 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 def drop_duplicates_in_table ( cursor : sqlite3 . Cursor , table : str , ids : list [ str ], * , keep : Literal [ \"first\" , \"last\" ] = \"last\" , scope_where : str | None = None , scope_params : tuple [ object , ... ] = (), ) -> None : \"\"\"Remove duplicate rows, keeping the first or last ROWID per key group. Raises: ValueError: If the table or column names are invalid. \"\"\" if not table . isidentifier (): msg = f \"Invalid table name: { table } \" raise ValueError ( msg ) if invalid := { column for column in ids if not column . isidentifier ()}: msg = f \"Invalid column names: { ', ' . join ( sorted ( invalid )) } \" raise ValueError ( msg ) ids_csv = \", \" . join ( f '\" { column } \"' for column in ids ) rowid_selector = \"MIN\" if keep == \"first\" else \"MAX\" if scope_where : delete_sql = ( f \"DELETE FROM { table } WHERE { scope_where } AND ROWID NOT IN\" # noqa: S608 f \" (SELECT { rowid_selector } (ROWID) FROM { table } WHERE { scope_where } \" f \" GROUP BY { ids_csv } )\" ) cursor . execute ( delete_sql , scope_params + scope_params ) return cursor . execute ( f \"DELETE FROM { table } WHERE ROWID NOT IN\" # noqa: S608 f \" (SELECT { rowid_selector } (ROWID) FROM { table } GROUP BY { ids_csv } )\" , ) drop_forming_rate_bar \u00b6 drop_forming_rate_bar ( df_rate : DataFrame ) -> DataFrame Return closed bars from chronologically ordered MT5 rate data. MetaTrader 5 copy_rates_from_pos(start_pos=0) includes the still-forming current bar as the last row. Slice it off so downstream logic only sees completed bars. Empty frames and single-row frames return empty results. Parameters: Name Type Description Default df_rate DataFrame Rate data ordered oldest-to-newest with the forming bar last. required Returns: Type Description DataFrame A new DataFrame with all rows except the last. Index and columns are DataFrame preserved. The input frame is not modified. Source code in mt5cli/history.py 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 def drop_forming_rate_bar ( df_rate : pd . DataFrame ) -> pd . DataFrame : \"\"\"Return closed bars from chronologically ordered MT5 rate data. MetaTrader 5 ``copy_rates_from_pos(start_pos=0)`` includes the still-forming current bar as the last row. Slice it off so downstream logic only sees completed bars. Empty frames and single-row frames return empty results. Args: df_rate: Rate data ordered oldest-to-newest with the forming bar last. Returns: A new DataFrame with all rows except the last. Index and columns are preserved. The input frame is not modified. \"\"\" return df_rate . iloc [: - 1 ] . copy () drop_rate_compatibility_views \u00b6 drop_rate_compatibility_views ( conn : Connection ) -> None Drop all mt5cli-managed rate_* compatibility views. Source code in mt5cli/history.py 1361 1362 1363 1364 1365 1366 1367 1368 def drop_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None : \"\"\"Drop all mt5cli-managed ``rate_*`` compatibility views.\"\"\" rows = conn . execute ( \"SELECT name FROM sqlite_master WHERE type = 'view' AND name GLOB 'rate_*'\" , ) . fetchall () for ( view_name ,) in rows : quoted_view_name = quote_sqlite_identifier ( str ( view_name )) conn . execute ( f \"DROP VIEW IF EXISTS { quoted_view_name } \" ) filter_incremental_history_deals_frame \u00b6 filter_incremental_history_deals_frame ( frame : DataFrame , symbols : Sequence [ str ], start_by_symbol : dict [ str , datetime ], account_event_start : datetime , ) -> DataFrame Filter incrementally fetched history_deals by symbol and event start times. Returns: Type Description DataFrame Rows for selected symbols at or after each symbol start, plus account DataFrame events at or after account_event_start . Source code in mt5cli/history.py 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 def filter_incremental_history_deals_frame ( frame : pd . DataFrame , symbols : Sequence [ str ], start_by_symbol : dict [ str , datetime ], account_event_start : datetime , ) -> pd . DataFrame : \"\"\"Filter incrementally fetched history_deals by symbol and event start times. Returns: Rows for selected symbols at or after each symbol start, plus account events at or after ``account_event_start``. \"\"\" if frame . empty : return frame . copy () parsed_times = _frame_parsed_times ( frame ) time_valid = parsed_times . notna () account_event_mask = _history_deals_account_event_mask ( frame ) account_keep = account_event_mask & ( parsed_times >= account_event_start ) trade_keep = pd . Series ( data = False , index = frame . index ) if \"symbol\" in frame . columns : for symbol in symbols : trade_keep |= ( ( frame [ \"symbol\" ] == symbol ) & ( parsed_times >= start_by_symbol [ symbol ]) & ~ account_event_mask ) keep = ( account_keep | trade_keep ) & time_valid return frame . loc [ keep ] . copy () filter_trade_history_frame \u00b6 filter_trade_history_frame ( frame : DataFrame , symbols : Sequence [ str ], * , include_account_events : bool , ) -> DataFrame Filter trade history rows to selected symbols and account events. Returns: Type Description DataFrame Filtered history rows. Source code in mt5cli/history.py 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 def filter_trade_history_frame ( frame : pd . DataFrame , symbols : Sequence [ str ], * , include_account_events : bool , ) -> pd . DataFrame : \"\"\"Filter trade history rows to selected symbols and account events. Returns: Filtered history rows. \"\"\" if \"symbol\" not in frame . columns : return frame symbol_mask = frame [ \"symbol\" ] . isin ( symbols ) if not include_account_events : return frame . loc [ symbol_mask ] . copy () account_event_mask = _history_deals_account_event_mask ( frame ) return frame . loc [ symbol_mask | account_event_mask ] . copy () get_history_deals_account_event_start_datetime \u00b6 get_history_deals_account_event_start_datetime ( conn : Connection , * , fallback_start : datetime ) -> datetime Return the next update start for account-level history_deals rows. Source code in mt5cli/history.py 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 def get_history_deals_account_event_start_datetime ( conn : sqlite3 . Connection , * , fallback_start : datetime , ) -> datetime : \"\"\"Return the next update start for account-level history_deals rows.\"\"\" table = Dataset . history_deals . table_name columns = get_table_columns ( conn , table ) if \"time\" not in columns : return fallback_start if \"type\" in columns : where_clause = f \"type NOT IN { _TRADE_DEAL_TYPES_SQL } \" elif \"symbol\" in columns : where_clause = \"symbol IS NULL OR symbol = ''\" else : return fallback_start row = conn . execute ( f \"SELECT MAX(time) FROM { table } WHERE { where_clause } \" , # noqa: S608 ) . fetchone () parsed = parse_sqlite_timestamp ( row [ 0 ] if row else None ) return parsed if parsed is not None else fallback_start get_incremental_start_datetime \u00b6 get_incremental_start_datetime ( conn : Connection , dataset : Dataset , * , symbol : str , timeframe : int | None , fallback_start : datetime , ) -> datetime Return the next update start datetime from existing MAX(time). Source code in mt5cli/history.py 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 def get_incremental_start_datetime ( conn : sqlite3 . Connection , dataset : Dataset , * , symbol : str , timeframe : int | None , fallback_start : datetime , ) -> datetime : \"\"\"Return the next update start datetime from existing MAX(time).\"\"\" timeframes = [ timeframe ] if timeframe is not None else None starts = load_incremental_start_datetimes ( conn , dataset , symbols = [ symbol ], timeframes = timeframes , fallback_start = fallback_start , ) return starts [ symbol , timeframe ] get_table_columns \u00b6 get_table_columns ( conn : Connection , table : str ) -> set [ str ] Return existing SQLite columns for a table. Source code in mt5cli/history.py 844 845 846 847 848 def get_table_columns ( conn : sqlite3 . Connection , table : str ) -> set [ str ]: \"\"\"Return existing SQLite columns for a table.\"\"\" quoted_table = quote_sqlite_identifier ( table ) rows = conn . execute ( f \"PRAGMA table_info( { quoted_table } )\" ) . fetchall () return { str ( row [ 1 ]) for row in rows } load_incremental_start_datetimes \u00b6 load_incremental_start_datetimes ( conn : Connection , dataset : Dataset , * , symbols : Sequence [ str ], timeframes : Sequence [ int ] | None = None , fallback_start : datetime , ) -> dict [ tuple [ str , int | None ], datetime ] Return next update start datetimes keyed by symbol and optional timeframe. Source code in mt5cli/history.py 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 def load_incremental_start_datetimes ( conn : sqlite3 . Connection , dataset : Dataset , * , symbols : Sequence [ str ], timeframes : Sequence [ int ] | None = None , fallback_start : datetime , ) -> dict [ tuple [ str , int | None ], datetime ]: \"\"\"Return next update start datetimes keyed by symbol and optional timeframe.\"\"\" table = dataset . table_name columns = get_table_columns ( conn , table ) if dataset is Dataset . rates and columns : _validate_rates_schema ( columns ) if \"time\" not in columns : if dataset is Dataset . rates and timeframes is not None : return { ( symbol , timeframe ): fallback_start for symbol in symbols for timeframe in timeframes } return {( symbol , None ): fallback_start for symbol in symbols } parsed_by_key : dict [ tuple [ str , int | None ], datetime ] = {} if ( dataset is Dataset . rates and timeframes is not None and { \"symbol\" , \"timeframe\" } . issubset ( columns ) ): symbol_placeholders = \", \" . join ( \"?\" for _ in symbols ) timeframe_placeholders = \", \" . join ( \"?\" for _ in timeframes ) grouped_rates_query = ( \"SELECT symbol, timeframe, MAX(time) FROM \" # noqa: S608 f \" { table } WHERE symbol IN ( { symbol_placeholders } )\" f \" AND timeframe IN ( { timeframe_placeholders } )\" \" GROUP BY symbol, timeframe\" ) rows = conn . execute ( grouped_rates_query , [ * symbols , * timeframes ], ) . fetchall () for row_symbol , row_timeframe , max_time in rows : parsed = parse_sqlite_timestamp ( max_time ) if parsed is not None : parsed_by_key [ str ( row_symbol ), int ( row_timeframe )] = parsed return { ( symbol , timeframe ): parsed_by_key . get ( ( symbol , timeframe ), fallback_start , ) for symbol in symbols for timeframe in timeframes } if \"symbol\" in columns : symbol_placeholders = \", \" . join ( \"?\" for _ in symbols ) rows = conn . execute ( f \"SELECT symbol, MAX(time) FROM { table } \" # noqa: S608 f \" WHERE symbol IN ( { symbol_placeholders } ) GROUP BY symbol\" , list ( symbols ), ) . fetchall () for row_symbol , max_time in rows : parsed = parse_sqlite_timestamp ( max_time ) if parsed is not None : parsed_by_key [ str ( row_symbol ), None ] = parsed return { ( symbol , None ): parsed_by_key . get (( symbol , None ), fallback_start ) for symbol in symbols } row = conn . execute ( f \"SELECT MAX(time) FROM { table } \" ) . fetchone () # noqa: S608 parsed = parse_sqlite_timestamp ( row [ 0 ] if row else None ) shared_start = parsed if parsed is not None else fallback_start return {( symbol , None ): shared_start for symbol in symbols } load_rate_data \u00b6 load_rate_data ( conn_or_path : SqliteConnOrPath , table : str , count : int | None = None , ) -> DataFrame Load rate-like data from a SQLite database path or connection. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath SQLite database path or open connection. required table str Source table or view name. required count int | None Optional number of most recent rows to load. None Returns: Type Description DataFrame DataFrame indexed by ascending time . Source code in mt5cli/history.py 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 def load_rate_data ( conn_or_path : SqliteConnOrPath , table : str , count : int | None = None , ) -> pd . DataFrame : \"\"\"Load rate-like data from a SQLite database path or connection. Args: conn_or_path: SQLite database path or open connection. table: Source table or view name. count: Optional number of most recent rows to load. Returns: DataFrame indexed by ascending ``time``. \"\"\" conn , should_close = _open_existing_sqlite_database ( conn_or_path ) try : return load_rate_data_from_connection ( conn , table , count = count ) finally : if should_close : conn . close () load_rate_data_from_connection \u00b6 load_rate_data_from_connection ( connection : Connection , table : str , count : int | None = None , ) -> DataFrame Load rate-like data from a SQLite table or view. Parameters: Name Type Description Default connection Connection Open SQLite connection. required table str Source table or view name. required count int | None Optional number of most recent rows to load. None Returns: Type Description DataFrame DataFrame indexed by ascending time . Raises: Type Description ValueError If inputs, schema, timestamps are invalid, or the table or view contains no rows. Source code in mt5cli/history.py 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 def load_rate_data_from_connection ( connection : sqlite3 . Connection , table : str , count : int | None = None , ) -> pd . DataFrame : \"\"\"Load rate-like data from a SQLite table or view. Args: connection: Open SQLite connection. table: Source table or view name. count: Optional number of most recent rows to load. Returns: DataFrame indexed by ascending ``time``. Raises: ValueError: If inputs, schema, timestamps are invalid, or the table or view contains no rows. \"\"\" table_name = _validate_rate_load_request ( table , count ) columns = get_table_columns ( connection , table_name ) _ensure_rate_columns ( columns , table_name ) quoted_table = quote_sqlite_identifier ( table_name ) if count is None : frame = cast ( \"pd.DataFrame\" , pd . read_sql_query ( # type: ignore[reportUnknownMemberType] f \"SELECT * FROM { quoted_table } ORDER BY time ASC\" , # noqa: S608 connection , ), ) else : frame = cast ( \"pd.DataFrame\" , pd . read_sql_query ( # type: ignore[reportUnknownMemberType] f \"SELECT * FROM { quoted_table } ORDER BY time DESC LIMIT ?\" , # noqa: S608 connection , params = ( count ,), ), ) if frame . empty : msg = f \"SQLite table or view { table_name !r} contains no rows.\" raise ValueError ( msg ) return _parse_rate_time_index ( frame , table_name ) load_rate_series_by_granularity \u00b6 load_rate_series_by_granularity ( conn_or_path : SqliteConnOrPath , symbols : Sequence [ str ], granularities : Sequence [ int | str ], count : int , * , explicit_tables : Sequence [ str ] | None = None , allow_missing_symbol : bool = False , ) -> dict [ tuple [ str | None , str ], DataFrame ] Load rate series keyed by symbol and string granularity name. Builds targets with :func: build_rate_targets and loads them with :func: load_rate_series_from_sqlite , then rekeys the result by granularity name (for example M1 ) instead of the integer timeframe to reduce downstream boilerplate. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath SQLite database path or open connection. required symbols Sequence [ str ] MT5 symbol names. May be empty when allow_missing_symbol . required granularities Sequence [ int | str ] MT5 timeframes as integers or names (for example M1 ). required count int Number of most recent rows to load per series. required explicit_tables Sequence [ str ] | None Optional explicit table or view names matching the built targets in row-major order. Required when symbols are omitted. None allow_missing_symbol bool When True and symbols is empty, build targets with symbol=None for each granularity instead of raising. False Returns: Type Description dict [ tuple [ str | None, str ], DataFrame ] Mapping keyed by (symbol | None, granularity_name) to each rate dict [ tuple [ str | None, str ], DataFrame ] DataFrame. Propagates ValueError (via :func: build_rate_targets and dict [ tuple [ str | None, str ], DataFrame ] func: load_rate_series_from_sqlite ) when inputs are empty or invalid, dict [ tuple [ str | None, str ], DataFrame ] table resolution fails, or duplicate targets are present. Source code in mt5cli/history.py 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 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 def load_rate_series_by_granularity ( conn_or_path : SqliteConnOrPath , symbols : Sequence [ str ], granularities : Sequence [ int | str ], count : int , * , explicit_tables : Sequence [ str ] | None = None , allow_missing_symbol : bool = False , ) -> dict [ tuple [ str | None , str ], pd . DataFrame ]: \"\"\"Load rate series keyed by symbol and string granularity name. Builds targets with :func:`build_rate_targets` and loads them with :func:`load_rate_series_from_sqlite`, then rekeys the result by granularity name (for example ``M1``) instead of the integer timeframe to reduce downstream boilerplate. Args: conn_or_path: SQLite database path or open connection. symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``. granularities: MT5 timeframes as integers or names (for example ``M1``). count: Number of most recent rows to load per series. explicit_tables: Optional explicit table or view names matching the built targets in row-major order. Required when symbols are omitted. allow_missing_symbol: When True and ``symbols`` is empty, build targets with ``symbol=None`` for each granularity instead of raising. Returns: Mapping keyed by ``(symbol | None, granularity_name)`` to each rate DataFrame. Propagates ``ValueError`` (via :func:`build_rate_targets` and :func:`load_rate_series_from_sqlite`) when inputs are empty or invalid, table resolution fails, or duplicate targets are present. \"\"\" targets = build_rate_targets ( symbols , granularities , allow_missing_symbol = allow_missing_symbol , ) series = load_rate_series_from_sqlite ( conn_or_path , targets , count , explicit_tables = explicit_tables , ) return { ( symbol , resolve_granularity_name ( timeframe )): frame for ( symbol , timeframe ), frame in series . items () } load_rate_series_from_sqlite \u00b6 load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : None = None , count : int | None = None , explicit_tables : None = None , * , table : str , ) -> DataFrame load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : None = None , count : int | None = None , explicit_tables : Sequence [ str ] | None = None , * , table : None = None , ) -> dict [ tuple [ str | None , int ], DataFrame ] load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : Sequence [ RateTarget ], count : int , explicit_tables : Sequence [ str ] | None = None , * , table : None = None , ) -> dict [ tuple [ str | None , int ], DataFrame ] load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : Sequence [ RateTarget ] | None = None , count : int | None = None , explicit_tables : Sequence [ str ] | None = None , * , table : str | None = None , ) -> dict [ tuple [ str | None , int ], DataFrame ] | DataFrame Load one table/view or multiple rate series from a SQLite database. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath SQLite database path or open connection. required targets Sequence [ RateTarget ] | None Rate targets to load. Each (symbol, timeframe_int) pair must be unique. Omit when loading a single explicit table . None count int | None Optional number of most recent rows to load per series. None explicit_tables Sequence [ str ] | None Optional explicit table or view names matching targets. When omitted, managed rate_* compatibility views must already exist in the database. None table str | None Optional single table or view name to load directly. None Returns: Type Description dict [ tuple [ str | None, int ], DataFrame ] | DataFrame A DataFrame when table is provided, otherwise a mapping keyed by dict [ tuple [ str | None, int ], DataFrame ] | DataFrame (symbol, timeframe_int) to each rate DataFrame. Raises: Type Description ValueError If count is not positive, targets are empty, duplicate (symbol, timeframe_int) pairs are present, or table resolution fails. Source code in mt5cli/history.py 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 def load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : Sequence [ RateTarget ] | None = None , count : int | None = None , explicit_tables : Sequence [ str ] | None = None , * , table : str | None = None , ) -> dict [ tuple [ str | None , int ], pd . DataFrame ] | pd . DataFrame : \"\"\"Load one table/view or multiple rate series from a SQLite database. Args: conn_or_path: SQLite database path or open connection. targets: Rate targets to load. Each ``(symbol, timeframe_int)`` pair must be unique. Omit when loading a single explicit ``table``. count: Optional number of most recent rows to load per series. explicit_tables: Optional explicit table or view names matching targets. When omitted, managed ``rate_*`` compatibility views must already exist in the database. table: Optional single table or view name to load directly. Returns: A DataFrame when ``table`` is provided, otherwise a mapping keyed by ``(symbol, timeframe_int)`` to each rate DataFrame. Raises: ValueError: If ``count`` is not positive, targets are empty, duplicate ``(symbol, timeframe_int)`` pairs are present, or table resolution fails. \"\"\" if table is not None : return load_rate_data ( conn_or_path , table , count = count ) if count is None or count <= 0 : msg = \"count must be positive.\" raise ValueError ( msg ) if targets is None : msg = \"targets are required when table is not provided.\" raise ValueError ( msg ) target_list = list ( targets ) if not target_list : msg = \"At least one rate target is required.\" raise ValueError ( msg ) if explicit_tables is None and any ( target . symbol is None for target in target_list ): msg = ( \"Cannot resolve a rate table for a target without a symbol; \" \"provide explicit_tables.\" ) raise ValueError ( msg ) seen_keys : set [ tuple [ str | None , int ]] = set () for target in target_list : key = ( target . symbol , target . timeframe_int ) if key in seen_keys : symbol_repr = repr ( target . symbol ) msg = f \"Duplicate rate target: ( { symbol_repr } , { target . timeframe_int } )\" raise ValueError ( msg ) seen_keys . add ( key ) tables = ( resolve_rate_tables ( None , target_list , explicit_tables ) if explicit_tables is not None else None ) conn , should_close = _open_existing_sqlite_database ( conn_or_path ) try : resolved_tables = tables or resolve_rate_tables ( conn , target_list , require_existing = True , ) return { ( target . symbol , target . timeframe_int ): load_rate_data_from_connection ( conn , table , count = count , ) for target , table in zip ( target_list , resolved_tables , strict = True ) } finally : if should_close : conn . close () parse_sqlite_timestamp \u00b6 parse_sqlite_timestamp ( value : object ) -> datetime | None Parse a SQLite history timestamp value. Returns: Type Description datetime | None Parsed timezone-aware datetime, or None when parsing fails. Source code in mt5cli/history.py 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 def parse_sqlite_timestamp ( value : object ) -> datetime | None : \"\"\"Parse a SQLite history timestamp value. Returns: Parsed timezone-aware datetime, or None when parsing fails. \"\"\" if value is None : return None if isinstance ( value , datetime ): return value if value . tzinfo is not None else value . replace ( tzinfo = UTC ) if isinstance ( value , int | float ): return datetime . fromtimestamp ( float ( value ), tz = UTC ) if isinstance ( value , str ): return _parse_string_sqlite_timestamp ( value ) logger . warning ( \"Ignoring unsupported history timestamp type: %s \" , type ( value )) return None quote_sqlite_identifier \u00b6 quote_sqlite_identifier ( identifier : str ) -> str Return a safely quoted SQLite identifier using double quotes. Source code in mt5cli/history.py 61 62 63 def quote_sqlite_identifier ( identifier : str ) -> str : \"\"\"Return a safely quoted SQLite identifier using double quotes.\"\"\" return '\"' + identifier . replace ( '\"' , '\"\"' ) + '\"' record_written_columns \u00b6 record_written_columns ( written_columns : dict [ Dataset , set [ str ]], dataset : Dataset , frame : DataFrame , ) -> None Remember columns for datasets written during collection. Source code in mt5cli/history.py 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 def record_written_columns ( written_columns : dict [ Dataset , set [ str ]], dataset : Dataset , frame : pd . DataFrame , ) -> None : \"\"\"Remember columns for datasets written during collection.\"\"\" columns = set ( frame . columns ) if dataset in written_columns : written_columns [ dataset ] . update ( columns ) else : written_columns [ dataset ] = columns resolve_granularity_name \u00b6 resolve_granularity_name ( timeframe : int ) -> str Return a granularity name for a timeframe integer when known. Source code in mt5cli/history.py 107 108 109 110 111 112 113 def resolve_granularity_name ( timeframe : int ) -> str : \"\"\"Return a granularity name for a timeframe integer when known.\"\"\" try : name = _get_timeframe_name ( timeframe ) except ValueError : return str ( timeframe ) return name . removeprefix ( \"TIMEFRAME_\" ) resolve_history_datasets \u00b6 resolve_history_datasets ( datasets : set [ Dataset ] | None , ) -> set [ Dataset ] Resolve configured history datasets. Returns: Type Description set [ Dataset ] DEFAULT_HISTORY_DATASETS (rates, history-orders, history-deals) set [ Dataset ] when datasets is None, otherwise the configured selection (which set [ Dataset ] may be empty or explicitly include Dataset.ticks ). Source code in mt5cli/history.py 66 67 68 69 70 71 72 73 74 75 76 def resolve_history_datasets ( datasets : set [ Dataset ] | None ) -> set [ Dataset ]: \"\"\"Resolve configured history datasets. Returns: ``DEFAULT_HISTORY_DATASETS`` (rates, history-orders, history-deals) when ``datasets`` is None, otherwise the configured selection (which may be empty or explicitly include ``Dataset.ticks``). \"\"\" if datasets is None : return set ( DEFAULT_HISTORY_DATASETS ) return set ( datasets ) resolve_history_tick_flags \u00b6 resolve_history_tick_flags ( flags : int | str ) -> int Resolve tick copy flags from an integer or name. Returns: Type Description int Integer tick flag value. Source code in mt5cli/history.py 98 99 100 101 102 103 104 def resolve_history_tick_flags ( flags : int | str ) -> int : \"\"\"Resolve tick copy flags from an integer or name. Returns: Integer tick flag value. \"\"\" return parse_tick_flags ( flags ) resolve_history_timeframes \u00b6 resolve_history_timeframes ( timeframes : Sequence [ int | str ] | None , ) -> list [ int ] Resolve rate timeframes, deduplicating aliases for the same integer. Returns: Type Description list [ int ] Ordered list of unique timeframe integers. Source code in mt5cli/history.py 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 def resolve_history_timeframes ( timeframes : Sequence [ int | str ] | None , ) -> list [ int ]: \"\"\"Resolve rate timeframes, deduplicating aliases for the same integer. Returns: Ordered list of unique timeframe integers. \"\"\" raw = timeframes if timeframes is not None else DEFAULT_HISTORY_TIMEFRAMES seen : set [ int ] = set () resolved : list [ int ] = [] for value in raw : tf = parse_timeframe ( value ) if tf not in seen : seen . add ( tf ) resolved . append ( tf ) return resolved resolve_rate_table_name \u00b6 resolve_rate_table_name ( symbol : str , granularity : str ) -> str Return the canonical normalized SQLite rate table name. The normalized history table stores all symbols and timeframes in rates ; use :func: resolve_rate_view_name for per-symbol compatibility view names. Returns: Type Description str Canonical normalized rates table name. Raises: Type Description ValueError If symbol or granularity is invalid. Source code in mt5cli/history.py 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 def resolve_rate_table_name ( symbol : str , granularity : str ) -> str : \"\"\"Return the canonical normalized SQLite rate table name. The normalized history table stores all symbols and timeframes in ``rates``; use :func:`resolve_rate_view_name` for per-symbol compatibility view names. Returns: Canonical normalized rates table name. Raises: ValueError: If ``symbol`` or ``granularity`` is invalid. \"\"\" parse_timeframe ( granularity ) if not symbol . strip (): msg = \"symbol must not be empty.\" raise ValueError ( msg ) return Dataset . rates . table_name resolve_rate_tables \u00b6 resolve_rate_tables ( conn_or_path : SqliteConnOrPath | None , targets : Sequence [ RateTarget ], explicit_tables : Sequence [ str ] | None = None , * , require_existing : bool = False , ) -> list [ str ] Resolve SQLite table or view names for rate targets. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath | None SQLite database path or open connection. May be None when explicit_tables is provided, or when require_existing is False and deterministic default view names are sufficient. required targets Sequence [ RateTarget ] Rate targets to resolve. required explicit_tables Sequence [ str ] | None Optional explicit table or view names. When provided, they are used as-is and must match the number of targets. None require_existing bool When True, require the database and managed views to exist for each symbol target. Ignored when explicit_tables is provided. False Returns: Type Description list [ str ] Table or view names aligned with targets . Raises: Type Description ValueError If targets is empty, explicit_tables length does not match the target count, a target without a symbol is resolved without an explicit table, or require_existing is True and the database or a managed view is missing. Source code in mt5cli/history.py 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 def resolve_rate_tables ( conn_or_path : SqliteConnOrPath | None , targets : Sequence [ RateTarget ], explicit_tables : Sequence [ str ] | None = None , * , require_existing : bool = False , ) -> list [ str ]: \"\"\"Resolve SQLite table or view names for rate targets. Args: conn_or_path: SQLite database path or open connection. May be None when ``explicit_tables`` is provided, or when ``require_existing`` is False and deterministic default view names are sufficient. targets: Rate targets to resolve. explicit_tables: Optional explicit table or view names. When provided, they are used as-is and must match the number of targets. require_existing: When True, require the database and managed views to exist for each symbol target. Ignored when ``explicit_tables`` is provided. Returns: Table or view names aligned with ``targets``. Raises: ValueError: If ``targets`` is empty, ``explicit_tables`` length does not match the target count, a target without a symbol is resolved without an explicit table, or ``require_existing`` is True and the database or a managed view is missing. \"\"\" target_list = list ( targets ) if not target_list : msg = \"At least one rate target is required.\" raise ValueError ( msg ) if explicit_tables is not None : tables = list ( explicit_tables ) if len ( tables ) != len ( target_list ): msg = ( f \"Expected { len ( target_list ) } explicit table(s) \" f \"to match the targets, got { len ( tables ) } .\" ) raise ValueError ( msg ) return tables if any ( target . symbol is None for target in target_list ): msg = ( \"Cannot resolve a rate table for a target without a symbol; \" \"provide explicit_tables.\" ) raise ValueError ( msg ) conn , should_close = _open_history_connection ( conn_or_path ) try : if conn is None : if require_existing : path = ( conn_or_path if isinstance ( conn_or_path , ( Path , str )) else \"database\" ) msg = f \"SQLite database not found: { path } \" raise ValueError ( msg ) timeframe_counts = None existing_views : set [ str ] = set () else : timeframe_counts = _load_rates_timeframe_counts ( conn ) existing_views = _load_existing_rate_views ( conn ) resolved : list [ str ] = [] for target in target_list : symbol = cast ( \"str\" , target . symbol ) timeframe = target . timeframe_int resolved . append ( _resolve_rate_view_name_from_context ( symbol = symbol , timeframe = timeframe , granularity_name = resolve_granularity_name ( timeframe ), timeframe_counts = timeframe_counts , existing_views = existing_views , require_existing = require_existing , ), ) return resolved finally : if should_close and conn is not None : conn . close () resolve_rate_view_name \u00b6 resolve_rate_view_name ( conn_or_path : SqliteConnOrPath | None , symbol : str , granularity : str , * , require_existing : bool = False , ) -> str Resolve the mt5cli-managed rate compatibility view name. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath | None SQLite database path or open connection. When None or a non-existing path and require_existing is False, the deterministic default view name is returned without creating a database file. required symbol str Symbol stored in the normalized rates table. required granularity str Timeframe name (for example M1 ) or integer string. required require_existing bool When True, require the database and a managed view to exist. False Returns: Type Description str View name such as rate_EURUSD__1 or rate_EURUSD__M1_1 . Raises: Type Description ValueError If require_existing is True and the database or view is missing. Source code in mt5cli/history.py 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 def resolve_rate_view_name ( conn_or_path : SqliteConnOrPath | None , symbol : str , granularity : str , * , require_existing : bool = False , ) -> str : \"\"\"Resolve the mt5cli-managed rate compatibility view name. Args: conn_or_path: SQLite database path or open connection. When None or a non-existing path and ``require_existing`` is False, the deterministic default view name is returned without creating a database file. symbol: Symbol stored in the normalized ``rates`` table. granularity: Timeframe name (for example ``M1``) or integer string. require_existing: When True, require the database and a managed view to exist. Returns: View name such as ``rate_EURUSD__1`` or ``rate_EURUSD__M1_1``. Raises: ValueError: If ``require_existing`` is True and the database or view is missing. \"\"\" timeframe = parse_timeframe ( granularity ) granularity_name = resolve_granularity_name ( timeframe ) conn , should_close = _open_history_connection ( conn_or_path ) try : if conn is None : if require_existing : path = ( conn_or_path if isinstance ( conn_or_path , ( Path , str )) else \"database\" ) msg = f \"SQLite database not found: { path } \" raise ValueError ( msg ) return build_rate_view_name ( symbol = symbol , granularity = granularity_name , granularity_count = 1 , timeframe = timeframe , ) return _resolve_rate_view_name_from_context ( symbol = symbol , timeframe = timeframe , granularity_name = granularity_name , timeframe_counts = _load_rates_timeframe_counts ( conn ), existing_views = _load_existing_rate_views ( conn ), require_existing = require_existing , ) finally : if should_close and conn is not None : conn . close () resolve_rate_view_names \u00b6 resolve_rate_view_names ( conn_or_path : SqliteConnOrPath | None , symbols : Sequence [ str ], granularities : Sequence [ str ], * , require_existing : bool = False , ) -> list [ str ] Resolve rate compatibility view names for symbol and granularity pairs. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath | None SQLite database path or open connection. When None or a non-existing path and require_existing is False, deterministic default view names are returned without creating a database file. required symbols Sequence [ str ] Symbols stored in the normalized rates table. required granularities Sequence [ str ] Timeframe names (for example M1 ) or integer strings. required require_existing bool When True, require the database and managed views to exist. False Returns: Type Description list [ str ] View names in row-major order: every granularity for the first list [ str ] symbol, then every granularity for the next symbol, and so on. Source code in mt5cli/history.py 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 def resolve_rate_view_names ( conn_or_path : SqliteConnOrPath | None , symbols : Sequence [ str ], granularities : Sequence [ str ], * , require_existing : bool = False , ) -> list [ str ]: \"\"\"Resolve rate compatibility view names for symbol and granularity pairs. Args: conn_or_path: SQLite database path or open connection. When None or a non-existing path and ``require_existing`` is False, deterministic default view names are returned without creating a database file. symbols: Symbols stored in the normalized ``rates`` table. granularities: Timeframe names (for example ``M1``) or integer strings. require_existing: When True, require the database and managed views to exist. Returns: View names in row-major order: every ``granularity`` for the first symbol, then every granularity for the next symbol, and so on. \"\"\" conn , should_close = _open_history_connection ( conn_or_path ) try : if conn is None : return [ resolve_rate_view_name ( conn_or_path , symbol , granularity , require_existing = require_existing , ) for symbol in symbols for granularity in granularities ] timeframe_counts = _load_rates_timeframe_counts ( conn ) existing_views = _load_existing_rate_views ( conn ) resolved : list [ str ] = [] for symbol in symbols : for granularity in granularities : timeframe = parse_timeframe ( granularity ) resolved . append ( _resolve_rate_view_name_from_context ( symbol = symbol , timeframe = timeframe , granularity_name = resolve_granularity_name ( timeframe ), timeframe_counts = timeframe_counts , existing_views = existing_views , require_existing = require_existing , ), ) return resolved finally : if should_close and conn is not None : conn . close () write_collected_datasets \u00b6 write_collected_datasets ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], datasets : set [ Dataset ], timeframe : int , flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Collect selected datasets and stream each symbol frame into SQLite. Returns: Type Description tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Written datasets and their columns. Source code in mt5cli/history.py 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 def write_collected_datasets ( conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], datasets : set [ Dataset ], timeframe : int , flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]: \"\"\"Collect selected datasets and stream each symbol frame into SQLite. Returns: Written datasets and their columns. \"\"\" written_columns : dict [ Dataset , set [ str ]] = {} written_tables : set [ Dataset ] = set () if Dataset . rates in datasets and write_rates_dataset ( conn , client , symbols , timeframe , date_from , date_to , if_exists , written_columns , ): written_tables . add ( Dataset . rates ) if Dataset . ticks in datasets and write_ticks_dataset ( conn , client , symbols , flags , date_from , date_to , if_exists , written_columns , ): written_tables . add ( Dataset . ticks ) if Dataset . history_orders in datasets and write_history_dataset ( conn , client . history_orders_get_as_df , Dataset . history_orders , symbols , date_from , date_to , if_exists , written_columns , include_account_events = False , ): written_tables . add ( Dataset . history_orders ) if Dataset . history_deals in datasets and write_history_dataset ( conn , client . history_deals_get_as_df , Dataset . history_deals , symbols , date_from , date_to , if_exists , written_columns , include_account_events = False , ): written_tables . add ( Dataset . history_deals ) return written_tables , written_columns write_history_dataset \u00b6 write_history_dataset ( conn : Connection , fetch : Callable [ ... , DataFrame ], dataset : Dataset , symbols : Sequence [ str ], date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], * , include_account_events : bool = False , ) -> bool Stream a history dataset into SQLite. Returns: Type Description bool True if the target table was written. Source code in mt5cli/history.py 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 def write_history_dataset ( conn : sqlite3 . Connection , fetch : Callable [ ... , pd . DataFrame ], dataset : Dataset , symbols : Sequence [ str ], date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], * , include_account_events : bool = False , ) -> bool : \"\"\"Stream a history dataset into SQLite. Returns: True if the target table was written. \"\"\" table_exists = False if include_account_events : frame = filter_trade_history_frame ( fetch ( date_from = date_from , date_to = date_to ), symbols , include_account_events = True , ) return write_streamed_frame ( conn , frame , dataset , table_exists , if_exists , written_columns , ) def _fetch_history_frame ( sym : str ) -> pd . DataFrame : return filter_trade_history_frame ( fetch ( date_from = date_from , date_to = date_to , symbol = sym ), [ sym ], include_account_events = False , ) return _stream_symbol_frames ( conn , symbols , dataset , if_exists , written_columns , _fetch_history_frame , ) write_incremental_datasets \u00b6 write_incremental_datasets ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], selected_datasets : set [ Dataset ], resolved_timeframes : list [ int ], resolved_tick_flags : int , fallback_start : datetime , end_date : datetime , * , deduplicate : bool , create_rate_views : bool , with_views : bool , include_account_events : bool , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Append selected datasets incrementally and refresh indexes and views. Returns: Type Description tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Written datasets and their columns. Source code in mt5cli/history.py 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 def write_incremental_datasets ( # noqa: PLR0913 conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], selected_datasets : set [ Dataset ], resolved_timeframes : list [ int ], resolved_tick_flags : int , fallback_start : datetime , end_date : datetime , * , deduplicate : bool , create_rate_views : bool , with_views : bool , include_account_events : bool , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]: \"\"\"Append selected datasets incrementally and refresh indexes and views. Returns: Written datasets and their columns. \"\"\" written_columns : dict [ Dataset , set [ str ]] = {} written_tables : set [ Dataset ] = set () dedup_scopes : dict [ Dataset , list [ DedupScope ]] = {} if Dataset . rates in selected_datasets : _write_incremental_rates ( conn , client , symbols , resolved_timeframes , fallback_start , end_date , written_columns , written_tables , dedup_scopes , ) if Dataset . ticks in selected_datasets : _write_incremental_ticks ( conn , client , symbols , resolved_tick_flags , fallback_start , end_date , written_columns , written_tables , dedup_scopes , ) if Dataset . history_orders in selected_datasets : _write_incremental_history_orders ( conn , client , symbols , fallback_start , end_date , written_columns , written_tables , dedup_scopes , ) if Dataset . history_deals in selected_datasets : _write_incremental_history_deals ( conn , client , symbols , fallback_start , end_date , written_columns , written_tables , dedup_scopes , include_account_events = include_account_events , ) _finalize_incremental_writes ( conn , selected_datasets , written_columns , written_tables , dedup_scopes , deduplicate = deduplicate , create_rate_views = create_rate_views , with_views = with_views , ) return written_tables , written_columns write_rates_dataset \u00b6 write_rates_dataset ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], timeframe : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool Stream rates frames into SQLite. Returns: Type Description bool True if the rates table was written. Source code in mt5cli/history.py 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 def write_rates_dataset ( conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], timeframe : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool : \"\"\"Stream rates frames into SQLite. Returns: True if the rates table was written. \"\"\" def _fetch_rates_frame ( sym : str ) -> pd . DataFrame : frame = client . copy_rates_range_as_df ( symbol = sym , timeframe = timeframe , date_from = date_from , date_to = date_to , ) . drop ( columns = [ \"symbol\" , \"timeframe\" ], errors = \"ignore\" ) if len ( frame . columns ) != 0 : frame . insert ( 0 , \"symbol\" , sym ) frame . insert ( 1 , \"timeframe\" , timeframe ) return frame return _stream_symbol_frames ( conn , symbols , Dataset . rates , if_exists , written_columns , _fetch_rates_frame , ) write_streamed_frame \u00b6 write_streamed_frame ( conn : Connection , frame : DataFrame , dataset : Dataset , table_exists : bool , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool Write one streamed dataset frame and track table state. Returns: Type Description bool True if the dataset table exists after this write attempt. Source code in mt5cli/history.py 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 def write_streamed_frame ( conn : sqlite3 . Connection , frame : pd . DataFrame , dataset : Dataset , table_exists : bool , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool : \"\"\"Write one streamed dataset frame and track table state. Returns: True if the dataset table exists after this write attempt. \"\"\" write_mode = IfExists . APPEND if table_exists else if_exists if append_dataframe ( conn , frame , dataset . table_name , write_mode ): record_written_columns ( written_columns , dataset , frame ) return True return table_exists write_ticks_dataset \u00b6 write_ticks_dataset ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool Stream ticks frames into SQLite. Returns: Type Description bool True if the ticks table was written. Source code in mt5cli/history.py 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 def write_ticks_dataset ( conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool : \"\"\"Stream ticks frames into SQLite. Returns: True if the ticks table was written. \"\"\" def _fetch_ticks_frame ( sym : str ) -> pd . DataFrame : frame = client . copy_ticks_range_as_df ( symbol = sym , date_from = date_from , date_to = date_to , flags = flags , ) . drop ( columns = [ \"symbol\" ], errors = \"ignore\" ) if len ( frame . columns ) != 0 : frame . insert ( 0 , \"symbol\" , sym ) return frame return _stream_symbol_frames ( conn , symbols , Dataset . ticks , if_exists , written_columns , _fetch_ticks_frame , ) collect-history schema \u00b6 The collect-history command (and the matching collect_history SDK function) writes selected MT5 datasets into one SQLite database. Each dataset becomes a table; column names and types mirror the pdmt5 DataFrame schema for that export, with two additions: symbol is prepended on every table. timeframe is prepended on rates so appended runs at different bar sizes stay distinguishable. SQLite does not declare foreign keys. Rows are linked logically by symbol , time windows, and (for deals) position_id / order . Duplicate rows are removed on append using dataset-specific keys (for example ticket on history tables, or (symbol, timeframe, time) on rates). Optional views are created when --with-views is set and the history-deals dataset was written. Entity-relationship diagram \u00b6 Sample layout for a full collection with --with-views : erDiagram rates { TEXT symbol \"dedup key\" INTEGER timeframe \"dedup key\" TEXT time \"dedup key\" REAL open REAL high REAL low REAL close INTEGER tick_volume INTEGER spread INTEGER real_volume } ticks { TEXT symbol \"dedup key\" TEXT time \"dedup key\" INTEGER time_msc \"dedup key (preferred)\" REAL bid REAL ask REAL last INTEGER volume INTEGER flags REAL volume_real } history_orders { INTEGER ticket \"dedup key\" TEXT symbol TEXT time INTEGER type INTEGER state REAL volume_initial REAL price_open REAL price_current INTEGER magic } history_deals { INTEGER ticket \"dedup key\" INTEGER order INTEGER position_id \"groups position view\" TEXT symbol TEXT time INTEGER type \"0/1 trade, else cash event\" INTEGER entry \"0 IN, 1 OUT, 2 INOUT, 3 OUT_BY\" REAL volume REAL price REAL profit REAL commission REAL swap REAL fee } cash_events { INTEGER ticket TEXT symbol TEXT time INTEGER type REAL profit } positions_reconstructed { INTEGER position_id TEXT symbol TEXT open_time TEXT close_time INTEGER direction REAL volume_open REAL volume_close REAL volume_reversal REAL open_price REAL close_price REAL total_profit INTEGER reversal_count INTEGER deals_count } rates ||--o{ history_deals : \"symbol (logical)\" ticks ||--o{ history_deals : \"symbol (logical)\" history_orders ||--o{ history_deals : \"order ~ ticket (logical)\" history_deals ||--|| cash_events : \"VIEW: type NOT IN (0,1)\" history_deals ||--o{ positions_reconstructed : \"VIEW: GROUP BY position_id\" Tables and views \u00b6 Object Kind Source Notes rates table copy_rates_range Indexed on (symbol, timeframe, time) when columns exist. ticks table copy_ticks_range Indexed on (symbol, time) when columns exist. history_orders table history_orders_get Fetched per --symbol , then concatenated. history_deals table history_deals_get Fetched per --symbol , then concatenated. Indexed on (position_id, symbol) when present. cash_events view history_deals Non-trade deal types (deposits, balance ops, etc.). Requires type column. positions_reconstructed view history_deals One row per closed position_id ; volume-weighted prices and reversal stats. Column sets can vary with terminal and pdmt5 version. Views are skipped with a warning when required columns are missing. Incremental collection \u00b6 The update_history SDK path uses the same base tables and optional cash_events / positions_reconstructed views. It additionally maintains rate___ compatibility views when create_rate_views=True . Rate view resolution \u00b6 Downstream tools can resolve mt5cli-managed compatibility view names from an existing SQLite history database without creating files or guessing naming schemes: from pathlib import Path from mt5cli.history import resolve_rate_view_name , resolve_rate_view_names # Single symbol and granularity view = resolve_rate_view_name ( Path ( \"history.db\" ), \"EURUSD\" , \"M1\" ) # Batch resolution in row-major order views = resolve_rate_view_names ( Path ( \"history.db\" ), [ \"EURUSD\" , \"GBPUSD\" ], [ \"M1\" , \"H1\" ], ) Resolution rules: Returns rate___ when a symbol stores one timeframe. Returns rate____ when multiple timeframes are stored for the same symbol. When multiple naming candidates apply, prefers an existing managed rate_*__* view from the candidate list. Falls back to single-timeframe naming when the database path is missing or rates metadata is unavailable. Pass require_existing=True to raise ValueError instead of returning a best-guess name when the database or view is missing. Accepts either a SQLite path or an open sqlite3.Connection . Rate data loading \u00b6 The canonical normalized rate table is rates ; compatibility views are named with rate___ for single-timeframe symbols or rate____ when a symbol has multiple stored timeframes. resolve_rate_table_name() returns rates , while resolve_rate_view_name() returns the per-symbol compatibility view name. Use load_rate_data() or load_rate_series_from_sqlite(..., table=...) to load a single table or view from a SQLite path. Use load_rate_series_by_granularity() to load multiple instrument/granularity targets without hard-coding view names: from pathlib import Path from mt5cli import ( load_rate_series_by_granularity , load_rate_series_from_sqlite , ) from mt5cli.history import ( load_rate_data , resolve_rate_table_name , resolve_rate_view_name , ) view = resolve_rate_view_name ( Path ( \"history.db\" ), \"EURUSD\" , \"M1\" , require_existing = True ) rates = load_rate_data ( Path ( \"history.db\" ), view , count = 1000 ) same_rates = load_rate_series_from_sqlite ( Path ( \"history.db\" ), table = view , count = 1000 ) table = resolve_rate_table_name ( \"EURUSD\" , \"M1\" ) # \"rates\" series = load_rate_series_by_granularity ( Path ( \"history.db\" ), symbols = [ \"EURUSD\" , \"GBPUSD\" ], granularities = [ \"M1\" , \"H1\" ], count = 500 , ) count returns the latest rows while preserving chronological order. Missing tables/views and mismatched explicit_tables lengths raise ValueError with the requested database target in the message. The loader accepts close-based OHLC rate data or tick-like bid/ask data. It validates that time exists, parses timestamps with pandas, and returns a DataFrame indexed by ascending DatetimeIndex named time . Multi-series rate loading \u00b6 For loading many rate series at once, build neutral RateTarget pairs and load them from SQLite in one call. View names are resolved via the same compatibility-view rules, or you can pass explicit_tables to bypass resolution: from pathlib import Path from mt5cli import build_rate_targets , load_rate_series_from_sqlite targets = build_rate_targets ([ \"EURUSD\" , \"GBPUSD\" ], [ \"M1\" , \"H1\" ]) series = load_rate_series_from_sqlite ( Path ( \"history.db\" ), targets , count = 1000 ) frame = series [ \"EURUSD\" , 1 ] # keyed by (symbol, integer timeframe) build_rate_targets() returns RateTarget(symbol, timeframe) pairs in row-major order, normalizing timeframe names such as \"M1\" to their integer values; set allow_missing_symbol=True to address series solely by explicit_tables (targets carry symbol=None ). resolve_rate_tables() maps targets to table or view names and validates that any explicit_tables count matches the target count. Pass require_existing=True to raise ValueError instead of returning a best-guess name when the database or managed view is missing. When explicit_tables is provided, names are returned as-is and require_existing is ignored. load_rate_series_from_sqlite() returns a mapping keyed by (symbol, integer timeframe) . Unless explicit_tables is supplied, it requires existing managed rate_* compatibility views and raises ValueError when they are missing. Duplicate (symbol, timeframe) targets are rejected. load_rate_series_by_granularity() is a thin wrapper that builds the targets, loads the series, and rekeys the result by granularity name to avoid converting integer timeframes downstream: from mt5cli import load_rate_series_by_granularity series = load_rate_series_by_granularity ( \"history.db\" , [ \"EURUSD\" ], [ \"M1\" , \"H1\" ], count = 1000 ) frame = series [ \"EURUSD\" , \"M1\" ] # keyed by (symbol | None, granularity_name)","title":"History Collection (SQLite)"},{"location":"api/history/#history-collection-sqlite","text":"","title":"History Collection (SQLite)"},{"location":"api/history/#mt5cli.history","text":"SQLite storage helpers for the collect-history incremental data pipeline.","title":"history"},{"location":"api/history/#mt5cli.history.DEFAULT_HISTORY_DATASETS","text":"DEFAULT_HISTORY_DATASETS : frozenset [ Dataset ] = frozenset ({ rates , history_orders , history_deals , })","title":"DEFAULT_HISTORY_DATASETS"},{"location":"api/history/#mt5cli.history.DEFAULT_HISTORY_TIMEFRAMES","text":"DEFAULT_HISTORY_TIMEFRAMES : tuple [ str , ... ] = ( TIMEFRAME_NAMES )","title":"DEFAULT_HISTORY_TIMEFRAMES"},{"location":"api/history/#mt5cli.history.SqliteConnOrPath","text":"SqliteConnOrPath = Connection | Path | str","title":"SqliteConnOrPath"},{"location":"api/history/#mt5cli.history.logger","text":"logger = getLogger ( __name__ )","title":"logger"},{"location":"api/history/#mt5cli.history.DedupScope","text":"DedupScope ( where : str , params : tuple [ object , ... ], required_columns : frozenset [ str ], ) Scoped deduplication predicate and the columns it references. Attributes: Name Type Description where str SQL predicate appended to the duplicate-removal query. params tuple [ object , ...] Parameters bound to the scope predicate. required_columns frozenset [ str ] Columns that must be present in the written table for the scope to run.","title":"DedupScope"},{"location":"api/history/#mt5cli.history.DedupScope.params","text":"params : tuple [ object , ... ]","title":"params"},{"location":"api/history/#mt5cli.history.DedupScope.required_columns","text":"required_columns : frozenset [ str ]","title":"required_columns"},{"location":"api/history/#mt5cli.history.DedupScope.where","text":"where : str","title":"where"},{"location":"api/history/#mt5cli.history.RateTarget","text":"RateTarget ( symbol : str | None , timeframe : int | str ) A single rate series identified by symbol and timeframe. Attributes: Name Type Description symbol str | None MT5 symbol name, or None when the rate series is addressed only by an explicit table (for example a custom SQLite view). timeframe int | str MT5 timeframe as an integer or name (for example M1 ).","title":"RateTarget"},{"location":"api/history/#mt5cli.history.RateTarget.symbol","text":"symbol : str | None","title":"symbol"},{"location":"api/history/#mt5cli.history.RateTarget.timeframe","text":"timeframe : int | str","title":"timeframe"},{"location":"api/history/#mt5cli.history.RateTarget.timeframe_int","text":"timeframe_int : int Return the timeframe as its integer MT5 value.","title":"timeframe_int"},{"location":"api/history/#mt5cli.history.RateTarget.__post_init__","text":"__post_init__ () -> None Normalize accepted timeframe aliases to the stored integer value. Source code in mt5cli/history.py 550 551 552 553 def __post_init__ ( self ) -> None : \"\"\"Normalize accepted timeframe aliases to the stored integer value.\"\"\" if not isinstance ( self . timeframe , int ): object . __setattr__ ( self , \"timeframe\" , parse_timeframe ( self . timeframe ))","title":"__post_init__"},{"location":"api/history/#mt5cli.history.append_dataframe","text":"append_dataframe ( conn : Connection , frame : DataFrame , table_name : str , if_exists : IfExists , ) -> bool Append a DataFrame to SQLite when it has a schema. Returns: Type Description bool True if a table was written, False if the frame had no columns. Source code in mt5cli/history.py 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 def append_dataframe ( conn : sqlite3 . Connection , frame : pd . DataFrame , table_name : str , if_exists : IfExists , ) -> bool : \"\"\"Append a DataFrame to SQLite when it has a schema. Returns: True if a table was written, False if the frame had no columns. \"\"\" if len ( frame . columns ) == 0 : logger . warning ( \"Skipping %s : dataset returned no columns\" , table_name ) return False frame . to_sql ( # type: ignore[reportUnknownMemberType] table_name , conn , if_exists = if_exists . value , index = False , chunksize = 50_000 , ) return True","title":"append_dataframe"},{"location":"api/history/#mt5cli.history.augment_written_columns_from_sqlite","text":"augment_written_columns_from_sqlite ( conn : Connection , datasets : set [ Dataset ], written_columns : dict [ Dataset , set [ str ]], ) -> None Add existing table columns to the written column map. Source code in mt5cli/history.py 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 def augment_written_columns_from_sqlite ( conn : sqlite3 . Connection , datasets : set [ Dataset ], written_columns : dict [ Dataset , set [ str ]], ) -> None : \"\"\"Add existing table columns to the written column map.\"\"\" for dataset in datasets : columns = get_table_columns ( conn , dataset . table_name ) if not columns : continue if dataset in written_columns : written_columns [ dataset ] . update ( columns ) else : written_columns [ dataset ] = columns","title":"augment_written_columns_from_sqlite"},{"location":"api/history/#mt5cli.history.build_rate_targets","text":"build_rate_targets ( symbols : Sequence [ str ], timeframes : Sequence [ int | str ], * , allow_missing_symbol : bool = False , ) -> list [ RateTarget ] Build rate targets for every symbol and timeframe combination. Parameters: Name Type Description Default symbols Sequence [ str ] MT5 symbol names. May be empty when allow_missing_symbol . required timeframes Sequence [ int | str ] MT5 timeframes as integers or names (for example M1 ). required allow_missing_symbol bool When True and symbols is empty, build targets with symbol=None for each timeframe instead of raising. False Returns: Type Description list [ RateTarget ] Targets in row-major order: every timeframe for the first symbol, then list [ RateTarget ] every timeframe for the next symbol, and so on. Raises: Type Description ValueError If timeframes is empty, or symbols is empty and allow_missing_symbol is False. Source code in mt5cli/history.py 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 def build_rate_targets ( symbols : Sequence [ str ], timeframes : Sequence [ int | str ], * , allow_missing_symbol : bool = False , ) -> list [ RateTarget ]: \"\"\"Build rate targets for every symbol and timeframe combination. Args: symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``. timeframes: MT5 timeframes as integers or names (for example ``M1``). allow_missing_symbol: When True and ``symbols`` is empty, build targets with ``symbol=None`` for each timeframe instead of raising. Returns: Targets in row-major order: every timeframe for the first symbol, then every timeframe for the next symbol, and so on. Raises: ValueError: If ``timeframes`` is empty, or ``symbols`` is empty and ``allow_missing_symbol`` is False. \"\"\" if not timeframes : msg = \"At least one timeframe is required.\" raise ValueError ( msg ) if not symbols : if not allow_missing_symbol : msg = \"At least one symbol is required.\" raise ValueError ( msg ) return [ RateTarget ( symbol = None , timeframe = tf ) for tf in timeframes ] return [ RateTarget ( symbol = symbol , timeframe = tf ) for symbol in symbols for tf in timeframes ]","title":"build_rate_targets"},{"location":"api/history/#mt5cli.history.build_rate_view_name","text":"build_rate_view_name ( * , symbol : str , granularity : str , granularity_count : int , timeframe : int , ) -> str Return a collision-free offline optimize view name. View names always include the timeframe integer after a __ separator so a symbol such as EURUSD_M1 cannot collide with EURUSD at timeframe M1 . Source code in mt5cli/history.py 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 def build_rate_view_name ( * , symbol : str , granularity : str , granularity_count : int , timeframe : int , ) -> str : \"\"\"Return a collision-free offline optimize view name. View names always include the timeframe integer after a ``__`` separator so a symbol such as ``EURUSD_M1`` cannot collide with ``EURUSD`` at timeframe ``M1``. \"\"\" if granularity_count == 1 : return f \"rate_ { symbol } __ { timeframe } \" return f \"rate_ { symbol } __ { granularity } _ { timeframe } \"","title":"build_rate_view_name"},{"location":"api/history/#mt5cli.history.create_cash_events_view","text":"create_cash_events_view ( conn : Connection , deals_columns : set [ str ] ) -> bool Create the cash_events SQLite view derived from history_deals. Returns: Type Description bool True if the view was created, False if required columns are missing. Source code in mt5cli/history.py 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 def create_cash_events_view ( conn : sqlite3 . Connection , deals_columns : set [ str ], ) -> bool : \"\"\"Create the cash_events SQLite view derived from history_deals. Returns: True if the view was created, False if required columns are missing. \"\"\" if \"type\" not in deals_columns : logger . warning ( \"Skipping cash_events view: history_deals.type is missing\" ) return False conn . execute ( \"DROP VIEW IF EXISTS cash_events\" ) conn . execute ( \"CREATE VIEW cash_events AS\" # noqa: S608 f \" SELECT * FROM history_deals WHERE type NOT IN { _TRADE_DEAL_TYPES_SQL } \" , ) return True","title":"create_cash_events_view"},{"location":"api/history/#mt5cli.history.create_history_indexes","text":"create_history_indexes ( conn : Connection , written_columns : dict [ Dataset , set [ str ]], ) -> None Create useful indexes for collected history tables when present. Source code in mt5cli/history.py 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 def create_history_indexes ( conn : sqlite3 . Connection , written_columns : dict [ Dataset , set [ str ]], ) -> None : \"\"\"Create useful indexes for collected history tables when present.\"\"\" if { \"symbol\" , \"timeframe\" , \"time\" } . issubset ( written_columns . get ( Dataset . rates , set ()), ): conn . execute ( \"CREATE INDEX IF NOT EXISTS idx_rates_symbol_timeframe_time\" \" ON rates(symbol, timeframe, time)\" , ) if { \"symbol\" , \"time\" } . issubset ( written_columns . get ( Dataset . ticks , set ())): conn . execute ( \"CREATE INDEX IF NOT EXISTS idx_ticks_symbol_time ON ticks(symbol, time)\" , ) if { \"position_id\" , \"symbol\" } . issubset ( written_columns . get ( Dataset . history_deals , set ()), ): conn . execute ( \"CREATE INDEX IF NOT EXISTS idx_history_deals_position_symbol\" \" ON history_deals(position_id, symbol)\" , )","title":"create_history_indexes"},{"location":"api/history/#mt5cli.history.create_positions_reconstructed_view","text":"create_positions_reconstructed_view ( conn : Connection , deals_columns : set [ str ] ) -> bool Create the positions_reconstructed SQLite view derived from history_deals. Returns: Type Description bool True if the view was created, False if required columns are missing. Source code in mt5cli/history.py 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 def create_positions_reconstructed_view ( conn : sqlite3 . Connection , deals_columns : set [ str ], ) -> bool : \"\"\"Create the positions_reconstructed SQLite view derived from history_deals. Returns: True if the view was created, False if required columns are missing. \"\"\" if not _POSITIONS_VIEW_REQUIRED_COLUMNS . issubset ( deals_columns ): missing = \", \" . join ( sorted ( _POSITIONS_VIEW_REQUIRED_COLUMNS - deals_columns )) logger . warning ( \"Skipping positions_reconstructed view: history_deals missing columns: %s \" , missing , ) return False conn . execute ( \"DROP VIEW IF EXISTS positions_reconstructed\" ) conn . execute ( \"CREATE VIEW positions_reconstructed AS\" # noqa: S608 \" SELECT\" \" position_id,\" \" symbol,\" \" MIN(CASE WHEN entry = 0 THEN time END) AS open_time,\" \" MAX(CASE WHEN entry IN (1, 2, 3) THEN time END) AS close_time,\" \" MIN(CASE WHEN entry = 0 THEN type END) AS direction,\" \" SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) AS volume_open,\" \" SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) AS volume_close,\" \" SUM(CASE WHEN entry = 2 THEN volume ELSE 0 END) AS volume_reversal,\" \" CASE\" \" WHEN SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END) > 0\" \" THEN SUM(CASE WHEN entry = 0 THEN price * volume ELSE 0 END)\" \" / SUM(CASE WHEN entry = 0 THEN volume ELSE 0 END)\" \" END AS open_price,\" \" CASE\" \" WHEN SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END) > 0\" \" THEN SUM(CASE WHEN entry IN (1, 2, 3) THEN price * volume ELSE 0 END)\" \" / SUM(CASE WHEN entry IN (1, 2, 3) THEN volume ELSE 0 END)\" \" END AS close_price,\" \" SUM(profit) AS total_profit,\" \" SUM(CASE WHEN entry = 2 THEN 1 ELSE 0 END) AS reversal_count,\" \" COUNT(*) AS deals_count\" \" FROM history_deals\" f \" WHERE type IN { _TRADE_DEAL_TYPES_SQL } AND position_id != 0\" \" GROUP BY position_id, symbol\" \" HAVING SUM(CASE WHEN entry IN (1, 2, 3) THEN 1 ELSE 0 END) > 0\" , ) return True","title":"create_positions_reconstructed_view"},{"location":"api/history/#mt5cli.history.create_rate_compatibility_views","text":"create_rate_compatibility_views ( conn : Connection ) -> None Create rate compatibility views from the normalized rates table. Source code in mt5cli/history.py 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 def create_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None : \"\"\"Create rate compatibility views from the normalized rates table.\"\"\" columns = get_table_columns ( conn , Dataset . rates . table_name ) if not { \"symbol\" , \"timeframe\" , \"time\" } . issubset ( columns ): return drop_rate_compatibility_views ( conn ) select_columns = sorted ( columns - { \"symbol\" , \"timeframe\" }) quoted_columns = \", \" . join ( f '\" { column } \"' for column in select_columns ) rows = conn . execute ( \"SELECT DISTINCT symbol, timeframe FROM rates ORDER BY symbol, timeframe\" , ) . fetchall () timeframes_by_symbol : dict [ str , list [ int ]] = {} for symbol , timeframe in rows : timeframes_by_symbol . setdefault ( str ( symbol ), []) . append ( int ( timeframe )) for symbol , timeframes in timeframes_by_symbol . items (): for timeframe in timeframes : granularity = resolve_granularity_name ( timeframe ) view_name = build_rate_view_name ( symbol = symbol , granularity = granularity , granularity_count = len ( timeframes ), timeframe = timeframe , ) quoted_view_name = quote_sqlite_identifier ( view_name ) escaped_symbol = symbol . replace ( \"'\" , \"''\" ) conn . execute ( f \"CREATE VIEW { quoted_view_name } AS\" # noqa: S608 f \" SELECT { quoted_columns } FROM rates\" f \" WHERE symbol = ' { escaped_symbol } '\" f \" AND timeframe = { timeframe } \" , )","title":"create_rate_compatibility_views"},{"location":"api/history/#mt5cli.history.deduplicate_history_tables","text":"deduplicate_history_tables ( conn : Connection , written_columns : dict [ Dataset , set [ str ]], written_tables : set [ Dataset ], dedup_scopes : Mapping [ Dataset , Sequence [ DedupScope ]] | None = None , ) -> None Deduplicate appended history tables by stable identifiers. Scopes whose required columns are not present in the written table are skipped. If all scopes for a dataset are skipped, the table receives one unscoped deduplication pass instead. Source code in mt5cli/history.py 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 def deduplicate_history_tables ( conn : sqlite3 . Connection , written_columns : dict [ Dataset , set [ str ]], written_tables : set [ Dataset ], dedup_scopes : Mapping [ Dataset , Sequence [ DedupScope ]] | None = None , ) -> None : \"\"\"Deduplicate appended history tables by stable identifiers. Scopes whose required columns are not present in the written table are skipped. If all scopes for a dataset are skipped, the table receives one unscoped deduplication pass instead. \"\"\" cursor = conn . cursor () for dataset in written_tables : columns = written_columns . get ( dataset , set ()) table = dataset . table_name keys = next ( ( candidate for candidate in _HISTORY_DEDUP_KEYS [ dataset ] if set ( candidate ) . issubset ( columns ) ), None , ) if keys is None : logger . warning ( \"Skipping %s deduplication: no supported key columns\" , table , ) continue raw_scopes : Sequence [ DedupScope ] = ( dedup_scopes . get ( dataset , ()) if dedup_scopes else () ) scopes = [ scope for scope in raw_scopes if scope . required_columns <= columns ] if scopes : for scope in scopes : drop_duplicates_in_table ( cursor , table , list ( keys ), keep = \"last\" , scope_where = scope . where , scope_params = scope . params , ) continue drop_duplicates_in_table ( cursor , table , list ( keys ), keep = \"last\" )","title":"deduplicate_history_tables"},{"location":"api/history/#mt5cli.history.drop_duplicates_in_table","text":"drop_duplicates_in_table ( cursor : Cursor , table : str , ids : list [ str ], * , keep : Literal [ \"first\" , \"last\" ] = \"last\" , scope_where : str | None = None , scope_params : tuple [ object , ... ] = (), ) -> None Remove duplicate rows, keeping the first or last ROWID per key group. Raises: Type Description ValueError If the table or column names are invalid. Source code in mt5cli/history.py 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 def drop_duplicates_in_table ( cursor : sqlite3 . Cursor , table : str , ids : list [ str ], * , keep : Literal [ \"first\" , \"last\" ] = \"last\" , scope_where : str | None = None , scope_params : tuple [ object , ... ] = (), ) -> None : \"\"\"Remove duplicate rows, keeping the first or last ROWID per key group. Raises: ValueError: If the table or column names are invalid. \"\"\" if not table . isidentifier (): msg = f \"Invalid table name: { table } \" raise ValueError ( msg ) if invalid := { column for column in ids if not column . isidentifier ()}: msg = f \"Invalid column names: { ', ' . join ( sorted ( invalid )) } \" raise ValueError ( msg ) ids_csv = \", \" . join ( f '\" { column } \"' for column in ids ) rowid_selector = \"MIN\" if keep == \"first\" else \"MAX\" if scope_where : delete_sql = ( f \"DELETE FROM { table } WHERE { scope_where } AND ROWID NOT IN\" # noqa: S608 f \" (SELECT { rowid_selector } (ROWID) FROM { table } WHERE { scope_where } \" f \" GROUP BY { ids_csv } )\" ) cursor . execute ( delete_sql , scope_params + scope_params ) return cursor . execute ( f \"DELETE FROM { table } WHERE ROWID NOT IN\" # noqa: S608 f \" (SELECT { rowid_selector } (ROWID) FROM { table } GROUP BY { ids_csv } )\" , )","title":"drop_duplicates_in_table"},{"location":"api/history/#mt5cli.history.drop_forming_rate_bar","text":"drop_forming_rate_bar ( df_rate : DataFrame ) -> DataFrame Return closed bars from chronologically ordered MT5 rate data. MetaTrader 5 copy_rates_from_pos(start_pos=0) includes the still-forming current bar as the last row. Slice it off so downstream logic only sees completed bars. Empty frames and single-row frames return empty results. Parameters: Name Type Description Default df_rate DataFrame Rate data ordered oldest-to-newest with the forming bar last. required Returns: Type Description DataFrame A new DataFrame with all rows except the last. Index and columns are DataFrame preserved. The input frame is not modified. Source code in mt5cli/history.py 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 def drop_forming_rate_bar ( df_rate : pd . DataFrame ) -> pd . DataFrame : \"\"\"Return closed bars from chronologically ordered MT5 rate data. MetaTrader 5 ``copy_rates_from_pos(start_pos=0)`` includes the still-forming current bar as the last row. Slice it off so downstream logic only sees completed bars. Empty frames and single-row frames return empty results. Args: df_rate: Rate data ordered oldest-to-newest with the forming bar last. Returns: A new DataFrame with all rows except the last. Index and columns are preserved. The input frame is not modified. \"\"\" return df_rate . iloc [: - 1 ] . copy ()","title":"drop_forming_rate_bar"},{"location":"api/history/#mt5cli.history.drop_rate_compatibility_views","text":"drop_rate_compatibility_views ( conn : Connection ) -> None Drop all mt5cli-managed rate_* compatibility views. Source code in mt5cli/history.py 1361 1362 1363 1364 1365 1366 1367 1368 def drop_rate_compatibility_views ( conn : sqlite3 . Connection ) -> None : \"\"\"Drop all mt5cli-managed ``rate_*`` compatibility views.\"\"\" rows = conn . execute ( \"SELECT name FROM sqlite_master WHERE type = 'view' AND name GLOB 'rate_*'\" , ) . fetchall () for ( view_name ,) in rows : quoted_view_name = quote_sqlite_identifier ( str ( view_name )) conn . execute ( f \"DROP VIEW IF EXISTS { quoted_view_name } \" )","title":"drop_rate_compatibility_views"},{"location":"api/history/#mt5cli.history.filter_incremental_history_deals_frame","text":"filter_incremental_history_deals_frame ( frame : DataFrame , symbols : Sequence [ str ], start_by_symbol : dict [ str , datetime ], account_event_start : datetime , ) -> DataFrame Filter incrementally fetched history_deals by symbol and event start times. Returns: Type Description DataFrame Rows for selected symbols at or after each symbol start, plus account DataFrame events at or after account_event_start . Source code in mt5cli/history.py 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 def filter_incremental_history_deals_frame ( frame : pd . DataFrame , symbols : Sequence [ str ], start_by_symbol : dict [ str , datetime ], account_event_start : datetime , ) -> pd . DataFrame : \"\"\"Filter incrementally fetched history_deals by symbol and event start times. Returns: Rows for selected symbols at or after each symbol start, plus account events at or after ``account_event_start``. \"\"\" if frame . empty : return frame . copy () parsed_times = _frame_parsed_times ( frame ) time_valid = parsed_times . notna () account_event_mask = _history_deals_account_event_mask ( frame ) account_keep = account_event_mask & ( parsed_times >= account_event_start ) trade_keep = pd . Series ( data = False , index = frame . index ) if \"symbol\" in frame . columns : for symbol in symbols : trade_keep |= ( ( frame [ \"symbol\" ] == symbol ) & ( parsed_times >= start_by_symbol [ symbol ]) & ~ account_event_mask ) keep = ( account_keep | trade_keep ) & time_valid return frame . loc [ keep ] . copy ()","title":"filter_incremental_history_deals_frame"},{"location":"api/history/#mt5cli.history.filter_trade_history_frame","text":"filter_trade_history_frame ( frame : DataFrame , symbols : Sequence [ str ], * , include_account_events : bool , ) -> DataFrame Filter trade history rows to selected symbols and account events. Returns: Type Description DataFrame Filtered history rows. Source code in mt5cli/history.py 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 def filter_trade_history_frame ( frame : pd . DataFrame , symbols : Sequence [ str ], * , include_account_events : bool , ) -> pd . DataFrame : \"\"\"Filter trade history rows to selected symbols and account events. Returns: Filtered history rows. \"\"\" if \"symbol\" not in frame . columns : return frame symbol_mask = frame [ \"symbol\" ] . isin ( symbols ) if not include_account_events : return frame . loc [ symbol_mask ] . copy () account_event_mask = _history_deals_account_event_mask ( frame ) return frame . loc [ symbol_mask | account_event_mask ] . copy ()","title":"filter_trade_history_frame"},{"location":"api/history/#mt5cli.history.get_history_deals_account_event_start_datetime","text":"get_history_deals_account_event_start_datetime ( conn : Connection , * , fallback_start : datetime ) -> datetime Return the next update start for account-level history_deals rows. Source code in mt5cli/history.py 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 def get_history_deals_account_event_start_datetime ( conn : sqlite3 . Connection , * , fallback_start : datetime , ) -> datetime : \"\"\"Return the next update start for account-level history_deals rows.\"\"\" table = Dataset . history_deals . table_name columns = get_table_columns ( conn , table ) if \"time\" not in columns : return fallback_start if \"type\" in columns : where_clause = f \"type NOT IN { _TRADE_DEAL_TYPES_SQL } \" elif \"symbol\" in columns : where_clause = \"symbol IS NULL OR symbol = ''\" else : return fallback_start row = conn . execute ( f \"SELECT MAX(time) FROM { table } WHERE { where_clause } \" , # noqa: S608 ) . fetchone () parsed = parse_sqlite_timestamp ( row [ 0 ] if row else None ) return parsed if parsed is not None else fallback_start","title":"get_history_deals_account_event_start_datetime"},{"location":"api/history/#mt5cli.history.get_incremental_start_datetime","text":"get_incremental_start_datetime ( conn : Connection , dataset : Dataset , * , symbol : str , timeframe : int | None , fallback_start : datetime , ) -> datetime Return the next update start datetime from existing MAX(time). Source code in mt5cli/history.py 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 def get_incremental_start_datetime ( conn : sqlite3 . Connection , dataset : Dataset , * , symbol : str , timeframe : int | None , fallback_start : datetime , ) -> datetime : \"\"\"Return the next update start datetime from existing MAX(time).\"\"\" timeframes = [ timeframe ] if timeframe is not None else None starts = load_incremental_start_datetimes ( conn , dataset , symbols = [ symbol ], timeframes = timeframes , fallback_start = fallback_start , ) return starts [ symbol , timeframe ]","title":"get_incremental_start_datetime"},{"location":"api/history/#mt5cli.history.get_table_columns","text":"get_table_columns ( conn : Connection , table : str ) -> set [ str ] Return existing SQLite columns for a table. Source code in mt5cli/history.py 844 845 846 847 848 def get_table_columns ( conn : sqlite3 . Connection , table : str ) -> set [ str ]: \"\"\"Return existing SQLite columns for a table.\"\"\" quoted_table = quote_sqlite_identifier ( table ) rows = conn . execute ( f \"PRAGMA table_info( { quoted_table } )\" ) . fetchall () return { str ( row [ 1 ]) for row in rows }","title":"get_table_columns"},{"location":"api/history/#mt5cli.history.load_incremental_start_datetimes","text":"load_incremental_start_datetimes ( conn : Connection , dataset : Dataset , * , symbols : Sequence [ str ], timeframes : Sequence [ int ] | None = None , fallback_start : datetime , ) -> dict [ tuple [ str , int | None ], datetime ] Return next update start datetimes keyed by symbol and optional timeframe. Source code in mt5cli/history.py 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 def load_incremental_start_datetimes ( conn : sqlite3 . Connection , dataset : Dataset , * , symbols : Sequence [ str ], timeframes : Sequence [ int ] | None = None , fallback_start : datetime , ) -> dict [ tuple [ str , int | None ], datetime ]: \"\"\"Return next update start datetimes keyed by symbol and optional timeframe.\"\"\" table = dataset . table_name columns = get_table_columns ( conn , table ) if dataset is Dataset . rates and columns : _validate_rates_schema ( columns ) if \"time\" not in columns : if dataset is Dataset . rates and timeframes is not None : return { ( symbol , timeframe ): fallback_start for symbol in symbols for timeframe in timeframes } return {( symbol , None ): fallback_start for symbol in symbols } parsed_by_key : dict [ tuple [ str , int | None ], datetime ] = {} if ( dataset is Dataset . rates and timeframes is not None and { \"symbol\" , \"timeframe\" } . issubset ( columns ) ): symbol_placeholders = \", \" . join ( \"?\" for _ in symbols ) timeframe_placeholders = \", \" . join ( \"?\" for _ in timeframes ) grouped_rates_query = ( \"SELECT symbol, timeframe, MAX(time) FROM \" # noqa: S608 f \" { table } WHERE symbol IN ( { symbol_placeholders } )\" f \" AND timeframe IN ( { timeframe_placeholders } )\" \" GROUP BY symbol, timeframe\" ) rows = conn . execute ( grouped_rates_query , [ * symbols , * timeframes ], ) . fetchall () for row_symbol , row_timeframe , max_time in rows : parsed = parse_sqlite_timestamp ( max_time ) if parsed is not None : parsed_by_key [ str ( row_symbol ), int ( row_timeframe )] = parsed return { ( symbol , timeframe ): parsed_by_key . get ( ( symbol , timeframe ), fallback_start , ) for symbol in symbols for timeframe in timeframes } if \"symbol\" in columns : symbol_placeholders = \", \" . join ( \"?\" for _ in symbols ) rows = conn . execute ( f \"SELECT symbol, MAX(time) FROM { table } \" # noqa: S608 f \" WHERE symbol IN ( { symbol_placeholders } ) GROUP BY symbol\" , list ( symbols ), ) . fetchall () for row_symbol , max_time in rows : parsed = parse_sqlite_timestamp ( max_time ) if parsed is not None : parsed_by_key [ str ( row_symbol ), None ] = parsed return { ( symbol , None ): parsed_by_key . get (( symbol , None ), fallback_start ) for symbol in symbols } row = conn . execute ( f \"SELECT MAX(time) FROM { table } \" ) . fetchone () # noqa: S608 parsed = parse_sqlite_timestamp ( row [ 0 ] if row else None ) shared_start = parsed if parsed is not None else fallback_start return {( symbol , None ): shared_start for symbol in symbols }","title":"load_incremental_start_datetimes"},{"location":"api/history/#mt5cli.history.load_rate_data","text":"load_rate_data ( conn_or_path : SqliteConnOrPath , table : str , count : int | None = None , ) -> DataFrame Load rate-like data from a SQLite database path or connection. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath SQLite database path or open connection. required table str Source table or view name. required count int | None Optional number of most recent rows to load. None Returns: Type Description DataFrame DataFrame indexed by ascending time . Source code in mt5cli/history.py 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 def load_rate_data ( conn_or_path : SqliteConnOrPath , table : str , count : int | None = None , ) -> pd . DataFrame : \"\"\"Load rate-like data from a SQLite database path or connection. Args: conn_or_path: SQLite database path or open connection. table: Source table or view name. count: Optional number of most recent rows to load. Returns: DataFrame indexed by ascending ``time``. \"\"\" conn , should_close = _open_existing_sqlite_database ( conn_or_path ) try : return load_rate_data_from_connection ( conn , table , count = count ) finally : if should_close : conn . close ()","title":"load_rate_data"},{"location":"api/history/#mt5cli.history.load_rate_data_from_connection","text":"load_rate_data_from_connection ( connection : Connection , table : str , count : int | None = None , ) -> DataFrame Load rate-like data from a SQLite table or view. Parameters: Name Type Description Default connection Connection Open SQLite connection. required table str Source table or view name. required count int | None Optional number of most recent rows to load. None Returns: Type Description DataFrame DataFrame indexed by ascending time . Raises: Type Description ValueError If inputs, schema, timestamps are invalid, or the table or view contains no rows. Source code in mt5cli/history.py 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 def load_rate_data_from_connection ( connection : sqlite3 . Connection , table : str , count : int | None = None , ) -> pd . DataFrame : \"\"\"Load rate-like data from a SQLite table or view. Args: connection: Open SQLite connection. table: Source table or view name. count: Optional number of most recent rows to load. Returns: DataFrame indexed by ascending ``time``. Raises: ValueError: If inputs, schema, timestamps are invalid, or the table or view contains no rows. \"\"\" table_name = _validate_rate_load_request ( table , count ) columns = get_table_columns ( connection , table_name ) _ensure_rate_columns ( columns , table_name ) quoted_table = quote_sqlite_identifier ( table_name ) if count is None : frame = cast ( \"pd.DataFrame\" , pd . read_sql_query ( # type: ignore[reportUnknownMemberType] f \"SELECT * FROM { quoted_table } ORDER BY time ASC\" , # noqa: S608 connection , ), ) else : frame = cast ( \"pd.DataFrame\" , pd . read_sql_query ( # type: ignore[reportUnknownMemberType] f \"SELECT * FROM { quoted_table } ORDER BY time DESC LIMIT ?\" , # noqa: S608 connection , params = ( count ,), ), ) if frame . empty : msg = f \"SQLite table or view { table_name !r} contains no rows.\" raise ValueError ( msg ) return _parse_rate_time_index ( frame , table_name )","title":"load_rate_data_from_connection"},{"location":"api/history/#mt5cli.history.load_rate_series_by_granularity","text":"load_rate_series_by_granularity ( conn_or_path : SqliteConnOrPath , symbols : Sequence [ str ], granularities : Sequence [ int | str ], count : int , * , explicit_tables : Sequence [ str ] | None = None , allow_missing_symbol : bool = False , ) -> dict [ tuple [ str | None , str ], DataFrame ] Load rate series keyed by symbol and string granularity name. Builds targets with :func: build_rate_targets and loads them with :func: load_rate_series_from_sqlite , then rekeys the result by granularity name (for example M1 ) instead of the integer timeframe to reduce downstream boilerplate. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath SQLite database path or open connection. required symbols Sequence [ str ] MT5 symbol names. May be empty when allow_missing_symbol . required granularities Sequence [ int | str ] MT5 timeframes as integers or names (for example M1 ). required count int Number of most recent rows to load per series. required explicit_tables Sequence [ str ] | None Optional explicit table or view names matching the built targets in row-major order. Required when symbols are omitted. None allow_missing_symbol bool When True and symbols is empty, build targets with symbol=None for each granularity instead of raising. False Returns: Type Description dict [ tuple [ str | None, str ], DataFrame ] Mapping keyed by (symbol | None, granularity_name) to each rate dict [ tuple [ str | None, str ], DataFrame ] DataFrame. Propagates ValueError (via :func: build_rate_targets and dict [ tuple [ str | None, str ], DataFrame ] func: load_rate_series_from_sqlite ) when inputs are empty or invalid, dict [ tuple [ str | None, str ], DataFrame ] table resolution fails, or duplicate targets are present. Source code in mt5cli/history.py 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 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 def load_rate_series_by_granularity ( conn_or_path : SqliteConnOrPath , symbols : Sequence [ str ], granularities : Sequence [ int | str ], count : int , * , explicit_tables : Sequence [ str ] | None = None , allow_missing_symbol : bool = False , ) -> dict [ tuple [ str | None , str ], pd . DataFrame ]: \"\"\"Load rate series keyed by symbol and string granularity name. Builds targets with :func:`build_rate_targets` and loads them with :func:`load_rate_series_from_sqlite`, then rekeys the result by granularity name (for example ``M1``) instead of the integer timeframe to reduce downstream boilerplate. Args: conn_or_path: SQLite database path or open connection. symbols: MT5 symbol names. May be empty when ``allow_missing_symbol``. granularities: MT5 timeframes as integers or names (for example ``M1``). count: Number of most recent rows to load per series. explicit_tables: Optional explicit table or view names matching the built targets in row-major order. Required when symbols are omitted. allow_missing_symbol: When True and ``symbols`` is empty, build targets with ``symbol=None`` for each granularity instead of raising. Returns: Mapping keyed by ``(symbol | None, granularity_name)`` to each rate DataFrame. Propagates ``ValueError`` (via :func:`build_rate_targets` and :func:`load_rate_series_from_sqlite`) when inputs are empty or invalid, table resolution fails, or duplicate targets are present. \"\"\" targets = build_rate_targets ( symbols , granularities , allow_missing_symbol = allow_missing_symbol , ) series = load_rate_series_from_sqlite ( conn_or_path , targets , count , explicit_tables = explicit_tables , ) return { ( symbol , resolve_granularity_name ( timeframe )): frame for ( symbol , timeframe ), frame in series . items () }","title":"load_rate_series_by_granularity"},{"location":"api/history/#mt5cli.history.load_rate_series_from_sqlite","text":"load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : None = None , count : int | None = None , explicit_tables : None = None , * , table : str , ) -> DataFrame load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : None = None , count : int | None = None , explicit_tables : Sequence [ str ] | None = None , * , table : None = None , ) -> dict [ tuple [ str | None , int ], DataFrame ] load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : Sequence [ RateTarget ], count : int , explicit_tables : Sequence [ str ] | None = None , * , table : None = None , ) -> dict [ tuple [ str | None , int ], DataFrame ] load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : Sequence [ RateTarget ] | None = None , count : int | None = None , explicit_tables : Sequence [ str ] | None = None , * , table : str | None = None , ) -> dict [ tuple [ str | None , int ], DataFrame ] | DataFrame Load one table/view or multiple rate series from a SQLite database. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath SQLite database path or open connection. required targets Sequence [ RateTarget ] | None Rate targets to load. Each (symbol, timeframe_int) pair must be unique. Omit when loading a single explicit table . None count int | None Optional number of most recent rows to load per series. None explicit_tables Sequence [ str ] | None Optional explicit table or view names matching targets. When omitted, managed rate_* compatibility views must already exist in the database. None table str | None Optional single table or view name to load directly. None Returns: Type Description dict [ tuple [ str | None, int ], DataFrame ] | DataFrame A DataFrame when table is provided, otherwise a mapping keyed by dict [ tuple [ str | None, int ], DataFrame ] | DataFrame (symbol, timeframe_int) to each rate DataFrame. Raises: Type Description ValueError If count is not positive, targets are empty, duplicate (symbol, timeframe_int) pairs are present, or table resolution fails. Source code in mt5cli/history.py 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 def load_rate_series_from_sqlite ( conn_or_path : SqliteConnOrPath , targets : Sequence [ RateTarget ] | None = None , count : int | None = None , explicit_tables : Sequence [ str ] | None = None , * , table : str | None = None , ) -> dict [ tuple [ str | None , int ], pd . DataFrame ] | pd . DataFrame : \"\"\"Load one table/view or multiple rate series from a SQLite database. Args: conn_or_path: SQLite database path or open connection. targets: Rate targets to load. Each ``(symbol, timeframe_int)`` pair must be unique. Omit when loading a single explicit ``table``. count: Optional number of most recent rows to load per series. explicit_tables: Optional explicit table or view names matching targets. When omitted, managed ``rate_*`` compatibility views must already exist in the database. table: Optional single table or view name to load directly. Returns: A DataFrame when ``table`` is provided, otherwise a mapping keyed by ``(symbol, timeframe_int)`` to each rate DataFrame. Raises: ValueError: If ``count`` is not positive, targets are empty, duplicate ``(symbol, timeframe_int)`` pairs are present, or table resolution fails. \"\"\" if table is not None : return load_rate_data ( conn_or_path , table , count = count ) if count is None or count <= 0 : msg = \"count must be positive.\" raise ValueError ( msg ) if targets is None : msg = \"targets are required when table is not provided.\" raise ValueError ( msg ) target_list = list ( targets ) if not target_list : msg = \"At least one rate target is required.\" raise ValueError ( msg ) if explicit_tables is None and any ( target . symbol is None for target in target_list ): msg = ( \"Cannot resolve a rate table for a target without a symbol; \" \"provide explicit_tables.\" ) raise ValueError ( msg ) seen_keys : set [ tuple [ str | None , int ]] = set () for target in target_list : key = ( target . symbol , target . timeframe_int ) if key in seen_keys : symbol_repr = repr ( target . symbol ) msg = f \"Duplicate rate target: ( { symbol_repr } , { target . timeframe_int } )\" raise ValueError ( msg ) seen_keys . add ( key ) tables = ( resolve_rate_tables ( None , target_list , explicit_tables ) if explicit_tables is not None else None ) conn , should_close = _open_existing_sqlite_database ( conn_or_path ) try : resolved_tables = tables or resolve_rate_tables ( conn , target_list , require_existing = True , ) return { ( target . symbol , target . timeframe_int ): load_rate_data_from_connection ( conn , table , count = count , ) for target , table in zip ( target_list , resolved_tables , strict = True ) } finally : if should_close : conn . close ()","title":"load_rate_series_from_sqlite"},{"location":"api/history/#mt5cli.history.parse_sqlite_timestamp","text":"parse_sqlite_timestamp ( value : object ) -> datetime | None Parse a SQLite history timestamp value. Returns: Type Description datetime | None Parsed timezone-aware datetime, or None when parsing fails. Source code in mt5cli/history.py 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 def parse_sqlite_timestamp ( value : object ) -> datetime | None : \"\"\"Parse a SQLite history timestamp value. Returns: Parsed timezone-aware datetime, or None when parsing fails. \"\"\" if value is None : return None if isinstance ( value , datetime ): return value if value . tzinfo is not None else value . replace ( tzinfo = UTC ) if isinstance ( value , int | float ): return datetime . fromtimestamp ( float ( value ), tz = UTC ) if isinstance ( value , str ): return _parse_string_sqlite_timestamp ( value ) logger . warning ( \"Ignoring unsupported history timestamp type: %s \" , type ( value )) return None","title":"parse_sqlite_timestamp"},{"location":"api/history/#mt5cli.history.quote_sqlite_identifier","text":"quote_sqlite_identifier ( identifier : str ) -> str Return a safely quoted SQLite identifier using double quotes. Source code in mt5cli/history.py 61 62 63 def quote_sqlite_identifier ( identifier : str ) -> str : \"\"\"Return a safely quoted SQLite identifier using double quotes.\"\"\" return '\"' + identifier . replace ( '\"' , '\"\"' ) + '\"'","title":"quote_sqlite_identifier"},{"location":"api/history/#mt5cli.history.record_written_columns","text":"record_written_columns ( written_columns : dict [ Dataset , set [ str ]], dataset : Dataset , frame : DataFrame , ) -> None Remember columns for datasets written during collection. Source code in mt5cli/history.py 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 def record_written_columns ( written_columns : dict [ Dataset , set [ str ]], dataset : Dataset , frame : pd . DataFrame , ) -> None : \"\"\"Remember columns for datasets written during collection.\"\"\" columns = set ( frame . columns ) if dataset in written_columns : written_columns [ dataset ] . update ( columns ) else : written_columns [ dataset ] = columns","title":"record_written_columns"},{"location":"api/history/#mt5cli.history.resolve_granularity_name","text":"resolve_granularity_name ( timeframe : int ) -> str Return a granularity name for a timeframe integer when known. Source code in mt5cli/history.py 107 108 109 110 111 112 113 def resolve_granularity_name ( timeframe : int ) -> str : \"\"\"Return a granularity name for a timeframe integer when known.\"\"\" try : name = _get_timeframe_name ( timeframe ) except ValueError : return str ( timeframe ) return name . removeprefix ( \"TIMEFRAME_\" )","title":"resolve_granularity_name"},{"location":"api/history/#mt5cli.history.resolve_history_datasets","text":"resolve_history_datasets ( datasets : set [ Dataset ] | None , ) -> set [ Dataset ] Resolve configured history datasets. Returns: Type Description set [ Dataset ] DEFAULT_HISTORY_DATASETS (rates, history-orders, history-deals) set [ Dataset ] when datasets is None, otherwise the configured selection (which set [ Dataset ] may be empty or explicitly include Dataset.ticks ). Source code in mt5cli/history.py 66 67 68 69 70 71 72 73 74 75 76 def resolve_history_datasets ( datasets : set [ Dataset ] | None ) -> set [ Dataset ]: \"\"\"Resolve configured history datasets. Returns: ``DEFAULT_HISTORY_DATASETS`` (rates, history-orders, history-deals) when ``datasets`` is None, otherwise the configured selection (which may be empty or explicitly include ``Dataset.ticks``). \"\"\" if datasets is None : return set ( DEFAULT_HISTORY_DATASETS ) return set ( datasets )","title":"resolve_history_datasets"},{"location":"api/history/#mt5cli.history.resolve_history_tick_flags","text":"resolve_history_tick_flags ( flags : int | str ) -> int Resolve tick copy flags from an integer or name. Returns: Type Description int Integer tick flag value. Source code in mt5cli/history.py 98 99 100 101 102 103 104 def resolve_history_tick_flags ( flags : int | str ) -> int : \"\"\"Resolve tick copy flags from an integer or name. Returns: Integer tick flag value. \"\"\" return parse_tick_flags ( flags )","title":"resolve_history_tick_flags"},{"location":"api/history/#mt5cli.history.resolve_history_timeframes","text":"resolve_history_timeframes ( timeframes : Sequence [ int | str ] | None , ) -> list [ int ] Resolve rate timeframes, deduplicating aliases for the same integer. Returns: Type Description list [ int ] Ordered list of unique timeframe integers. Source code in mt5cli/history.py 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 def resolve_history_timeframes ( timeframes : Sequence [ int | str ] | None , ) -> list [ int ]: \"\"\"Resolve rate timeframes, deduplicating aliases for the same integer. Returns: Ordered list of unique timeframe integers. \"\"\" raw = timeframes if timeframes is not None else DEFAULT_HISTORY_TIMEFRAMES seen : set [ int ] = set () resolved : list [ int ] = [] for value in raw : tf = parse_timeframe ( value ) if tf not in seen : seen . add ( tf ) resolved . append ( tf ) return resolved","title":"resolve_history_timeframes"},{"location":"api/history/#mt5cli.history.resolve_rate_table_name","text":"resolve_rate_table_name ( symbol : str , granularity : str ) -> str Return the canonical normalized SQLite rate table name. The normalized history table stores all symbols and timeframes in rates ; use :func: resolve_rate_view_name for per-symbol compatibility view names. Returns: Type Description str Canonical normalized rates table name. Raises: Type Description ValueError If symbol or granularity is invalid. Source code in mt5cli/history.py 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 def resolve_rate_table_name ( symbol : str , granularity : str ) -> str : \"\"\"Return the canonical normalized SQLite rate table name. The normalized history table stores all symbols and timeframes in ``rates``; use :func:`resolve_rate_view_name` for per-symbol compatibility view names. Returns: Canonical normalized rates table name. Raises: ValueError: If ``symbol`` or ``granularity`` is invalid. \"\"\" parse_timeframe ( granularity ) if not symbol . strip (): msg = \"symbol must not be empty.\" raise ValueError ( msg ) return Dataset . rates . table_name","title":"resolve_rate_table_name"},{"location":"api/history/#mt5cli.history.resolve_rate_tables","text":"resolve_rate_tables ( conn_or_path : SqliteConnOrPath | None , targets : Sequence [ RateTarget ], explicit_tables : Sequence [ str ] | None = None , * , require_existing : bool = False , ) -> list [ str ] Resolve SQLite table or view names for rate targets. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath | None SQLite database path or open connection. May be None when explicit_tables is provided, or when require_existing is False and deterministic default view names are sufficient. required targets Sequence [ RateTarget ] Rate targets to resolve. required explicit_tables Sequence [ str ] | None Optional explicit table or view names. When provided, they are used as-is and must match the number of targets. None require_existing bool When True, require the database and managed views to exist for each symbol target. Ignored when explicit_tables is provided. False Returns: Type Description list [ str ] Table or view names aligned with targets . Raises: Type Description ValueError If targets is empty, explicit_tables length does not match the target count, a target without a symbol is resolved without an explicit table, or require_existing is True and the database or a managed view is missing. Source code in mt5cli/history.py 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 def resolve_rate_tables ( conn_or_path : SqliteConnOrPath | None , targets : Sequence [ RateTarget ], explicit_tables : Sequence [ str ] | None = None , * , require_existing : bool = False , ) -> list [ str ]: \"\"\"Resolve SQLite table or view names for rate targets. Args: conn_or_path: SQLite database path or open connection. May be None when ``explicit_tables`` is provided, or when ``require_existing`` is False and deterministic default view names are sufficient. targets: Rate targets to resolve. explicit_tables: Optional explicit table or view names. When provided, they are used as-is and must match the number of targets. require_existing: When True, require the database and managed views to exist for each symbol target. Ignored when ``explicit_tables`` is provided. Returns: Table or view names aligned with ``targets``. Raises: ValueError: If ``targets`` is empty, ``explicit_tables`` length does not match the target count, a target without a symbol is resolved without an explicit table, or ``require_existing`` is True and the database or a managed view is missing. \"\"\" target_list = list ( targets ) if not target_list : msg = \"At least one rate target is required.\" raise ValueError ( msg ) if explicit_tables is not None : tables = list ( explicit_tables ) if len ( tables ) != len ( target_list ): msg = ( f \"Expected { len ( target_list ) } explicit table(s) \" f \"to match the targets, got { len ( tables ) } .\" ) raise ValueError ( msg ) return tables if any ( target . symbol is None for target in target_list ): msg = ( \"Cannot resolve a rate table for a target without a symbol; \" \"provide explicit_tables.\" ) raise ValueError ( msg ) conn , should_close = _open_history_connection ( conn_or_path ) try : if conn is None : if require_existing : path = ( conn_or_path if isinstance ( conn_or_path , ( Path , str )) else \"database\" ) msg = f \"SQLite database not found: { path } \" raise ValueError ( msg ) timeframe_counts = None existing_views : set [ str ] = set () else : timeframe_counts = _load_rates_timeframe_counts ( conn ) existing_views = _load_existing_rate_views ( conn ) resolved : list [ str ] = [] for target in target_list : symbol = cast ( \"str\" , target . symbol ) timeframe = target . timeframe_int resolved . append ( _resolve_rate_view_name_from_context ( symbol = symbol , timeframe = timeframe , granularity_name = resolve_granularity_name ( timeframe ), timeframe_counts = timeframe_counts , existing_views = existing_views , require_existing = require_existing , ), ) return resolved finally : if should_close and conn is not None : conn . close ()","title":"resolve_rate_tables"},{"location":"api/history/#mt5cli.history.resolve_rate_view_name","text":"resolve_rate_view_name ( conn_or_path : SqliteConnOrPath | None , symbol : str , granularity : str , * , require_existing : bool = False , ) -> str Resolve the mt5cli-managed rate compatibility view name. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath | None SQLite database path or open connection. When None or a non-existing path and require_existing is False, the deterministic default view name is returned without creating a database file. required symbol str Symbol stored in the normalized rates table. required granularity str Timeframe name (for example M1 ) or integer string. required require_existing bool When True, require the database and a managed view to exist. False Returns: Type Description str View name such as rate_EURUSD__1 or rate_EURUSD__M1_1 . Raises: Type Description ValueError If require_existing is True and the database or view is missing. Source code in mt5cli/history.py 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 def resolve_rate_view_name ( conn_or_path : SqliteConnOrPath | None , symbol : str , granularity : str , * , require_existing : bool = False , ) -> str : \"\"\"Resolve the mt5cli-managed rate compatibility view name. Args: conn_or_path: SQLite database path or open connection. When None or a non-existing path and ``require_existing`` is False, the deterministic default view name is returned without creating a database file. symbol: Symbol stored in the normalized ``rates`` table. granularity: Timeframe name (for example ``M1``) or integer string. require_existing: When True, require the database and a managed view to exist. Returns: View name such as ``rate_EURUSD__1`` or ``rate_EURUSD__M1_1``. Raises: ValueError: If ``require_existing`` is True and the database or view is missing. \"\"\" timeframe = parse_timeframe ( granularity ) granularity_name = resolve_granularity_name ( timeframe ) conn , should_close = _open_history_connection ( conn_or_path ) try : if conn is None : if require_existing : path = ( conn_or_path if isinstance ( conn_or_path , ( Path , str )) else \"database\" ) msg = f \"SQLite database not found: { path } \" raise ValueError ( msg ) return build_rate_view_name ( symbol = symbol , granularity = granularity_name , granularity_count = 1 , timeframe = timeframe , ) return _resolve_rate_view_name_from_context ( symbol = symbol , timeframe = timeframe , granularity_name = granularity_name , timeframe_counts = _load_rates_timeframe_counts ( conn ), existing_views = _load_existing_rate_views ( conn ), require_existing = require_existing , ) finally : if should_close and conn is not None : conn . close ()","title":"resolve_rate_view_name"},{"location":"api/history/#mt5cli.history.resolve_rate_view_names","text":"resolve_rate_view_names ( conn_or_path : SqliteConnOrPath | None , symbols : Sequence [ str ], granularities : Sequence [ str ], * , require_existing : bool = False , ) -> list [ str ] Resolve rate compatibility view names for symbol and granularity pairs. Parameters: Name Type Description Default conn_or_path SqliteConnOrPath | None SQLite database path or open connection. When None or a non-existing path and require_existing is False, deterministic default view names are returned without creating a database file. required symbols Sequence [ str ] Symbols stored in the normalized rates table. required granularities Sequence [ str ] Timeframe names (for example M1 ) or integer strings. required require_existing bool When True, require the database and managed views to exist. False Returns: Type Description list [ str ] View names in row-major order: every granularity for the first list [ str ] symbol, then every granularity for the next symbol, and so on. Source code in mt5cli/history.py 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 def resolve_rate_view_names ( conn_or_path : SqliteConnOrPath | None , symbols : Sequence [ str ], granularities : Sequence [ str ], * , require_existing : bool = False , ) -> list [ str ]: \"\"\"Resolve rate compatibility view names for symbol and granularity pairs. Args: conn_or_path: SQLite database path or open connection. When None or a non-existing path and ``require_existing`` is False, deterministic default view names are returned without creating a database file. symbols: Symbols stored in the normalized ``rates`` table. granularities: Timeframe names (for example ``M1``) or integer strings. require_existing: When True, require the database and managed views to exist. Returns: View names in row-major order: every ``granularity`` for the first symbol, then every granularity for the next symbol, and so on. \"\"\" conn , should_close = _open_history_connection ( conn_or_path ) try : if conn is None : return [ resolve_rate_view_name ( conn_or_path , symbol , granularity , require_existing = require_existing , ) for symbol in symbols for granularity in granularities ] timeframe_counts = _load_rates_timeframe_counts ( conn ) existing_views = _load_existing_rate_views ( conn ) resolved : list [ str ] = [] for symbol in symbols : for granularity in granularities : timeframe = parse_timeframe ( granularity ) resolved . append ( _resolve_rate_view_name_from_context ( symbol = symbol , timeframe = timeframe , granularity_name = resolve_granularity_name ( timeframe ), timeframe_counts = timeframe_counts , existing_views = existing_views , require_existing = require_existing , ), ) return resolved finally : if should_close and conn is not None : conn . close ()","title":"resolve_rate_view_names"},{"location":"api/history/#mt5cli.history.write_collected_datasets","text":"write_collected_datasets ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], datasets : set [ Dataset ], timeframe : int , flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Collect selected datasets and stream each symbol frame into SQLite. Returns: Type Description tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Written datasets and their columns. Source code in mt5cli/history.py 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 def write_collected_datasets ( conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], datasets : set [ Dataset ], timeframe : int , flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]: \"\"\"Collect selected datasets and stream each symbol frame into SQLite. Returns: Written datasets and their columns. \"\"\" written_columns : dict [ Dataset , set [ str ]] = {} written_tables : set [ Dataset ] = set () if Dataset . rates in datasets and write_rates_dataset ( conn , client , symbols , timeframe , date_from , date_to , if_exists , written_columns , ): written_tables . add ( Dataset . rates ) if Dataset . ticks in datasets and write_ticks_dataset ( conn , client , symbols , flags , date_from , date_to , if_exists , written_columns , ): written_tables . add ( Dataset . ticks ) if Dataset . history_orders in datasets and write_history_dataset ( conn , client . history_orders_get_as_df , Dataset . history_orders , symbols , date_from , date_to , if_exists , written_columns , include_account_events = False , ): written_tables . add ( Dataset . history_orders ) if Dataset . history_deals in datasets and write_history_dataset ( conn , client . history_deals_get_as_df , Dataset . history_deals , symbols , date_from , date_to , if_exists , written_columns , include_account_events = False , ): written_tables . add ( Dataset . history_deals ) return written_tables , written_columns","title":"write_collected_datasets"},{"location":"api/history/#mt5cli.history.write_history_dataset","text":"write_history_dataset ( conn : Connection , fetch : Callable [ ... , DataFrame ], dataset : Dataset , symbols : Sequence [ str ], date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], * , include_account_events : bool = False , ) -> bool Stream a history dataset into SQLite. Returns: Type Description bool True if the target table was written. Source code in mt5cli/history.py 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 def write_history_dataset ( conn : sqlite3 . Connection , fetch : Callable [ ... , pd . DataFrame ], dataset : Dataset , symbols : Sequence [ str ], date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], * , include_account_events : bool = False , ) -> bool : \"\"\"Stream a history dataset into SQLite. Returns: True if the target table was written. \"\"\" table_exists = False if include_account_events : frame = filter_trade_history_frame ( fetch ( date_from = date_from , date_to = date_to ), symbols , include_account_events = True , ) return write_streamed_frame ( conn , frame , dataset , table_exists , if_exists , written_columns , ) def _fetch_history_frame ( sym : str ) -> pd . DataFrame : return filter_trade_history_frame ( fetch ( date_from = date_from , date_to = date_to , symbol = sym ), [ sym ], include_account_events = False , ) return _stream_symbol_frames ( conn , symbols , dataset , if_exists , written_columns , _fetch_history_frame , )","title":"write_history_dataset"},{"location":"api/history/#mt5cli.history.write_incremental_datasets","text":"write_incremental_datasets ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], selected_datasets : set [ Dataset ], resolved_timeframes : list [ int ], resolved_tick_flags : int , fallback_start : datetime , end_date : datetime , * , deduplicate : bool , create_rate_views : bool , with_views : bool , include_account_events : bool , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Append selected datasets incrementally and refresh indexes and views. Returns: Type Description tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]] Written datasets and their columns. Source code in mt5cli/history.py 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 def write_incremental_datasets ( # noqa: PLR0913 conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], selected_datasets : set [ Dataset ], resolved_timeframes : list [ int ], resolved_tick_flags : int , fallback_start : datetime , end_date : datetime , * , deduplicate : bool , create_rate_views : bool , with_views : bool , include_account_events : bool , ) -> tuple [ set [ Dataset ], dict [ Dataset , set [ str ]]]: \"\"\"Append selected datasets incrementally and refresh indexes and views. Returns: Written datasets and their columns. \"\"\" written_columns : dict [ Dataset , set [ str ]] = {} written_tables : set [ Dataset ] = set () dedup_scopes : dict [ Dataset , list [ DedupScope ]] = {} if Dataset . rates in selected_datasets : _write_incremental_rates ( conn , client , symbols , resolved_timeframes , fallback_start , end_date , written_columns , written_tables , dedup_scopes , ) if Dataset . ticks in selected_datasets : _write_incremental_ticks ( conn , client , symbols , resolved_tick_flags , fallback_start , end_date , written_columns , written_tables , dedup_scopes , ) if Dataset . history_orders in selected_datasets : _write_incremental_history_orders ( conn , client , symbols , fallback_start , end_date , written_columns , written_tables , dedup_scopes , ) if Dataset . history_deals in selected_datasets : _write_incremental_history_deals ( conn , client , symbols , fallback_start , end_date , written_columns , written_tables , dedup_scopes , include_account_events = include_account_events , ) _finalize_incremental_writes ( conn , selected_datasets , written_columns , written_tables , dedup_scopes , deduplicate = deduplicate , create_rate_views = create_rate_views , with_views = with_views , ) return written_tables , written_columns","title":"write_incremental_datasets"},{"location":"api/history/#mt5cli.history.write_rates_dataset","text":"write_rates_dataset ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], timeframe : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool Stream rates frames into SQLite. Returns: Type Description bool True if the rates table was written. Source code in mt5cli/history.py 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 def write_rates_dataset ( conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], timeframe : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool : \"\"\"Stream rates frames into SQLite. Returns: True if the rates table was written. \"\"\" def _fetch_rates_frame ( sym : str ) -> pd . DataFrame : frame = client . copy_rates_range_as_df ( symbol = sym , timeframe = timeframe , date_from = date_from , date_to = date_to , ) . drop ( columns = [ \"symbol\" , \"timeframe\" ], errors = \"ignore\" ) if len ( frame . columns ) != 0 : frame . insert ( 0 , \"symbol\" , sym ) frame . insert ( 1 , \"timeframe\" , timeframe ) return frame return _stream_symbol_frames ( conn , symbols , Dataset . rates , if_exists , written_columns , _fetch_rates_frame , )","title":"write_rates_dataset"},{"location":"api/history/#mt5cli.history.write_streamed_frame","text":"write_streamed_frame ( conn : Connection , frame : DataFrame , dataset : Dataset , table_exists : bool , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool Write one streamed dataset frame and track table state. Returns: Type Description bool True if the dataset table exists after this write attempt. Source code in mt5cli/history.py 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 def write_streamed_frame ( conn : sqlite3 . Connection , frame : pd . DataFrame , dataset : Dataset , table_exists : bool , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool : \"\"\"Write one streamed dataset frame and track table state. Returns: True if the dataset table exists after this write attempt. \"\"\" write_mode = IfExists . APPEND if table_exists else if_exists if append_dataframe ( conn , frame , dataset . table_name , write_mode ): record_written_columns ( written_columns , dataset , frame ) return True return table_exists","title":"write_streamed_frame"},{"location":"api/history/#mt5cli.history.write_ticks_dataset","text":"write_ticks_dataset ( conn : Connection , client : Mt5DataClient , symbols : Sequence [ str ], flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool Stream ticks frames into SQLite. Returns: Type Description bool True if the ticks table was written. Source code in mt5cli/history.py 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 def write_ticks_dataset ( conn : sqlite3 . Connection , client : Mt5DataClient , symbols : Sequence [ str ], flags : int , date_from : datetime , date_to : datetime , if_exists : IfExists , written_columns : dict [ Dataset , set [ str ]], ) -> bool : \"\"\"Stream ticks frames into SQLite. Returns: True if the ticks table was written. \"\"\" def _fetch_ticks_frame ( sym : str ) -> pd . DataFrame : frame = client . copy_ticks_range_as_df ( symbol = sym , date_from = date_from , date_to = date_to , flags = flags , ) . drop ( columns = [ \"symbol\" ], errors = \"ignore\" ) if len ( frame . columns ) != 0 : frame . insert ( 0 , \"symbol\" , sym ) return frame return _stream_symbol_frames ( conn , symbols , Dataset . ticks , if_exists , written_columns , _fetch_ticks_frame , )","title":"write_ticks_dataset"},{"location":"api/history/#collect-history-schema","text":"The collect-history command (and the matching collect_history SDK function) writes selected MT5 datasets into one SQLite database. Each dataset becomes a table; column names and types mirror the pdmt5 DataFrame schema for that export, with two additions: symbol is prepended on every table. timeframe is prepended on rates so appended runs at different bar sizes stay distinguishable. SQLite does not declare foreign keys. Rows are linked logically by symbol , time windows, and (for deals) position_id / order . Duplicate rows are removed on append using dataset-specific keys (for example ticket on history tables, or (symbol, timeframe, time) on rates). Optional views are created when --with-views is set and the history-deals dataset was written.","title":"collect-history schema"},{"location":"api/history/#entity-relationship-diagram","text":"Sample layout for a full collection with --with-views : erDiagram rates { TEXT symbol \"dedup key\" INTEGER timeframe \"dedup key\" TEXT time \"dedup key\" REAL open REAL high REAL low REAL close INTEGER tick_volume INTEGER spread INTEGER real_volume } ticks { TEXT symbol \"dedup key\" TEXT time \"dedup key\" INTEGER time_msc \"dedup key (preferred)\" REAL bid REAL ask REAL last INTEGER volume INTEGER flags REAL volume_real } history_orders { INTEGER ticket \"dedup key\" TEXT symbol TEXT time INTEGER type INTEGER state REAL volume_initial REAL price_open REAL price_current INTEGER magic } history_deals { INTEGER ticket \"dedup key\" INTEGER order INTEGER position_id \"groups position view\" TEXT symbol TEXT time INTEGER type \"0/1 trade, else cash event\" INTEGER entry \"0 IN, 1 OUT, 2 INOUT, 3 OUT_BY\" REAL volume REAL price REAL profit REAL commission REAL swap REAL fee } cash_events { INTEGER ticket TEXT symbol TEXT time INTEGER type REAL profit } positions_reconstructed { INTEGER position_id TEXT symbol TEXT open_time TEXT close_time INTEGER direction REAL volume_open REAL volume_close REAL volume_reversal REAL open_price REAL close_price REAL total_profit INTEGER reversal_count INTEGER deals_count } rates ||--o{ history_deals : \"symbol (logical)\" ticks ||--o{ history_deals : \"symbol (logical)\" history_orders ||--o{ history_deals : \"order ~ ticket (logical)\" history_deals ||--|| cash_events : \"VIEW: type NOT IN (0,1)\" history_deals ||--o{ positions_reconstructed : \"VIEW: GROUP BY position_id\"","title":"Entity-relationship diagram"},{"location":"api/history/#tables-and-views","text":"Object Kind Source Notes rates table copy_rates_range Indexed on (symbol, timeframe, time) when columns exist. ticks table copy_ticks_range Indexed on (symbol, time) when columns exist. history_orders table history_orders_get Fetched per --symbol , then concatenated. history_deals table history_deals_get Fetched per --symbol , then concatenated. Indexed on (position_id, symbol) when present. cash_events view history_deals Non-trade deal types (deposits, balance ops, etc.). Requires type column. positions_reconstructed view history_deals One row per closed position_id ; volume-weighted prices and reversal stats. Column sets can vary with terminal and pdmt5 version. Views are skipped with a warning when required columns are missing.","title":"Tables and views"},{"location":"api/history/#incremental-collection","text":"The update_history SDK path uses the same base tables and optional cash_events / positions_reconstructed views. It additionally maintains rate___ compatibility views when create_rate_views=True .","title":"Incremental collection"},{"location":"api/history/#rate-view-resolution","text":"Downstream tools can resolve mt5cli-managed compatibility view names from an existing SQLite history database without creating files or guessing naming schemes: from pathlib import Path from mt5cli.history import resolve_rate_view_name , resolve_rate_view_names # Single symbol and granularity view = resolve_rate_view_name ( Path ( \"history.db\" ), \"EURUSD\" , \"M1\" ) # Batch resolution in row-major order views = resolve_rate_view_names ( Path ( \"history.db\" ), [ \"EURUSD\" , \"GBPUSD\" ], [ \"M1\" , \"H1\" ], ) Resolution rules: Returns rate___ when a symbol stores one timeframe. Returns rate____ when multiple timeframes are stored for the same symbol. When multiple naming candidates apply, prefers an existing managed rate_*__* view from the candidate list. Falls back to single-timeframe naming when the database path is missing or rates metadata is unavailable. Pass require_existing=True to raise ValueError instead of returning a best-guess name when the database or view is missing. Accepts either a SQLite path or an open sqlite3.Connection .","title":"Rate view resolution"},{"location":"api/history/#rate-data-loading","text":"The canonical normalized rate table is rates ; compatibility views are named with rate___ for single-timeframe symbols or rate___