Skip to content

Commit

Permalink
Merge branch 'dev-v1.1.21' of github.com:xingetouzi/vnpy_fxdayu into …
Browse files Browse the repository at this point in the history
…dev-v1.1.21
  • Loading branch information
caimeng committed Jul 29, 2019
2 parents b3ab9da + 8ca6fb9 commit 3d9a636
Show file tree
Hide file tree
Showing 7 changed files with 1,578 additions and 1,516 deletions.
5 changes: 3 additions & 2 deletions vnpy/applications/VnTerminal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@


@click.option('-m', '--monitor', is_flag=True)
def app_cli(monitor=False):
main(monitor=monitor)
@click.option('-k', '--keep', is_flag=True)
def app_cli(monitor=False, keep=False):
main(monitor=monitor, keep=keep)
46 changes: 30 additions & 16 deletions vnpy/applications/VnTerminal/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ def run(self, monitor=False):
le.info(u"开始所有策略")
cta.startAll()

def activeStrategyCount(self):
count = 0
for name, strategy in self.cta.strategyDict.items():
if strategy.trading:
count += 1
return count

def join(self):
while self.running:
sleep(1)
Expand All @@ -160,7 +167,7 @@ def stop(self):
logging.info(u"交易程序正常退出")
except Exception as e:
logging.exception(e)

self.me.exit()

class DaemonApp(object):
def __init__(self):
Expand All @@ -184,34 +191,36 @@ def run(self, monitor=False):

self.process = None # 子进程句柄

def join(self):
def join(self, keep=False):
while self.running:
currentTime = datetime.now().time()
recording = True

# TODO: 设置交易时段
# 判断当前处于的时间段
# if ((currentTime >= DAY_START and currentTime <= DAY_END) or
# (currentTime >= NIGHT_START) or
# (currentTime <= NIGHT_END)):
# recording = True

# 记录时间则需要启动子进程
if recording and self.process is None:
# TODO: 可能多次启动,可能要在启动前对pipe进行清理或重新创建
logging.info(u'启动子进程')
self.process = multiprocessing.Process(
target=self._run_child,
args=(self.pchild, ),
kwargs={"monitor": self._run_with_monitor})
kwargs={"monitor": self._run_with_monitor},
daemon=True
)
self.process.start()
logging.info(u'子进程启动成功')

if self.process:
if not self.process.is_alive():
if keep:
self._stop_child()
else:
self.stop()
# 非记录时间则退出子进程
if not recording and self.process is not None:
self._stop_child()
# if not recording and self.process is not None:
# self._stop_child()
sleep(5)

logging.info("停止CTA策略守护父进程")


@staticmethod
def _run_child(p, monitor=False):
Expand All @@ -231,6 +240,11 @@ def interrupt(signal, event):
p.recv()
raise KeyboardInterrupt
else:
if not app.ee.isActive:
app.stop()
elif not app.activeStrategyCount():
app.stop()

continue
except KeyboardInterrupt:
app.stop()
Expand Down Expand Up @@ -260,11 +274,11 @@ def _stop_child(self):
logging.info(u'子进程关闭成功')

def stop(self):
self.runing = False
self.running = False
self._stop_child()


def main(monitor=False):
def main(monitor=False, keep=False):
import signal
import logging

Expand All @@ -276,6 +290,6 @@ def interrupt(signal, event):
app = DaemonApp()
try:
app.run(monitor=monitor)
app.join()
app.join(keep)
except KeyboardInterrupt:
app.stop()
Loading

0 comments on commit 3d9a636

Please sign in to comment.