-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_trading.py
More file actions
79 lines (56 loc) · 2.1 KB
/
real_trading.py
File metadata and controls
79 lines (56 loc) · 2.1 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
import json
import logging
import os
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.blocking import BlockingScheduler
from bot.emulate_bot import EmulateBot
from strategy.example.VTCompLong import VTCompLong
# Schedule balance log
def schedule_balance_log(bot):
scheduler = BackgroundScheduler()
scheduler.add_job(bot.output_balance, 'cron', minute='*/5', second='30')
scheduler.start()
# Schedule strategy trigger
def schedule_strategy_trigger(strategy):
# Schedule strategy trigger (trigger special events between two strategy executions)
scheduler = BackgroundScheduler()
# Per 1 minute (offset 15 seconds)
scheduler.add_job(strategy.run_trigger, 'cron', minute='*/1', second='15')
scheduler.start()
# Schedule execution of strategy
def schedule_strategy(strategy):
scheduler = BlockingScheduler()
# Per hour (offset 10 seconds)
# scheduler.add_job(job, 'cron', hour='*/1', second='10')
# Per 15 minutes (offset 3 seconds)
scheduler.add_job(strategy.run, 'cron', minute='*/15', second='3')
# Every 3 seconds for test
# scheduler.add_job(strategy.run, 'interval', seconds=3)
scheduler.start()
def set_logging(config):
# Set logging
dir = config['logs_dir']
if not os.path.isdir(dir):
os.mkdir(dir)
log_name = f'{dir}/log_{datetime.now().date()}.log'
logging.basicConfig(filename=log_name,
filemode='a', format='%(asctime)s - %(message)s', level=logging.INFO)
# Real-trading entry
if __name__ == "__main__":
# Load configuration file
with open('./config.json') as f:
config = json.load(f)
# Set logging
set_logging(config)
# Load trading bot
# bot = RealBot(config) # Uncomment this to run real bot for trading when everything is ready.
bot = EmulateBot(config)
# Load strategy
strategy = VTCompLong(bot)
# Schedule balance log
schedule_balance_log(bot)
# Schedule strategy trigger
schedule_strategy_trigger(strategy)
# Schedule strategy
schedule_strategy(strategy)