MT5APIOrder.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //+------------------------------------------------------------------+
  2. //| MetaTrader 5 API |
  3. //| Copyright 2000-2019, MetaQuotes Software Corp. |
  4. //| http://www.metaquotes.net |
  5. //+------------------------------------------------------------------+
  6. #pragma once
  7. //+------------------------------------------------------------------+
  8. //| Trade Order Interface |
  9. //+------------------------------------------------------------------+
  10. class IMTOrder
  11. {
  12. public:
  13. //--- order types
  14. enum EnOrderType
  15. {
  16. OP_BUY =0, // buy order
  17. OP_SELL =1, // sell order
  18. OP_BUY_LIMIT =2, // buy limit order
  19. OP_SELL_LIMIT =3, // sell limit order
  20. OP_BUY_STOP =4, // buy stop order
  21. OP_SELL_STOP =5, // sell stop order
  22. OP_BUY_STOP_LIMIT =6, // buy stop limit order
  23. OP_SELL_STOP_LIMIT =7, // sell stop limit order
  24. OP_CLOSE_BY =8, // close by
  25. //--- enumeration borders
  26. OP_FIRST =OP_BUY,
  27. OP_LAST =OP_CLOSE_BY
  28. };
  29. //--- order filling types
  30. enum EnOrderFilling
  31. {
  32. ORDER_FILL_FOK =0, // fill or kill
  33. ORDER_FILL_IOC =1, // immediate or cancel
  34. ORDER_FILL_RETURN =2, // return order in queue
  35. //--- enumeration borders
  36. ORDER_FILL_FIRST =ORDER_FILL_FOK,
  37. ORDER_FILL_LAST =ORDER_FILL_RETURN
  38. };
  39. //--- order expiration types
  40. enum EnOrderTime
  41. {
  42. ORDER_TIME_GTC =0, // good till cancel
  43. ORDER_TIME_DAY =1, // good till day
  44. ORDER_TIME_SPECIFIED =2, // good till specified
  45. ORDER_TIME_SPECIFIED_DAY =3, // good till specified day
  46. //--- enumeration borders
  47. ORDER_TIME_FIRST =ORDER_TIME_GTC,
  48. ORDER_TIME_LAST =ORDER_TIME_SPECIFIED_DAY
  49. };
  50. //--- order state
  51. enum EnOrderState
  52. {
  53. ORDER_STATE_STARTED =0, // order started
  54. ORDER_STATE_PLACED =1, // order placed in system
  55. ORDER_STATE_CANCELED =2, // order canceled by client
  56. ORDER_STATE_PARTIAL =3, // order partially filled
  57. ORDER_STATE_FILLED =4, // order filled
  58. ORDER_STATE_REJECTED =5, // order rejected
  59. ORDER_STATE_EXPIRED =6, // order expired
  60. ORDER_STATE_REQUEST_ADD =7, // order requested to add
  61. ORDER_STATE_REQUEST_MODIFY=8, // order requested to modify
  62. ORDER_STATE_REQUEST_CANCEL=9, // order requested to cancel
  63. //--- enumeration borders
  64. ORDER_STATE_FIRST =ORDER_STATE_STARTED,
  65. ORDER_STATE_LAST =ORDER_STATE_REQUEST_CANCEL
  66. };
  67. //--- order activation state
  68. enum EnOrderActivation
  69. {
  70. ACTIVATION_NONE =0, // none
  71. ACTIVATION_PENDING =1, // pending order activated
  72. ACTIVATION_STOPLIMIT =2, // stop-limit order activated
  73. ACTIVATION_EXPIRATION =3, // order expired
  74. ACTIVATION_STOPOUT =4, // order activate for stop-out
  75. //--- enumeration borders
  76. ACTIVATION_FIRST =ACTIVATION_NONE,
  77. ACTIVATION_LAST =ACTIVATION_STOPOUT
  78. };
  79. //--- order creation reasons
  80. enum EnOrderReason
  81. {
  82. ORDER_REASON_CLIENT =0, // order placed manually
  83. ORDER_REASON_EXPERT =1, // order placed by expert
  84. ORDER_REASON_DEALER =2, // order placed by dealer
  85. ORDER_REASON_SL =3, // order placed due SL
  86. ORDER_REASON_TP =4, // order placed due TP
  87. ORDER_REASON_SO =5, // order placed due Stop-Out
  88. ORDER_REASON_ROLLOVER =6, // order placed due rollover
  89. ORDER_REASON_EXTERNAL_CLIENT =7, // order placed from the external system by client
  90. ORDER_REASON_VMARGIN =8, // order placed due variation margin
  91. ORDER_REASON_GATEWAY =9, // order placed by gateway
  92. ORDER_REASON_SIGNAL =10, // order placed by signal service
  93. ORDER_REASON_SETTLEMENT =11, // order placed by settlement
  94. ORDER_REASON_TRANSFER =12, // order placed due transfer
  95. ORDER_REASON_SYNC =13, // order placed due synchronization
  96. ORDER_REASON_EXTERNAL_SERVICE=14,// order placed from the external system due service issues
  97. ORDER_REASON_MIGRATION =15, // order placed due account migration from MetaTrader 4 or MetaTrader 5
  98. ORDER_REASON_MOBILE =16, // order placed manually by mobile terminal
  99. ORDER_REASON_WEB =17, // order placed manually by web terminal
  100. ORDER_REASON_SPLIT =18, // order placed due split
  101. //--- enumeration borders
  102. ORDER_REASON_FIRST =ORDER_REASON_CLIENT,
  103. ORDER_REASON_LAST =ORDER_REASON_SPLIT
  104. };
  105. //--- order activation flags
  106. enum EnTradeActivationFlags
  107. {
  108. ACTIV_FLAGS_NO_LIMIT =0x00000001,
  109. ACTIV_FLAGS_NO_STOP =0x00000002,
  110. ACTIV_FLAGS_NO_SLIMIT =0x00000004,
  111. ACTIV_FLAGS_NO_SL =0x00000008,
  112. ACTIV_FLAGS_NO_TP =0x00000010,
  113. ACTIV_FLAGS_NO_SO =0x00000020,
  114. ACTIV_FLAGS_NO_EXPIRATION=0x00000040,
  115. //--- enumeration borders
  116. ACTIV_FLAGS_NONE =0x00000000,
  117. ACTIV_FLAGS_ALL =ACTIV_FLAGS_NO_LIMIT|ACTIV_FLAGS_NO_STOP|ACTIV_FLAGS_NO_SLIMIT|ACTIV_FLAGS_NO_SL|
  118. ACTIV_FLAGS_NO_TP|ACTIV_FLAGS_NO_SO|ACTIV_FLAGS_NO_EXPIRATION
  119. };
  120. //--- modification flags
  121. enum EnTradeModifyFlags
  122. {
  123. MODIFY_FLAGS_ADMIN =0x00000001,
  124. MODIFY_FLAGS_MANAGER =0x00000002,
  125. MODIFY_FLAGS_POSITION =0x00000004,
  126. MODIFY_FLAGS_RESTORE =0x00000008,
  127. MODIFY_FLAGS_API_ADMIN =0x00000010,
  128. MODIFY_FLAGS_API_MANAGER =0x00000020,
  129. MODIFY_FLAGS_API_SERVER =0x00000040,
  130. MODIFY_FLAGS_API_GATEWAY =0x00000080,
  131. //--- enumeration borders
  132. MODIFY_FLAGS_NONE =0x00000000,
  133. MODIFY_FLAGS_ALL =MODIFY_FLAGS_ADMIN|MODIFY_FLAGS_MANAGER|MODIFY_FLAGS_POSITION|MODIFY_FLAGS_RESTORE|
  134. MODIFY_FLAGS_API_ADMIN|MODIFY_FLAGS_API_MANAGER|MODIFY_FLAGS_API_SERVER|MODIFY_FLAGS_API_GATEWAY
  135. };
  136. //--- common methods
  137. virtual void Release(void)=0;
  138. virtual MTAPIRES Assign(const IMTOrder* order)=0;
  139. virtual MTAPIRES Clear(void)=0;
  140. virtual LPCWSTR Print(MTAPISTR& string) const=0;
  141. //--- order ticket
  142. virtual UINT64 Order(void) const=0;
  143. //--- order ticket in external system (exchange, ECN, etc)
  144. virtual LPCWSTR ExternalID(void) const=0;
  145. virtual MTAPIRES ExternalID(LPCWSTR id)=0;
  146. //--- client login
  147. virtual UINT64 Login(void) const=0;
  148. virtual MTAPIRES Login(const UINT64 order)=0;
  149. //--- processed dealer login (0-means auto)
  150. virtual UINT64 Dealer(void) const=0;
  151. virtual MTAPIRES Dealer(const UINT64 dealer)=0;
  152. //--- order symbol
  153. virtual LPCWSTR Symbol(void) const=0;
  154. virtual MTAPIRES Symbol(LPCWSTR symbol)=0;
  155. //--- price digits
  156. virtual UINT Digits(void) const=0;
  157. virtual MTAPIRES Digits(const UINT digits)=0;
  158. //--- currency digits
  159. virtual UINT DigitsCurrency(void) const=0;
  160. virtual MTAPIRES DigitsCurrency(const UINT digits)=0;
  161. //--- contract size
  162. virtual double ContractSize(void) const=0;
  163. virtual MTAPIRES ContractSize(const double contract_size)=0;
  164. //--- EnOrderState
  165. virtual UINT State(void) const=0;
  166. //--- EnOrderReason
  167. virtual UINT Reason(void) const=0;
  168. //--- order setup time
  169. virtual INT64 TimeSetup(void) const=0;
  170. virtual MTAPIRES TimeSetup(const INT64 time)=0;
  171. //--- order expiration
  172. virtual INT64 TimeExpiration(void) const=0;
  173. virtual MTAPIRES TimeExpiration(const INT64 time)=0;
  174. //--- order filling/cancel time
  175. virtual INT64 TimeDone(void) const=0;
  176. virtual MTAPIRES TimeDone(const INT64 time)=0;
  177. //--- EnOrderType
  178. virtual UINT Type(void) const=0;
  179. virtual MTAPIRES Type(const UINT type)=0;
  180. //--- EnOrderFilling
  181. virtual UINT TypeFill(void) const=0;
  182. virtual MTAPIRES TypeFill(const UINT type)=0;
  183. //--- EnOrderTime
  184. virtual UINT TypeTime(void) const=0;
  185. virtual MTAPIRES TypeTime(const UINT type)=0;
  186. //--- order price
  187. virtual double PriceOrder(void) const=0;
  188. virtual MTAPIRES PriceOrder(const double price)=0;
  189. //--- order trigger price (stop-limit price)
  190. virtual double PriceTrigger(void) const=0;
  191. virtual MTAPIRES PriceTrigger(const double price)=0;
  192. //--- order current price
  193. virtual double PriceCurrent(void) const=0;
  194. virtual MTAPIRES PriceCurrent(const double price)=0;
  195. //--- order SL
  196. virtual double PriceSL(void) const=0;
  197. virtual MTAPIRES PriceSL(const double price)=0;
  198. //--- order TP
  199. virtual double PriceTP(void) const=0;
  200. virtual MTAPIRES PriceTP(const double price)=0;
  201. //--- order initial volume
  202. virtual UINT64 VolumeInitial(void) const=0;
  203. virtual MTAPIRES VolumeInitial(const UINT64 volume)=0;
  204. //--- order current volume
  205. virtual UINT64 VolumeCurrent(void) const=0;
  206. virtual MTAPIRES VolumeCurrent(const UINT64 volume)=0;
  207. //--- expert id (filled by expert advisor)
  208. virtual UINT64 ExpertID(void) const=0;
  209. virtual MTAPIRES ExpertID(const UINT64 id)=0;
  210. //--- position id
  211. virtual UINT64 PositionID(void) const=0;
  212. virtual MTAPIRES PositionID(const UINT64 id)=0;
  213. //--- order comment
  214. virtual LPCWSTR Comment(void) const=0;
  215. virtual MTAPIRES Comment(LPCWSTR comment)=0;
  216. //--- order activation state, time and price
  217. virtual UINT ActivationMode(void) const=0;
  218. virtual INT64 ActivationTime(void) const=0;
  219. virtual double ActivationPrice(void) const=0;
  220. virtual UINT ActivationFlags(void) const=0;
  221. //--- order internal data for API usage
  222. virtual MTAPIRES ApiDataSet(const USHORT app_id,const UCHAR id,const INT64 value)=0;
  223. virtual MTAPIRES ApiDataSet(const USHORT app_id,const UCHAR id,const UINT64 value)=0;
  224. virtual MTAPIRES ApiDataSet(const USHORT app_id,const UCHAR id,const double value)=0;
  225. virtual MTAPIRES ApiDataGet(const USHORT app_id,const UCHAR id,INT64& value) const=0;
  226. virtual MTAPIRES ApiDataGet(const USHORT app_id,const UCHAR id,UINT64& value) const=0;
  227. virtual MTAPIRES ApiDataGet(const USHORT app_id,const UCHAR id,double& value) const=0;
  228. virtual MTAPIRES ApiDataClear(const USHORT app_id)=0;
  229. virtual MTAPIRES ApiDataClearAll(void)=0;
  230. //--- order setup time in msc since 1970.01.01
  231. virtual INT64 TimeSetupMsc(void) const=0;
  232. virtual MTAPIRES TimeSetupMsc(const INT64 time)=0;
  233. //--- order setup time in msc since 1970.01.01
  234. virtual INT64 TimeDoneMsc(void) const=0;
  235. virtual MTAPIRES TimeDoneMsc(const INT64 time)=0;
  236. //--- order activation state, time and price
  237. virtual MTAPIRES ActivationMode(const UINT mode)=0;
  238. virtual MTAPIRES ActivationTime(const INT64 atm)=0;
  239. virtual MTAPIRES ActivationPrice(const double price)=0;
  240. virtual MTAPIRES ActivationFlags(const UINT flags)=0;
  241. //--- margin conversion rate (from symbol margin currency to deposit currency)
  242. virtual double RateMargin(void) const=0;
  243. virtual MTAPIRES RateMargin(const double rate)=0;
  244. //--- user record internal data for API usage
  245. virtual MTAPIRES ApiDataUpdate(const UINT pos,const USHORT app_id,const UCHAR id,const INT64 value)=0;
  246. virtual MTAPIRES ApiDataUpdate(const UINT pos,const USHORT app_id,const UCHAR id,const UINT64 value)=0;
  247. virtual MTAPIRES ApiDataUpdate(const UINT pos,const USHORT app_id,const UCHAR id,const double value)=0;
  248. virtual MTAPIRES ApiDataNext(const UINT pos,USHORT& app_id,UCHAR& id,INT64& value) const=0;
  249. virtual MTAPIRES ApiDataNext(const UINT pos,USHORT& app_id,UCHAR& id,UINT64& value) const=0;
  250. virtual MTAPIRES ApiDataNext(const UINT pos,USHORT& app_id,UCHAR& id,double& value) const=0;
  251. //--- order ticket
  252. virtual MTAPIRES OrderSet(const UINT64 order)=0;
  253. //--- position by id
  254. virtual UINT64 PositionByID(void) const=0;
  255. virtual MTAPIRES PositionByID(const UINT64 id)=0;
  256. //--- modification flags
  257. virtual UINT ModificationFlags(void) const=0;
  258. //--- EnOrderState
  259. virtual MTAPIRES StateSet(const UINT state)=0;
  260. //--- EnOrderReason
  261. virtual MTAPIRES ReasonSet(const UINT reason)=0;
  262. //--- order initial volume with extended accuracy
  263. virtual UINT64 VolumeInitialExt(void) const=0;
  264. virtual MTAPIRES VolumeInitialExt(const UINT64 volume)=0;
  265. //--- order current volume with extended accuracy
  266. virtual UINT64 VolumeCurrentExt(void) const=0;
  267. virtual MTAPIRES VolumeCurrentExt(const UINT64 volume)=0;
  268. };
  269. //+------------------------------------------------------------------+
  270. //| Order array interface |
  271. //+------------------------------------------------------------------+
  272. class IMTOrderArray
  273. {
  274. public:
  275. //--- common methods
  276. virtual void Release(void)=0;
  277. virtual MTAPIRES Assign(const IMTOrderArray* array)=0;
  278. virtual MTAPIRES Clear(void)=0;
  279. //--- add
  280. virtual MTAPIRES Add(IMTOrder* order)=0;
  281. virtual MTAPIRES AddCopy(const IMTOrder* order)=0;
  282. virtual MTAPIRES Add(IMTOrderArray* array)=0;
  283. virtual MTAPIRES AddCopy(const IMTOrderArray* array)=0;
  284. //--- delete & detach
  285. virtual MTAPIRES Delete(const UINT pos)=0;
  286. virtual IMTOrder* Detach(const UINT pos)=0;
  287. //--- update
  288. virtual MTAPIRES Update(const UINT pos,IMTOrder* order)=0;
  289. virtual MTAPIRES UpdateCopy(const UINT pos,const IMTOrder* order)=0;
  290. virtual MTAPIRES Shift(const UINT pos,const int shift)=0;
  291. //--- data access
  292. virtual UINT Total(void) const=0;
  293. virtual IMTOrder* Next(const UINT index) const=0;
  294. //--- sorting and search
  295. virtual MTAPIRES Sort(MTSortFunctionPtr sort_function)=0;
  296. virtual int Search(const void *key,MTSortFunctionPtr sort_function) const=0;
  297. virtual int SearchGreatOrEq(const void *key,MTSortFunctionPtr sort_function) const=0;
  298. virtual int SearchGreater(const void *key,MTSortFunctionPtr sort_function) const=0;
  299. virtual int SearchLessOrEq(const void *key,MTSortFunctionPtr sort_function) const=0;
  300. virtual int SearchLess(const void *key,MTSortFunctionPtr sort_function) const=0;
  301. virtual int SearchLeft(const void *key,MTSortFunctionPtr sort_function) const=0;
  302. virtual int SearchRight(const void *key,MTSortFunctionPtr sort_function) const=0;
  303. };
  304. //+------------------------------------------------------------------+
  305. //| Open orders events notification interface |
  306. //+------------------------------------------------------------------+
  307. class IMTOrderSink
  308. {
  309. public:
  310. virtual void OnOrderAdd(const IMTOrder* /*order*/) { }
  311. virtual void OnOrderUpdate(const IMTOrder* /*order*/) { }
  312. virtual void OnOrderDelete(const IMTOrder* /*order*/) { }
  313. virtual void OnOrderClean(const UINT64 /*login*/) { }
  314. virtual void OnOrderSync(void) { }
  315. };
  316. //+------------------------------------------------------------------+
  317. //| History orders |
  318. //+------------------------------------------------------------------+
  319. class IMTHistorySink
  320. {
  321. public:
  322. virtual void OnHistoryAdd(const IMTOrder* /*order*/) { }
  323. virtual void OnHistoryUpdate(const IMTOrder* /*order*/) { }
  324. virtual void OnHistoryDelete(const IMTOrder* /*order*/) { }
  325. virtual void OnHistoryClean(const UINT64 /*login*/) { }
  326. virtual void OnHistorySync(void) { }
  327. };
  328. //+------------------------------------------------------------------+