# coding=utf-8
from xtquant import xtdata
from datetime import datetime as dt
import pandas as pd
from sqlalchemy import create_engine

path = 'c:\\qmt\\userdata_mini'


if __name__ == '__main__':
    fre = '1d'
    stocks = xtdata.get_stock_list_in_sector('沪深A股')
    print(stocks, '\n')
    stocks.sort()



    # df_data = xtdata.get_local_data(field_list=[], stock_code=stocks, start_time='', end_time='',
    #                                 period='1d',  count=-1)
    # print(df_data)
    for s in stocks[0:4]:
        print(s)
        cq = xtdata.get_divid_factors(s, start_time='19910101', end_time='20130115')
        print(cq)
        df_data = xtdata.get_local_data(field_list=[], stock_code=[s], start_time='', end_time='',
                                        period='1d', count=-1, dividend_type='back')
        df = pd.concat([df_data[i].T for i in ['time', 'open', 'high', 'low', 'close', 'volume', 'amount']], axis=1)
        df.columns = ['time', 'open', 'high', 'low', 'close', 'volume', 'amount']
        df['time'] = df['time'].apply(lambda x: dt.fromtimestamp(x / 1000.0))
        # print(df)
        df['time'] = pd.to_datetime(df['time'], unit='ms')
        df['time'] = pd.to_datetime(df['time'], format='%Y-%m-%d')
        df.reset_index(drop=True, inplace=True)
        # df['time'] = df['time'] + timedelta(hours=8)
        print(df)
