当前位置:   article > 正文

Skr-Eric的数据分析课堂(五)--Numpy的常用函数(下)_mp.gcf().autofmt_xdate()

mp.gcf().autofmt_xdate()

10.布林带

布林带包括三条曲线:

中轨:移动平均线,表示趋势和方向

下轨:移动平均线-2x标准差,表示支撑

上轨:移动平均线+2x标准差,表示压力

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime as dt
  4. import numpy as np
  5. import matplotlib.pyplot as mp
  6. import matplotlib.dates as md
  7. # 将日-月-年格式的日期变为年-月-日格式的转换器函数
  8. def dmy2ymd(dmy):
  9. # 将UTF-8编码的字节串转换为UCS-4编码字符串
  10. dmy = str(dmy, encoding='utf-8')
  11. '''
  12. d, m, y = dmy.split('-')
  13. ymd = y + "-" + m + "-" + d
  14. '''
  15. # 将日-月-年格式的日期字符串解析为datetime
  16. # 类型的对象,再取其date类型的日期子对象
  17. date = dt.datetime.strptime(
  18. dmy, '%d-%m-%Y').date()
  19. # 将date类型的日期对象格式
  20. # 化为年-月-日形式的字符串
  21. ymd = date.strftime('%Y-%m-%d')
  22. return ymd
  23. # 从aapl.csv文件中读取苹果公司一段时间内的
  24. # 股票价格:开盘价,最高价,最低价和收盘价
  25. dates, closing_prices = np.loadtxt(
  26. '../../data/aapl.csv', delimiter=",",
  27. usecols=(1, 6), unpack=True,
  28. dtype='M8[D], f8', converters={1: dmy2ymd})
  29. N = 5
  30. medios = np.convolve(closing_prices,
  31. np.ones(N) / N, 'valid')
  32. stds = np.zeros(medios.size)
  33. for i in range(stds.size):
  34. stds[i] = closing_prices[i:i + N].std()
  35. stds *= 2
  36. lowers = medios - stds
  37. uppers = medios + stds
  38. mp.figure('Bollinger Bands', facecolor='lightgray')
  39. mp.title('Bollinger Bands', fontsize=20)
  40. mp.xlabel('Date', fontsize=14)
  41. mp.ylabel('Price', fontsize=14)
  42. ax = mp.gca()
  43. # 主刻度表示每个星期的星期一
  44. ax.xaxis.set_major_locator(
  45. md.WeekdayLocator(byweekday=md.MO))
  46. # 次刻度表示每一天
  47. ax.xaxis.set_minor_locator(md.DayLocator())
  48. # 设置主刻度的标签格式:日 月(英文缩写) 年
  49. ax.xaxis.set_major_formatter(
  50. md.DateFormatter('%d %b %Y'))
  51. mp.tick_params(labelsize=10)
  52. mp.grid(linestyle=':')
  53. # Numpy.datetime64[D]->
  54. # Matplotlib.dates.datetime.datetime
  55. dates = dates.astype(md.datetime.datetime)
  56. mp.plot(dates, closing_prices, c='lightgray',
  57. label='Closing Price')
  58. mp.plot(dates[N - 1:], medios, c='dodgerblue',
  59. label='Medio')
  60. mp.plot(dates[N - 1:], lowers, c='limegreen',
  61. label='Lower')
  62. mp.plot(dates[N - 1:], uppers, c='orangered',
  63. label='Upper')
  64. mp.legend()
  65. mp.gcf().autofmt_xdate()
  66. mp.show()

 

