forked from zhangshuiyong/nodequant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainEngine.js
More file actions
462 lines (355 loc) · 13.5 KB
/
Copy pathmainEngine.js
File metadata and controls
462 lines (355 loc) · 13.5 KB
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
* Created by Administrator on 2017/6/12.
*/
let path=require("path");
require("../common.js");
require("../systemConfig");
require("../userConfig.js");
let NodeQuantLog=require("../util/NodeQuantLog");
let NodeQuantError=require("../util/NodeQuantError");
let ClientFactory=require("../model/client/ClientFactory");
function _isTimeToWork() {
let systemConfigPath=path.resolve(__dirname+"/../systemConfig.js");
require.cache[systemConfigPath]=null;
require("../systemConfig");
let NowDateTime=new Date();
let NowDateStr=NowDateTime.toLocaleDateString();
//夜盘结束时间
let NightStopTimeStr=NowDateStr+" "+ SystemConfig.NightStopTime;
let NightStopDateTime=new Date(NightStopTimeStr);
//自然日的周末,周六的凌晨停止时间以后不用工作
let weekDay=NowDateTime.getDay();
if(weekDay<1)
{
return MainEngineStatus.Stop;
}else if(weekDay===6)
{
//周六凌晨03:00以后就不工作
if(NightStopDateTime<NowDateTime)
{
return MainEngineStatus.NightStop;
}
}
//工作日的工作时间
let DayStartDateTimeStr=NowDateStr+" "+SystemConfig.DayStartTime;
let DayStartDateTime=new Date(DayStartDateTimeStr);
let DayStopDateTimeStr= NowDateStr +" "+ SystemConfig.DayStopTime;
let DayStopDateTime=new Date(DayStopDateTimeStr);
let NightStartDateTimeStr= NowDateStr +" "+ SystemConfig.NightStartTime;
let NightStartDateTime=new Date(NightStartDateTimeStr);
if(NightStopDateTime<DayStartDateTime && NightStopDateTime<NowDateTime && NowDateTime<DayStartDateTime)
{
//在当天凌晨3:00 ~ 早上9:00是不需要打开的
return MainEngineStatus.NightStop;
}else if(NightStopDateTime>DayStartDateTime && NightStopDateTime<NowDateTime)
{
//现在时间大于结束时间23:00
return MainEngineStatus.NightStop;
}else if(DayStopDateTime<NowDateTime && NowDateTime<NightStartDateTime)
{
//在当天下午15:30:00 ~ 晚上20:00是不需要打开的
return MainEngineStatus.DayStop;
}
return MainEngineStatus.Start;
}
function _registerEvent(myEngine) {
//每隔5分钟检查是否需要自动退出交易客户端
setInterval(function () {
let enginStatus=_isTimeToWork();
if(MainEngineStatus.Start!==enginStatus)
{
if(myEngine.isWorking)
{
myEngine.Stop(enginStatus);
}
}else
{
if(false===myEngine.isWorking)
{
myEngine.ReStart();
}
}
},2*60*1000);
global.AppEventEmitter.on(EVENT.OnReceivedAllContract,function (clientName) {
//策略引擎是否已经启动
if(global.NodeQuant.StrategyEngine.IsWorking === false)
{
//策略引擎还没启动,检查所有策略引擎所需要的交易客户端是否已经都启动了
//检查所有交易客户端是否已经都连接上
for (let clientNameInstance in myEngine.clientDic) {
if (myEngine.clientDic[clientNameInstance].IsGetAllContract() === false) {
return;
}
//获得所有合约是交易端,市场端与交易端相互独立,订阅要市场端订阅
//发生过交易端登录了,返回所有合约列表,但是市场端没有登录成功情况
if(myEngine.clientDic[clientNameInstance].IsMdConnected() === false)
{
let message="获得"+clientNameInstance+"所有市场合约,但是其市场前置没登录成功,不会启动StrategyEngine.";
let error=new NodeQuantError("MainEngine",ErrorType.DBError,message);
global.AppEventEmitter.emit(EVENT.OnError,error);
return;
}
}
if(myEngine.contractDic==={})
{
console.log("Received all contract But contract dictionnary is empty.Will not start strategy engine.please check");
return;
}
global.AppEventEmitter.emit(EVENT.OnAllConfigClientReadyed, "AllConfigClientReadyed");
}
});
global.AppEventEmitter.on(EVENT.OnAllConfigClientReadyed,function (msg) {
//所有配置的客户端,启动策略引擎
if(global.NodeQuant.StrategyEngine.IsWorking===false)
{
//没有启动过,但是所有客户端已经连接成功,策略引擎启动过,策略启动过
global.NodeQuant.StrategyEngine.Start();
}
});
global.AppEventEmitter.on(EVENT.OnContract,function (contract) {
if(myEngine.contractDic[contract.clientName]===undefined)
{
myEngine.contractDic[contract.clientName]={};
}
myEngine.contractDic[contract.clientName][contract.symbol]=contract;
//交易合约点数对应价值
myEngine.contractSizeDic[contract.symbol]=contract.size;
});
global.AppEventEmitter.on(EVENT.OnDisconnected,function (clientName) {
//响应连接断开
let message=clientName+" Disconnected";
let log = new NodeQuantLog(clientName,LogType.INFO,new Date().toLocaleString(),message);
global.AppEventEmitter.emit(EVENT.OnLog,log);
});
//绑定事件多次,会有多次回调
global.AppEventEmitter.on(EVENT.OnSubscribeContract,function (contractName,clientName,error) {
let message="";
if(error.ErrorID!==0)
{
message="Subscribe "+contractName+" Error,errorID:"+error.ErrorID+",errorMsg:"+error.Message;
}else
{
message="Subscribe "+contractName+",Successful";
}
let log=new NodeQuantLog("MainEngine",LogType.INFO,new Date().toLocaleString(),message);
global.AppEventEmitter.emit(EVENT.OnLog,log);
});
global.AppEventEmitter.on(EVENT.OnUnSubscribeContract,function (contractName,error) {
let message="UnSubscribe "+contractName+",errorID:"+error.ErrorID+",errorMsg:"+error.Message;
let log=new NodeQuantLog("MainEngine",LogType.INFO,new Date().toLocaleString(),message);
global.AppEventEmitter.emit(EVENT.OnLog,log);
});
global.AppEventEmitter.on(EVENT.OnError,function (error) {
myEngine.RecordError(error);
console.log("出现错误来源"+error.Source+" ,Msg:"+error.Message);
});
global.AppEventEmitter.on(EVENT.OnLog,function (log) {
//汇总打印Log到数据库
myEngine.RecordLog(log);
console.log(log.Datetime+",信息来源"+log.Source+" ,Msg:"+log.Message);
});
};
class MainEngine{
constructor(){
this.isWorking = false;
//交易日
this.TradingDay="";
this.clientDic = {};
this.contractDic={};
//合约每点的价值字典,用于计算净值,而且与交易客户端无关
this.contractSizeDic={};
_registerEvent(this);
}
Start(){
let enginStatus=_isTimeToWork();
if(MainEngineStatus.Start!==enginStatus)
return;
let log=new NodeQuantLog("MainEngine",LogType.INFO,new Date().toLocaleString(),"MainEngine Start");
global.AppEventEmitter.emit(EVENT.OnLog,log);
//重置主引擎变量
this.Reset();
//创建所有客户端
this.CreateAllClient();
//连接所有客户端
this.ConnectAllClient();
}
CreateAllClient()
{
for(let clientName in ClientConfig)
{
if(SupportClients[clientName]!==undefined)
{
this.clientDic[clientName]=ClientFactory.Create(clientName);
}
}
}
Reset()
{
//重置开关
this.isWorking = true;
//重置合约字典
this.contractDic={};
//重置交易客户端
this.clientDic={};
}
ReStart() {
let log=new NodeQuantLog("MainEngine",LogType.INFO,new Date().toLocaleString(),"MainEngine ReStart");
global.AppEventEmitter.emit(EVENT.OnLog,log);
//重置主引擎变量
this.Reset();
//创建所有客户端
this.CreateAllClient();
//连接Clients
this.ConnectAllClient();
}
ConnectAllClient()
{
for(let clientName in this.clientDic)
{
this.Connect(clientName);
}
}
//主引擎进程可以启动多个交易客户端
Connect(clientName) {
this.clientDic[clientName].Connect();
}
Stop(mainEngineStatus) {
//1.停止策略引擎
global.NodeQuant.StrategyEngine.Stop(mainEngineStatus);
//2.断开Clients
for(let key in this.clientDic)
{
if(this.clientDic[key]!==undefined)
{
//已经连接上交易前端,断开
if(this.clientDic[key].IsMdConnected()){
this.clientDic[key].Exit();
//删除pointer
this.clientDic[key]=null;
}
}
}
this.clientDic=null;
//释放交易客户端
this.clientDic={};
//设置主引擎停止工作标志
this.isWorking = false;
let log=new NodeQuantLog("MainEngine",LogType.INFO,new Date().toLocaleString(),"MainEngine Stop");
global.AppEventEmitter.emit(EVENT.OnLog,log);
}
RecordError(error)
{
global.NodeQuant.SystemDBClient.lpush(System_Error_DB,JSON.stringify(error),function (err,reply) {
if(err) {
throw new Error("记录System_Error失败,原因:"+err);
}
});
}
RecordLog(log){
global.NodeQuant.SystemDBClient.lpush(System_Log_DB,JSON.stringify(log),function (err,reply) {
if(err) {
let message="记录System_Log失败,原因:"+err;
let error=new NodeQuantError("MainEngine",ErrorType.DBError,message);
global.AppEventEmitter.emit(EVENT.OnError,error);
throw new Error(message);
}
});
}
GetClient(clientName) {
return this.clientDic[clientName];
}
GetAllClient() {
return this.clientDic;
};
GetTradingDay()
{
for(let clientName in this.clientDic)
{
return this.clientDic[clientName].GetTradingDay();
}
return undefined;
}
GetContract(clientName,contractName){
if(this.contractDic[clientName])
{
return this.contractDic[clientName][contractName];
}else
{
return undefined;
}
};
GetAllContract(){
return this.contractDic;
};
//获取交易合约每点价值
GetContractSize(contractName)
{
return this.contractSizeDic[contractName];
}
Subscribe(clientName,contractName) {
let client=this.clientDic[clientName];
let ret =-1;
if(client)
{
ret = client.Subscribe(contractName);
}
return ret;
}
UnSubscribe(clientName,contractName) {
let ret = this.clientDic[clientName].UnSubscribe(contractName);
return ret;
}
SendMarketOrder(clientName,contractName,direction,openclose,volume) {
let ret = this.clientDic[clientName].SendMarketOrder(contractName,direction,openclose,volume);
return ret;
}
SendLimitOrder(clientName,contractName,direction,openclose,volume,limitPrice) {
let ret = this.clientDic[clientName].SendLimitOrder(contractName,direction,openclose,volume,limitPrice);
//console.timeEnd("NodeQuant-TickToFinishSendOrder");
return ret;
}
SendMarketIfTouchedOrder(clientName,contractName,direction,openclose,volume,stopPriceCondition,stopPrice) {
let ret = this.clientDic[clientName].SendMarketIfTouchedOrder(contractName,direction,openclose,volume,stopPriceCondition,stopPrice);
return ret;
}
SendStopLimitOrder(clientName,contractName,direction,openclose,volume,limitPrice,stopPriceCondition,stopPrice) {
let ret = this.clientDic[clientName].SendStopLimitOrder(contractName,direction,openclose,volume,limitPrice,stopPriceCondition,stopPrice)
return ret;
}
SendFillAndKillLimitOrder(clientName,contractName,direction,openclose,volume,limitPrice) {
let ret = this.clientDic[clientName].SendFillAndKillLimitOrder(contractName,direction,openclose,volume,limitPrice);
return ret;
}
SendFillOrKillLimitOrder(clientName,contractName,direction,openclose,volume,limitPrice) {
let ret = this.clientDic[clientName].SendFillOrKillLimitOrder(contractName,direction,openclose,volume,limitPrice);
return ret;
}
QueryInvestorPosition(clientName) {
let ret = this.clientDic[clientName].QueryInvestorPosition();
return ret;
}
QueryTradingAccount(clientName)
{
let ret = this.clientDic[clientName].QueryTradingAccount();
return ret;
}
QuerySettlementInfo(clientName){
let ret = this.clientDic[clientName].QuerySettlementInfo();
return ret;
}
QueryCommissionRate(clientName,contractSymbol)
{
let ret = this.clientDic[clientName].QueryCommissionRate(contractSymbol);
return ret;
}
QueryDeferFeeRate(clientName,contractSymbol)
{
let ret = this.clientDic[clientName].QueryDeferFeeRate(contractSymbol);
return ret;
}
CancelOrder(clientName,order) {
let ret = this.clientDic[clientName].CancelOrder(order);
return ret;
}
}
module.exports=MainEngine;