MT5APIGateway.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. //+------------------------------------------------------------------+
  2. //| MetaTrader 5 API Gateway |
  3. //| Copyright 2000-2019, MetaQuotes Software Corp. |
  4. //| http://www.metaquotes.net |
  5. //+------------------------------------------------------------------+
  6. #pragma once
  7. #include <wincrypt.h>
  8. #include "MT5APIConstants.h"
  9. #include "MT5APITools.h"
  10. #include "MT5APILogger.h"
  11. #include "MT5APIPublicKey.h"
  12. #include "Config\MT5APIConfigFeeder.h"
  13. #include "Config\MT5APIConfigGateway.h"
  14. #include "Config\MT5APIConfigSymbol.h"
  15. #include "Config\MT5APIConfigGroup.h"
  16. #include "Config\MT5APIConfigNetwork.h"
  17. #include "Config\MT5APIConfigTime.h"
  18. #include "Config\MT5APIConfigSpread.h"
  19. #include "Bases\MT5APIAccount.h"
  20. #include "Bases\MT5APIBook.h"
  21. #include "Bases\MT5APIConfirm.h"
  22. #include "Bases\MT5APIExecution.h"
  23. #include "Bases\MT5APIMail.h"
  24. #include "Bases\MT5APIOrder.h"
  25. #include "Bases\MT5APIPosition.h"
  26. #include "Bases\MT5APIRequest.h"
  27. #include "Bases\MT5APITick.h"
  28. #include "Bases\MT5APIUser.h"
  29. #include "Bases\MT5APIChart.h"
  30. //+------------------------------------------------------------------+
  31. //| Include necessary library |
  32. //+------------------------------------------------------------------+
  33. #pragma comment(lib,"advapi32.lib")
  34. //+------------------------------------------------------------------+
  35. //| Gateway API version |
  36. //+------------------------------------------------------------------+
  37. #define MTGatewayAPIVersion 2190
  38. #define MTGatewayAPIDate L"18 Oct 2019"
  39. //+------------------------------------------------------------------+
  40. //| Gateway description info |
  41. //+------------------------------------------------------------------+
  42. #pragma pack(push,1)
  43. struct MTGatewayInfo
  44. {
  45. //--- gateway modes flags
  46. enum EnModes
  47. {
  48. GATEWAY_MODE_QUOTES =1, // receive quotes
  49. GATEWAY_MODE_NEWS =2, // receive news
  50. GATEWAY_MODE_POSITIONS =4, // gateway can process requests of trade positions
  51. //--- enumeration borders
  52. GATEWAY_MODE_NONE =0,
  53. GATEWAY_MODE_ALL =GATEWAY_MODE_QUOTES|GATEWAY_MODE_NEWS|GATEWAY_MODE_POSITIONS
  54. };
  55. //--- necessary fields flags
  56. enum EnFields
  57. {
  58. GATEWAY_FIELD_SERVER =1, // server field
  59. GATEWAY_FIELD_LOGIN =2, // login field
  60. GATEWAY_FIELD_PASSWORD =4, // password field
  61. GATEWAY_FIELD_PARAMS =8, // parameters
  62. //--- enumeration borders
  63. GATEWAY_FIELD_NONE =0,
  64. GATEWAY_FIELD_ALL =GATEWAY_FIELD_SERVER|GATEWAY_FIELD_LOGIN|GATEWAY_FIELD_PASSWORD|GATEWAY_FIELD_PARAMS
  65. };
  66. //--- data
  67. UINT version; // gateway version
  68. UINT version_api; // gateway API version
  69. wchar_t name_default[128]; // gateway default name
  70. wchar_t copyright[128]; // copyright
  71. wchar_t server_default[128]; // default dataserver address:port
  72. wchar_t login_default[64]; // gateway default login
  73. wchar_t password_default[64]; // gateway default password
  74. wchar_t parameters_default[512]; // additional parameters
  75. UINT mode; // EnModes
  76. UINT fields; // EnFields
  77. wchar_t description[512]; // description
  78. wchar_t module_id[16]; // gateway module id
  79. wchar_t build_date[16]; // datafeed\gateway build date
  80. wchar_t build_api_date[16]; // Gateway API build date
  81. UINT reserved[32]; // reserved
  82. };
  83. #pragma pack(pop)
  84. //+------------------------------------------------------------------+
  85. //| News structure |
  86. //+------------------------------------------------------------------+
  87. #pragma pack(push,1)
  88. struct MTNews
  89. {
  90. //--- constants
  91. enum constants
  92. {
  93. MAX_NEWS_BODY_LEN=15*1024*1024 // max. body len
  94. };
  95. //--- news topic flags
  96. enum EnNewsFlags
  97. {
  98. FLAG_PRIORITY =1, // priority flag
  99. FLAG_CALENDAR =2, // calendar item flag
  100. FLAG_MIME =4, // MIME content flag
  101. FLAG_ALLOW_DEMO =8 // allow news body for 'topic only' accounts
  102. };
  103. UINT language; // news language (WinAPI LANGID)
  104. wchar_t subject[256]; // news subject
  105. wchar_t category[256]; // news category
  106. UINT flags; // EnNewsFlags
  107. wchar_t *body; // body
  108. UINT body_len; // body length
  109. UINT languages_list[32]; // list of languages available for news
  110. INT64 datetime; // datetime
  111. UINT reserved[30]; // reserved
  112. };
  113. #pragma pack(pop)
  114. //+------------------------------------------------------------------+
  115. //| Economic event structure |
  116. //+------------------------------------------------------------------+
  117. #pragma pack(push,1)
  118. struct MTEconomicEvent
  119. {
  120. //--- news topic flags
  121. enum EnEconomicEventPriority
  122. {
  123. PRIORITY_LOW =0, // low priority of event
  124. PRIORITY_NORMAL =1, // normal priority of event
  125. PRIORITY_HIGH =2 // high priority of event
  126. };
  127. INT64 eventtime; // event time
  128. wchar_t name[128]; // event name
  129. wchar_t currency[32]; // event currency
  130. UINT priority; // event priority
  131. wchar_t period[128]; // event period
  132. wchar_t val_previous[32]; // event previous value
  133. wchar_t val_forecast[32]; // event forecast value
  134. wchar_t val_actual[32]; // event actual value
  135. UINT language; // news language (WinAPI LANGID)
  136. UINT languages_list[32]; // list of languages available for news
  137. UINT reserved[31]; // reserved
  138. };
  139. #pragma pack(pop)
  140. //+------------------------------------------------------------------+
  141. //| Gateway events notification interface |
  142. //+------------------------------------------------------------------+
  143. class IMTGatewaySink
  144. {
  145. public:
  146. //--- connection type
  147. enum EnConnectType
  148. {
  149. CONNECT_TYPE_TRADE =1, // Trade server
  150. CONNECT_TYPE_HISTORY =2, // History server
  151. CONNECT_TYPE_ECN =3, // ECN
  152. CONNECT_TYPE_BACKUP =4, // Backup server
  153. };
  154. public:
  155. //--- MetaTrader Server's connection events and hooks
  156. virtual MTAPIRES HookServerConnect(LPCWSTR /*address*/,
  157. const UINT /*type*/,
  158. const UINT64 /*login*/) { return(MT_RET_OK); }
  159. virtual void OnServerDisconnect(LPCWSTR /*address*/,
  160. const UINT /*type*/,
  161. const UINT64 /*login*/) { }
  162. //--- gateway events
  163. virtual void OnGatewayConfig(const UINT64 /*login*/,const IMTConGateway* /*config*/) { }
  164. virtual void OnGatewayConfig(const UINT64 /*login*/,const IMTConFeeder* /*config*/) { }
  165. virtual void OnGatewayStart(void) { }
  166. virtual void OnGatewayStop(void) { }
  167. virtual void OnGatewayShutdown(const UINT64 /*login*/) { }
  168. //--- asynchronous methods notifications
  169. //--- DealerLock result notification
  170. virtual void OnDealerLock(const MTAPIRES /*retcode*/,
  171. const IMTRequest* /*request*/,
  172. const IMTUser* /*user*/,
  173. const IMTAccount* /*account*/,
  174. const IMTOrder* /*order*/,
  175. const IMTPosition* /*position*/) { }
  176. //--- DealerAnswer result notification
  177. virtual void OnDealerAnswer(const MTAPIRES /*retcode*/,
  178. const IMTConfirm* /*confirm*/) { }
  179. virtual void OnDealerAnswer(const UINT64 /*login*/,
  180. const MTAPIRES /*retcode*/,
  181. const IMTExecution* /*execution*/) { }
  182. //--- external trading system positions control hooks
  183. virtual MTAPIRES HookGatewayPositionsRequest(void) { return(MT_RET_ERR_NOTIMPLEMENT); }
  184. virtual MTAPIRES HookGatewayPositionsCheck(void) { return(MT_RET_ERR_NOTIMPLEMENT); }
  185. virtual MTAPIRES HookGatewayOrdersRequest(void) { return(MT_RET_ERR_NOTIMPLEMENT); }
  186. virtual MTAPIRES HookGatewayAccountRequest(UINT64 /*login*/,
  187. LPCWSTR /*account_id*/) { return(MT_RET_ERR_NOTIMPLEMENT); }
  188. virtual void OnGatewayAccountAnswer(const MTAPIRES /*retcode*/,
  189. const INT64 /*request_id*/,
  190. const IMTUser* /*user*/,
  191. const IMTAccount* /*account*/,
  192. const IMTOrderArray* /*orders*/,
  193. const IMTPositionArray* /*positions*/) { }
  194. virtual void OnGatewayAccountSet(const MTAPIRES /*retcode*/,
  195. const INT64 /*request_id*/,
  196. const IMTUser* /*user*/,
  197. const IMTAccount* /*account*/,
  198. const IMTOrderArray* /*orders*/,
  199. const IMTPositionArray* /*positions*/) { }
  200. //--- quotes symbols changes
  201. virtual void OnServerSymbolAdd(LPCWSTR /*symbol*/) { }
  202. virtual void OnServerSymbolDelete(LPCWSTR /*symbol*/) { }
  203. //--- MetaTrader Server's synchronization event
  204. virtual void OnServerSynchronized(LPCWSTR /*address*/,
  205. const UINT /*type*/,
  206. const UINT64 /*id*/) { }
  207. };
  208. //+------------------------------------------------------------------+
  209. //| Gateway API interface |
  210. //+------------------------------------------------------------------+
  211. class IMTGatewayAPI
  212. {
  213. public:
  214. //--- dealer flags
  215. enum EnDealerRequestFlags
  216. {
  217. DEALER_FLAG_NONE =0x00000000, // zero flags
  218. DEALER_FLAG_AUTOLOCK =0x00000001, // automatically lock new requests from request queue
  219. DEALER_FLAG_USER =0x00000002, // get IMTUser data on each request
  220. DEALER_FLAG_ACCOUNT =0x00000004, // get IMTAccount data on each request
  221. DEALER_FLAG_ORDER =0x00000008, // get IMTOrder data on each request
  222. DEALER_FLAG_POSITION =0x00000010, // get IMTPosition data on each request
  223. DEALER_FLAG_EXTERNAL_ACC =0x00000020, // fill ExternalAccount field on each request
  224. DEALER_FLAG_MARKUP_TRADES =0x00000040 // allow modify trading prices according to markup settings
  225. };
  226. //--- interface release
  227. virtual void Release(void)=0;
  228. //--- gateway license check
  229. virtual MTAPIRES LicenseCheck(MTLicenseCheck &check)=0;
  230. //--- gateway server start-stop
  231. virtual MTAPIRES Start(IMTGatewaySink *sink,LPCWSTR address=NULL)=0;
  232. virtual MTAPIRES Stop(void)=0;
  233. //--- gateway external connection state
  234. virtual MTAPIRES StateConnect(const UINT state)=0;
  235. virtual MTAPIRES StateTraffic(const UINT received_bytes,const UINT sent_bytes)=0;
  236. virtual MTAPIRES StateReserved1(void)=0;
  237. virtual MTAPIRES StateReserved2(void)=0;
  238. virtual MTAPIRES StateReserved3(void)=0;
  239. virtual MTAPIRES StateReserved4(void)=0;
  240. //--- client connection management
  241. virtual MTAPIRES ClientAdd(const UINT64 login,LPCWSTR password)=0;
  242. virtual MTAPIRES ClientAllowIP(LPCWSTR address)=0;
  243. virtual MTAPIRES ClientReserved1(void)=0;
  244. virtual MTAPIRES ClientReserved2(void)=0;
  245. virtual MTAPIRES ClientReserved3(void)=0;
  246. virtual MTAPIRES ClientReserved4(void)=0;
  247. //--- gateway server data access
  248. virtual MTAPIRES ServerIP(MTAPISTR &ip)=0;
  249. virtual UINT ServerPort(void)=0;
  250. virtual UINT ServerConnections(void)=0;
  251. virtual MTAPIRES ServerReserved1(void)=0;
  252. virtual MTAPIRES ServerReserved2(void)=0;
  253. virtual MTAPIRES ServerReserved3(void)=0;
  254. virtual MTAPIRES ServerReserved4(void)=0;
  255. //--- log functions
  256. virtual MTAPIRES LoggerOut(const UINT code,LPCWSTR format,...)=0;
  257. virtual void LoggerFlush(void)=0;
  258. virtual LPCWSTR LoggerPath(MTAPISTR& ip)=0;
  259. virtual MTAPIRES LoggerReserved2(void)=0;
  260. virtual MTAPIRES LoggerReserved3(void)=0;
  261. virtual MTAPIRES LoggerReserved4(void)=0;
  262. //--- send data functions
  263. virtual MTAPIRES SendTickStats(MTTickStat *stats,const UINT stats_total)=0;
  264. virtual MTAPIRES SendTicks(MTTick *ticks,const UINT ticks_total)=0;
  265. virtual MTAPIRES SendBookDiffs(MTBookDiff *bookdiffs,const UINT bookdiffs_total)=0;
  266. virtual MTAPIRES SendNews(MTNews *news,const UINT news_total)=0;
  267. virtual MTAPIRES SendEconomicEvents(MTEconomicEvent *events,const UINT events_total)=0;
  268. virtual MTAPIRES SendBooks(MTBook *books,const UINT books_total)=0;
  269. virtual MTAPIRES SendReserved1(void)=0;
  270. virtual MTAPIRES SendReserved2(void)=0;
  271. //--- feeder configuration
  272. virtual IMTConFeeder* FeederCreate(void)=0;
  273. virtual IMTConParam* FeederParamCreate(void)=0;
  274. virtual IMTConFeederTranslate* FeederTranslateCreate(void)=0;
  275. //--- gateway configuration
  276. virtual IMTConGateway* GatewayCreate(void)=0;
  277. virtual IMTConParam* GatewayParamCreate(void)=0;
  278. virtual IMTConGatewayTranslate* GatewayTranslateCreate(void)=0;
  279. //--- clients group configuration
  280. virtual IMTConGroup* GroupCreate(void)=0;
  281. virtual IMTConGroupSymbol* GroupSymbolCreate(void)=0;
  282. virtual IMTConCommission* GroupCommissionCreate(void)=0;
  283. virtual IMTConCommTier* GroupTierCreate(void)=0;
  284. virtual MTAPIRES GroupSubscribe(IMTConGroupSink *sink)=0;
  285. virtual MTAPIRES GroupUnsubscribe(IMTConGroupSink *sink)=0;
  286. virtual UINT GroupTotal(void)=0;
  287. virtual MTAPIRES GroupNext(const UINT pos,IMTConGroup *group)=0;
  288. virtual MTAPIRES GroupGet(LPCWSTR name,IMTConGroup *group)=0;
  289. virtual MTAPIRES GroupReserved1(void)=0;
  290. virtual MTAPIRES GroupReserved2(void)=0;
  291. virtual MTAPIRES GroupReserved3(void)=0;
  292. virtual MTAPIRES GroupReserved4(void)=0;
  293. //--- trade server symbol configuration access
  294. virtual IMTConSymbol* SymbolCreate(void)=0;
  295. virtual IMTConSymbolSession* SymbolSessionCreate(void)=0;
  296. virtual MTAPIRES SymbolSubscribe(IMTConSymbolSink *sink)=0;
  297. virtual MTAPIRES SymbolUnsubscribe(IMTConSymbolSink *sink)=0;
  298. virtual MTAPIRES SymbolAddPreliminary(IMTConSymbol *symbol)=0;
  299. virtual UINT SymbolTotal(void)=0;
  300. virtual MTAPIRES SymbolNext(const UINT pos,IMTConSymbol *symbol)=0;
  301. virtual MTAPIRES SymbolGet(LPCWSTR name,IMTConSymbol *symbol)=0;
  302. virtual MTAPIRES SymbolGet(LPCWSTR name,LPCWSTR name_group,IMTConSymbol *symbol)=0;
  303. virtual MTAPIRES SymbolUpdate(IMTConSymbol *symbol)=0;
  304. virtual MTAPIRES SymbolDelete(LPCWSTR name)=0;
  305. virtual MTAPIRES SymbolReserved1(void)=0;
  306. //--- time config
  307. virtual IMTConTime* TimeCreate(void)=0;
  308. virtual MTAPIRES TimeSubscribe(IMTConTimeSink *sink)=0;
  309. virtual MTAPIRES TimeUnsubscribe(IMTConTimeSink *sink)=0;
  310. virtual INT64 TimeCurrent(void)=0;
  311. virtual MTAPIRES TimeGet(IMTConTime *common)=0;
  312. virtual MTAPIRES TimeReserved1(void)=0;
  313. virtual MTAPIRES TimeReserved2(void)=0;
  314. virtual MTAPIRES TimeReserved3(void)=0;
  315. virtual MTAPIRES TimeReserved4(void)=0;
  316. //--- requests
  317. virtual IMTRequest* RequestCreate(void)=0;
  318. virtual IMTRequestArray* RequestArrayCreate(void)=0;
  319. virtual MTAPIRES RequestSubscribe(IMTRequestSink *sink)=0;
  320. virtual MTAPIRES RequestUnsubscribe(IMTRequestSink *sink)=0;
  321. virtual UINT RequestTotal(void)=0;
  322. virtual MTAPIRES RequestNext(const UINT pos,IMTRequest *request,IMTUser *user,IMTAccount *account,IMTOrder *order,IMTPosition *position)=0;
  323. virtual MTAPIRES RequestGet(const UINT id,IMTRequest *request,IMTUser *user,IMTAccount *account,IMTOrder *order,IMTPosition *position)=0;
  324. virtual MTAPIRES RequestGetAll(IMTRequestArray *requests)=0;
  325. virtual MTAPIRES RequestReserved1(void)=0;
  326. virtual MTAPIRES RequestReserved2(void)=0;
  327. virtual MTAPIRES RequestReserved3(void)=0;
  328. virtual MTAPIRES RequestReserved4(void)=0;
  329. //--- dealing
  330. virtual IMTConfirm* DealerConfirmCreate(void)=0;
  331. virtual IMTExecution* DealerExecutionCreate(void)=0;
  332. virtual MTAPIRES DealerStart(const UINT flags)=0;
  333. virtual MTAPIRES DealerStop(void)=0;
  334. virtual MTAPIRES DealerGetAsync(void)=0;
  335. virtual MTAPIRES DealerLockAsync(const UINT id)=0;
  336. virtual MTAPIRES DealerAnswerAsync(IMTConfirm *confirm)=0;
  337. virtual MTAPIRES DealerExecuteAsync(IMTExecution *execution)=0;
  338. virtual MTAPIRES DealerReserved1(void)=0;
  339. virtual MTAPIRES DealerReserved2(void)=0;
  340. virtual MTAPIRES DealerReserved3(void)=0;
  341. virtual MTAPIRES DealerReserved4(void)=0;
  342. //--- internal email
  343. virtual IMTMail* MailCreate(void)=0;
  344. virtual MTAPIRES MailSend(IMTMail *mail)=0;
  345. virtual MTAPIRES MailReserved1(void)=0;
  346. virtual MTAPIRES MailReserved2(void)=0;
  347. virtual MTAPIRES MailReserved3(void)=0;
  348. virtual MTAPIRES MailReserved4(void)=0;
  349. //--- clients database
  350. virtual IMTUser* UserCreate(void)=0;
  351. virtual IMTAccount* UserCreateAccount(void)=0;
  352. //--- open orders database
  353. virtual IMTOrder* OrderCreate(void)=0;
  354. //--- trade positions database
  355. virtual IMTPosition* PositionCreate(void)=0;
  356. //--- custom settings control
  357. virtual MTAPIRES SettingsAdd(const IMTConParam *param)=0;
  358. virtual MTAPIRES SettingsUpdate(const UINT pos,const IMTConParam *param)=0;
  359. virtual MTAPIRES SettingsUpdate(const IMTConParam *param)=0;
  360. virtual MTAPIRES SettingsDelete(const UINT pos)=0;
  361. virtual MTAPIRES SettingsDelete(LPCWSTR name)=0;
  362. virtual MTAPIRES SettingsClear(void)=0;
  363. virtual UINT SettingsTotal(void)=0;
  364. virtual MTAPIRES SettingsNext(const UINT pos,IMTConParam *param)=0;
  365. virtual MTAPIRES SettingsGet(LPCWSTR name,IMTConParam *param)=0;
  366. virtual MTAPIRES SettingsReserved1(void)=0;
  367. virtual MTAPIRES SettingsReserved2(void)=0;
  368. virtual MTAPIRES SettingsReserved3(void)=0;
  369. virtual MTAPIRES SettingsReserved4(void)=0;
  370. //--- external trading system positions control
  371. virtual IMTConParamArray* GatewayParamArrayCreate(void)=0;
  372. virtual IMTPositionArray* GatewayPositionArrayCreate(void)=0;
  373. virtual MTAPIRES GatewayPositionsAnswer(const MTAPIRES result,const INT64 positions_time,const IMTPositionArray *positions)=0;
  374. virtual MTAPIRES GatewayPositionsCheck(const MTAPIRES result,const INT64 positions_time,const IMTPositionArray *positions)=0;
  375. virtual MTAPIRES GatewayPositionsReserved1(void)=0;
  376. virtual MTAPIRES GatewayPositionsReserved2(void)=0;
  377. virtual MTAPIRES GatewayPositionsReserved3(void)=0;
  378. virtual MTAPIRES GatewayPositionsReserved4(void)=0;
  379. //--- chart data
  380. virtual MTAPIRES ChartRequest(LPCWSTR symbol,const INT64 from,const INT64 to,MTChartBar *&bars,UINT &bars_total)=0;
  381. virtual MTAPIRES ChartDelete(LPCWSTR symbol,const INT64 *bars_dates,const UINT bars_total)=0;
  382. virtual MTAPIRES ChartUpdate(LPCWSTR symbol,const MTChartBar *bars,const UINT bars_total)=0;
  383. virtual MTAPIRES ChartReplace(LPCWSTR symbol,const INT64 from,const INT64 to,const MTChartBar *bars,const UINT bars_total)=0;
  384. virtual MTAPIRES ChartReserved1(void)=0;
  385. virtual MTAPIRES ChartReserved2(void)=0;
  386. virtual MTAPIRES ChartReserved3(void)=0;
  387. virtual MTAPIRES ChartReserved4(void)=0;
  388. //--- memory management
  389. virtual void* Allocate(const UINT bytes)=0;
  390. virtual void Free(void *ptr)=0;
  391. //--- spreads configuration
  392. virtual IMTConSpread* SpreadCreate(void)=0;
  393. virtual IMTConSpreadLeg* SpreadLegCreate(void)=0;
  394. virtual MTAPIRES SpreadSubscribe(IMTConSpreadSink *sink)=0;
  395. virtual MTAPIRES SpreadUnsubscribe(IMTConSpreadSink *sink)=0;
  396. virtual MTAPIRES SpreadAdd(IMTConSpread *spread)=0;
  397. virtual MTAPIRES SpreadDelete(const UINT pos)=0;
  398. virtual MTAPIRES SpreadShift(const UINT pos,const int shift)=0;
  399. virtual UINT SpreadTotal(void)=0;
  400. virtual MTAPIRES SpreadNext(const UINT pos,IMTConSpread *spread)=0;
  401. virtual MTAPIRES SpreadGet(const UINT id,IMTConSpread *spread)=0;
  402. virtual MTAPIRES SpreadReserved1(void)=0;
  403. virtual MTAPIRES SpreadReserved2(void)=0;
  404. virtual MTAPIRES SpreadReserved3(void)=0;
  405. virtual MTAPIRES SpreadReserved4(void)=0;
  406. //--- external trading system pending orders control
  407. virtual IMTOrderArray* GatewayOrderArrayCreate(void)=0;
  408. virtual MTAPIRES GatewayOrdersAnswer(const MTAPIRES result,const INT64 orders_time,const IMTOrderArray *orders)=0;
  409. virtual MTAPIRES GatewayOrdersReserved1(void)=0;
  410. virtual MTAPIRES GatewayOrdersReserved2(void)=0;
  411. virtual MTAPIRES GatewayOrdersReserved3(void)=0;
  412. virtual MTAPIRES GatewayOrdersReserved4(void)=0;
  413. //--- external trading system trading accounts control
  414. virtual MTAPIRES GatewayAccountAnswer(const MTAPIRES result,
  415. const INT64 accounts_time,
  416. const IMTUser *user,
  417. const IMTAccount *account,
  418. const IMTOrderArray *orders,
  419. const IMTPositionArray *positions)=0;
  420. virtual MTAPIRES GatewayAccountRequest(const INT64 request_id,
  421. const IMTUser *user)=0;
  422. virtual MTAPIRES GatewayAccountSet(const INT64 request_id,
  423. const IMTUser *user,
  424. const IMTAccount *account,
  425. const IMTOrderArray *orders,
  426. const IMTPositionArray *positions)=0;
  427. virtual MTAPIRES GatewayAccountsReserved1(void)=0;
  428. virtual MTAPIRES GatewayAccountsReserved2(void)=0;
  429. //--- platform server config
  430. virtual IMTConServer* NetServerCreate(void)=0;
  431. virtual IMTConServerRange* NetServerRangeCreate(void)=0;
  432. virtual MTAPIRES NetServerSubscribe(IMTConServerSink* sink)=0;
  433. virtual MTAPIRES NetServerUnsubscribe(IMTConServerSink* sink)=0;
  434. virtual UINT NetServerTotal(void)=0;
  435. virtual MTAPIRES NetServerNext(const UINT pos,IMTConServer* config)=0;
  436. virtual MTAPIRES NetServerGet(const UINT64 id,IMTConServer* config)=0;
  437. virtual MTAPIRES NetServerReserved1(void)=0;
  438. virtual MTAPIRES NetServerReserved2(void)=0;
  439. virtual MTAPIRES NetServerReserved3(void)=0;
  440. virtual MTAPIRES NetServerReserved4(void)=0;
  441. //--- clients database
  442. virtual MTAPIRES UserSubscribe(IMTUserSink* sink)=0;
  443. virtual MTAPIRES UserUnsubscribe(IMTUserSink* sink)=0;
  444. virtual UINT UserTotal(void)=0;
  445. virtual MTAPIRES UserGet(const UINT64 login,IMTUser* user)=0;
  446. virtual MTAPIRES UserGroup(const UINT64 login,MTAPISTR& group)=0;
  447. virtual MTAPIRES UserLogins(UINT64*& logins,UINT& logins_total)=0;
  448. virtual MTAPIRES UserGetByAccount(LPCWSTR account,IMTUser* user)=0;
  449. virtual MTAPIRES UserReserved1(void)=0;
  450. virtual MTAPIRES UserReserved2(void)=0;
  451. virtual MTAPIRES UserReserved3(void)=0;
  452. virtual MTAPIRES UserReserved4(void)=0;
  453. virtual MTAPIRES UserReserved5(void)=0;
  454. virtual MTAPIRES UserReserved6(void)=0;
  455. virtual MTAPIRES UserReserved7(void)=0;
  456. //--- gateway symbol configuration access
  457. virtual MTAPIRES GatewaySymbolAdd(IMTConSymbol *symbol)=0;
  458. virtual UINT GatewaySymbolTotal(void)=0;
  459. virtual MTAPIRES GatewaySymbolNext(const UINT pos,IMTConSymbol *symbol)=0;
  460. virtual MTAPIRES GatewaySymbolGet(LPCWSTR name,IMTConSymbol *symbol)=0;
  461. virtual MTAPIRES GatewaySymbolDelete(LPCWSTR name)=0;
  462. virtual MTAPIRES GatewaySymbolClear(void)=0;
  463. virtual MTAPIRES GatewaySymbolReserver1(void)=0;
  464. virtual MTAPIRES GatewaySymbolReserver2(void)=0;
  465. virtual MTAPIRES GatewaySymbolReserver3(void)=0;
  466. virtual MTAPIRES GatewaySymbolReserver4(void)=0;
  467. //--- ticks history
  468. virtual MTAPIRES TickHistoryRequest(LPCWSTR symbol,const INT64 from,const INT64 to,MTTickRate*& ticks,UINT& ticks_total)=0;
  469. virtual MTAPIRES TickHistoryRequestRaw(LPCWSTR symbol,const INT64 from,const INT64 to,MTTickRate*& ticks,UINT& ticks_total)=0;
  470. virtual MTAPIRES TickHistoryAdd(LPCWSTR symbol,const MTTickRate* ticks,const UINT ticks_total)=0;
  471. virtual MTAPIRES TickHistoryReplace(LPCWSTR symbol,const INT64 from_msc,const INT64 to_msc,const MTTickRate* ticks,const UINT ticks_total)=0;
  472. virtual MTAPIRES TickHistoryReserver1(void)=0;
  473. virtual MTAPIRES TickHistoryReserver2(void)=0;
  474. virtual MTAPIRES TickHistoryReserver3(void)=0;
  475. virtual MTAPIRES TickHistoryReserver4(void)=0;
  476. };
  477. //+------------------------------------------------------------------+
  478. //| Entry point functions declarations |
  479. //+------------------------------------------------------------------+
  480. typedef /*__declspec(dllexport)*/ MTAPIRES(*MTGatewayVersion_t) (UINT &version);
  481. typedef /*__declspec(dllexport)*/ MTAPIRES(*MTGatewayCreate_t) (MTGatewayInfo &info,IMTGatewayAPI **gateway);
  482. typedef /*__declspec(dllexport)*/ MTAPIRES(*MTGatewayCreateLocal_t) (MTGatewayInfo &info,IMTGatewayAPI **gateway,int argc,wchar_t** argv);
  483. //+------------------------------------------------------------------+
  484. //| Entry point functions names |
  485. //+------------------------------------------------------------------+
  486. static const LPCSTR s_MTGatewayVersion ="MTGatewayVersion";
  487. static const LPCSTR s_MTGatewayCreate ="MTGatewayCreate";
  488. static const LPCSTR s_MTGatewayCreateLocal ="MTGatewayCreateLocal";
  489. //+------------------------------------------------------------------+
  490. //| Gateway factory |
  491. //+------------------------------------------------------------------+
  492. class CMTGatewayAPIFactory
  493. {
  494. HMODULE m_hmodule;
  495. MTGatewayVersion_t m_mtversion;
  496. MTGatewayCreate_t m_mtcreate;
  497. MTGatewayCreateLocal_t m_mtcreate_local;
  498. public:
  499. CMTGatewayAPIFactory() : m_hmodule(NULL),m_mtversion(NULL),m_mtcreate(NULL),m_mtcreate_local(NULL) {}
  500. //--- initialize & shutdown
  501. MTAPIRES Initialize(void);
  502. MTAPIRES Shutdown(void);
  503. //--- gateway creation
  504. MTAPIRES Create(MTGatewayInfo &info,IMTGatewayAPI **gateway,int argc=0,wchar_t** argv=NULL) const;
  505. //--- check license sign
  506. static MTAPIRES LicenseCheck(IMTGatewayAPI *gateway,LPCWSTR name);
  507. //--- version info
  508. MTAPIRES Version(UINT &version) const;
  509. private:
  510. bool FindLibrary(wchar_t *path,const size_t path_maxlen);
  511. };
  512. //+------------------------------------------------------------------+
  513. //| Library initialization |
  514. //+------------------------------------------------------------------+
  515. inline MTAPIRES CMTGatewayAPIFactory::Initialize(void)
  516. {
  517. wchar_t path[MAX_PATH]={};
  518. //--- find the Gateway API DLL
  519. if(!FindLibrary(path,_countof(path)-1))
  520. return(MT_RET_ERR_NOTFOUND);
  521. //--- try to load Gateway API DLL from current folder
  522. if((m_hmodule=::LoadLibrary(path))==NULL)
  523. return(MT_RET_ERR_NOTFOUND);
  524. //--- load Gateway API DLL
  525. if(m_hmodule)
  526. {
  527. //--- find entry point addresses
  528. m_mtversion =reinterpret_cast<MTGatewayVersion_t>(::GetProcAddress(m_hmodule,s_MTGatewayVersion));
  529. m_mtcreate =reinterpret_cast<MTGatewayCreate_t>(::GetProcAddress(m_hmodule,s_MTGatewayCreate));
  530. m_mtcreate_local=reinterpret_cast<MTGatewayCreateLocal_t>(::GetProcAddress(m_hmodule,s_MTGatewayCreateLocal));
  531. //--- check results
  532. if(!m_mtversion || !m_mtcreate || !m_mtcreate_local)
  533. {
  534. Shutdown();
  535. //--- not found
  536. return(MT_RET_ERR_NOTFOUND);
  537. }
  538. }
  539. //--- ok
  540. return(MT_RET_OK);
  541. }
  542. //+------------------------------------------------------------------+
  543. //| Library shutdown |
  544. //+------------------------------------------------------------------+
  545. inline MTAPIRES CMTGatewayAPIFactory::Shutdown(void)
  546. {
  547. //--- check & free
  548. if(m_hmodule)
  549. {
  550. ::FreeLibrary((HMODULE)m_hmodule);
  551. m_hmodule=NULL;
  552. }
  553. //--- zero members
  554. m_mtversion =NULL;
  555. m_mtcreate =NULL;
  556. m_mtcreate_local=NULL;
  557. //--- ok
  558. return(MT_RET_OK);
  559. }
  560. //+------------------------------------------------------------------+
  561. //| Find the library |
  562. //+------------------------------------------------------------------+
  563. inline bool CMTGatewayAPIFactory::FindLibrary(wchar_t *path,const size_t path_maxlen)
  564. {
  565. wchar_t folder[MAX_PATH]={};
  566. wchar_t *ptr;
  567. //--- find the folder
  568. ::GetModuleFileNameW(NULL,folder,_countof(folder)-1);
  569. //--- check for 5 levels
  570. for(int i=0;i<5;i++)
  571. {
  572. //--- remove level
  573. if((ptr=wcsrchr(folder,L'\\'))==NULL)
  574. break;
  575. *ptr=L'\0';
  576. //--- check the file
  577. if(sizeof(void*)==4)
  578. swprintf_s(path,path_maxlen,L"%s\\MT5APIGateway.dll",folder);
  579. else
  580. swprintf_s(path,path_maxlen,L"%s\\MT5APIGateway64.dll",folder);
  581. if(GetFileAttributesW(path)!=INVALID_FILE_ATTRIBUTES)
  582. return(true);
  583. //--- check /libs folder
  584. if(sizeof(void*)==4)
  585. swprintf_s(path,path_maxlen,L"%s\\libs\\MT5APIGateway.dll",folder);
  586. else
  587. swprintf_s(path,path_maxlen,L"%s\\libs\\MT5APIGateway64.dll",folder);
  588. if(GetFileAttributesW(path)!=INVALID_FILE_ATTRIBUTES)
  589. return(true);
  590. }
  591. //---
  592. return(false);
  593. }
  594. //+------------------------------------------------------------------+
  595. //| Gateway interface creation |
  596. //+------------------------------------------------------------------+
  597. inline MTAPIRES CMTGatewayAPIFactory::Create(MTGatewayInfo& info,IMTGatewayAPI **gateway,int argc/*=0*/,wchar_t** argv/*=NULL*/) const
  598. {
  599. MTAPIRES res=MT_RET_ERROR;
  600. //--- check members & parameters
  601. if(!m_hmodule || !m_mtcreate || !m_mtcreate_local || !gateway)
  602. return(MT_RET_ERROR);
  603. //--- call common or local-mode creation
  604. if(!argc || !argv)
  605. res=m_mtcreate(info,gateway);
  606. else
  607. res=m_mtcreate_local(info,gateway,argc,argv);
  608. //--- result
  609. return(res);
  610. }
  611. //+------------------------------------------------------------------+
  612. //| Gateway license sign check |
  613. //+------------------------------------------------------------------+
  614. inline MTAPIRES CMTGatewayAPIFactory::LicenseCheck(IMTGatewayAPI *gateway,LPCWSTR name)
  615. {
  616. //--- checking
  617. if(!gateway || !name || name[0]==L'\0')
  618. return(MT_RET_ERROR);
  619. //--- fill license information, copy name
  620. MTLicenseCheck license={};
  621. HCRYPTPROV prov=NULL;
  622. HCRYPTHASH hash=NULL;
  623. HCRYPTKEY key =NULL;
  624. MTAPIRES res=MT_RET_ERROR;
  625. wcscpy_s(license.name,_countof(license.name),name);
  626. //--- initialize random generator
  627. srand((unsigned int)GetTickCount());
  628. //--- generate sequence of random numbers
  629. for(license.random_size=0;license.random_size<sizeof(license.random);license.random_size++)
  630. license.random[license.random_size]=char(rand()&0xFF);
  631. //--- check license
  632. gateway->LicenseCheck(license);
  633. //--- check retcode
  634. if(license.retcode!=MT_RET_OK)
  635. return(license.retcode);
  636. //--- check license sign, initialize provider
  637. if(CryptAcquireContext(&prov,NULL,MS_DEF_PROV,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT)==FALSE)
  638. return(MT_RET_ERROR);
  639. //--- import public key
  640. if(CryptImportKey(prov,(const BYTE*)MT5APIPublicKey,sizeof(MT5APIPublicKey),0,0,&key)!=FALSE)
  641. {
  642. //--- initialize hash object
  643. if(CryptCreateHash(prov,CALG_MD5,0,0,&hash)!=FALSE)
  644. {
  645. //--- set hash from data
  646. if(CryptHashData(hash,(BYTE*)&license,(DWORD)offsetof(MTLicenseCheck,retcode),0)!=FALSE)
  647. {
  648. //--- verify hash sign, using public key
  649. if(CryptVerifySignature(hash,(BYTE*)license.sign,(DWORD)license.sign_size,key,NULL,0)!=FALSE)
  650. res=license.retcode;
  651. else
  652. res=MT_RET_ERR_DATA;
  653. }
  654. }
  655. }
  656. //--- destroy hash
  657. if(hash)
  658. CryptDestroyHash(hash);
  659. //--- destroy key
  660. if(key)
  661. CryptDestroyKey(key);
  662. //--- release provider
  663. if(prov)
  664. CryptReleaseContext(prov,0);
  665. //--- return result
  666. return(res);
  667. }
  668. //+------------------------------------------------------------------+
  669. //| Version |
  670. //+------------------------------------------------------------------+
  671. inline MTAPIRES CMTGatewayAPIFactory::Version(UINT &version) const
  672. {
  673. //--- check members & parameters
  674. if(!m_hmodule || !m_mtversion)
  675. return(MT_RET_ERROR);
  676. //--- result
  677. return(m_mtversion(version));
  678. }
  679. //+------------------------------------------------------------------+