|  | @@ -36,3 +36,58 @@ for key,value in num_industry.items():
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  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 not emaArray:
 | 
	
		
			
				|  |  | +        emaArray.extend(np.tile([np.nan], (nanCounter + period - 1)))
 | 
	
		
			
				|  |  | +        firstema = np.mean(closeArray[nanCounter:nanCounter + period - 1])
 | 
	
		
			
				|  |  | +        emaArray.append(firstema)
 | 
	
		
			
				|  |  | +        for i in range(nanCounter + period, length):
 | 
	
		
			
				|  |  | +            ema = (2 * closeArray[i] + (period - 1) * emaArray[-1]) / (period + 1)
 | 
	
		
			
				|  |  | +            emaArray.append(ema)
 | 
	
		
			
				|  |  | +    return np.array(emaArray)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +def calculateMACD(closeArray, shortPeriod=12, longPeriod=26, signalPeriod=9):
 | 
	
		
			
				|  |  | +    ema12 = calculateEMA(shortPeriod, closeArray, [])
 | 
	
		
			
				|  |  | +    print(ema12)
 | 
	
		
			
				|  |  | +    ema26 = calculateEMA(longPeriod, closeArray, [])
 | 
	
		
			
				|  |  | +    print(ema26)
 | 
	
		
			
				|  |  | +    diff = ema12 - ema26
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    dea = calculateEMA(signalPeriod, diff, [])
 | 
	
		
			
				|  |  | +    macd = 2 * (diff - dea)
 | 
	
		
			
				|  |  | +    return macd, diff, dea
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +stock = '000010.XSHE'
 | 
	
		
			
				|  |  | +fre = '1d'
 | 
	
		
			
				|  |  | +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']
 | 
	
		
			
				|  |  | +df2 = calculateMACD(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)
 |