Coverage for src/PythonMetaTrader5/legacy/views.py: 100%

47 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-12 12:29 +0000

1"""Positions and receipts in the exact shapes the 2.x API returned. 

2 

32.x handed out raw MetaTrader5 objects (TradePosition, OrderSendResult), so 

4scripts read any field, index them and call ``_asdict()``. NamedTuples with the 

5same field names in the same order keep all three working. 

6""" 

7 

8from __future__ import annotations 

9 

10from typing import Any, NamedTuple 

11 

12from PythonMetaTrader5.domain.market import Position, TradeReceipt 

13from PythonMetaTrader5.domain.orders import Side 

14 

15# MT5 POSITION_TYPE_BUY / POSITION_TYPE_SELL: 2.x callers compare `p.type == 0`. 

16_POSITION_TYPE = {Side.BUY: 0, Side.SELL: 1} 

17 

18# The keys 2.x `as_dict=True` built by hand. A projection of as_object() rather 

19# than a second mapping, so the shared fields cannot drift apart; only sl/tp 

20# differ on purpose (None instead of 0.0, as in 2.x). 

21_V2_DICT_KEYS = ( 

22 "ticket", 

23 "symbol", 

24 "type", 

25 "volume", 

26 "price_open", 

27 "sl", 

28 "tp", 

29 "price_current", 

30 "profit", 

31 "time", 

32 "magic", 

33 "comment", 

34) 

35 

36 

37class LegacyPosition(NamedTuple): 

38 """Field-for-field and in order the same as MetaTrader5.TradePosition.""" 

39 

40 ticket: int 

41 time: int 

42 time_msc: int 

43 time_update: int 

44 time_update_msc: int 

45 type: int 

46 magic: int 

47 identifier: int 

48 reason: int 

49 volume: float 

50 price_open: float 

51 sl: float 

52 tp: float 

53 price_current: float 

54 swap: float 

55 profit: float 

56 symbol: str 

57 comment: str 

58 external_id: str 

59 

60 

61class LegacyReceipt(NamedTuple): 

62 """Field-for-field and in order the same as MetaTrader5.OrderSendResult.""" 

63 

64 retcode: int 

65 deal: int 

66 order: int 

67 volume: float 

68 price: float 

69 bid: float 

70 ask: float 

71 comment: str 

72 request_id: int 

73 retcode_external: int 

74 # The echoed TradeRequest is not kept by 3.x; always None. 

75 request: None = None 

76 

77 

78def as_object(p: Position) -> LegacyPosition: 

79 return LegacyPosition( 

80 ticket=p.ticket, 

81 time=p.time, 

82 time_msc=p.time_msc, 

83 time_update=p.time_update, 

84 time_update_msc=p.time_update_msc, 

85 type=_POSITION_TYPE[p.side], 

86 magic=p.magic, 

87 identifier=p.identifier, 

88 reason=p.reason, 

89 volume=p.volume, 

90 price_open=p.price_open, 

91 # Raw MT5 objects report a missing stop as 0.0; 2.x passed them through as is. 

92 sl=p.sl or 0.0, 

93 tp=p.tp or 0.0, 

94 price_current=p.price_current, 

95 swap=p.swap, 

96 profit=p.profit, 

97 symbol=p.symbol, 

98 comment=p.comment, 

99 external_id=p.external_id, 

100 ) 

101 

102 

103def as_dict(p: Position) -> dict[str, Any]: 

104 row = as_object(p)._asdict() 

105 # 2.x `as_dict=True` used None for a missing stop, unlike the raw objects. 

106 return {key: row[key] for key in _V2_DICT_KEYS} | {"sl": p.sl, "tp": p.tp} 

107 

108 

109def as_receipt(receipt: TradeReceipt | None) -> LegacyReceipt | None: 

110 if receipt is None: 

111 return None 

112 return LegacyReceipt( 

113 retcode=receipt.retcode, 

114 deal=receipt.deal, 

115 order=receipt.order, 

116 volume=receipt.volume, 

117 price=receipt.price, 

118 bid=receipt.bid, 

119 ask=receipt.ask, 

120 comment=receipt.comment, 

121 request_id=receipt.request_id, 

122 retcode_external=receipt.retcode_external, 

123 )