52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
import argparse
|
|
import json
|
|
import sys
|
|
from .config import Settings
|
|
|
|
|
|
def main():
|
|
if hasattr(sys.stdout, 'reconfigure'):
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
if hasattr(sys.stderr, 'reconfigure'):
|
|
sys.stderr.reconfigure(encoding='utf-8')
|
|
parser = argparse.ArgumentParser(description='Separate local MT5 desk')
|
|
parser.add_argument('command', choices=['serve', 'supervise', 'sync', 'status'], nargs='?', default='serve')
|
|
parser.add_argument('--no-auto-refresh', action='store_true', help='serve cached state without a background MT5 worker')
|
|
args = parser.parse_args()
|
|
config = Settings.load()
|
|
if args.command == 'supervise':
|
|
from .supervisor import run
|
|
run(config)
|
|
elif args.command == 'serve':
|
|
from .app import run
|
|
run(config, auto_refresh=not args.no_auto_refresh)
|
|
else:
|
|
from .service import Desk
|
|
desk = Desk(config)
|
|
if args.command == 'status':
|
|
state = desk.state()
|
|
print(json.dumps({'job': state['job'], 'history': state['history'],
|
|
'automation': state['automation'],
|
|
'signal_as_of': state['research'].get('signal_as_of')}, ensure_ascii=False, indent=2))
|
|
else:
|
|
from .research import download_history, run_research
|
|
from .store import now_iso
|
|
def progress(stage, message, done=0, total=1):
|
|
desk.progress(stage, message, done, total)
|
|
if stage != 'history' or done % 25 == 0 or done == total:
|
|
print(message, flush=True)
|
|
try:
|
|
desk.broker.snapshot()
|
|
download_history(desk.broker, desk.store, config, progress)
|
|
result = run_research(desk.store, config, progress)
|
|
desk.update_quotes(desk.broker.snapshot(), result)
|
|
desk.store.put('job', {'running': False, 'stage': 'complete', 'message': 'พร้อมใช้งาน', 'updated_at': now_iso()})
|
|
print('Signals:', len(result['signals']), 'as of', result['signal_as_of'])
|
|
except Exception as exc:
|
|
desk.store.put('job', {'running': False, 'stage': 'failed', 'message': str(exc), 'updated_at': now_iso()})
|
|
raise
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|