Skip to content

Commit

Permalink
代码更新
Browse files Browse the repository at this point in the history
  • Loading branch information
zlq4863947 committed Nov 7, 2017
1 parent ffc0db3 commit 02c4e60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ Log.init(Log.category.system, Log.level.ALL, 'ns-expert-advisor');

const expertAdvisor = new ExpertAdvisor(config);

const stopServ = (serv: ExpertAdvisor) => {
Log.system.info('EA程序退出方法[启动]');
// 资源释放
const stopTask = new Scheduler('01 15 * * *');
stopTask.invok((ea: ExpertAdvisor) => {
Log.system.info('启动定时终止EA程序[服务退出]');
ea.destroy();
// 删除定时任务
stopTask.reminder.cancel();
}, serv);
Log.system.info('EA程序退出方法[终了]');
}

// 如果为交易时间,直接启动EA服务
if (Util.isTradeTime()) {
Log.system.info('当前为交易时间,直接启动EA程序');
expertAdvisor.start();
stopServ(expertAdvisor);
}
Log.system.info('注册定时EA服务程序[开始]');
const eaTask = new Scheduler('0 9 * * *'); // */3 * * * * * // 0 9 * * *
Expand All @@ -24,5 +38,6 @@ eaTask.invok((ea: ExpertAdvisor) => {
expertAdvisor.start().catch((err) => {
Log.system.error(`EA服务异常:${err.stack}`);
});
stopServ(expertAdvisor);
}, expertAdvisor);
Log.system.info('注册定时EA服务程序[终了]');
12 changes: 10 additions & 2 deletions src/lib/expert-advisor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class ExpertAdvisor {
};
// 实时监测间隔
interval: number;
worker: number;
manager: Manager;
trader: Trader;
dataProvider: DataProvider;
Expand All @@ -46,6 +47,7 @@ export class ExpertAdvisor {
}

destroy() {
clearInterval(this.worker);
this.manager.destroy();
this.trader.end();
this.dataProvider.close();
Expand All @@ -59,7 +61,7 @@ export class ExpertAdvisor {
Log.system.warn(`账户:${this.account.id},可用余额:0,不执行EA程序!`);
return;
}
setInterval(this.onPretrade.bind(this), this.interval);
this.worker = setInterval(this.onPretrade.bind(this), this.interval);
await this.trader.init();
}

Expand Down Expand Up @@ -120,6 +122,7 @@ export class ExpertAdvisor {
}

async get5minData(symbol: string): Promise<types.Bar[]> {
Log.system.info('获取5分钟数据方法[启动]');
if (this.backtest.test) {
return await this.getTest5minData(symbol);
}
Expand All @@ -130,15 +133,20 @@ export class ExpertAdvisor {
p: '1d',
i: 300
});
// 获取当天最5分钟k线
Log.system.info('google获取数据:', hisData);
Log.system.info('len: ', hisData.length)
// 获取当天5分钟k线
const barData = await this.dataProvider.getLast5minBar(symbol);
Log.system.info(' 获取当天5分钟k线', barData);
Log.system.info('len: ', barData.length);
// 合并数据
barData.map((bar) => {
const res = hisData.find((his) => his.time !== bar.time);
if (res) {
hisData.push(res);
}
})
Log.system.info('获取5分钟数据方法[终了]');
return hisData;
}

Expand Down

0 comments on commit 02c4e60

Please sign in to comment.