from jqdatasdk import *
from jqdatasdk.technical_analysis import *
from sqlalchemy import create_engine
import pandas as pd
import pymysql
import datetime


auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
strattime = datetime.datetime.now()
print(get_query_count())

stocks = list(get_all_securities(['stock']).index)
qihuo = get_price(['IF9999.CCFX', 'IC9999.CCFX','IH9999.CCFX', 'IM9999.CCFX'], start_date='2015-01-01', end_date='2015-12-31', frequency='daily', fields=None, skip_paused=False, fq='pre' ,panel=True)

print(qihuo)
for fre in ['30m', '1d']:
    print(fre)
    # 连接数据库
    db = pymysql.connect(host='localhost',
                         user='root',
                         port=3307,
                         password='r6kEwqWU9!v3',
                         database='hlfx')
    cursor = db.cursor()
    cursor.execute("show tables like '%%%s%%' " % fre)
    table_list = [tuple[0] for tuple in cursor.fetchall()]
    print('取得 table_list %s' % fre)

time = datetime.datetime(2010, 1, 4)
print(time)

for stock in table_list:
    print(stock)
    sql = ("select date_format(date, '%%Y-%%m-%%d') from `%s` where HL='L'" % stock)
    cheak_time = cursor.execute(sql)
    print(cheak_time)
    time_list = [''.join(time) for time in cursor.fetchall()]
    print(time_list)

    break
    #stock = normalize_code(stock[3:9])


# 定义股票池列表
security_list1 = '000001.XSHE'
# security_list2 = ['000001.XSHE','000002.XSHE','601211.XSHG','603177.XSHG']
# # 计算并输出 security_list1 的 MACD 值
macd_dif, macd_dea, macd_macd = MACD(security_list1, check_date=time_list[1], SHORT = 12, LONG = 26, MID = 9)
print(macd_dif[security_list1])
print(macd_dea[security_list1])
print(macd_macd[security_list1])

# 输出 security_list2 的 MACD 值
macd_dif, macd_dea, macd_macd = MACD(security_list2,check_date=datetime.datetime.today(), SHORT = 12, LONG = 26, MID = 9)
for stock in security_list2:
    print(macd_dif[stock])
    print(macd_dea[stock])
    print(macd_macd[stock])

df = get_bars(stocks, count=10, unit='30m',
              fields=['date','open','close','high','low','volume','money'],include_now=False,end_dt=datetime.date.today())
print(df)
endtime = datetime.datetime.now()
print('单次时长为：', (endtime - strattime).seconds)


#get_ticks("000001.XSHE", "2022-01-01", datetime.datetime.today())