11.线性模型

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]

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime as dt
  4. import numpy as np
  5. import pandas as pd
  6. import matplotlib.pyplot as mp
  7. import matplotlib.dates as md
  8. # 将日-月-年格式的日期变为年-月-日格式的转换器函数
  9. def dmy2ymd(dmy):
  10. # 将UTF-8编码的字节串转换为UCS-4编码字符串
  11. dmy = str(dmy, encoding='utf-8')
  12. '''
  13. d, m, y = dmy.split('-')
  14. ymd = y + "-" + m + "-" + d
  15. '''
  16. # 将日-月-年格式的日期字符串解析为datetime
  17. # 类型的对象,再取其date类型的日期子对象
  18. date = dt.datetime.strptime(
  19. dmy, '%d-%m-%Y').date()
  20. # 将date类型的日期对象格式
  21. # 化为年-月-日形式的字符串
  22. ymd = date.strftime('%Y-%m-%d')
  23. return ymd
  24. # 从aapl.csv文件中读取苹果公司一段时间内的
  25. # 股票价格:开盘价,最高价,最低价和收盘价
  26. dates, closing_prices = np.loadtxt(
  27. '../../data/aapl.csv', delimiter=",",
  28. usecols=(1, 6), unpack=True,
  29. dtype='M8[D], f8', converters={1: dmy2ymd})
  30. N = 5
  31. pred_prices = np.zeros(
  32. closing_prices.size - 2 * N + 1)
  33. for i in range(pred_prices.size):
  34. a = np.zeros((N, N))
  35. for j in range(N):
  36. a[j, ] = closing_prices[i + j:i + j + N]
  37. b = closing_prices[i + N:i + 2 * N]
  38. x = np.linalg.lstsq(a, b)[0]
  39. pred_prices[i] = b.dot(x)
  40. mp.figure('Linear Prediction', facecolor='lightgray')
  41. mp.title('Linear Prediction', fontsize=20)
  42. mp.xlabel('Date', fontsize=14)
  43. mp.ylabel('Price', fontsize=14)
  44. ax = mp.gca()
  45. # 主刻度表示每个星期的星期一
  46. ax.xaxis.set_major_locator(
  47. md.WeekdayLocator(byweekday=md.MO))
  48. # 次刻度表示每一天
  49. ax.xaxis.set_minor_locator(md.DayLocator())
  50. # 设置主刻度的标签格式:日 月(英文缩写) 年
  51. ax.xaxis.set_major_formatter(
  52. md.DateFormatter('%d %b %Y'))
  53. mp.tick_params(labelsize=10)
  54. mp.grid(linestyle=':')
  55. # Numpy.datetime64[D]->
  56. # Matplotlib.dates.datetime.datetime
  57. dates = dates.astype(md.datetime.datetime)
  58. mp.plot(dates, closing_prices, 'o-', c='lightgray',
  59. label='Closing Price')
  60. dates = np.append(
  61. dates, dates[-1] + pd.tseries.offsets.BDay())
  62. mp.plot(dates[2 * N:], pred_prices, 'o', c='orangered',
  63. label='Predicted Price')
  64. print(dates[-1], '->', pred_prices[-1])
  65. mp.legend()
  66. mp.gcf().autofmt_xdate()
  67. 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]

