• 符号数组


    符号数组

    sign函数可以把样本数组的变成对应的符号数组,正数变为1,负数变为-1,0则变为0。

    ary = np.sign(源数组)

    净额成交量(OBV)

    成交量可以反映市场对某支股票的人气,而成交量是一只股票上涨的能量。一支股票的上涨往往需要较大的成交量。而下跌时则不然。

    若相比上一天的收盘价上涨,则为正成交量;若相比上一天的收盘价下跌,则为负成交量。

    绘制OBV柱状图

    import numpy as np
    import matplotlib.pyplot as mp
    import datetime as dt
    import matplotlib.dates as md
    
    
    def dmy2ymd(dmy):
      """
      把日月年转年月日
      :param day:
      :return:
      """
      dmy = str(dmy, encoding='utf-8')
      t = dt.datetime.strptime(dmy, '%d-%m-%Y')
      s = t.date().strftime('%Y-%m-%d')
      return s
    dates, closing_prices, volumes = np.loadtxt(
        'aapl.csv', delimiter=',',
        usecols=(1, 6, 7), unpack=True,
        dtype='M8[D], f8, f8', converters={1: dmy2ymd})
    diff_closing_prices = np.diff(closing_prices)
    sign_closing_prices = np.sign(diff_closing_prices)
    obvs = volumes[1:] * sign_closing_prices
    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)
    mp.bar(dates, obvs, 1.0, color='dodgerblue',
           edgecolor='white', label='OBV')
    mp.legend()
    mp.gcf().autofmt_xdate()
    mp.show()

    数组处理函数

    ary = np.piecewise(源数组, 条件序列, 取值序列)
    np.piecewise(ary,[ary>0,ary<0,ary==0],[1,-1,0])

    针对源数组中的每一个元素,检测其是否符合条件序列中的每一个条件,符合哪个条件就用取值系列中与之对应的值,表示该元素,放到目标 数组中返回。

    条件序列: [a < 0, a == 0, a > 0]

    取值序列: [-1, 0, 1]

    import numpy as np
    a = np.array([70, 80, 60, 30, 40])
    d = np.piecewise(
        a, 
        [a < 60, a == 60, a > 60],
        [-1, 0, 1])
    print(d)
    # d = [ 1 1 0 -1 -1]
    d = np.piecewise(
    a,
    [np.all([a <= 100, a >= 60], axis=0), a < 60],
    [1, 0])
    print(d)
    # d= [1 1 1 0 0]
     
  • 相关阅读:
    ThreadPoolExecutor源码解析
    AQS框架
    (转)rvm安装与常用命令
    (转).gitignore详解
    (转)可简化iOS 应用程序开发的6个Xcode小技巧
    (转)webView清除缓存
    (转)git常见错误
    iOS本地通知
    (转)iOS获取设备型号
    (转)iOS平台UDID方案比较
  • 原文地址:https://www.cnblogs.com/maplethefox/p/11471400.html
Copyright © 2020-2023  润新知