一、净额成交量
1、案例分析
- 符号数组a: [10 -20 30 40 -50]
- 用法:numpy.sign(a)
结果是[1 -1 1 1 -1] - 净额成交量:简称OBV,赚了是正,赔了是负
- 利用条件筛选来得到盈亏指标:一参是差分数组,二参是条件数组,三参是各个条件对应的值数组
2、第二种筛选方法
sign_closing_price = np.piecewise(
diff_closing_price,
[diff_closing_price < 0,
diff_closing_price == 0,
diff_closing_price > 0],
[-1, 0 , 1]
)
3、案例源码
import datetime as dt
import numpy as np
import matplotlib.pylab as mp
import matplotlib.dates as md
def dmy2ymd(dmy):
dmy = str(dmy, encoding='utf-8')
date = dt.datetime.strptime(dmy, '%d-%m-%Y').date()
ymd = date.strftime('%Y-%m-%d')
return ymd
dates, closing_prices, volumes = np.loadtxt(
'0=数据源/beer_price2.csv', delimiter=',',
usecols=(0, 4, 5), unpack=True,
dtype=np.dtype('M8[D], f8, f8'),
converters={0: dmy2ymd}
)
diff_closing_price = np.diff(closing_prices)
sign_closing_price = np.sign(diff_closing_price)
sign_closing_price = np.piecewise(
diff_closing_price,
[diff_closing_price < 0,
diff_closing_price == 0,
diff_closing_price > 0],
[-1, 0, 1]
)
obvs = volumes[1:] * sign_closing_price
mp.figure('On-Balance Volume', facecolor='lightgray')
mp.title('On-Balance Volume', fontsize=20)
mp.xlabel('Date', fontsize=14)
mp.ylabel('OBV', fontsize=14)
ax = mp.gca()
ax.xaxis.set_major_locator(
md.WeekdayLocator(byweekday=md.MO)
)
ax.xaxis.set_minor_locator(
md.DayLocator()
)
ax.xaxis.set_major_formatter(
md.DateFormatter('%d %b %Y')
)
mp.tick_params(labelsize=10)
mp.grid(axis='y', linestyle=':')
dates = dates[1:].astype(md.datetime.datetime)
rise = obvs > 0
fall = obvs < 0
fc = np.zeros(dates.size, dtype='3f4')
ec = np.zeros(dates.size, dtype='3f4')
fc[rise], fc[fall] = (1, 0, 0), (0, 0.5, 0)
ec[rise], ec[fall] = (1, 1, 1), (1, 1, 1)
mp.bar(dates, obvs, 1.0, 0, color=fc, edgecolor=ec, label='OBV')
mp.legend()
mp.gcf().autofmt_xdate()
mp.show()
二、标量函数矢量化
1、矢量化概念
def 标量函数(标量参数1, 标量参数2, ...)
...
return 标量返回值1, 标量返回值2, ...
矢量化:np.vectorize(标量函数)——>转换成矢量函数
def 矢量函数(矢量参数1, 矢量参数2, ...)
...
矢量返回值1, 矢量返回值2, ...
2、练习代码
import numpy as np
def fun1(a, b):
return a+b, a-b, a*b
A = np.array([10, 20, 30])
B = np.array([100, 200, 300])
C = np.vectorize(fun1)(A, B)
print(C)
三、数据曲线平滑
1、数据平滑
卷积降噪(消除随机噪声干扰)——>曲线拟合(获得数学模型)——>求特征值(反映业务特征)
2、平滑过程
y=f(x)
y=g(x)
y1=f(x1)
y1=g(x1)
f(x1)=g(x1)
f(x1)-g(x1)=0
f(x)-g(x)=0的根就是x1
np.polysub(p1, p2)得到p3,即为方程f(x)-g(x)=0的各个项系数
np.roots(p3) 得到p3的根,即x1
3、案例源码
import datetime as dt
import numpy as np
import matplotlib.pylab as mp
import matplotlib.dates as md
def dmy2ymd(dmy):
dmy = str(dmy, encoding='utf-8')
date = dt.datetime.strptime(dmy, '%d-%m-%Y').date()
ymd = date.strftime('%Y-%m-%d')
return ymd
dates, beer_closing_prices = np.loadtxt(
'0=数据源/beer_price2.csv', delimiter=',',
usecols=(0, 4), unpack=True,
dtype=np.dtype('M8[D], f8'),
converters={0: dmy2ymd}
)
__, apple_closing_prices = np.loadtxt(
'0=数据源/apple_price.csv', delimiter=',',
usecols=(0, 4), unpack=True,
dtype=np.dtype('M8[D], f8'),
converters={0: dmy2ymd}
)
beer_returns = np.diff(beer_closing_prices)/beer_closing_prices[:-1]
apple_returns = np.diff(apple_closing_prices)/apple_closing_prices[:-1]
N = 8
weights = np.hanning(N)
weights /= weights.sum()
beer_smooth_returns = np.convolve(beer_closing_prices, weights, 'valid')
apple_smooth_returns = np.convolve(apple_closing_prices, weights, 'valid')
days = dates[N-2:-1].astype(int)
degree = 5
beer_p = np.polyfit(days, beer_smooth_returns, degree)
beer_fitted_returns = np.polyval(beer_p, days)
apple_p = np.polyfit(days, apple_smooth_returns, degree)
apple_fitted_returns = np.polyval(apple_p, days)
sub_p = np.polysub(beer_p, apple_p)
roots = np.roots(sub_p)
reals = roots[np.isreal(roots)].real
inters = []
for real in reals:
if days[0] <= real <= days[-1]:
inters.append([real, np.polyval(beer_p, real)])
inters.sort()
inters = np.array(inters)
mp.figure('Smooth returns', facecolor='lightgray')
mp.title('Smooth returns', fontsize=20)
mp.xlabel('Date', fontsize=14)
mp.ylabel('Returns', fontsize=14)
ax = mp.gca()
ax.xaxis.set_major_locator(
md.WeekdayLocator(byweekday=md.MO)
)
ax.xaxis.set_minor_locator(
md.DayLocator()
)
ax.xaxis.set_major_formatter(
md.DateFormatter('%d %b %Y')
)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
dates = dates.astype(md.datetime.datetime)
mp.plot(dates[:-1], beer_returns, c='dodgerblue', alpha=0.25, label='Beer')
mp.plot(dates[:-1], apple_returns, c='limegreen', alpha=0.25, label='Apple')
'''
此处由于数据不够匹配,因此差别很大,绘制不出想要的效果
'''
dates, returns = np.hsplit(inters, 2)
dates = dates.astype(int).astype('M8[D]').astype(md.datetime.datetime)
mp.scatter(dates, returns, marker='x', c='firebrick', s=100, lw=3, zorder=3)
mp.legend()
mp.gcf().autofmt_xdate()
mp.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)