趋势线、压力线和支撑线

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime as dt
  4. import numpy as np
  5. import matplotlib.pyplot as mp
  6. import matplotlib.dates as md
  7. # 将日-月-年格式的日期变为年-月-日格式的转换器函数
  8. def dmy2ymd(dmy):
  9. # 将UTF-8编码的字节串转换为UCS-4编码字符串
  10. dmy = str(dmy, encoding='utf-8')
  11. '''
  12. d, m, y = dmy.split('-')
  13. ymd = y + "-" + m + "-" + d
  14. '''
  15. # 将日-月-年格式的日期字符串解析为datetime
  16. # 类型的对象,再取其date类型的日期子对象
  17. date = dt.datetime.strptime(
  18. dmy, '%d-%m-%Y').date()
  19. # 将date类型的日期对象格式
  20. # 化为年-月-日形式的字符串
  21. ymd = date.strftime('%Y-%m-%d')
  22. return ymd
  23. # 从aapl.csv文件中读取苹果公司一段时间内的
  24. # 股票价格:开盘价,最高价,最低价和收盘价
  25. dates, opening_prices, highest_prices, \
  26. lowest_prices, closing_prices = np.loadtxt(
  27. '../../data/aapl.csv', delimiter=",",
  28. usecols=(1, 3, 4, 5, 6), unpack=True,
  29. dtype='M8[D], f8, f8, f8, f8',
  30. converters={1: dmy2ymd})
  31. trend_points = (highest_prices + lowest_prices +
  32. closing_prices) / 3
  33. spreads = highest_prices - lowest_prices
  34. resistance_points = trend_points + spreads
  35. support_points = trend_points - spreads
  36. days = dates.astype(int)
  37. a = np.column_stack((days, np.ones_like(days)))
  38. x = np.linalg.lstsq(a, resistance_points)[0]
  39. resistance_line = days * x[0] + x[1]
  40. x = np.linalg.lstsq(a, trend_points)[0]
  41. trend_line = days * x[0] + x[1]
  42. x = np.linalg.lstsq(a, support_points)[0]
  43. support_line = days * x[0] + x[1]
  44. mp.figure('Trend', facecolor='lightgray')
  45. mp.title('Trend', fontsize=20)
  46. mp.xlabel('Date', fontsize=14)
  47. mp.ylabel('Price', fontsize=14)
  48. ax = mp.gca()
  49. # 主刻度表示每个星期的星期一
  50. ax.xaxis.set_major_locator(
  51. md.WeekdayLocator(byweekday=md.MO))
  52. # 次刻度表示每一天
  53. ax.xaxis.set_minor_locator(md.DayLocator())
  54. # 设置主刻度的标签格式:日 月(英文缩写) 年
  55. ax.xaxis.set_major_formatter(
  56. md.DateFormatter('%d %b %Y'))
  57. mp.tick_params(labelsize=10)
  58. mp.grid(axis='y', linestyle=':')
  59. # Numpy.datetime64[D]->
  60. # Matplotlib.dates.datetime.datetime
  61. dates = dates.astype(md.datetime.datetime)
  62. rise = closing_prices - opening_prices >= 0.01
  63. fall = opening_prices - closing_prices >= 0.01
  64. fc = np.zeros(dates.size, dtype='3f4')
  65. ec = np.zeros(dates.size, dtype='3f4')
  66. fc[rise], fc[fall] = (1, 1, 1), (0.85, 0.85, 0.85)
  67. ec[rise], ec[fall] = (0.85, 0.85, 0.85), (0.85, 0.85, 0.85)
  68. mp.bar(dates, highest_prices - lowest_prices,
  69. 0, lowest_prices, color=fc, edgecolor=ec)
  70. mp.bar(dates, closing_prices - opening_prices,
  71. 0.8, opening_prices, color=fc, edgecolor=ec)
  72. mp.scatter(dates, resistance_points, c='orangered',
  73. alpha=0.5, s=60, zorder=2)
  74. mp.scatter(dates, trend_points, c='dodgerblue',
  75. alpha=0.5, s=60, zorder=2)
  76. mp.scatter(dates, support_points, c='limegreen',
  77. alpha=0.5, s=60, zorder=2)
  78. mp.plot(dates, resistance_line, c='orangered',
  79. linewidth=3, label='Resistance')
  80. mp.plot(dates, trend_line, c='dodgerblue',
  81. linewidth=3, label='Trend')
  82. mp.plot(dates, support_line, c='limegreen',
  83. linewidth=3, label='Support')
  84. mp.legend()
  85. mp.gcf().autofmt_xdate()
  86. mp.show()

 

12.裁剪、压缩和累乘

ndarray.clip(min=下限, max=上限)

返回新数组,在新数组中与原数组对应的元素凡是比下限小比上限大者都被限制为下限和上限

ndarray.compress(条件)

返回新数组,在新数组中仅包含原数组中满足给定条件的元素

ndarray.prod()

返回数组中各元素的乘积

ndarray.cumprod()

