# Path: fooltrader/api/technical.py
# def convert_to_list_if_need(input):
# def get_security_list(security_type='stock', exchanges=None, start_code=None, end_code=None,
#                       mode='simple', start_list_date=None, codes=None):
# def _get_security_item(security_type, exchanges, code=None):
# def to_security_item(security_item, exchange=None):
# def get_ticks(security_item, the_date=None, start_date=None, end_date=None):
# def _parse_tick(tick_path, security_item):
# def get_available_tick_dates(security_item):
# def get_kdata(security_item, exchange=None, the_date=None, start_date=None, end_date=None, fuquan='bfq', source=None,
#               level='day', generate_id=False):
# def get_latest_download_trading_date(security_item, return_next=True, source=None):
# def get_trading_calendar(security_type='future', exchange='shfe'):
# def get_trading_dates(security_item, dtype='list', ignore_today=False, source='163', fuquan='bfq'):
# def kdata_exist(security_item, year, quarter, fuquan=None, source='163'):
# def parse_shfe_day_data(force_parse=False):
# def parse_shfe_data(force_parse=False):
#             KDATA_COLUMN_FUTURE = ['timestamp', 'code', 'name', 'low', 'open', 'close', 'high', 'volume', 'turnover',
#                                    'securityId',
#                                    'preClose', 'change', 'changePct', 'openInterest', 'settlement', 'preSettlement',
#                                    'change1',
#                                    'changePct1']
# 
# Path: fooltrader/contract/data_contract.py
# KDATA_COLUMN_SINA = ['timestamp', 'code', 'low', 'open', 'close', 'high', 'volume', 'turnover', 'securityId']
# 
# Path: fooltrader/utils/utils.py
# def get_exchange(code):
#     if code >= '333333':
#         return 'sh'
#     else:
#         return 'sz'


from pytdx.hq import TdxHq_API
from fooltrader.api import technical
from fooltrader.contract.data_contract import KDATA_COLUMN_SINA
from fooltrader.utils.utils import get_exchange
# -*- coding: utf-8 -*-




def get_tdx_kdata(security_item, start, end):
    api = TdxHq_API()
    with api.connect():
        # open close high low vol amount date code
        # KDATA_COLUMN = ['timestamp', 'code', 'low', 'open', 'close', 'high', 'volume', 'turnover', 'securityId']

        df = api.get_k_data(security_item['code'], start, end)
        df = df[['date', 'code', 'low', 'open', 'close', 'high', 'vol', 'amount']]
        df['securityId'] = df['code'].apply(lambda x: 'stock_{}_{}'.format(get_exchange(x), x))
        df['vol'] = df['vol'].apply(lambda x: x * 100)
        df.columns = KDATA_COLUMN_SINA
    return df


# Path: fooltrader/api/technical.py
# def convert_to_list_if_need(input):
# def get_security_list(security_type='stock', exchanges=None, start_code=None, end_code=None,
#                       mode='simple', start_list_date=None, codes=None):
# def _get_security_item(security_type, exchanges, code=None):
# def to_security_item(security_item, exchange=None):
# def get_ticks(security_item, the_date=None, start_date=None, end_date=None):
# def _parse_tick(tick_path, security_item):
# def get_available_tick_dates(security_item):
# def get_kdata(security_item, exchange=None, the_date=None, start_date=None, end_date=None, fuquan='bfq', source=None,
#               level='day', generate_id=False):
# def get_latest_download_trading_date(security_item, return_next=True, source=None):
# def get_trading_calendar(security_type='future', exchange='shfe'):
# def get_trading_dates(security_item, dtype='list', ignore_today=False, source='163', fuquan='bfq'):
# def kdata_exist(security_item, year, quarter, fuquan=None, source='163'):
# def parse_shfe_day_data(force_parse=False):
# def parse_shfe_data(force_parse=False):
#             KDATA_COLUMN_FUTURE = ['timestamp', 'code', 'name', 'low', 'open', 'close', 'high', 'volume', 'turnover',
#                                    'securityId',
#                                    'preClose', 'change', 'changePct', 'openInterest', 'settlement', 'preSettlement',
#                                    'change1',
#                                    'changePct1']
# 
# Path: fooltrader/contract/data_contract.py
# KDATA_COLUMN_SINA = ['timestamp', 'code', 'low', 'open', 'close', 'high', 'volume', 'turnover', 'securityId']
# 
# Path: fooltrader/utils/utils.py
# def get_exchange(code):
#     if code >= '333333':
#         return 'sh'
#     else:
#         return 'sz'


# Path: fooltrader/datasource/tdx.py
from pytdx.hq import TdxHq_API
from fooltrader.api import technical
from fooltrader.contract.data_contract import KDATA_COLUMN_SINA
from fooltrader.utils.utils import get_exchange

# -*- coding: utf-8 -*-




def get_tdx_kdata(security_item, start, end):
    api = TdxHq_API()
    with api.connect():
        # open close high low vol amount date code
        # KDATA_COLUMN = ['timestamp', 'code', 'low', 'open', 'close', 'high', 'volume', 'turnover', 'securityId']

        df = api.get_k_data(security_item['code'], start, end)
        df = df[['date', 'code', 'low', 'open', 'close', 'high', 'vol', 'amount']]
        df['securityId'] = df['code'].apply(lambda x: 'stock_{}_{}'.format(get_exchange(x), x))
        df['vol'] = df['vol'].apply(lambda x: x * 100)
        df.columns = KDATA_COLUMN_SINA
    return df


if __name__ == '__main__':