"""
Created on Sat May 5 12:43:52 2018
@author: luogan
"""
"""
Created on Thu Dec 14 15:26:31 2017
@author: 量化之王
"""
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 talib
from dateutil.parser import parse
import tushare as ts
client1 = pymongo.MongoClient('192.168.10.182',27017)
db1 = client1.stock.low_close
def before_month_lastday(ti,k):
from dateutil.parser import parse
today=parse(str(ti))
lastMonth = today - datetime.timedelta(days=k)
def plus(k):
if k<10:
return '0'+str(k)
else:
return str(k)
y=lastMonth.year
m=lastMonth.month
d=lastMonth.day
cc=str(y)+plus(m)+plus(d)
return int(cc)
def polyfit(c,k):
xlist=list(range(len(c)))
bbz1 = np.polyfit(xlist, c,k)
return bbz1[0]
def potential_index(tl):
df=ts.get_hist_data(tl[0],start=tl[1],end=tl[2])
if str(type(df))!="<class 'NoneType'>":
if df.shape[0]>250:
date=df.index
date1=list(map(parse,date))
df['date']=date1
df=df.sort_values(by='date')
closed=df['close'].values
opend=df['open'].values
low=df['low'].values
p=closed[-1]
o=opend[-1]
opp=min(p,o)
n=low[-1]
ra=(opp-n)/opp
if ra>=0.05:
print('ra=',ra)
print('name',tl[0])
tt=before_month_lastday(tl[2],0)
db1.replace_one(
{"name":tl[0],"date":tt},
{ "name":tl[0],"date":tt,'ratio':round(ra,2)
},True
)
print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
ak=ts.get_stock_basics()
code=list(ak.index)
def front_step_time(day):
now = datetime.datetime.now()
front = now - datetime.timedelta(days=day)
d1 = front.strftime('%Y-%m-%d')
return d1
now=front_step_time(0)
bf=front_step_time(720)
sheet=pd.DataFrame()
sheet['code']=code
sheet['bf']=bf
sheet['sta']=now
import time
from multiprocessing import Pool
import numpy as np
te =sheet.values
'''
for name in te:
mm=potential_index(name)
#print(name,mm)
'''
if __name__ == "__main__" :
startTime = time.time()
testFL =sheet.values
pool = Pool(20)
pool.map(potential_index,testFL)
pool.close()
pool.join()
endTime = time.time()
print ("time :", endTime - startTime)
{
"_id" : ObjectId("5aed3d531815c4ffea37f824"),
"name" : "300519",
"date" : NumberInt(20180505),
"ratio" : 0.06
}
{
"_id" : ObjectId("5aed3d571815c4ffea37f835"),
"name" : "002806",
"date" : NumberInt(20180505),
"ratio" : 0.06
}
{
"_id" : ObjectId("5aed3d5e1815c4ffea37f850"),
"name" : "600753",
"date" : NumberInt(20180505),
"ratio" : 0.08
}
{
"_id" : ObjectId("5aed3d5e1815c4ffea37f854"),
"name" : "002047",
"date" : NumberInt(20180505),
"ratio" : 0.06
}
{
"_id" : ObjectId("5aed3d621815c4ffea37f865"),
"name" : "002726",
"date" : NumberInt(20180505),
"ratio" : 0.08
}
{
"_id" : ObjectId("5aed3d661815c4ffea37f878"),
"name" : "002592",
"date" : NumberInt(20180505),
"ratio" : 0.08
}
{
"_id" : ObjectId("5aed3d661815c4ffea37f87a"),
"name" : "002584",
"date" : NumberInt(20180505),
"ratio" : 0.1
}
{
"_id" : ObjectId("5aed3d671815c4ffea37f87e"),
"name" : "002161",
"date" : NumberInt(20180505),
"ratio" : 0.06
}
{
"_id" : ObjectId("5aed3d801815c4ffea37f8e1"),
"name" : "002691",
"date" : NumberInt(20180505),
"ratio" : 0.09
}