zoukankan      html  css  js  c++  java
  • 符号数组

    符号数组

    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]
     
  • 相关阅读:
    【安全运维】在Windows平台利用sysmon进行安全分析
    【渗透测试】利用分块传输绕安全狗
    【渗透测试】如何获取目标网站真实IP
    【渗透测试】渗透测试常用在线工具
    【读书笔记】《互联网企业安全建设高级指南》6-17
    【安全运维】linux安全加固项目
    【安全运维】初识osquery
    【渗透测试】使用隧道模式访问目标数据库
    【企业安全】使用文件hash进行威胁分析
    【编程开发】python学习-判断是否是私网IP地址
  • 原文地址:https://www.cnblogs.com/maplethefox/p/11471400.html
Copyright © 2011-2022 走看看