stock house (continue)

This commit is contained in:
fasiondog 2020-08-17 00:35:01 +08:00
parent ab45778e4f
commit a5df0098f9
2 changed files with 84 additions and 32 deletions

View File

@ -53,5 +53,6 @@ from .deprecated import *
from .indicator import *
from .trade_manage import *
from .trade_sys import *
from .stockhouse import *
__version__ = get_version()

View File

@ -8,23 +8,41 @@
#===============================================================================
import os
import sys
import git
from configparser import ConfigParser
from sqlalchemy import create_engine, Sequence, Column, Integer, String, and_
from sqlalchemy import (create_engine, Sequence, Column, Integer, String, and_, UniqueConstraint)
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class RemoteHouse(Base):
__tablename__ = 'remote_house'
class ConfigModel(Base):
__tablename__ = 'house_config'
id = Column(Integer, Sequence('config_id_seq'), primary_key=True)
key = Column(String, index=True) #
value = Column(String) #
__table_args__ = (UniqueConstraint('key'), )
def __str__(self):
return "ConfigModel(id={}, key={}, value={})".format(self.id, self.key, self.value)
def __repr__(self):
return "<{}>".format(self.__str__())
class HouseModel(Base):
__tablename__ = 'house_repo'
id = Column(Integer, Sequence('remote_id_seq'), primary_key=True)
name = Column(String, index=True) #
house_type = Column(String)
local = Column(String) #
url = Column(String) # git
branch = Column(String) #
local = Column(String) #
__table_args__ = (UniqueConstraint('name'), )
def __str__(self):
return "RemoteHouse(id={}, url={}, local={})".format(self.id, self.url, self.local)
@ -33,6 +51,15 @@ class RemoteHouse(Base):
return "<{}>".format(self.__str__())
class PartModel(Base):
__tablename__ = 'house_part'
id = Column(Integer, Sequence('part_id_seq'), primary_key=True)
house_id = Column(Integer) #
part = Column(String) #
name = Column(String) #
path = Column(String) #
class StockHouse(object):
"""策略库管理"""
def __init__(self):
@ -44,46 +71,70 @@ class StockHouse(object):
Session = sessionmaker(bind=engine)
self._session = Session()
# 获取策略仓库配置信息
self.config_file = "{}/.hikyuu/stockhouse.ini".format(usr_dir)
self.config = ConfigParser()
if os.path.exists(self.config_file):
self.config.read(self.config_file, encoding='utf-8')
# 检查并建立远端仓库的本地缓存目录
self.remote_cache_dir = self._session.query(ConfigModel.value
).filter(ConfigModel.key == 'remote_cache_dir'
).first()
if self.remote_cache_dir is None:
self.remote_cache_dir = "{}/.hikyuu/stockhouse".format(usr_dir)
record = ConfigModel(key='remote_cache_dir', value=self.remote_cache_dir)
self._session.add(record)
self._session.commit()
else:
# 如果配置文件不存在,则创建默认配置信息
# 创建本地默认策略仓库缓存目录
house_dir = "{}/.hikyuu/stockhouse".format(usr_dir)
if not os.path.lexists(house_dir):
os.makedirs(house_dir)
self.config["remote"] = {
"cache": house_dir,
"hikyuu": "https://gitee.com/fasiondog/stockhouse.git"
}
self.add_remote_house(self.config['remote']['hikyuu'], 'hikyuu', 'master')
self.remote_cache_dir = self.remote_cache_dir[0]
self._save_config()
if not os.path.lexists(self.remote_cache_dir):
os.makedirs(self.remote_cache_dir)
def _save_config(self):
"""保存配置信息"""
with open(self.config_file, 'w', encoding='utf-8') as f:
self.config.write(f)
# 检查并下载 hikyuu 策略仓库
hikyuu_house_path = self._session.query(HouseModel.local
).filter(HouseModel.name == 'hikyuu_house').first()
if hikyuu_house_path is None:
self.add_remote_house(
'hikyuu_house', 'https://gitee.com/fasiondog/stockhouse.git', 'master'
)
def add_remote_house(self, url, name, branch='master'):
record = self._session.query(RemoteHouse).filter(RemoteHouse.name == name).first()
def add_remote_house(self, name, url, branch='master'):
"""增加远程策略仓库
:param str name:
:param str url: git
:param str branch: git
"""
record = self._session.query(HouseModel).filter(HouseModel.name == name).first()
assert record is None, ''
record = self._session.query(RemoteHouse).filter(
and_(RemoteHouse.url == url, RemoteHouse.branch == branch)
record = self._session.query(HouseModel).filter(
and_(HouseModel.url == url, HouseModel.branch == branch)
).first()
assert record is None, ''
local_cache = "{}/{}".format(self.config["remote"]["cache"], name)
clone = git.Repo.clone_from(url, local_cache, branch=branch)
record = RemoteHouse(name=name, url=url, branch=branch, local=local_cache)
# 如果存在同名缓存目录,则强制删除
local_dir = "{}/{}".format(self.remote_cache_dir, name)
if os.path.lexists(local_dir):
os.removedirs(local_dir)
print(' "stockhouse/{}"'.format(name))
try:
clone = git.Repo.clone_from(url, local_dir, branch=branch)
except:
raise RuntimeError("请检查网络是否正常或链接地址({})是否正确!".format(url))
# 导入仓库各部件策略信息
self.import_part_to_db(local_dir)
# 更新仓库记录
record = HouseModel(name=name, house_type='remote', url=url, branch=branch, local=local_dir)
self._session.add(record)
self._session.commit()
def import_part_to_db(self, local_dir):
for root, dirs, files in os.walk(local_dir):
for dir_name in dirs:
print(dir_name)
for file_name in files:
print(file_name)
if __name__ == "__main__":
house = StockHouse()