返回数组中各元素累乘的过程

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import numpy as np
  4. a = np.arange(1, 10).reshape(3, 3)
  5. print(a)
  6. b = a.clip(min=3)
  7. print(b)
  8. c = a.clip(max=7)
  9. print(c)
  10. d = a.clip(3, 7)
  11. print(d)
  12. e = a.compress((a % 2 != 0).ravel())
  13. print(e)
  14. f = a.prod()
  15. print(f)
  16. g = a.cumprod()
  17. print(g)

def factor(n):

    return 1 if n == 0 else n * factor(n-1)

np.arange(1, n+1).prod()

 

13.相关性

样本

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) -> 相关性矩阵

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime as dt
  4. import numpy as np
  5. import matplotlib.pyplot as mp
  6. import matplotlib.dates as md
  7. # 将日-月-年格式的日期变为年-月-日格式的转换器函数
  8. def dmy2ymd(dmy):
  9. # 将UTF-8编码的字节串转换为UCS-4编码字符串
  10. dmy = str(dmy, encoding='utf-8')
  11. '''
  12. d, m, y = dmy.split('-')
  13. ymd = y + "-" + m + "-" + d
  14. '''
  15. # 将日-月-年格式的日期字符串解析为datetime
  16. # 类型的对象,再取其date类型的日期子对象
  17. date = dt.datetime.strptime(
  18. dmy, '%d-%m-%Y').date()
  19. # 将date类型的日期对象格式
  20. # 化为年-月-日形式的字符串
  21. ymd = date.strftime('%Y-%m-%d')
  22. return ymd
  23. dates, bhp_closing_prices = np.loadtxt(
  24. '../../data/bhp.csv', delimiter=",",
  25. usecols=(1, 6), unpack=True,
  26. dtype='M8[D], f8', converters={1: dmy2ymd})
  27. _, vale_closing_prices = np.loadtxt(
  28. '../../data/vale.csv', delimiter=",",
  29. usecols=(1, 6), unpack=True,
  30. dtype='M8[D], f8', converters={1: dmy2ymd})
  31. bhp_returns = np.diff(
  32. bhp_closing_prices) / bhp_closing_prices[:-1]
  33. vale_returns = np.diff(
  34. vale_closing_prices) / vale_closing_prices[:-1]
  35. ave_a = bhp_returns.mean()
  36. dev_a = bhp_returns - ave_a
  37. var_a = (dev_a * dev_a).sum() / (dev_a.size - 1)
  38. std_a = np.sqrt(var_a)
  39. ave_b = vale_returns.mean()
  40. dev_b = vale_returns - ave_b
  41. var_b = (dev_b * dev_b).sum() / (dev_b.size - 1)
  42. std_b = np.sqrt(var_b)
  43. cov_ab = (dev_a * dev_b).sum() / (dev_a.size - 1)
  44. cov_ba = (dev_b * dev_a).sum() / (dev_b.size - 1)
  45. covs = np.array([
  46. [var_a, cov_ab],
  47. [cov_ba, var_b]])
  48. print(covs)
  49. stds = np.array([
  50. [std_a * std_a, std_a * std_b],
  51. [std_b * std_a, std_b * std_b]])
  52. print(stds)
  53. corr = covs / stds
  54. print(corr)
  55. covs = np.cov(bhp_returns, vale_returns)
  56. print(covs)
  57. corr = np.corrcoef(bhp_returns, vale_returns)
  58. print(corr)
  59. mp.figure('Correlation of Returns',
  60. facecolor='lightgray')
  61. mp.title('Correlation of Returns', fontsize=20)
  62. mp.xlabel('Date', fontsize=14)
  63. mp.ylabel('Returns', fontsize=14)
  64. ax = mp.gca()
  65. # 主刻度表示每个星期的星期一
  66. ax.xaxis.set_major_locator(
  67. md.WeekdayLocator(byweekday=md.MO))
  68. # 次刻度表示每一天
  69. ax.xaxis.set_minor_locator(md.DayLocator())
  70. # 设置主刻度的标签格式:日 月(英文缩写) 年
  71. ax.xaxis.set_major_formatter(
  72. md.DateFormatter('%d %b %Y'))
  73. mp.tick_params(labelsize=10)
  74. mp.grid(linestyle=':')
  75. # Numpy.datetime64[D]->
  76. # Matplotlib.dates.datetime.datetime
  77. dates = dates.astype(md.datetime.datetime)
  78. mp.plot(dates[:-1], bhp_returns, c='orangered',
  79. label='BHP')
  80. mp.plot(dates[:-1], vale_returns, c='dodgerblue',
  81. label='VALE')
  82. mp.legend()
  83. mp.gcf().autofmt_xdate()
  84. mp.show()

