# Path: kquant_data/config.py
# __CONFIG_H5_STK_DIR__ = r'D:\DATA_STK'
# 
# __CONFIG_H5_STK_FACTOR_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'factor')
# 
# Path: kquant_data/wind_resume/wsd.py
# def resume_download_delist_date(
#         w,
#         wind_codes,
#         root_path,
#         field='delist_date',
#         dtype=np.datetime64):
#     """
#     下载每支股票的delist_date
#     如果以后有同类的每个股票一个数，但可能上新股票都得更新的field就可以用
#     :param w:
#     :param wind_codes:
#     :param field:
#     :param dtype:
#     :param root_path:
#     :return:
#     """
#     wind_codes_set = set(wind_codes)
# 
#     date_str = datetime.today().strftime('%Y-%m-%d')
# 
#     path = os.path.join(root_path, '%s.csv' % field)
#     if dtype == np.datetime64:
#         df_old = read_datetime_dataframe(path)
#     else:
#         df_old = read_data_dataframe(path)
# 
#     if df_old is None:
#         new_symbols = wind_codes_set
#     else:
#         df_old.dropna(axis=1, inplace=True)
#         new_symbols = wind_codes_set - set(df_old.columns)
# 
#     # 没有新数据好办，只有一个数据怎么办？会出错吗
#     if len(new_symbols) == 0:
#         print('没有空合约，没有必要更新%s' % field)
#         # 可能排序不行，还是再处理下
#         df_new = df_old.copy()
#     else:
#         # 第一次下全，以后每次下最新的
#         df_new = download_daily_at(w, list(new_symbols), field, date_str)
# 
#     # 新旧数据的合并
#     df = pd.DataFrame(columns=wind_codes)
#     if df_old is not None:
#         df[df_old.columns] = df_old
#         df.index = df_new.index
#         df[df_new.columns] = df_new
#     else:
#         df = df_new
# 
#     # 排序有点乱，得处理
#     df = df[wind_codes]
#     if dtype == np.datetime64:
#         write_datetime_dataframe(path, df)
#     else:
#         write_data_dataframe(path, df)
# 
# Path: kquant_data/api.py
# def all_instruments(path=None, type=None):
#     """
#     得到合约列表
#     :param type:
#     :return:
#     """
#     if path is None:
#         path = os.path.join(__CONFIG_H5_STK_DIR__, "daily", 'Symbol.csv')
# 
#     df = pd.read_csv(path, dtype={'code': str})
# 
#     return df


import os
import numpy as np
from WindPy import w
from kquant_data.config import __CONFIG_H5_STK_DIR__, __CONFIG_H5_STK_FACTOR_DIR__
from kquant_data.wind_resume.wsd import resume_download_delist_date
from kquant_data.api import all_instruments
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
执行次数很早的算法
比如下载行业分类列表，下载
"""

if __name__ == '__main__':
    w.start()

    # 加载股票列表，这里需要在每天收盘后导出日线数据才能做
    path = os.path.join(__CONFIG_H5_STK_DIR__, '1day', 'Symbol.csv')
    Symbols = all_instruments(path)
    wind_codes = Symbols['wind_code']

    # 增量下载ipo_date，由于每周都有上市，但因为新上市股票不参加交易，所以看情况进行
    if True:
        resume_download_delist_date(w, wind_codes, __CONFIG_H5_STK_FACTOR_DIR__,
                                    field='ipo_date',
                                    dtype=np.datetime64)
    # 增量下载delist_date，由于退市频率比较低，隔一定时间做一次即可
    if False:

# Path: kquant_data/config.py
# __CONFIG_H5_STK_DIR__ = r'D:\DATA_STK'
# 
# __CONFIG_H5_STK_FACTOR_DIR__ = os.path.join(__CONFIG_H5_STK_DIR__, 'factor')
# 
# Path: kquant_data/wind_resume/wsd.py
# def resume_download_delist_date(
#         w,
#         wind_codes,
#         root_path,
#         field='delist_date',
#         dtype=np.datetime64):
#     """
#     下载每支股票的delist_date
#     如果以后有同类的每个股票一个数，但可能上新股票都得更新的field就可以用
#     :param w:
#     :param wind_codes:
#     :param field:
#     :param dtype:
#     :param root_path:
#     :return:
#     """
#     wind_codes_set = set(wind_codes)
# 
#     date_str = datetime.today().strftime('%Y-%m-%d')
# 
#     path = os.path.join(root_path, '%s.csv' % field)
#     if dtype == np.datetime64:
#         df_old = read_datetime_dataframe(path)
#     else:
#         df_old = read_data_dataframe(path)
# 
#     if df_old is None:
#         new_symbols = wind_codes_set
#     else:
#         df_old.dropna(axis=1, inplace=True)
#         new_symbols = wind_codes_set - set(df_old.columns)
# 
#     # 没有新数据好办，只有一个数据怎么办？会出错吗
#     if len(new_symbols) == 0:
#         print('没有空合约，没有必要更新%s' % field)
#         # 可能排序不行，还是再处理下
#         df_new = df_old.copy()
#     else:
#         # 第一次下全，以后每次下最新的
#         df_new = download_daily_at(w, list(new_symbols), field, date_str)
# 
#     # 新旧数据的合并
#     df = pd.DataFrame(columns=wind_codes)
#     if df_old is not None:
#         df[df_old.columns] = df_old
#         df.index = df_new.index
#         df[df_new.columns] = df_new
#     else:
#         df = df_new
# 
#     # 排序有点乱，得处理
#     df = df[wind_codes]
#     if dtype == np.datetime64:
#         write_datetime_dataframe(path, df)
#     else:
#         write_data_dataframe(path, df)
# 
# Path: kquant_data/api.py
# def all_instruments(path=None, type=None):
#     """
#     得到合约列表
#     :param type:
#     :return:
#     """
#     if path is None:
#         path = os.path.join(__CONFIG_H5_STK_DIR__, "daily", 'Symbol.csv')
# 
#     df = pd.read_csv(path, dtype={'code': str})
# 
#     return df


# Path: demo_stock/A_1day/C01_ipo_date.py
import os
import numpy as np
from WindPy import w
from kquant_data.config import __CONFIG_H5_STK_DIR__, __CONFIG_H5_STK_FACTOR_DIR__
from kquant_data.wind_resume.wsd import resume_download_delist_date
from kquant_data.api import all_instruments

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
执行次数很早的算法
比如下载行业分类列表，下载
"""

if __name__ == '__main__':
    w.start()

    # 加载股票列表，这里需要在每天收盘后导出日线数据才能做
    path = os.path.join(__CONFIG_H5_STK_DIR__, '1day', 'Symbol.csv')
    Symbols = all_instruments(path)
    wind_codes = Symbols['wind_code']

    # 增量下载ipo_date，由于每周都有上市，但因为新上市股票不参加交易，所以看情况进行
    if True:
        resume_download_delist_date(w, wind_codes, __CONFIG_H5_STK_FACTOR_DIR__,
                                    field='ipo_date',
                                    dtype=np.datetime64)
    # 增量下载delist_date，由于退市频率比较低，隔一定时间做一次即可
    if False:

        resume_download_delist_date(w, wind_codes, __CONFIG_H5_STK_FACTOR_DIR__,