Lấy số lượng và danh sách các vị thế đang mở
Hàm positions_total trả về số lượng các vị thế đang mở.
int positions_total()
Hàm này là một tương tự của PositionsTotal.
Để lấy thông tin chi tiết về từng vị thế, sử dụng hàm positions_get với nhiều tùy chọn. Tất cả các biến thể trả về một mảng của các tuple có tên TradePosition với các khóa tương ứng với thuộc tính vị thế (xem các phần tử của ENUM_POSITION_PROPERTY_enumerations, không có tiền tố "POSITION_", ở dạng chữ thường). Trong trường hợp xảy ra lỗi, kết quả là None.
namedtuple[] positions_get()
namedtuple[] positions_get(symbol = SYMBOL)
namedtuple[] positions_get(group = PATTERN)
namedtuple[] positions_get(ticket = TICKET)
Hàm không có tham số trả về tất cả các vị thế đang mở.
Hàm với tham số symbol cho phép chọn các vị thế cho biểu tượng được chỉ định.
Hàm với tham số group cung cấp khả năng lọc theo mặt nạ tìm kiếm với ký tự đại diện '*' (thay thế bất kỳ ký tự nào) và phủ định logic '!'. Để biết chi tiết, xem phần Lấy thông tin về các công cụ tài chính.
Phiên bản với tham số ticket chọn một vị thế với mã cụ thể (thuộc tính POSITION_TICKET).
Hàm positions_get có thể được sử dụng để lấy tất cả các vị thế và thuộc tính của chúng trong một lần gọi, điều này khiến nó tương tự như tập hợp các hàm PositionsTotal, PositionSelect, và PositionGet functions.
Trong script MQL5/Scripts/MQL5Book/Python/positionsget.py, chúng ta yêu cầu các vị thế cho một biểu tượng cụ thể và mặt nạ tìm kiếm.
import MetaTrader5 as mt5
import pandas as pd
pd.set_option('display.max_columns', 500) # how many columns to show
pd.set_option('display.width', 1500) # max. table width to display
# let's establish a connection to the MetaTrader 5 terminal
if not mt5.initialize():
print("initialize() failed, error code =", mt5.last_error())
quit()
# get open positions on USDCHF
positions = mt5.positions_get(symbol = "USDCHF")
if positions == None:
print("No positions on USDCHF, error code={}".format(mt5.last_error()))
elif len(positions) > 0:
print("Total positions on USDCHF =", len(positions))
# display all open positions
for position in positions:
print(position)
# get a list of positions on symbols whose names contain "*USD*"
usd_positions = mt5.positions_get(group = "*USD*")
if usd_positions == None:
print("No positions with group=\"*USD*\", error code={}".format(mt5.last_error()))
elif len(usd_positions) > 0:
print("positions_get(group=\"*USD*\") = {}".format(len(usd_positions)))
# display the positions as a table using pandas.DataFrame
df=pd.DataFrame(list(usd_positions), columns = usd_positions[0]._asdict().keys())
df['time'] = pd.to_datetime(df['time'], unit='s')
df.drop(['time_update', 'time_msc', 'time_update_msc', 'external_id'],
axis=1, inplace=True)
print(df)
# complete the connection to the MetaTrader 5 terminal
mt5.shutdown()2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Dưới đây là kết quả có thể xảy ra:
Total positions on USDCHF = 1
TradePosition(ticket=1468454363, time=1664217233, time_msc=1664217233239, time_update=1664217233,
time_update_msc=1664217233239, type=1, magic=0, identifier=1468454363, reason=0, volume=0.01, price_open=0.99145,
sl=0.0, tp=0.0, price_current=0.9853, swap=-0.01, profit=6.24, symbol='USDCHF', comment='', external_id='')
positions_get(group="*USD*") = 2
ticket time type ... identifier volume price_open ... _current swap profit symbol comment
0 1468454363 2022-09-26 18:33:53 1 ... 1468454363 0.01 0.99145 ... 0.98530 -0.01 6.24 USDCHF
1 1468475849 2022-09-26 18:44:00 0 ... 1468475849 0.01 1.06740 ... 1.08113 0.00 13.73 GBPUSD2
3
4
5
6
7
8
