CLI Module¶
mt5cli.cli ¶
Command-line interface for MetaTrader 5 data and execution utilities.
app
module-attribute
¶
app = typer.Typer(
name="mt5cli",
help="MT5 data and execution utilities — 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.",
)
account_info ¶
close_positions ¶
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,
filling_mode: Annotated[
str | None,
Option(
"--filling-mode",
help="Order filling mode for close requests (IOC, FOK, or RETURN). Defaults to per-symbol broker resolution.",
),
] = None,
deviation: Annotated[
int | None,
Option(
help="Optional slippage/deviation for each close request."
),
] = None,
comment: Annotated[
str | None,
Option(
help="Optional comment attached to each close request."
),
] = None,
magic: Annotated[
int | None,
Option(
help="Optional magic tag for close requests and position filtering."
),
] = 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 |
Source code in mt5cli/cli.py
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
collect_history ¶
collect_history(
ctx: Context,
symbol: Annotated[
list[str],
Option(
"--symbol",
"-s",
help="Symbol to collect (repeat for multiple symbols).",
),
],
date_from: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
help="Start date.",
),
],
date_to: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
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 and symbols metadata are opt-in: pass --dataset ticks or --dataset symbols to include them.",
),
] = None,
timeframe: Annotated[
int,
Option(
parser=_parse_timeframe_parameter,
help="Rates timeframe (e.g., M1, H1, D1).",
),
] = 1,
flags: Annotated[
int,
Option(
parser=_parse_tick_flags_parameter,
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).
symbols is also opt-in: pass --dataset symbols to snapshot
per-symbol metadata (point, digits, contract size, volume limits) at
date_to, one row per symbol per collection.
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
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 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 | |
history_deals ¶
history_deals(
ctx: Context,
date_from: Annotated[
datetime | None,
Option(
parser=_parse_datetime_parameter,
help="Start date.",
),
] = None,
date_to: Annotated[
datetime | None,
Option(
parser=_parse_datetime_parameter,
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
history_gaps ¶
history_gaps(
ctx: Context,
sqlite3_path: Annotated[
Path,
Option(
"--sqlite3",
help="Source SQLite history database to analyze.",
),
],
table: Annotated[
list[str] | None,
Option(
"--table",
help="Rate table to inspect (repeat for multiple); defaults to the canonical rates table.",
),
] = None,
granularity_seconds: Annotated[
int | None,
Option(
help="Explicit bar interval in seconds for custom tables without timeframe metadata."
),
] = None,
min_gap_intervals: Annotated[
int,
Option(
help="Minimum missing-bar count required to emit a gap row."
),
] = 1,
) -> None
Export SQLite rate gaps without connecting to MT5.
Raises:
| Type | Description |
|---|---|
BadParameter
|
If the database, table, or granularity is invalid. |
Source code in mt5cli/cli.py
history_orders ¶
history_orders(
ctx: Context,
date_from: Annotated[
datetime | None,
Option(
parser=_parse_datetime_parameter,
help="Start date.",
),
] = None,
date_to: Annotated[
datetime | None,
Option(
parser=_parse_datetime_parameter,
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
last_error ¶
latest_rates ¶
latest_rates(
ctx: Context,
symbol: Annotated[str, Option(help="Symbol name.")],
timeframe: Annotated[
int,
Option(
parser=_parse_timeframe_parameter,
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
main ¶
market_book ¶
Export market depth (order book) for a symbol.
Source code in mt5cli/cli.py
minimum_margins ¶
Export minimum-volume buy and sell margin requirements.
Source code in mt5cli/cli.py
mt5_summary ¶
Export a compact terminal/account status summary.
order_check ¶
order_check(
ctx: Context,
request: Annotated[
dict[str, Any],
Option(
parser=_parse_request_parameter,
help=_REQUEST_OPTION_HELP,
),
],
) -> None
Check funds sufficiency for a trading operation.
Source code in mt5cli/cli.py
order_send ¶
order_send(
ctx: Context,
request: Annotated[
dict[str, Any],
Option(
parser=_parse_request_parameter,
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 — 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
orders ¶
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
positions ¶
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
publish_dashboard_command ¶
publish_dashboard_command(
ctx: Context,
sqlite3_path: Annotated[
Path,
Option(
"--sqlite3",
help="Source SQLite history database to publish from.",
),
],
) -> None
Publish Parquet analytics datasets for static dashboards without MT5.
The global --output is the manifest path (for example
dist/data/manifest.json); trades, daily_pnl,
strategy_stats, equity and account_snapshots Parquet files are
written beside it. Requires the parquet extra.
Raises:
| Type | Description |
|---|---|
BadParameter
|
If the source database is invalid, the manifest path is not JSON, or no dataset can be built. |
Source code in mt5cli/cli.py
rates_from ¶
rates_from(
ctx: Context,
symbol: Annotated[str, Option(help="Symbol name.")],
timeframe: Annotated[
int,
Option(
parser=_parse_timeframe_parameter,
help="Timeframe (e.g., M1, H1, D1, or integer).",
),
],
date_from: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
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
rates_from_pos ¶
rates_from_pos(
ctx: Context,
symbol: Annotated[str, Option(help="Symbol name.")],
timeframe: Annotated[
int,
Option(
parser=_parse_timeframe_parameter,
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
rates_range ¶
rates_range(
ctx: Context,
symbol: Annotated[str, Option(help="Symbol name.")],
timeframe: Annotated[
int,
Option(
parser=_parse_timeframe_parameter,
help="Timeframe.",
),
],
date_from: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
help="Start date.",
),
],
date_to: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
help="End date.",
),
],
) -> None
Export rates for a date range.
Source code in mt5cli/cli.py
recent_history_deals ¶
recent_history_deals(
ctx: Context,
hours: Annotated[
float, Option(help="Lookback window in hours.")
],
date_to: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
help="Window end date.",
),
],
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
snapshot ¶
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,
) -> 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
symbol_info ¶
Export symbol details.
Source code in mt5cli/cli.py
symbol_info_tick ¶
Export the last tick for a symbol.
Source code in mt5cli/cli.py
symbols ¶
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
terminal_info ¶
ticks_from ¶
ticks_from(
ctx: Context,
symbol: Annotated[str, Option(help="Symbol name.")],
date_from: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
help="Start date.",
),
],
count: Annotated[int, Option(help="Number of ticks.")],
flags: Annotated[
int,
Option(
parser=_parse_tick_flags_parameter,
help="Tick flags (ALL, INFO, TRADE, or integer).",
),
],
) -> None
Export ticks from a start date.
Source code in mt5cli/cli.py
ticks_range ¶
ticks_range(
ctx: Context,
symbol: Annotated[str, Option(help="Symbol name.")],
date_from: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
help="Start date.",
),
],
date_to: Annotated[
datetime,
Option(
parser=_parse_datetime_parameter,
help="End date.",
),
],
flags: Annotated[
int,
Option(
parser=_parse_tick_flags_parameter,
help="Tick flags.",
),
],
) -> None
Export ticks for a date range.
Source code in mt5cli/cli.py
ticks_recent ¶
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(
parser=_parse_datetime_parameter,
help="Window end date.",
),
] = None,
count: Annotated[
int,
Option(help="Maximum number of ticks to return."),
] = 10000,
flags: Annotated[
int,
Option(
parser=_parse_tick_flags_parameter,
help="Tick flags (ALL, INFO, TRADE, or integer).",
),
] = "ALL",
) -> None
Export ticks from a recent time window.