#coding:utf-8
import datetime
import time
from xtquant import xtdata
import pandas as pd


xtdata.get_client()
print(xtdata.data_dir)


def call_back(historyArrived=1,dataBackType=0,data=[]):
    """
    :param historyArrived: 1表示服务器数据，0表示本地数据
    :param dataBackType: 0:全量历史数据 1:最新一条数据，覆盖上一条 2:追加的最新新数据
    :param data: 订阅的数据
    :return:
    """
    now = datetime.datetime.now()
    now = datetime.datetime.strftime(now, "%Y-%m-%d %H:%M:%S")
    print('callback_data:', now, historyArrived, dataBackType,  data)


if __name__ == '__main__':
    stock_list = xtdata.get_stock_list_in_sector("沪深300")
    print(stock_list)
    t1 = time.time()
    count = 0
    for s in stock_list:
        # 行情订阅
        subscribe_no = xtdata.subscribe_quote(stock_code=s,  end_time=time.strftime("%Y%m%d"), count=1,  period="1d", callback=call_back)
        # print("subscribe_no:", subscribe_no)
        # # 阻塞线程接收行情回调
        #xtdata.subscribing()
        continue

        data_day = xtdata.get_market_data_ex(['high', 'open', 'low', 'close', 'volume'], [s],
                                             period='1d', end_time= time.strftime("%Y%m%d"), dividend_type='front', count=1,)
        print(count, s, data_day[s].to_dict('index'))
        count += 1

