# coding=UTF-8
# encoding:UTF-8
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import tushare as ts
def Stock_KDJ_Calculation(stock_number):
stock_price_daily_temp = ts.get_hist_data(stock_number)
stock_price_daily = stock_price_daily_temp.sort_index(ascending = True)
# print stock_price_daily[:1]
# print stock_price_daily[0]
RSV = pd.Series(0.00,index = stock_price_daily.index)
K = pd.Series(0.00,index = stock_price_daily.index)
D = pd.Series(0.00,index = stock_price_daily.index)
J = pd.Series(0.00,index = stock_price_daily.index)
# print RSV
stock_price_daily['RSV'] = RSV
stock_price_daily['K'] = K
stock_price_daily['D'] = D
stock_price_daily['J'] = J
# print stock_price_daily
stock_price_daily.RSV = (stock_price_daily.close - stock_price_daily.low)/(stock_price_daily.high - stock_price_daily.low) * 100.00
stock_price_daily.K[0] = 50.00
stock_price_daily.D[0] = 50.00
for i in range(1,len(stock_price_daily)):
stock_price_daily.K[i] =stock_price_daily.K[i-1]*2/3 + stock_price_daily.RSV[i]*1/3
stock_price_daily.D[i] =stock_price_daily.D[i-1]*2/3 + stock_price_daily.K[i]*1/3
stock_price_daily.J[i] =stock_price_daily.D[i]*3 - stock_price_daily.K[i]*2
# print stock_price_daily
return stock_price_daily
#Stock_KDJ_Calculation('000858')
#fig, ax = plt.subplots(2,1)
#ax[0].plot(stock_price_daily.close[-100:], label = 'Close')
#ax[1].plot(stock_price_daily.K[-100:], label = 'K')
#ax[1].plot(stock_price_daily.D[-100:], label = 'D')
#ax[1].plot(stock_price_daily.J[-100:], label = 'J')
#ax[0].tick_params(labelsize = 5.5)
#ax[1].tick_params(labelsize = 5.5)
#plt.legend()
#plt.show()
#tock_list = ts.get_stock_basics()
#rint stock_list
#stock_finance_report = ts.get_report_data(2018,3)
#print stock_finance_report
HS300_list = pd.Series()
HS300_list_temp = pd.Series()
HS300_list_temp = ts.get_hs300s() #沪深300成分股
HS300_list = HS300_list_temp.ix[:,'code']
#print HS300_list_temp
#print HS300_list
HS300_list_temp.to_csv('/home/zhou/eclipse-workspace/stock/HS300_list.csv',encoding='gbk')
#for i in range(0,len(HS300_list)):
# Stock_MA5_MA20_Analysis(HS300_list.code[i])
def Stock_KDJ_Analysis(stock_number):
# for i in range (0, len(stock_list)):
# print stock_list.code[i]
# try:
stock_price_daily = Stock_KDJ_Calculation(stock_number)
stock_price_daily_short_term = stock_price_daily[-2:]
# print stock_price_daily_short_term
# print stock_price_daily_short_term.K[0]
# print stock_price_daily_short_term.K[1]
# print stock_price_daily
# print stock_price_daily.close[:5]
# stock_price_daily = stock_price_daily_temp.sort_index(ascending = True)
# try:
# stock_price_close_temp = stock_price_daily.close[:2]
# except AttributeError:
# print stock_list.code[i]
# continue
# else:
# stock_price_close = stock_price_close_temp.sort_index(ascending = True)
# print stock_price_close[0]
# print stock_price_close[1]
# print stock_price_close[2]
# print stock_price_close[0:2]
# stock_price_ma5_temp = stock_price_daily.ma5[:2]
# stock_price_ma5 = stock_price_ma5_temp.sort_index(ascending = True)
# stock_price_ma10_temp = stock_price_daily.ma10[:2]
# stock_price_ma10 = stock_price_ma10_temp.sort_index(ascending = True)
# stock_price_ma20_temp = stock_price_daily.ma20[:2]
# stock_price_ma20 = stock_price_ma20_temp.sort_index(ascending = True)
stock_buy_signal = False
for j in range(1, len(stock_price_daily_short_term)):
if stock_price_daily_short_term.K[j-1]-stock_price_daily_short_term.D[j-1] < 0.00 and stock_price_daily_short_term.K[j] - stock_price_daily_short_term.D[j] > 1.00:
if stock_price_daily_short_term.K[j] < 60.00 and stock_price_daily_short_term.D[j] < 60.00:
stock_buy_signal = True
if stock_buy_signal == True:
print stock_number
# except:
# print stock_list.code[i]
# i = i +1
# continue
#Stock_KDJ_Analysis('000858')
# elif stock_price_ma5[i]-stock_price_ma20[i] < 0.00 and stock_price_ma5[i-1] - stock_price_ma20[i-1] > 0.00:
# stock_signal_ma[i] = -1
for i in range(0,len(HS300_list)):
Stock_KDJ_Analysis(HS300_list[i])