赞
踩
布林带包括三条曲线:
中轨:移动平均线,表示趋势和方向
下轨:移动平均线-2x标准差,表示支撑
上轨:移动平均线+2x标准差,表示压力
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import datetime as dt
- import numpy as np
- import matplotlib.pyplot as mp
- import matplotlib.dates as md
-
- # 将日-月-年格式的日期变为年-月-日格式的转换器函数
- def dmy2ymd(dmy):
- # 将UTF-8编码的字节串转换为UCS-4编码字符串
- dmy = str(dmy, encoding='utf-8')
- '''
- d, m, y = dmy.split('-')
- ymd = y + "-" + m + "-" + d
- '''
- # 将日-月-年格式的日期字符串解析为datetime
- # 类型的对象,再取其date类型的日期子对象
- date = dt.datetime.strptime(
- dmy, '%d-%m-%Y').date()
- # 将date类型的日期对象格式
- # 化为年-月-日形式的字符串
- ymd = date.strftime('%Y-%m-%d')
- return ymd
-
- # 从aapl.csv文件中读取苹果公司一段时间内的
- # 股票价格:开盘价,最高价,最低价和收盘价
- dates, closing_prices = np.loadtxt(
- '../../data/aapl.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- N = 5
- medios = np.convolve(closing_prices,
- np.ones(N) / N, 'valid')
- stds = np.zeros(medios.size)
- for i in range(stds.size):
- stds[i] = closing_prices[i:i + N].std()
- stds *= 2
- lowers = medios - stds
- uppers = medios + stds
- mp.figure('Bollinger Bands', facecolor='lightgray')
- mp.title('Bollinger Bands', fontsize=20)
- mp.xlabel('Date', fontsize=14)
- mp.ylabel('Price', 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=':')
- # Numpy.datetime64[D]->
- # Matplotlib.dates.datetime.datetime
- dates = dates.astype(md.datetime.datetime)
- mp.plot(dates, closing_prices, c='lightgray',
- label='Closing Price')
- mp.plot(dates[N - 1:], medios, c='dodgerblue',
- label='Medio')
- mp.plot(dates[N - 1:], lowers, c='limegreen',
- label='Lower')
- mp.plot(dates[N - 1:], uppers, c='orangered',
- label='Upper')
- mp.legend()
- mp.gcf().autofmt_xdate()
- mp.show()
y = f(x1,x2,x3)
y = k1x1 + k2x2 + k3x3 + b
1)线性预测
a b c d e f g
?
线性假设:第n+1天的股价是其前n天股价的线性组合。
n=3
Aa + Bb + Cc = d \
Ab + Bc + Cd = e | -> A B C -> Ad + Be + Cf => g
Ac + Bd + Ce = f /
/ a b c \ / A \ / d \
| b c d | X | B | = | e |
\ c d e / \ C / \ f /
--------- ----- ------
a x b
= np.linalg.lstsq(a, b)[0]
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import datetime as dt
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as mp
- import matplotlib.dates as md
-
- # 将日-月-年格式的日期变为年-月-日格式的转换器函数
- def dmy2ymd(dmy):
- # 将UTF-8编码的字节串转换为UCS-4编码字符串
- dmy = str(dmy, encoding='utf-8')
- '''
- d, m, y = dmy.split('-')
- ymd = y + "-" + m + "-" + d
- '''
- # 将日-月-年格式的日期字符串解析为datetime
- # 类型的对象,再取其date类型的日期子对象
- date = dt.datetime.strptime(
- dmy, '%d-%m-%Y').date()
- # 将date类型的日期对象格式
- # 化为年-月-日形式的字符串
- ymd = date.strftime('%Y-%m-%d')
- return ymd
-
- # 从aapl.csv文件中读取苹果公司一段时间内的
- # 股票价格:开盘价,最高价,最低价和收盘价
- dates, closing_prices = np.loadtxt(
- '../../data/aapl.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- N = 5
- pred_prices = np.zeros(
- closing_prices.size - 2 * N + 1)
- for i in range(pred_prices.size):
- a = np.zeros((N, N))
- for j in range(N):
- a[j, ] = closing_prices[i + j:i + j + N]
- b = closing_prices[i + N:i + 2 * N]
- x = np.linalg.lstsq(a, b)[0]
- pred_prices[i] = b.dot(x)
- mp.figure('Linear Prediction', facecolor='lightgray')
- mp.title('Linear Prediction', fontsize=20)
- mp.xlabel('Date', fontsize=14)
- mp.ylabel('Price', 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=':')
- # Numpy.datetime64[D]->
- # Matplotlib.dates.datetime.datetime
- dates = dates.astype(md.datetime.datetime)
- mp.plot(dates, closing_prices, 'o-', c='lightgray',
- label='Closing Price')
- dates = np.append(
- dates, dates[-1] + pd.tseries.offsets.BDay())
- mp.plot(dates[2 * N:], pred_prices, 'o', c='orangered',
- label='Predicted Price')
- print(dates[-1], '->', pred_prices[-1])
- mp.legend()
- mp.gcf().autofmt_xdate()
- mp.show()
2)线性拟合
kx1 + b = y1 y1' (y1 - y1')^2
kx2 + b = y2 y2' (y2 - y2')^2
kx3 + b = y3 y3' (y3 - y3')^2
...
kxn + b = yn yn' (yn - yn')^2 (+
-----------------
E = f(k, b)
找到合适的k和b,使得E取得极小值,由此k和b所确定的直线即为拟合指向。这种获取线性拟合的方法,称为最小二乘法。
/ x1 1 \ / y1' \
| x2 1 | / k \ | y2' |
| x3 1 | X | | -> | y3' |
| ... | \ b / | ... |
\xn 1 / \ yn' /
-------- ----- -------
a x b
= np.linalg.lstsq(a, b)[0]
趋势线、压力线和支撑线
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import datetime as dt
- import numpy as np
- import matplotlib.pyplot as mp
- import matplotlib.dates as md
-
- # 将日-月-年格式的日期变为年-月-日格式的转换器函数
- def dmy2ymd(dmy):
- # 将UTF-8编码的字节串转换为UCS-4编码字符串
- dmy = str(dmy, encoding='utf-8')
- '''
- d, m, y = dmy.split('-')
- ymd = y + "-" + m + "-" + d
- '''
- # 将日-月-年格式的日期字符串解析为datetime
- # 类型的对象,再取其date类型的日期子对象
- date = dt.datetime.strptime(
- dmy, '%d-%m-%Y').date()
- # 将date类型的日期对象格式
- # 化为年-月-日形式的字符串
- ymd = date.strftime('%Y-%m-%d')
- return ymd
-
- # 从aapl.csv文件中读取苹果公司一段时间内的
- # 股票价格:开盘价,最高价,最低价和收盘价
- dates, opening_prices, highest_prices, \
- lowest_prices, closing_prices = np.loadtxt(
- '../../data/aapl.csv', delimiter=",",
- usecols=(1, 3, 4, 5, 6), unpack=True,
- dtype='M8[D], f8, f8, f8, f8',
- converters={1: dmy2ymd})
- trend_points = (highest_prices + lowest_prices +
- closing_prices) / 3
- spreads = highest_prices - lowest_prices
- resistance_points = trend_points + spreads
- support_points = trend_points - spreads
- days = dates.astype(int)
- a = np.column_stack((days, np.ones_like(days)))
- x = np.linalg.lstsq(a, resistance_points)[0]
- resistance_line = days * x[0] + x[1]
- x = np.linalg.lstsq(a, trend_points)[0]
- trend_line = days * x[0] + x[1]
- x = np.linalg.lstsq(a, support_points)[0]
- support_line = days * x[0] + x[1]
- mp.figure('Trend', facecolor='lightgray')
- mp.title('Trend', fontsize=20)
- mp.xlabel('Date', fontsize=14)
- mp.ylabel('Price', 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=':')
- # Numpy.datetime64[D]->
- # Matplotlib.dates.datetime.datetime
- dates = dates.astype(md.datetime.datetime)
- rise = closing_prices - opening_prices >= 0.01
- fall = opening_prices - closing_prices >= 0.01
- fc = np.zeros(dates.size, dtype='3f4')
- ec = np.zeros(dates.size, dtype='3f4')
- fc[rise], fc[fall] = (1, 1, 1), (0.85, 0.85, 0.85)
- ec[rise], ec[fall] = (0.85, 0.85, 0.85), (0.85, 0.85, 0.85)
- mp.bar(dates, highest_prices - lowest_prices,
- 0, lowest_prices, color=fc, edgecolor=ec)
- mp.bar(dates, closing_prices - opening_prices,
- 0.8, opening_prices, color=fc, edgecolor=ec)
- mp.scatter(dates, resistance_points, c='orangered',
- alpha=0.5, s=60, zorder=2)
- mp.scatter(dates, trend_points, c='dodgerblue',
- alpha=0.5, s=60, zorder=2)
- mp.scatter(dates, support_points, c='limegreen',
- alpha=0.5, s=60, zorder=2)
- mp.plot(dates, resistance_line, c='orangered',
- linewidth=3, label='Resistance')
- mp.plot(dates, trend_line, c='dodgerblue',
- linewidth=3, label='Trend')
- mp.plot(dates, support_line, c='limegreen',
- linewidth=3, label='Support')
- mp.legend()
- mp.gcf().autofmt_xdate()
- mp.show()
ndarray.clip(min=下限, max=上限)
返回新数组,在新数组中与原数组对应的元素凡是比下限小比上限大者都被限制为下限和上限
ndarray.compress(条件)
返回新数组,在新数组中仅包含原数组中满足给定条件的元素
ndarray.prod()
返回数组中各元素的乘积
ndarray.cumprod()
返回数组中各元素累乘的过程
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import numpy as np
- a = np.arange(1, 10).reshape(3, 3)
- print(a)
- b = a.clip(min=3)
- print(b)
- c = a.clip(max=7)
- print(c)
- d = a.clip(3, 7)
- print(d)
- e = a.compress((a % 2 != 0).ravel())
- print(e)
- f = a.prod()
- print(f)
- g = a.cumprod()
- print(g)
def factor(n):
return 1 if n == 0 else n * factor(n-1)
np.arange(1, n+1).prod()
样本
A = [a1, a2, ..., an]
B = [b1, b2, ..., bn]
均值
ave_a = (a1+a2+...+an)/n
ave_b = (b1+b2+...+bn)/n
离差
dev_a = A - ave_a
dev_b = B - ave_b
方差
var_a = ave(dev_a x dev_a)
var_b = ave(dev_b x dev_b)
标准差
std_a = sqrt(var_a)
std_b = sqrt(var_b)
协方差
cov_ab = ave(dev_a x dev_b) \ 相
cov_ba = ave(dev_b x dev_a) / 等
方差矩阵
/ var_a cov_ab \
\ cov_ba var_b /
标准差矩阵
/ std_a x std_a std_a x std_b \
\ std_b x std_a std_b x std_b /
相关性矩阵
/ var_a cov_ab \
| --------------- --------------- |
| std_a x std_a std_a x std_b |
| |
| cov_ba var_b |
| --------------- --------------- |
\ std_b x std_a std_b x std_b /
在相关性矩阵中,主对角线上的元素是1,表示每个随机量关于其自身一定是最强的正相关,辅对角线上的元素为去除了分散性以后的净相关性指标——相关系数,介于[-1, 1]之间的值,其正负表示了相关性的方向,其绝对值表示了相关性的强弱。
np.cov(A,B) -> 方差矩阵,用n-1做分母,样本(协)方差
np.corrcoef(A,B) -> 相关性矩阵
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import datetime as dt
- import numpy as np
- import matplotlib.pyplot as mp
- import matplotlib.dates as md
-
- # 将日-月-年格式的日期变为年-月-日格式的转换器函数
- def dmy2ymd(dmy):
- # 将UTF-8编码的字节串转换为UCS-4编码字符串
- dmy = str(dmy, encoding='utf-8')
- '''
- d, m, y = dmy.split('-')
- ymd = y + "-" + m + "-" + d
- '''
- # 将日-月-年格式的日期字符串解析为datetime
- # 类型的对象,再取其date类型的日期子对象
- date = dt.datetime.strptime(
- dmy, '%d-%m-%Y').date()
- # 将date类型的日期对象格式
- # 化为年-月-日形式的字符串
- ymd = date.strftime('%Y-%m-%d')
- return ymd
-
- dates, bhp_closing_prices = np.loadtxt(
- '../../data/bhp.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- _, vale_closing_prices = np.loadtxt(
- '../../data/vale.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- bhp_returns = np.diff(
- bhp_closing_prices) / bhp_closing_prices[:-1]
- vale_returns = np.diff(
- vale_closing_prices) / vale_closing_prices[:-1]
- ave_a = bhp_returns.mean()
- dev_a = bhp_returns - ave_a
- var_a = (dev_a * dev_a).sum() / (dev_a.size - 1)
- std_a = np.sqrt(var_a)
- ave_b = vale_returns.mean()
- dev_b = vale_returns - ave_b
- var_b = (dev_b * dev_b).sum() / (dev_b.size - 1)
- std_b = np.sqrt(var_b)
- cov_ab = (dev_a * dev_b).sum() / (dev_a.size - 1)
- cov_ba = (dev_b * dev_a).sum() / (dev_b.size - 1)
- covs = np.array([
- [var_a, cov_ab],
- [cov_ba, var_b]])
- print(covs)
- stds = np.array([
- [std_a * std_a, std_a * std_b],
- [std_b * std_a, std_b * std_b]])
- print(stds)
- corr = covs / stds
- print(corr)
- covs = np.cov(bhp_returns, vale_returns)
- print(covs)
- corr = np.corrcoef(bhp_returns, vale_returns)
- print(corr)
- mp.figure('Correlation of Returns',
- facecolor='lightgray')
- mp.title('Correlation of 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=':')
- # Numpy.datetime64[D]->
- # Matplotlib.dates.datetime.datetime
- dates = dates.astype(md.datetime.datetime)
- mp.plot(dates[:-1], bhp_returns, c='orangered',
- label='BHP')
- mp.plot(dates[:-1], vale_returns, c='dodgerblue',
- label='VALE')
- mp.legend()
- mp.gcf().autofmt_xdate()
- mp.show()
收益率
a b c d e ...
(b-a)/a (c-b)/b (d-c)/c (e-d)/d
f(x)=p0x^n + p1x^n-1 + p2x^n-2 + ... + pn
f(x1) = y1 <=> y1' (y1' - y1)^2
f(x2) = y2 <=> y2' (y2' - y2)^2
...
f(xm) = ym <=> ym' (ym'-ym)^2 (+
-----------------
E = f(p0,p1,...,pn)
找到使E取得极小值的p0,p1,...,pn,即为拟合多项式的系数。
np.polyfit(X, Y, 次数)->P
np.polyval(P, X) -> Y
np.polyder(P)->Q - 导函数的系数向量
f(x) = 3x^2+4x+5, P=[3, 4, 5]
f'(x) = 6x + 4, Q=[6, 4]
np.roots(P)->多项式方程的根:f(x)=0
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import datetime as dt
- import numpy as np
- import matplotlib.pyplot as mp
- import matplotlib.dates as md
-
- # 将日-月-年格式的日期变为年-月-日格式的转换器函数
- def dmy2ymd(dmy):
- # 将UTF-8编码的字节串转换为UCS-4编码字符串
- dmy = str(dmy, encoding='utf-8')
- '''
- d, m, y = dmy.split('-')
- ymd = y + "-" + m + "-" + d
- '''
- # 将日-月-年格式的日期字符串解析为datetime
- # 类型的对象,再取其date类型的日期子对象
- date = dt.datetime.strptime(
- dmy, '%d-%m-%Y').date()
- # 将date类型的日期对象格式
- # 化为年-月-日形式的字符串
- ymd = date.strftime('%Y-%m-%d')
- return ymd
-
- dates, bhp_closing_prices = np.loadtxt(
- '../../data/bhp.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- _, vale_closing_prices = np.loadtxt(
- '../../data/vale.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- diff_closing_prices = bhp_closing_prices - \
- vale_closing_prices
- days = dates.astype(int)
- p = np.polyfit(days, diff_closing_prices, 4) # f(x)
- poly_closing_prices = np.polyval(p, days)
- q = np.polyder(p) # f'(x)
- roots = np.roots(q) # f'(x) = 0
- peeks = np.polyval(p, roots)
- mp.figure('Polynomial Fitting',
- facecolor='lightgray')
- mp.title('Polynomial Fitting', fontsize=20)
- mp.xlabel('Date', fontsize=14)
- mp.ylabel('Difference Price', 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=':')
- # Numpy.datetime64[D]->
- # Matplotlib.dates.datetime.datetime
- dates = dates.astype(md.datetime.datetime)
- mp.plot(dates, poly_closing_prices, c='dodgerblue',
- linewidth=3, label='Polynomial Fitting')
- mp.scatter(dates, diff_closing_prices,
- c='limegreen', alpha=0.5, s=60,
- label='Difference Price')
- roots = roots.astype(int).astype(
- 'M8[D]').astype(md.datetime.datetime)
- mp.scatter(roots, peeks, marker='^', c='orangered',
- s=80, zorder=3)
- mp.legend()
- mp.gcf().autofmt_xdate()
- mp.show()
[30 -10 20 0 10 -30 0 -20] -符号数组->
[ 1 -1 1 0 1 -1 0 -1]
np.sign(数组)->符号数组
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import numpy as np
- a = np.array([30, -10, 20, 0, 10, -30, 0, -20])
- #b = np.sign(a)
- b = np.piecewise(a, [a < 0, a == 0, a > 0],
- [-1, 0, 1])
- print(a, b, sep='\n')
def 标量函数(标量参数1, 标量参数2, ...):
...
return 标量返回值1, 标量返回值2, ...
矢量函数 = np.vectorize(标量函数)
矢量返回值 = 矢量函数(矢量参数)
代码:vec.py
模拟交易
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import datetime as dt
- import numpy as np
- import matplotlib.pyplot as mp
- import matplotlib.dates as md
-
- # 将日-月-年格式的日期变为年-月-日格式的转换器函数
- def dmy2ymd(dmy):
- # 将UTF-8编码的字节串转换为UCS-4编码字符串
- dmy = str(dmy, encoding='utf-8')
- '''
- d, m, y = dmy.split('-')
- ymd = y + "-" + m + "-" + d
- '''
- # 将日-月-年格式的日期字符串解析为datetime
- # 类型的对象,再取其date类型的日期子对象
- date = dt.datetime.strptime(
- dmy, '%d-%m-%Y').date()
- # 将date类型的日期对象格式
- # 化为年-月-日形式的字符串
- ymd = date.strftime('%Y-%m-%d')
- return ymd
-
- dates, opening_prices, highest_prices, \
- lowest_prices, closing_prices = np.loadtxt(
- '../../data/bhp.csv', delimiter=",",
- usecols=(1, 3, 4, 5, 6), unpack=True,
- dtype='M8[D], f8, f8, f8, f8',
- converters={1: dmy2ymd})
-
- def profit(opening_price, highest_price,
- lowest_price, closing_price):
- buying_price = opening_price * 0.99
- if lowest_price <= buying_price <= highest_price:
- return (closing_price - buying_price) * \
- 100 / buying_price
- return np.nan
-
- profits = np.vectorize(profit)(opening_prices,
- highest_prices, lowest_prices, closing_prices)
- nan = np.isnan(profits)
- dates, profits = dates[~nan], profits[~nan]
- gain_dates, gain_profits = \
- dates[profits > 0], profits[profits > 0]
- loss_dates, loss_profits = \
- dates[profits < 0], profits[profits < 0]
- mp.figure('Trading Simulation', facecolor='lightgray')
- mp.title('Trading Simulation', fontsize=20)
- mp.xlabel('Date', fontsize=14)
- mp.ylabel('Profit', 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=':')
- # Numpy.datetime64[D]->
- # Matplotlib.dates.datetime.datetime
- if dates.size > 0:
- dates = dates.astype(md.datetime.datetime)
- mp.plot(dates, profits, c='gray', label='Profit')
- mp.axhline(y=profits.mean(), linestyle='--',
- color='gray')
- if gain_dates.size > 0:
- gain_dates = gain_dates.astype(md.datetime.datetime)
- mp.plot(gain_dates, gain_profits, 'o', c='orangered',
- label='Gain Profit')
- mp.axhline(y=gain_profits.mean(), linestyle='--',
- color='orangered')
- if loss_dates.size > 0:
- loss_dates = loss_dates.astype(md.datetime.datetime)
- mp.plot(loss_dates, loss_profits, 'o', c='limegreen',
- label='Loss Profit')
- mp.axhline(y=loss_profits.mean(), linestyle='--',
- color='limegreen')
- mp.legend()
- mp.gcf().autofmt_xdate()
- mp.show()
卷积->多项式拟合->解差曲线方程
y = f(x)
y = g(x)
(xo,yo)
yo = f(xo)
yo = g(xo)
f(xo) - g(xo) = 0
f(x) - g(x) == 0的根就是(xo, yo)
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- import datetime as dt
- import numpy as np
- import matplotlib.pyplot as mp
- import matplotlib.dates as md
-
- # 将日-月-年格式的日期变为年-月-日格式的转换器函数
- def dmy2ymd(dmy):
- # 将UTF-8编码的字节串转换为UCS-4编码字符串
- dmy = str(dmy, encoding='utf-8')
- '''
- d, m, y = dmy.split('-')
- ymd = y + "-" + m + "-" + d
- '''
- # 将日-月-年格式的日期字符串解析为datetime
- # 类型的对象,再取其date类型的日期子对象
- date = dt.datetime.strptime(
- dmy, '%d-%m-%Y').date()
- # 将date类型的日期对象格式
- # 化为年-月-日形式的字符串
- ymd = date.strftime('%Y-%m-%d')
- return ymd
-
- dates, bhp_closing_prices = np.loadtxt(
- '../../data/bhp.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- _, vale_closing_prices = np.loadtxt(
- '../../data/vale.csv', delimiter=",",
- usecols=(1, 6), unpack=True,
- dtype='M8[D], f8', converters={1: dmy2ymd})
- bhp_returns = np.diff(
- bhp_closing_prices) / bhp_closing_prices[:-1]
- vale_returns = np.diff(
- vale_closing_prices) / vale_closing_prices[:-1]
- # ...
- mp.figure('Smoothing Returns', facecolor='lightgray')
- mp.title('Smoothing 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=':')
- # Numpy.datetime64[D]->
- # Matplotlib.dates.datetime.datetime
- dates = dates.astype(md.datetime.datetime)
- mp.plot(dates[:-1], bhp_returns, c='orangered',
- alpha=0.25, label='BHP')
- mp.plot(dates[:-1], vale_returns, c='dodgerblue',
- alpha=0.25, label='VALE')
- mp.legend()
- mp.gcf().autofmt_xdate()
- mp.show()
想要看更多的课程请微信关注SkrEric的编程课堂
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。