Python量化(八)下影线选股法
生活随笔
收集整理的這篇文章主要介紹了
Python量化(八)下影线选股法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 5 12:43:52 2018@author: luogan
"""# -*- coding: utf-8 -*-
"""
Created on Thu Dec 14 15:26:31 2017@author: 量化之王
"""import pymongo
import pandasimport 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_ohlcfrom matplotlib.pylab import date2numimport talib
from dateutil.parser import parse
import tushare as tsclient1 = pymongo.MongoClient('192.168.10.182',27017)
db1 = client1.stock.low_closedef before_month_lastday(ti,k):from dateutil.parser import parsetoday=parse(str(ti))#first = datetime.date(day=1, month=today.month, year=today.year)lastMonth = today - datetime.timedelta(days=k)def plus(k):if k<10:return '0'+str(k)else:return str(k)y=lastMonth.yearm=lastMonth.monthd=lastMonth.day#day=calendar.monthrange(y,m)[1]cc=str(y)+plus(m)+plus(d)#bb=parse(cc)#pacific = pytz.timezone('Asia/Shanghai')#return pacific.localize(bb) return int(cc) def polyfit(c,k):#print(close)xlist=list(range(len(c)))bbz1 = np.polyfit(xlist, c,k)# 生成多項式對象{#bbp1 = np.poly1d(bbz1)#f5=bbp1(pl-1)#f6=bbp1(pl)return bbz1[0]def potential_index(tl):#df=ts.get_hist_data(name,start=bf,end=now)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.indexdate1=list(map(parse,date))df['date']=date1df=df.sort_values(by='date')#print('df=',df)#df=ts.get_k_data('002230',start='2015-01-12',end='2018-04-30')#提取收盤價closed=df['close'].valuesopend=df['open'].valueslow=df['low'].values#獲取均線的數據,通過timeperiod參數來分別獲取 5,10,20 日均線的數據。#ma5=talib.SMA(closed,timeperiod=30)#ma10=talib.SMA(closed,timeperiod=60)#ma250=talib.SMA(closed,timeperiod=250)p=closed[-1]o=opend[-1]opp=min(p,o)n=low[-1]#print('p=',p)#print('n=',n)ra=(opp-n)/opp#print('kk=',kk)#print('ra=',ra)if ra>=0.05:#print('kk=',kk)print('ra=',ra)print('name',tl[0])#db1.insert_one({'name':tl[0],'ratio':ra})#db1.save({'name':tl[0]})tt=before_month_lastday(tl[2],0)#db1.save({'name':tl[0],'potential':vv})#return vv*1.0db1.replace_one({"name":tl[0],"date":tt},{ "name":tl[0],"date":tt,'ratio':round(ra,2)},True)print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')#return vv*1.0#mm=potential_index(code[100])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 int(d1)return d1now=front_step_time(0)bf=front_step_time(720)sheet=pd.DataFrame()
sheet['code']=codesheet['bf']=bf
sheet['sta']=now
#name='600354'
#b1=potential_vocanol(name,'2017-11-14','2018-02-14')
#b2=potential_vocanol(name,'2018-02-14','2018-04-13')import time
from multiprocessing import Pool
import numpy as npte =sheet.values'''
for name in te:mm=potential_index(name)#print(name,mm)'''
if __name__ == "__main__" :startTime = time.time()testFL =sheet.values#ll=codepool = Pool(20)#可以同時跑10個進程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
}
總結
以上是生活随笔為你收集整理的Python量化(八)下影线选股法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 量化(四)计算股票的移动平
- 下一篇: git-commit编辑器nano改vi