收益率

     a           b            c          d       e ...

(b-a)/a (c-b)/b (d-c)/c (e-d)/d

 

14.多项式拟合

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

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime as dt
  4. import numpy as np
  5. import matplotlib.pyplot as mp
  6. import matplotlib.dates as md
  7. # 将日-月-年格式的日期变为年-月-日格式的转换器函数
  8. def dmy2ymd(dmy):
  9. # 将UTF-8编码的字节串转换为UCS-4编码字符串
  10. dmy = str(dmy, encoding='utf-8')
  11. '''
  12. d, m, y = dmy.split('-')
  13. ymd = y + "-" + m + "-" + d
  14. '''
  15. # 将日-月-年格式的日期字符串解析为datetime
  16. # 类型的对象,再取其date类型的日期子对象
  17. date = dt.datetime.strptime(
  18. dmy, '%d-%m-%Y').date()
  19. # 将date类型的日期对象格式
  20. # 化为年-月-日形式的字符串
  21. ymd = date.strftime('%Y-%m-%d')
  22. return ymd
  23. dates, bhp_closing_prices = np.loadtxt(
  24. '../../data/bhp.csv', delimiter=",",
  25. usecols=(1, 6), unpack=True,
  26. dtype='M8[D], f8', converters={1: dmy2ymd})
  27. _, vale_closing_prices = np.loadtxt(
  28. '../../data/vale.csv', delimiter=",",
  29. usecols=(1, 6), unpack=True,
  30. dtype='M8[D], f8', converters={1: dmy2ymd})
  31. diff_closing_prices = bhp_closing_prices - \
  32. vale_closing_prices
  33. days = dates.astype(int)
  34. p = np.polyfit(days, diff_closing_prices, 4) # f(x)
  35. poly_closing_prices = np.polyval(p, days)
  36. q = np.polyder(p) # f'(x)
  37. roots = np.roots(q) # f'(x) = 0
  38. peeks = np.polyval(p, roots)
  39. mp.figure('Polynomial Fitting',
  40. facecolor='lightgray')
  41. mp.title('Polynomial Fitting', fontsize=20)
  42. mp.xlabel('Date', fontsize=14)
  43. mp.ylabel('Difference Price', fontsize=14)
  44. ax = mp.gca()
  45. # 主刻度表示每个星期的星期一
  46. ax.xaxis.set_major_locator(
  47. md.WeekdayLocator(byweekday=md.MO))
  48. # 次刻度表示每一天
  49. ax.xaxis.set_minor_locator(md.DayLocator())
  50. # 设置主刻度的标签格式:日 月(英文缩写) 年
  51. ax.xaxis.set_major_formatter(
  52. md.DateFormatter('%d %b %Y'))
  53. mp.tick_params(labelsize=10)
  54. mp.grid(linestyle=':')
  55. # Numpy.datetime64[D]->
  56. # Matplotlib.dates.datetime.datetime
  57. dates = dates.astype(md.datetime.datetime)
  58. mp.plot(dates, poly_closing_prices, c='dodgerblue',
  59. linewidth=3, label='Polynomial Fitting')
  60. mp.scatter(dates, diff_closing_prices,
  61. c='limegreen', alpha=0.5, s=60,
  62. label='Difference Price')
  63. roots = roots.astype(int).astype(
  64. 'M8[D]').astype(md.datetime.datetime)
  65. mp.scatter(roots, peeks, marker='^', c='orangered',
  66. s=80, zorder=3)
  67. mp.legend()
  68. mp.gcf().autofmt_xdate()
  69. mp.show()

 

