Merge branch 'master' of http://gitlab.fangjia.com/dev0/PriceSystem
This commit is contained in:
@@ -0,0 +1,354 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Fri Dec 22 10:32:05 2017
|
||||
|
||||
@author: Administrator
|
||||
"""
|
||||
|
||||
import pymongo
|
||||
import pandas
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pylab as pl
|
||||
import datetime
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
|
||||
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
|
||||
|
||||
from matplotlib.pylab import date2num
|
||||
import potential
|
||||
|
||||
import talib
|
||||
|
||||
################################
|
||||
|
||||
now_time = datetime.datetime.now()
|
||||
last_month = now_time - datetime.timedelta(days=25)
|
||||
date=int(last_month.strftime('%Y%m'))
|
||||
|
||||
ddd136='mongo.db.fangjia.com'
|
||||
################################
|
||||
|
||||
|
||||
client136 = pymongo.MongoClient(ddd136,27017)
|
||||
db136 = client136.fangjia
|
||||
|
||||
|
||||
data=client136.fangjia_stat
|
||||
potent=data.potential_index
|
||||
|
||||
def city_list():
|
||||
#client = pymongo.MongoClient('192.168.0.136',27017) #'192.168.0.65',27777
|
||||
#db = client.fangjia
|
||||
city = db136.server_area
|
||||
#seawater.find_one()
|
||||
|
||||
|
||||
pos= city.find({"display":{"$ne":"全国"}},
|
||||
{"display":1,"city_grade":1,"_id":1}).sort([("city_grade",1),("_id",1)])
|
||||
|
||||
|
||||
|
||||
data=pandas.DataFrame(list(pos))
|
||||
#print(data)
|
||||
return data['display']
|
||||
|
||||
def region_list(city):
|
||||
#client = pymongo.MongoClient('192.168.0.136',27017)
|
||||
#db = client.fangjia
|
||||
region = db136.region_block
|
||||
#seawater.find_one()
|
||||
query = {"city":city,"category":"region"}
|
||||
fields1 = {"name":1}
|
||||
pos = list(region.find(query,fields1))
|
||||
|
||||
|
||||
data=pandas.DataFrame(pos)
|
||||
return data['name']
|
||||
|
||||
def average_index():
|
||||
|
||||
client=pymongo.MongoClient('192.168.0.136',27017)
|
||||
db1 = client.fangjia_stat.potential_index
|
||||
now_time = datetime.datetime.now()
|
||||
last_month = now_time - datetime.timedelta(days=25)
|
||||
date=int(last_month.strftime('%Y%m'))
|
||||
|
||||
|
||||
query={'date':date}
|
||||
|
||||
a=db1.find(query)
|
||||
ll=['rsi','monment','pl12','pl13','pl14','pl35','macd']
|
||||
a1=list(a)
|
||||
a2=pandas.DataFrame(a1)
|
||||
a3=a2[ll]
|
||||
|
||||
a4=a3.mean()
|
||||
return a4
|
||||
|
||||
def city_index(ct,date,avg):
|
||||
query = {"city":ct,"date":date,"index":{"$gte":50}}
|
||||
up= potent.count(query)
|
||||
|
||||
query1 = {"city":ct,"date":date,"index":{"$lt":50}}
|
||||
|
||||
query2 = {"city":ct,"date":date,}
|
||||
|
||||
ctall=potent.find(query2)
|
||||
c1=list(ctall)
|
||||
c2=pd.DataFrame(c1)
|
||||
d=c2.shape[0]
|
||||
print('d=',d)
|
||||
if d>200:
|
||||
ll=['rsi','monment','pl12','pl13','pl14','pl35','macd']
|
||||
e=c2[ll]
|
||||
|
||||
e1=e.sum()
|
||||
e3=100*e1/d
|
||||
|
||||
def turn(k):
|
||||
return float(round(k,2))
|
||||
|
||||
down= potent.count(query1)
|
||||
mind=min(up,down)
|
||||
|
||||
this_index=e.mean()
|
||||
|
||||
dvide=this_index/avg
|
||||
|
||||
print('dvide=',dvide)
|
||||
|
||||
if mind>0 and (up+down)>500:
|
||||
|
||||
inde=int(100*up/(up+down))
|
||||
|
||||
######################################################
|
||||
|
||||
def apeak(dvide1):
|
||||
|
||||
#print('dvide1=',dvide1)
|
||||
|
||||
bset=set(dvide1.index)
|
||||
cset=set(['pl12','pl13','pl14','pl35'] )
|
||||
gg=['rsi','monment','macd']
|
||||
|
||||
dset=bset & cset
|
||||
dlist=list(dset)
|
||||
|
||||
def takepl(dlist):
|
||||
for i in ['pl35','pl14','pl13','pl12']:
|
||||
if i in dlist:
|
||||
return i
|
||||
poly=takepl(dlist)
|
||||
|
||||
#print('poly=',poly)
|
||||
|
||||
gg=['rsi','monment','macd']
|
||||
|
||||
gset=set(gg)&set(dvide1.index)
|
||||
gglist=list(gset)
|
||||
|
||||
lplus=gglist+[poly]
|
||||
import pandas
|
||||
lplus=list(pandas.Series(lplus).dropna())
|
||||
|
||||
#print(lplus)
|
||||
|
||||
all_columns=['rsi','monment','pl12','pl13','pl14','pl35','macd']
|
||||
chines=['RSI相对强弱指标','monment动量交易策略','近两月的房价趋势','近三月的房价趋势',
|
||||
|
||||
'近四月的房价趋势','近五月的房价趋势','macd异同移动平均线']
|
||||
bb1=pd.Series(chines,index=all_columns)
|
||||
bb2=bb1[lplus]
|
||||
return list(bb2)
|
||||
|
||||
def big_small(dvide,kk):
|
||||
if kk==1:
|
||||
dvide1=dvide[dvide>1]
|
||||
if len(dvide1)>0:
|
||||
spp=apeak(dvide1)
|
||||
return inde,e3,spp+['显示买入']
|
||||
else:
|
||||
dvide2=dvide.sort_values(ascending=False)
|
||||
dvide2=dvide2.iloc[:2]
|
||||
spp=apeak(dvide2)
|
||||
return inde,e3,spp+['显示买入']
|
||||
else:
|
||||
dvide3=dvide[dvide<1]
|
||||
print('dvide3=',dvide3)
|
||||
if len(dvide3)>0:
|
||||
spp=apeak(dvide3)
|
||||
print('spp=',spp)
|
||||
return inde,e3,spp+['显示卖出']
|
||||
else:
|
||||
dvide2=dvide.sort_values(ascending=True)
|
||||
dvide2=dvide2.iloc[:2]
|
||||
spp=apeak(dvide2)
|
||||
return inde,e3,spp+['显示卖出']
|
||||
|
||||
|
||||
|
||||
|
||||
print('e3=',e3)
|
||||
print('inde=',inde)
|
||||
if inde>50:
|
||||
'''
|
||||
dvide1=dvide[dvide>1]
|
||||
|
||||
spp=apeak(dvide1)
|
||||
#print('spp=',spp)
|
||||
return inde,e3,spp+['显示买入']
|
||||
'''
|
||||
ppk=big_small(dvide,1)
|
||||
return ppk
|
||||
|
||||
|
||||
elif inde<50:
|
||||
'''
|
||||
dvide2=dvide[dvide<1]
|
||||
spp=apeak(dvide2)
|
||||
return inde,e3,spp+['显示卖出']
|
||||
'''
|
||||
ppk=big_small(dvide,0)
|
||||
return ppk
|
||||
|
||||
else:
|
||||
|
||||
all_columns=['rsi','monment','pl12','pl13','pl14','pl35','macd']
|
||||
chines=['RSI相对强弱指标','monment动量交易策略','近两月的房价趋势','近三月的房价趋势',
|
||||
|
||||
'近四月的房价趋势','近五月的房价趋势','macd异同移动平均线']
|
||||
bb1=pd.Series(chines,index=all_columns)
|
||||
|
||||
dvide2=dvide.sort_values(ascending=True)
|
||||
|
||||
klj=list(dvide2.index)
|
||||
mnn=klj[0]
|
||||
sell=[bb1[mnn]]+['显示卖出']
|
||||
|
||||
mmm=klj[-1]
|
||||
buy=[bb1[mmm]]+['显示买入']
|
||||
|
||||
|
||||
return inde,e3, buy+sell
|
||||
'''
|
||||
ppk=big_small(dvide,1)
|
||||
|
||||
ppk1=big_small(dvide,0)
|
||||
|
||||
kkl=[ppk[2][0]]+[ppk[2][-1]]+[ppk1[2][0]]+[ppk1[2][-1]]
|
||||
return inde,e3,kkl
|
||||
'''
|
||||
|
||||
|
||||
#return inde,e3
|
||||
|
||||
def region_index(ct,rl,date):
|
||||
query = {"city":ct,"region":rl,"date":date,"index":{"$gte":50}}
|
||||
up= potent.count(query)
|
||||
|
||||
query1 = {"city":ct,"region":rl,"date":date,"index":{"$lt":50}}
|
||||
|
||||
query2 = {"city":ct,"date":date,"region":rl}
|
||||
|
||||
ctall=potent.find(query2)
|
||||
c1=list(ctall)
|
||||
c2=pd.DataFrame(c1)
|
||||
d=c2.shape[0]
|
||||
|
||||
if d>200:
|
||||
|
||||
ll=['rsi','monment','pl12','pl13','pl14','pl35','macd']
|
||||
e=c2[ll]
|
||||
|
||||
e1=e.sum()
|
||||
e3=100*e1/d
|
||||
|
||||
down= potent.count(query1)
|
||||
mind=min(up,down)
|
||||
if mind>0:
|
||||
|
||||
inde=int(100*up/(up+down))
|
||||
return inde,e3
|
||||
|
||||
cit=city_list()
|
||||
db1 = client136.fangjia_stat
|
||||
stat_city = db1.city_potential
|
||||
|
||||
stat_region = db1.region_potential
|
||||
|
||||
avg=average_index()
|
||||
|
||||
for ct in cit[0:]:
|
||||
cde= city_index(ct,date,avg)
|
||||
|
||||
|
||||
if cde!=None:
|
||||
|
||||
cx=cde[0]
|
||||
cvb=cde[1]
|
||||
|
||||
#print(ct,cx,cvb,cde[2])
|
||||
print(ct,'index=',cx,cde[2])
|
||||
tt=datetime.datetime.now()
|
||||
#timm=datetime.datetime.now()
|
||||
"""
|
||||
stat_city.insert_one({"city":ct,
|
||||
"city_index":cde, "date":date,"time":tt})
|
||||
"""
|
||||
|
||||
stat_city.replace_one(
|
||||
|
||||
{"city":ct,"date":date},
|
||||
|
||||
{"city":ct,"city_index":cx,"speak":cde[2],
|
||||
|
||||
|
||||
"rsi":int(cvb['rsi']),'monment':int(cvb['monment']),'pl12':int(cvb['pl12']),'pl14':int(cvb['pl14']),
|
||||
'pl13':int(cvb['pl13']),'pl35':int(cvb['pl35']),'macd':int(cvb['macd']),
|
||||
|
||||
"date":date,"time":tt},True
|
||||
)
|
||||
|
||||
|
||||
region=region_list(ct)#[ppr:ppr+1]
|
||||
|
||||
for rl in region:
|
||||
red=region_index(ct,rl,date)
|
||||
|
||||
#print(ct,rl,red)
|
||||
if red!=None:
|
||||
rx=red[0]
|
||||
rvb=red[1]
|
||||
tt=datetime.datetime.now()
|
||||
#print(ct,rl,rx,date)
|
||||
"""
|
||||
stat_region.insert_one({"city":ct,"region":rl,
|
||||
"region_index":red, "date":date,"time":tt})
|
||||
"""
|
||||
|
||||
stat_region.replace_one(
|
||||
|
||||
{"city":ct,"region":rl,"date":date},
|
||||
|
||||
{"city":ct,"region":rl,
|
||||
|
||||
"rsi":int(rvb['rsi']),'monment':int(rvb['monment']),'pl12':int(rvb['pl12']),'pl14':int(rvb['pl14']),
|
||||
'pl13':int(rvb['pl13']),'pl35':int(rvb['pl35']),'macd':int(rvb['macd']),
|
||||
|
||||
"region_index":rx, "date":date,"time":tt},True
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,477 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Dec 18 15:45:01 2017
|
||||
|
||||
@author: Administrator
|
||||
"""
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Thu Dec 14 15:26:31 2017
|
||||
|
||||
@author: Administrator
|
||||
"""
|
||||
|
||||
import pymongo
|
||||
import pandas
|
||||
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pylab as pl
|
||||
import datetime
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
|
||||
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
|
||||
|
||||
from matplotlib.pylab import date2num
|
||||
import potential
|
||||
|
||||
import talib
|
||||
|
||||
now_time = datetime.datetime.now()
|
||||
future_time = now_time - datetime.timedelta(days=1000)
|
||||
|
||||
|
||||
def some_district_information(ct,rl,lk):
|
||||
|
||||
|
||||
tt=future_time
|
||||
|
||||
|
||||
|
||||
query = {"city":ct,"region":rl,"district_name":lk,'weekend':{"$gt":tt}}
|
||||
fields1 = {"city":1,"region":1, "district_name":1,
|
||||
'calc_value':1,'calc_value':1,'weekend':1}
|
||||
|
||||
return potential.findHouse(query,fields1)
|
||||
|
||||
|
||||
|
||||
def potential_index(dd1):
|
||||
|
||||
dd2=dd1[['calc_value','weekend']]
|
||||
d=[int(i.strftime('%Y%m%d')) for i in dd2['weekend'] ]
|
||||
|
||||
dtime=[(i.strftime('%Y-%m-%d')) for i in dd2['weekend'] ]
|
||||
|
||||
dd2['ymd']=d
|
||||
|
||||
dd2['ym']=dd2['ymd']//100
|
||||
dd2['dtime']=dtime
|
||||
|
||||
d1=list(set(dd2['ym']))
|
||||
|
||||
d1.sort()
|
||||
|
||||
|
||||
def candle(p,i):
|
||||
|
||||
sh=p.shape[0]
|
||||
if sh>0:
|
||||
dic = [p.iloc[0]['calc_value'],
|
||||
p['calc_value'].max(),
|
||||
p.iloc[-1]['calc_value'],
|
||||
p['calc_value'].min(),
|
||||
p.iloc[0]['dtime']]
|
||||
return dic
|
||||
|
||||
|
||||
def normalize(d1,dd2):
|
||||
|
||||
ss=[]
|
||||
for i in d1:
|
||||
#print('i=',i)
|
||||
p=dd2[dd2['ym']==i]
|
||||
p1=p.sort_values(by='ymd',ascending=True)
|
||||
ss.append(candle(p1,i))
|
||||
|
||||
dcc=pandas.DataFrame(ss,columns=['open','high','close','low','date'])
|
||||
dcc.index=dcc['date']
|
||||
return dcc
|
||||
|
||||
|
||||
dcc=normalize(d1,dd2)
|
||||
|
||||
df=dcc
|
||||
|
||||
|
||||
"""
|
||||
#以下代码为绘制某小区的k线图
|
||||
hist_data=df
|
||||
# 创建子图
|
||||
fig = plt.figure(figsize=(10, 6))
|
||||
#ax1 = plt.subplot2grid((10,4),(0,0),rowspan=5,colspan=4)
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
fig.subplots_adjust(bottom=0.2)
|
||||
# 设置X轴刻度为日期时间
|
||||
ax.xaxis_date()
|
||||
plt.xticks(rotation=45)
|
||||
plt.yticks()
|
||||
plt.title(lk)
|
||||
plt.xlabel("时间")
|
||||
plt.ylabel("股价(元)")
|
||||
#candlestick_ohlc(ax,data_list,width=1.5,colorup='r',colordown='g')
|
||||
plt.grid()
|
||||
|
||||
hist_data=df .sort_index()
|
||||
# 对tushare获取到的数据转换成candlestick_ohlc()方法可读取的格式
|
||||
data_list = []
|
||||
for dates,row in hist_data.iterrows():
|
||||
# 将时间转换为数字
|
||||
date_time = datetime.datetime.strptime(dates,'%Y-%m-%d')
|
||||
t = date2num(date_time)
|
||||
open,high,close,low = row[:4]
|
||||
datas = (t,open,high,low,close)
|
||||
data_list.append(datas)
|
||||
|
||||
|
||||
|
||||
mondays = WeekdayLocator(MONDAY) # 主要刻度
|
||||
alldays = DayLocator() # 次要刻度
|
||||
#weekFormatter = DateFormatter('%b %d') # 如:Jan 12
|
||||
mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015
|
||||
dayFormatter = DateFormatter('%d') # 如:12
|
||||
plt.rcParams['font.sans-serif'] = ['SimHei']
|
||||
plt.rcParams['axes.unicode_minus'] = False
|
||||
|
||||
candlestick_ohlc(ax,data_list,width=5,colorup='r',colordown='g')
|
||||
|
||||
"""
|
||||
|
||||
|
||||
|
||||
df=dcc.drop(['date'],axis=1)*1.0
|
||||
|
||||
df.index=list(range(df.shape[0]))
|
||||
|
||||
#df[df['volume']==0]=np.nan
|
||||
|
||||
#print('df=',df)
|
||||
|
||||
"""
|
||||
def myMACD(price, fastperiod=12, slowperiod=26, signalperiod=9):
|
||||
ewma12 = pd.ewma(price,span=fastperiod)
|
||||
ewma60 = pd.ewma(price,span=slowperiod)
|
||||
dif = ewma12-ewma60
|
||||
dea = pd.ewma(dif,span=signalperiod)
|
||||
bar = (dif-dea) #有些地方的bar = (dif-dea)*2,但是talib中MACD的计算是bar = (dif-dea)*1
|
||||
return dif,dea,bar
|
||||
"""
|
||||
|
||||
#print(df['close'].values)
|
||||
|
||||
macd, signal, hist = talib.MACD(df['close'].values, fastperiod=6, slowperiod=12, signalperiod=9)
|
||||
|
||||
"""
|
||||
#mydif,mydea,mybar = myMACD(df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)
|
||||
|
||||
fig = plt.figure(figsize=[10,5])
|
||||
plt.plot(df.index,macd,label='macd dif')
|
||||
plt.plot(df.index,signal,label='signal dea')
|
||||
plt.plot(df.index,hist,label='hist bar')
|
||||
#plt.plot(df.index,mydea,label='my dea')
|
||||
#plt.plot(df.index,mybar,label='my bar')
|
||||
plt.legend(loc='best')
|
||||
"""
|
||||
close = [float(x) for x in df['close']]
|
||||
|
||||
def macscore( hist):
|
||||
|
||||
span=len(macd)-1
|
||||
|
||||
h1=hist[span]
|
||||
if h1>0:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
|
||||
def RSI(df):
|
||||
|
||||
df['RSI']=talib.RSI(np.array(close), timeperiod=12)
|
||||
aa=list(df['RSI'])
|
||||
|
||||
b=aa[::-1]
|
||||
#print(b)
|
||||
if b[0]>50:
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
def monment(df):
|
||||
df['MOM']=talib.MOM(np.array(close), timeperiod=5)
|
||||
aa=list(df['MOM'])
|
||||
b=aa[::-1]
|
||||
if b[0]>0:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
def polyfit(close,k,pl):
|
||||
|
||||
near_six=close[len(close)-pl:len(close)]
|
||||
xlist=list(range(pl))
|
||||
bbz1 = np.polyfit(xlist, near_six,k)
|
||||
# 生成多项式对象{
|
||||
bbp1 = np.poly1d(bbz1)
|
||||
f5=bbp1(pl-1)
|
||||
f6=bbp1(pl)
|
||||
if f6>f5:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
all_index=[RSI(df),monment(df),polyfit(close,1,2),polyfit(close,1,3),polyfit(close,1,4),polyfit(close,3,5),macscore( hist)]
|
||||
all_weight=[2,2,3,2,1,2,2]
|
||||
|
||||
score_mul=(np.array(all_index)).T@np.array(all_weight)
|
||||
|
||||
vv1=int(100*score_mul/14)
|
||||
|
||||
print('score_mul=',vv1)
|
||||
#score=2*RSI(df)+2*monment(df)+3*polyfit(close,1,2)+2*polyfit(close,1,3)+1*polyfit(close,1,4)+2*polyfit(close,3,5)+2*macscore( hist)
|
||||
|
||||
#poindex=score/14
|
||||
#vv=int(poindex*100)
|
||||
all_columns=['RSI','monment','pl12','pl13','pl14','pl35','macd']
|
||||
bb=pd.Series(all_index,index=all_columns)
|
||||
|
||||
|
||||
chines=['RSI相对强弱指标','monment动量交易策略','近两月的房价趋势','近三月的房价趋势',
|
||||
|
||||
'近四月的房价趋势','近五月的房价趋势','macd异同移动平均线']
|
||||
bb1=pd.Series(all_index,index=chines)
|
||||
def turn_number_char(bb1):
|
||||
jjj=[]
|
||||
|
||||
for n in bb1.index:
|
||||
if bb1[n]==1:
|
||||
jjj.append(n+'显示买入')
|
||||
else:
|
||||
jjj.append(n+'显示卖出')
|
||||
return jjj
|
||||
|
||||
|
||||
|
||||
conclusion=turn_number_char(bb1)
|
||||
|
||||
|
||||
chines=['RSI相对强弱指标','monment动量交易策略','近两月的房价趋势','近三月的房价趋势',
|
||||
|
||||
'近四月的房价趋势','近五月的房价趋势','macd异同移动平均线']
|
||||
|
||||
aaa=[all_index,chines,conclusion]
|
||||
|
||||
boo=pd.DataFrame(aaa,columns=all_columns)
|
||||
|
||||
|
||||
|
||||
def speak(vv1,bb,boo):
|
||||
bset=set(bb.index)
|
||||
cset=set(['pl12','pl13','pl14','pl35'] )
|
||||
gg=['RSI','monment','macd']
|
||||
|
||||
def strr(bb1,bset,cset,gg):
|
||||
aset=set(bb1.index)
|
||||
dset=aset & cset
|
||||
|
||||
dlist=list(dset)
|
||||
bo1=boo[dlist]
|
||||
|
||||
def takepl(bo1):
|
||||
|
||||
for i in ['pl35','pl14','pl13','pl12']:
|
||||
if i in dlist:
|
||||
return boo[i][2]
|
||||
a11=takepl(bo1)
|
||||
gg=['RSI','monment','macd']
|
||||
gset=set(gg)&set(bb1.index)
|
||||
gglist=list(gset)
|
||||
boo2=boo[gglist]
|
||||
|
||||
b11=list(boo2.values[2])
|
||||
ba11=b11+[a11]
|
||||
#print(ba11)
|
||||
return ba11
|
||||
|
||||
if vv1>50:
|
||||
bb1=bb[bb>0]
|
||||
return strr(bb1,bset,cset,gg)
|
||||
|
||||
elif vv1<50:
|
||||
bb2=bb[bb==0]
|
||||
return strr(bb2,bset,cset,gg)
|
||||
else:
|
||||
ff1=bb[bb==1]
|
||||
ff2=bb[bb==0]
|
||||
buy=strr(ff1,bset,cset,gg)
|
||||
sell=strr(ff2,bset,cset,gg)
|
||||
return buy+sell
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ddd=speak(vv1,bb,boo)
|
||||
|
||||
print('ddd=',ddd)
|
||||
|
||||
return vv1,bb,ddd
|
||||
|
||||
|
||||
def city_list():
|
||||
|
||||
city = db136.server_area
|
||||
#seawater.find_one()
|
||||
|
||||
pos= city.find({"display":{"$ne":"全国"}},
|
||||
{"display":1,"city_grade":1,"_id":1}).sort([("city_grade",1),("_id",1)])
|
||||
data=pandas.DataFrame(list(pos))
|
||||
#print(data)
|
||||
return data['display']
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def region_list(city):
|
||||
#client = pymongo.MongoClient('192.168.0.136',27017)
|
||||
#db = client.fangjia
|
||||
region = db136.region_block
|
||||
#seawater.find_one()
|
||||
query = {"city":city,"category":"region"}
|
||||
fields1 = {"name":1}
|
||||
pos = list(region.find(query,fields1))
|
||||
|
||||
|
||||
data=pandas.DataFrame(pos)
|
||||
return data['name']
|
||||
|
||||
def region_district_list(ct,rl):
|
||||
#import pandas
|
||||
#client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
#db1 = client1.fangjia
|
||||
#seaweed1 = db1.seaweed
|
||||
|
||||
query1 = {"status":0,"cat":"district","city":ct,"region":rl}
|
||||
fields1 = {"lat2":1,"lng2":1, "city":1,"region":1,
|
||||
"cat":1,"name":1,'estate_type2':1,'alias':1}
|
||||
|
||||
lf=potential.find_seaweed(query1,fields1)
|
||||
return lf
|
||||
|
||||
|
||||
#########################################################
|
||||
#########################################################
|
||||
#########################################################
|
||||
|
||||
ip='mongo.db.fangjia.com'
|
||||
client136 = pymongo.MongoClient(ip,27017)
|
||||
db136 = client136.fangjia
|
||||
cit=city_list()
|
||||
|
||||
last_month = now_time - datetime.timedelta(days=25)
|
||||
date=int(last_month.strftime('%Y%m'))
|
||||
|
||||
#timm=datetime.datetime.now()
|
||||
db1 = client136.fangjia_stat
|
||||
stat = db1.potential_index
|
||||
#########################################################
|
||||
#########################################################
|
||||
#########################################################
|
||||
|
||||
|
||||
|
||||
for ct in cit[0:]:
|
||||
|
||||
|
||||
|
||||
region=region_list(ct)
|
||||
|
||||
for rl in region:
|
||||
|
||||
lf=region_district_list(ct,rl)
|
||||
|
||||
|
||||
|
||||
if lf.shape[0]>1:
|
||||
|
||||
|
||||
for lk in lf['name']:
|
||||
|
||||
dd1=some_district_information(ct,rl,lk)
|
||||
|
||||
#print(dd1)
|
||||
|
||||
|
||||
if dd1.shape[0]>5:
|
||||
|
||||
|
||||
a=list(dd1['calc_value'].dropna())
|
||||
|
||||
if len(a)>30:
|
||||
|
||||
mm=potential_index(dd1)
|
||||
#print(ct,rl,lk,mm)
|
||||
timm=datetime.datetime.now()
|
||||
|
||||
aina=mm[1]
|
||||
#print(aina,'ooooooooooooo')
|
||||
|
||||
dindex=int(mm[0])
|
||||
print('mm[0]=',dindex)
|
||||
|
||||
|
||||
try:
|
||||
stat.replace_one(
|
||||
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,"date":date},
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,'apeak':mm[2],
|
||||
|
||||
"rsi":int(aina['RSI']),'monment':int(aina['monment']),'pl12':int(aina['pl12']),'pl14':int(aina['pl14']),
|
||||
'pl13':int(aina['pl13']),'pl35':int(aina['pl35']),'macd':int(aina['macd']),
|
||||
"index":dindex, "date":date,"time":timm},True
|
||||
)
|
||||
print(ct,rl,lk,mm,date)
|
||||
|
||||
|
||||
except:
|
||||
|
||||
stat.replace_one(
|
||||
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,"date":date},
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,'apeak':mm[2],
|
||||
|
||||
"rsi":int(aina['RSI']),'monment':int(aina['monment']),'pl12':int(aina['pl12']),'pl14':int(aina['pl14']),
|
||||
'pl13':int(aina['pl13']),'pl35':int(aina['pl35']),'macd':int(aina['macd']),
|
||||
"index":dindex, "date":date,"time":timm},True
|
||||
)
|
||||
print(ct,rl,lk,mm,date)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
from potential import mongoClients as mongoClients
|
||||
import time
|
||||
import pandas
|
||||
|
||||
def get_collection(db, name, host="192.168.0.136", port=27017):
|
||||
return mongoClients.MongoClients(db, name, host, port).collection
|
||||
|
||||
db136="mongo.db.fangjia.com"
|
||||
|
||||
db222='222.73.196.152'
|
||||
|
||||
|
||||
def findHouse(query, fields=None):
|
||||
|
||||
seawater105 = get_collection("fangjia", "district_stat", db136, 27017)
|
||||
for err_count in range(0, 10):
|
||||
|
||||
try:
|
||||
|
||||
lt= seawater105.count(query)
|
||||
pos = list(seawater105.find(query).limit(lt))
|
||||
data=pandas.DataFrame(pos)
|
||||
del pos
|
||||
return data
|
||||
|
||||
except :
|
||||
|
||||
#err_count = err_count + 1
|
||||
lt= seawater105.count(query)
|
||||
pos = list(seawater105.find(query).limit(lt))
|
||||
data=pandas.DataFrame(pos)
|
||||
del pos
|
||||
return data
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def find_seaweed(query, fields=None):
|
||||
|
||||
#dirlt=p[0]
|
||||
|
||||
#client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
#db1 = client1.fangjia
|
||||
#seaweed1 = db1.seaweed
|
||||
|
||||
seawater105 = get_collection("fangjia", "seaweed", "192.168.0.136", 27017)
|
||||
for err_count in range(0, 10):
|
||||
|
||||
try:
|
||||
|
||||
lt= seawater105.count(query)
|
||||
|
||||
pos = list(seawater105.find(query).limit(lt))
|
||||
data=pandas.DataFrame(pos)
|
||||
del pos
|
||||
return data
|
||||
|
||||
except :
|
||||
|
||||
#err_count = err_count + 1
|
||||
lt= seawater105.count(query)
|
||||
pos = list(seawater105.find(query).limit(lt))
|
||||
data=pandas.DataFrame(pos)
|
||||
del pos
|
||||
return data
|
||||
|
||||
raise Exception("yang ben cha xun shi bai1")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Wed Nov 15 22:45:06 2017
|
||||
|
||||
@author: wenyan
|
||||
"""
|
||||
|
||||
from pymongo import MongoClient
|
||||
|
||||
|
||||
class MongoClients:
|
||||
|
||||
cache = {}
|
||||
|
||||
def __init__(self, db, name, host="127.0.0.0", port=27017):
|
||||
|
||||
key = db + "_" + name + "_" + host + "_" + str(port)
|
||||
if key in MongoClients.cache.keys():
|
||||
self.collection = MongoClients.cache[key]
|
||||
else:
|
||||
print("init " + key)
|
||||
collection = MongoClient(host, port).get_database(db).get_collection(name)
|
||||
MongoClients.cache[key] = collection
|
||||
self.collection = MongoClients.cache[key]
|
||||
"""
|
||||
def find(self, *args, **kwargs):
|
||||
return self.collection.find(*args, **kwargs)
|
||||
|
||||
def find2(self, query, fields, call):
|
||||
|
||||
for item in self.collection.find(query):
|
||||
call(item)
|
||||
"""
|
||||
@@ -0,0 +1,385 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Dec 18 15:45:01 2017
|
||||
|
||||
@author: Administrator
|
||||
"""
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Thu Dec 14 15:26:31 2017
|
||||
|
||||
@author: Administrator
|
||||
"""
|
||||
|
||||
import pymongo
|
||||
import pandas
|
||||
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pylab as pl
|
||||
import datetime
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
|
||||
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
|
||||
|
||||
from matplotlib.pylab import date2num
|
||||
import potential
|
||||
|
||||
import talib
|
||||
|
||||
now_time = datetime.datetime.now()
|
||||
future_time = now_time - datetime.timedelta(days=1000)
|
||||
|
||||
|
||||
def some_district_information(ct,rl,lk):
|
||||
|
||||
|
||||
tt=future_time
|
||||
|
||||
|
||||
|
||||
query = {"city":ct,"region":rl,"district_name":lk,'weekend':{"$gt":tt}}
|
||||
fields1 = {"city":1,"region":1, "district_name":1,
|
||||
'calc_value':1,'calc_value':1,'weekend':1}
|
||||
|
||||
return potential.findHouse(query,fields1)
|
||||
|
||||
|
||||
|
||||
def potential_index(dd1):
|
||||
|
||||
dd2=dd1[['calc_value','weekend']]
|
||||
d=[int(i.strftime('%Y%m%d')) for i in dd2['weekend'] ]
|
||||
|
||||
dtime=[(i.strftime('%Y-%m-%d')) for i in dd2['weekend'] ]
|
||||
|
||||
dd2['ymd']=d
|
||||
|
||||
dd2['ym']=dd2['ymd']//100
|
||||
dd2['dtime']=dtime
|
||||
|
||||
d1=list(set(dd2['ym']))
|
||||
|
||||
d1.sort()
|
||||
|
||||
|
||||
def candle(p,i):
|
||||
|
||||
sh=p.shape[0]
|
||||
if sh>0:
|
||||
dic = [p.iloc[0]['calc_value'],
|
||||
p['calc_value'].max(),
|
||||
p.iloc[-1]['calc_value'],
|
||||
p['calc_value'].min(),
|
||||
p.iloc[0]['dtime']]
|
||||
return dic
|
||||
|
||||
|
||||
def normalize(d1,dd2):
|
||||
|
||||
ss=[]
|
||||
for i in d1:
|
||||
#print('i=',i)
|
||||
p=dd2[dd2['ym']==i]
|
||||
p1=p.sort_values(by='ymd',ascending=True)
|
||||
ss.append(candle(p1,i))
|
||||
|
||||
dcc=pandas.DataFrame(ss,columns=['open','high','close','low','date'])
|
||||
dcc.index=dcc['date']
|
||||
return dcc
|
||||
|
||||
|
||||
dcc=normalize(d1,dd2)
|
||||
|
||||
df=dcc
|
||||
|
||||
|
||||
"""
|
||||
#以下代码为绘制某小区的k线图
|
||||
hist_data=df
|
||||
# 创建子图
|
||||
fig = plt.figure(figsize=(10, 6))
|
||||
#ax1 = plt.subplot2grid((10,4),(0,0),rowspan=5,colspan=4)
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
fig.subplots_adjust(bottom=0.2)
|
||||
# 设置X轴刻度为日期时间
|
||||
ax.xaxis_date()
|
||||
plt.xticks(rotation=45)
|
||||
plt.yticks()
|
||||
plt.title(lk)
|
||||
plt.xlabel("时间")
|
||||
plt.ylabel("股价(元)")
|
||||
#candlestick_ohlc(ax,data_list,width=1.5,colorup='r',colordown='g')
|
||||
plt.grid()
|
||||
|
||||
hist_data=df .sort_index()
|
||||
# 对tushare获取到的数据转换成candlestick_ohlc()方法可读取的格式
|
||||
data_list = []
|
||||
for dates,row in hist_data.iterrows():
|
||||
# 将时间转换为数字
|
||||
date_time = datetime.datetime.strptime(dates,'%Y-%m-%d')
|
||||
t = date2num(date_time)
|
||||
open,high,close,low = row[:4]
|
||||
datas = (t,open,high,low,close)
|
||||
data_list.append(datas)
|
||||
|
||||
|
||||
|
||||
mondays = WeekdayLocator(MONDAY) # 主要刻度
|
||||
alldays = DayLocator() # 次要刻度
|
||||
#weekFormatter = DateFormatter('%b %d') # 如:Jan 12
|
||||
mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015
|
||||
dayFormatter = DateFormatter('%d') # 如:12
|
||||
plt.rcParams['font.sans-serif'] = ['SimHei']
|
||||
plt.rcParams['axes.unicode_minus'] = False
|
||||
|
||||
candlestick_ohlc(ax,data_list,width=5,colorup='r',colordown='g')
|
||||
|
||||
"""
|
||||
|
||||
|
||||
|
||||
df=dcc.drop(['date'],axis=1)*1.0
|
||||
|
||||
df.index=list(range(df.shape[0]))
|
||||
|
||||
#df[df['volume']==0]=np.nan
|
||||
|
||||
#print('df=',df)
|
||||
|
||||
"""
|
||||
def myMACD(price, fastperiod=12, slowperiod=26, signalperiod=9):
|
||||
ewma12 = pd.ewma(price,span=fastperiod)
|
||||
ewma60 = pd.ewma(price,span=slowperiod)
|
||||
dif = ewma12-ewma60
|
||||
dea = pd.ewma(dif,span=signalperiod)
|
||||
bar = (dif-dea) #有些地方的bar = (dif-dea)*2,但是talib中MACD的计算是bar = (dif-dea)*1
|
||||
return dif,dea,bar
|
||||
"""
|
||||
|
||||
#print(df['close'].values)
|
||||
|
||||
macd, signal, hist = talib.MACD(df['close'].values, fastperiod=6, slowperiod=12, signalperiod=9)
|
||||
|
||||
"""
|
||||
#mydif,mydea,mybar = myMACD(df['close'].values, fastperiod=12, slowperiod=26, signalperiod=9)
|
||||
|
||||
fig = plt.figure(figsize=[10,5])
|
||||
plt.plot(df.index,macd,label='macd dif')
|
||||
plt.plot(df.index,signal,label='signal dea')
|
||||
plt.plot(df.index,hist,label='hist bar')
|
||||
#plt.plot(df.index,mydea,label='my dea')
|
||||
#plt.plot(df.index,mybar,label='my bar')
|
||||
plt.legend(loc='best')
|
||||
"""
|
||||
close = [float(x) for x in df['close']]
|
||||
|
||||
def macscore( hist):
|
||||
|
||||
span=len(macd)-1
|
||||
|
||||
h1=hist[span]
|
||||
if h1>0:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
|
||||
def RSI(df):
|
||||
|
||||
df['RSI']=talib.RSI(np.array(close), timeperiod=12)
|
||||
aa=list(df['RSI'])
|
||||
|
||||
b=aa[::-1]
|
||||
#print(b)
|
||||
if b[0]>50:
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
def monment(df):
|
||||
df['MOM']=talib.MOM(np.array(close), timeperiod=5)
|
||||
aa=list(df['MOM'])
|
||||
b=aa[::-1]
|
||||
if b[0]>0:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
def polyfit(close,k,pl):
|
||||
|
||||
near_six=close[len(close)-pl:len(close)]
|
||||
xlist=list(range(pl))
|
||||
bbz1 = np.polyfit(xlist, near_six,k)
|
||||
# 生成多项式对象{
|
||||
bbp1 = np.poly1d(bbz1)
|
||||
f5=bbp1(pl-1)
|
||||
f6=bbp1(pl)
|
||||
if f6>f5:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
score=2*RSI(df)+2*monment(df)+3*polyfit(close,1,2)+2*polyfit(close,1,3)+1*polyfit(close,1,4)+2*polyfit(close,3,5)+2*macscore( hist)
|
||||
|
||||
poindex=score/14
|
||||
vv=int(poindex*100)
|
||||
return vv
|
||||
|
||||
|
||||
def city_list():
|
||||
|
||||
city = db136.server_area
|
||||
#seawater.find_one()
|
||||
|
||||
pos= city.find({"display":{"$ne":"全国"}},
|
||||
{"display":1,"city_grade":1,"_id":1}).sort([("city_grade",1),("_id",1)])
|
||||
data=pandas.DataFrame(list(pos))
|
||||
#print(data)
|
||||
return data['display']
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def region_list(city):
|
||||
#client = pymongo.MongoClient('192.168.0.136',27017)
|
||||
#db = client.fangjia
|
||||
region = db136.region_block
|
||||
#seawater.find_one()
|
||||
query = {"city":city,"category":"region"}
|
||||
fields1 = {"name":1}
|
||||
pos = list(region.find(query,fields1))
|
||||
|
||||
|
||||
data=pandas.DataFrame(pos)
|
||||
return data['name']
|
||||
|
||||
def region_district_list(ct,rl):
|
||||
#import pandas
|
||||
#client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
#db1 = client1.fangjia
|
||||
#seaweed1 = db1.seaweed
|
||||
|
||||
query1 = {"status":0,"cat":"district","city":ct,"region":rl}
|
||||
fields1 = {"lat2":1,"lng2":1, "city":1,"region":1,
|
||||
"cat":1,"name":1,'estate_type2':1,'alias':1}
|
||||
|
||||
lf=potential.find_seaweed(query1,fields1)
|
||||
return lf
|
||||
|
||||
|
||||
#########################################################
|
||||
#########################################################
|
||||
#########################################################
|
||||
|
||||
ip='mongo.db.fangjia.com'
|
||||
client136 = pymongo.MongoClient(ip,27017)
|
||||
db136 = client136.fangjia
|
||||
cit=city_list()
|
||||
|
||||
|
||||
last_month = now_time - datetime.timedelta(days=15)
|
||||
date=int(last_month.strftime('%Y%m'))
|
||||
|
||||
#timm=datetime.datetime.now()
|
||||
db1 = client136.fangjia_stat
|
||||
stat = db1.potential_index_test
|
||||
#########################################################
|
||||
#########################################################
|
||||
#########################################################
|
||||
|
||||
|
||||
|
||||
for ct in cit[0:]:
|
||||
|
||||
|
||||
|
||||
region=region_list(ct)
|
||||
|
||||
for rl in region:
|
||||
|
||||
lf=region_district_list(ct,rl)
|
||||
|
||||
|
||||
|
||||
if lf.shape[0]>1:
|
||||
|
||||
|
||||
for lk in lf['name']:
|
||||
|
||||
dd1=some_district_information(ct,rl,lk)
|
||||
|
||||
#print(dd1)
|
||||
|
||||
|
||||
if dd1.shape[0]>5:
|
||||
|
||||
|
||||
a=list(dd1['calc_value'].dropna())
|
||||
|
||||
if len(a)>30:
|
||||
|
||||
mm=potential_index(dd1)
|
||||
#print(ct,rl,lk,mm)
|
||||
timm=datetime.datetime.now()
|
||||
|
||||
try:
|
||||
"""
|
||||
#date=201712
|
||||
timm=datetime.datetime.now()
|
||||
client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
db1 = client1.luogan
|
||||
stat = db1.potential_index
|
||||
|
||||
stat.insert_one({"city":ct,"region":rl,"district_name":lk,
|
||||
"index":mm, "date":date,"time":timm})
|
||||
"""
|
||||
stat.replace_one(
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,"date":date},
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,
|
||||
"index":mm, "date":date,"time":timm},True
|
||||
)
|
||||
print(ct,rl,lk,mm,date)
|
||||
except:
|
||||
"""
|
||||
timm=datetime.datetime.now()
|
||||
client1 = pymongo.MongoClient('192.168.0.136',27017)
|
||||
db1 = client1.luogan
|
||||
stat = db1.potential_index
|
||||
|
||||
stat.insert_one({"city":ct,"region":rl,"district_name":lk,
|
||||
"index":mm, "date":date,"time":timm})
|
||||
"""
|
||||
|
||||
stat.replace_one(
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,"date":date},
|
||||
|
||||
{"city":ct,"region":rl,"district_name":lk,
|
||||
"index":mm, "date":date,"time":timm},True
|
||||
)
|
||||
print(ct,rl,lk,mm)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user