CLI Module¶
mt5cli.cli ¶
Command-line interface for MetaTrader 5 data export.
TICK_FLAG_MAP
module-attribute
¶
TIMEFRAME_MAP
module-attribute
¶
TIMEFRAME_MAP: dict[str, int] = {
"M1": 1,
"M2": 2,
"M3": 3,
"M4": 4,
"M5": 5,
"M6": 6,
"M10": 10,
"M12": 12,
"M15": 15,
"M20": 20,
"M30": 30,
"H1": 16385,
"H2": 16386,
"H3": 16387,
"H4": 16388,
"H6": 16390,
"H8": 16392,
"H12": 16396,
"D1": 16408,
"W1": 32769,
"MN1": 49153,
}
app
module-attribute
¶
Dataset ¶
Bases: StrEnum
Datasets supported by the collect-history command.
IfExists ¶
LogLevel ¶
OutputFormat ¶
account_info ¶
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(
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 all: rates, ticks, history-orders, history-deals.",
),
] = 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).",
),
] = 1,
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, ticks,
history_orders, history_deals. 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
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 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 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 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 | |
detect_format ¶
Detect the output format from a file extension or explicit format string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Path
|
Path to the output file. |
required |
explicit_format
|
str | None
|
Explicitly specified format, if any. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The detected format string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format cannot be determined. |
Source code in mt5cli/cli.py
export_dataframe ¶
export_dataframe(
df: DataFrame,
output_path: Path,
output_format: str,
table_name: str = "data",
) -> None
Export a pandas DataFrame to the specified file format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame to export. |
required |
output_path
|
Path
|
Path to the output file. |
required |
output_format
|
str
|
Output format (csv, json, parquet, or sqlite3). |
required |
table_name
|
str
|
Table name for SQLite3 output. |
'data'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the output format is not supported. |
Source code in mt5cli/cli.py
history_deals ¶
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
history_orders ¶
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
last_error ¶
main ¶
market_book ¶
Export market depth (order book) for a symbol.
Source code in mt5cli/cli.py
order_check ¶
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
order_send ¶
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 trading operation request to the trade server.
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
parse_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/cli.py
parse_request ¶
Parse a JSON-formatted order request string or file reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
JSON object string, or '@path' to read JSON from a file. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Parsed request dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the request file cannot be read or the value is not a JSON object. |
Source code in mt5cli/cli.py
parse_tick_flags ¶
Parse tick flags string or integer value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Tick flag name (ALL, INFO, TRADE) or integer value. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer tick flag value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the flag is invalid. |
Source code in mt5cli/cli.py
parse_timeframe ¶
Parse a timeframe string or integer value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
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/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
rates_from ¶
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
rates_from_pos ¶
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
rates_range ¶
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
symbol_info ¶
Export symbol details.
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(
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
ticks_range ¶
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.