15.符号数组

[30 -10 20 0 10 -30 0 -20] -符号数组->

[  1    -1  1 0   1    -1 0   -1]

np.sign(数组)->符号数组

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import numpy as np
  4. a = np.array([30, -10, 20, 0, 10, -30, 0, -20])
  5. #b = np.sign(a)
  6. b = np.piecewise(a, [a < 0, a == 0, a > 0],
  7. [-1, 0, 1])
  8. print(a, b, sep='\n')

 

16.标量函数矢量化

def 标量函数(标量参数1, 标量参数2, ...):

     ...

     return 标量返回值1, 标量返回值2, ...

矢量函数 = np.vectorize(标量函数)

矢量返回值 = 矢量函数(矢量参数)

代码:vec.py

模拟交易

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime as dt
  4. import numpy as np
  5. import matplotlib.pyplot as mp
  6. import matplotlib.dates as md
  7. # 将日-月-年格式的日期变为年-月-日格式的转换器函数
  8. def dmy2ymd(dmy):
  9. # 将UTF-8编码的字节串转换为UCS-4编码字符串
  10. dmy = str(dmy, encoding='utf-8')
  11. '''
  12. d, m, y = dmy.split('-')
  13. ymd = y + "-" + m + "-" + d
  14. '''
  15. # 将日-月-年格式的日期字符串解析为datetime
  16. # 类型的对象,再取其date类型的日期子对象
  17. date = dt.datetime.strptime(
  18. dmy, '%d-%m-%Y').date()
  19. # 将date类型的日期对象格式
  20. # 化为年-月-日形式的字符串
  21. ymd = date.strftime('%Y-%m-%d')
  22. return ymd
  23. dates, opening_prices, highest_prices, \
  24. lowest_prices, closing_prices = np.loadtxt(
  25. '../../data/bhp.csv', delimiter=",",
  26. usecols=(1, 3, 4, 5, 6), unpack=True,
  27. dtype='M8[D], f8, f8, f8, f8',
  28. converters={1: dmy2ymd})
  29. def profit(opening_price, highest_price,
  30. lowest_price, closing_price):
  31. buying_price = opening_price * 0.99
  32. if lowest_price <= buying_price <= highest_price:
  33. return (closing_price - buying_price) * \
  34. 100 / buying_price
  35. return np.nan
  36. profits = np.vectorize(profit)(opening_prices,
  37. highest_prices, lowest_prices, closing_prices)
  38. nan = np.isnan(profits)
  39. dates, profits = dates[~nan], profits[~nan]
  40. gain_dates, gain_profits = \
  41. dates[profits > 0], profits[profits > 0]
  42. loss_dates, loss_profits = \
  43. dates[profits < 0], profits[profits < 0]
  44. mp.figure('Trading Simulation', facecolor='lightgray')
  45. mp.title('Trading Simulation', fontsize=20)
  46. mp.xlabel('Date', fontsize=14)
  47. mp.ylabel('Profit', fontsize=14)
  48. ax = mp.gca()
  49. # 主刻度表示每个星期的星期一
  50. ax.xaxis.set_major_locator(
  51. md.WeekdayLocator(byweekday=md.MO))
  52. # 次刻度表示每一天
  53. ax.xaxis.set_minor_locator(md.DayLocator())
  54. # 设置主刻度的标签格式:日 月(英文缩写) 年
  55. ax.xaxis.set_major_formatter(
  56. md.DateFormatter('%d %b %Y'))
  57. mp.tick_params(labelsize=10)
  58. mp.grid(linestyle=':')
  59. # Numpy.datetime64[D]->
  60. # Matplotlib.dates.datetime.datetime
  61. if dates.size > 0:
  62. dates = dates.astype(md.datetime.datetime)
  63. mp.plot(dates, profits, c='gray', label='Profit')
  64. mp.axhline(y=profits.mean(), linestyle='--',
  65. color='gray')
  66. if gain_dates.size > 0:
  67. gain_dates = gain_dates.astype(md.datetime.datetime)
  68. mp.plot(gain_dates, gain_profits, 'o', c='orangered',
  69. label='Gain Profit')
  70. mp.axhline(y=gain_profits.mean(), linestyle='--',
  71. color='orangered')
  72. if loss_dates.size > 0:
  73. loss_dates = loss_dates.astype(md.datetime.datetime)
  74. mp.plot(loss_dates, loss_profits, 'o', c='limegreen',
  75. label='Loss Profit')
  76. mp.axhline(y=loss_profits.mean(), linestyle='--',
  77. color='limegreen')
  78. mp.legend()
  79. mp.gcf().autofmt_xdate()
  80. mp.show()

 

