1、字符串分割
price_str = '30.14,29.58,26.36,32.56,32.82'
price_array = price_str.split(',')
2、日期叠加
date_array = []
date_base = 20170118
# 索引ind
date_array =[str(date_base+ind) for ind,_ in enumerate(price_array)]
3、日期价格组合成字典
stock_tuple_list = dict((data,price)
for data,price in zip(date_array,price_array))
3、相隔两天的价格
price_float_array = [float(price) for price in stock_tuple_list.values()]
pp_array = [(price1,price2) for price1,price2 in
zip(price_float_array[:-1],price_float_array[1:])]
pp_array
image.png
4、计算每天涨幅
change_array = map(lambda pp : reduce(lambda a,b:round((b-a)/a,3),pp),pp_array)
#pyhton3中map返回结果需要用list转换
change_array = list(change_array)
#第一天涨幅为0
change_array.insert(0,0)
print(change_array)
image.png
4、生成数据
from collections import namedtuple
from collections import OrderedDict
stock_namedtuple = namedtuple('stock',('date,price,change'))
#按照日期排序
stock_dict = OrderedDict((date,stock_namedtuple(date,price,change))
for date,price,change in zip(date_array,price_array,change_array))
stock_dict
image.png
5、选出上涨日期(filter的使用)
up_days = filter(lambda day:day.change>0,stock_dict.values())
up_days = list(up_days)
up_days
image.png
6、使用函数计算算上涨(下跌)日 和 累计涨(跌)幅
def filter_stock(stock_array_list,want_up = True,want_calc_sum=False):
if not isinstance(stock_array_list,OrderedDict):
raise TypeError('type error')
filter_func =(lambda day:day.change>0 ) if want_up else (lambda day:day.change<0)
want_days = filter(filter_func,stock_array_list.values())
want_days = list(want_days)
if not want_calc_sum:
return want_days
change_sum = 0.0
for day in want_days:
change_sum += float(day.change)
return change_sum
print('所有上涨交易日:',filter_stock(stock_dict))
print('所有上涨日累计涨幅:',filter_stock(stock_dict,want_calc_sum=True))
print('所有下跌日累计跌幅:',filter_stock(stock_dict,want_up=False,want_calc_sum=True))
image.png
7、偏函数的使用 避免filter_stock 参数值过多容易出错
from functools import partial
filter_stock_up_days = partial(filter_stock,want_up = True,want_calc_sum = False)
filter_stock_down_days = partial(filter_stock,want_up = False,want_calc_sum = False)
filter_stock_up_sum = partial(filter_stock,want_up = True,want_calc_sum = True)
filter_stock_down_sum = partial(filter_stock,want_up = False,want_calc_sum = True)
8、面向对象编程
from collections import namedtuple
from collections import OrderedDict
class StockTradeDays(object):
def __init__(self,price_array,start_day,date_array=None):
self.__price_array = price_array
self.__date_array = self.__init_days(start_day,date_array)
self.__change_array = self.__init_change()
self.stock_dict = self.__init_stock_dict()
def __init_days(self,start_day,date_array):
if date_array is None:
date_array = [str(start_day+ind) for ind,_ in enumerate(self.__price_array)]
else:
date_array = [str(date) for date in date_array]
return date_array
def __init_change(self):
price_float_array = [float(price) for price in self.__price_array]
pp_array = [(price1,price2) for price1,price2 in zip(price_float_array[:-1],price_float_array[1:])]
change_array = map(lambda pp: reduce(lambda a,b:round((b-a)/a,3),pp),pp_array)
change_array = list(change_array)
change_array.insert(0,0)
return change_array
def __init_stock_dict(self):
stock_namedtuple = namedtuple('stock',('date','price','chenge'))
stock_dict = OrderedDict((date,stock_namedtuple(date,price,change))
for date,price,change in zip(self.__date_array,self.__price_array,self.__change_array))
return stock_dict
def __filter_stock(self,want_up=True,want_calc_sum=False):
filter_func = (lambda day:day.change>0) if want_up else (lambda day:day.change<0)
want_days = filter(filter_func,self.stock_dict.values())
if not want_calc_sum:
return want_days
change_sum = 0.0
for day in want_days:
change_sum += float(day.change)
return change_sum
def __str__(self):
return str(self.stock_dict)
def __iter__(self):
for key in self.stock_dict:
yield self.stock_dict[key]
def __getitem__(self,ind):
date_key = self.__date_array[ind]
return self.stock_dict[date_key]
def __len__(self):
return len(self.stock_dict)
price_str = '30.14,29.58,26.36,32.56,32.82'
price_array = price_str.split(',')
date_base = 20170118
trade_days = StockTradeDays(price_array,date_base)
print(trade_days)
网友评论