backtrader/utils.py

38 lines
1.1 KiB
Python
Raw Normal View History

2025-04-04 15:24:02 +08:00
import os
import yaml
import tushare as ts
import pandas as pd
from datetime import datetime, timedelta
from sqlalchemy import create_engine
2025-04-04 15:24:02 +08:00
def load_config():
config_path = 'config.yaml'
if not os.path.exists(config_path):
config = {
'tushare_token': 'your_token_here',
'mysql': {
'user': 'user',
'password': 'password',
'host': '127.0.0.1',
'port': 3306,
'database': 'tushare',
'charset': 'utf8'
}
}
2025-04-04 15:24:02 +08:00
with open(config_path, 'w') as f:
yaml.dump(config, f)
print(f"请在 {config_path} 中填入您的 tushare token 和 MySQL 连接信息")
2025-04-04 15:24:02 +08:00
exit(1)
2025-04-04 15:24:02 +08:00
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
return config
def create_engine_from_config(config):
mysql = config['mysql']
connection_string = f"mysql+pymysql://{mysql['user']}:{mysql['password']}@{mysql['host']}:{mysql['port']}/{mysql['database']}?charset={mysql['charset']}&use_unicode=1"
return create_engine(connection_string)