17曲线平滑

卷积->多项式拟合->解差曲线方程

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)

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime as dt
  4. import numpy as np
  5. import matplotlib.pyplot as mp
  6. import matplotlib.dates as md
  7. # 将日-月-年格式的日期变为年-月-日格式的转换器函数
  8. def dmy2ymd(dmy):
  9. # 将UTF-8编码的字节串转换为UCS-4编码字符串
  10. dmy = str(dmy, encoding='utf-8')
  11. '''
  12. d, m, y = dmy.split('-')
  13. ymd = y + "-" + m + "-" + d
  14. '''
  15. # 将日-月-年格式的日期字符串解析为datetime
  16. # 类型的对象,再取其date类型的日期子对象
  17. date = dt.datetime.strptime(
  18. dmy, '%d-%m-%Y').date()
  19. # 将date类型的日期对象格式
  20. # 化为年-月-日形式的字符串
  21. ymd = date.strftime('%Y-%m-%d')
  22. return ymd
  23. dates, bhp_closing_prices = np.loadtxt(
  24. '../../data/bhp.csv', delimiter=",",
  25. usecols=(1, 6), unpack=True,
  26. dtype='M8[D], f8', converters={1: dmy2ymd})
  27. _, vale_closing_prices = np.loadtxt(
  28. '../../data/vale.csv', delimiter=",",
  29. usecols=(1, 6), unpack=True,
  30. dtype='M8[D], f8', converters={1: dmy2ymd})
  31. bhp_returns = np.diff(
  32. bhp_closing_prices) / bhp_closing_prices[:-1]
  33. vale_returns = np.diff(
  34. vale_closing_prices) / vale_closing_prices[:-1]
  35. # ...
  36. mp.figure('Smoothing Returns', facecolor='lightgray')
  37. mp.title('Smoothing Returns', fontsize=20)
  38. mp.xlabel('Date', fontsize=14)
  39. mp.ylabel('Returns', fontsize=14)
  40. ax = mp.gca()
  41. # 主刻度表示每个星期的星期一
  42. ax.xaxis.set_major_locator(
  43. md.WeekdayLocator(byweekday=md.MO))
  44. # 次刻度表示每一天
  45. ax.xaxis.set_minor_locator(md.DayLocator())
  46. # 设置主刻度的标签格式:日 月(英文缩写) 年
  47. ax.xaxis.set_major_formatter(
  48. md.DateFormatter('%d %b %Y'))
  49. mp.tick_params(labelsize=10)
  50. mp.grid(linestyle=':')
  51. # Numpy.datetime64[D]->
  52. # Matplotlib.dates.datetime.datetime
  53. dates = dates.astype(md.datetime.datetime)
  54. mp.plot(dates[:-1], bhp_returns, c='orangered',
  55. alpha=0.25, label='BHP')
  56. mp.plot(dates[:-1], vale_returns, c='dodgerblue',
  57. alpha=0.25, label='VALE')
  58. mp.legend()
  59. mp.gcf().autofmt_xdate()
  60. mp.show()

 

 

 

 

想要看更多的课程请微信关注SkrEric的编程课堂

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/142287
推荐阅读
相关标签
  

闽ICP备14008679号