from jqdatasdk import * from datetime import datetime as dt import pandas as pd import pymysql from sqlalchemy import create_engine import time auth('18616891214', 'Ea?*7f68nD.dafcW34d!') # fre = ['30m', '1d'] engine_hlfx_pool = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8') stock_pool = pd.read_sql_query( 'select value from MA5_1d', engine_hlfx_pool) stock_pool = stock_pool.iloc[-2, 0].split(",") print(type(stock_pool), len(stock_pool),stock_pool) num_industry = get_industry(stock_pool) print(num_industry) results = [] a = [] for key in num_industry.values(): for key2 in key.values(): results.append(key2['industry_name']) results = pd.value_counts(results) print(results) results = results[0:3] results = list(results.index) print(results) for key,value in num_industry.items(): for key2 in value.values(): if key2['industry_name'] in results: a.append(key) print(set(a)) from jqdatasdk import * from datetime import datetime as dt import pandas as pd import pymysql from sqlalchemy import create_engine import numpy as np from jqdatasdk.technical_analysis import * auth('18616891214', 'Ea?*7f68nD.dafcW34d!') def calculateEMA(period, closeArray, emaArray): """计算指数移动平均""" length = len(closeArray) nanCounter = np.count_nonzero(np.isnan(closeArray)) if emaArray: # emaArray.extend(np.tile([np.nan], (nanCounter + period - 1))) # firstema = np.mean(closeArray[nanCounter:nanCounter + period - 1]) firstema = emaArray[-1] emaArray.append(firstema) for i in range(nanCounter, length): ema = (2 * closeArray[i] + (period - 1) * emaArray[-1]) / (period + 1) emaArray.append(ema) return np.array(emaArray) def calculateMACD(emaArray, closeArray, shortPeriod=12, longPeriod=26, signalPeriod=9): ema12 = calculateEMA(shortPeriod, closeArray, emaArray) print(ema12) ema26 = calculateEMA(longPeriod, closeArray, emaArray) print(ema26) diff = ema12 - ema26 dea = calculateEMA(signalPeriod, diff, []) macd = 2 * (diff - dea) return macd, diff, dea stock = '000010.XSHE' fre = '1d' emaArray = [] engine_stock = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8') df = pd.read_sql_query('select date,open,close,high,low,volume,money from `stk%s_%s`' % (stock, fre), engine_stock) df_close = df['close'] emaArray.append = EMA(stock, check_date='2010-01-04', timeperiod=fre)[stock] df2 = calculateMACD(emaArray, df_close) print(df2) print(len(df), len(df2[0])) df3 = pd.concat([df, pd.Series(df2[0]).rename('macd'), pd.Series(df2[1]).rename('diff'), pd.Series(df2[2]).rename('dea')], axis=1) print(df3.loc[df.date== '2010-02-25',:]) x_macd_dif, x_macd_dea, x_macd_macd = MACD(stock, check_date='2010-02-26 00:00:00', SHORT=12, LONG=26, MID=9, unit=fre) print(x_macd_macd, x_macd_dif, x_macd_dea)