7.3 股票分析(project)

目录


本关任务:完成涨幅和成交量股票分析。

相关知识

1.sorted()函数 2.集合运算

sorted()函数

sorted() 函数对所有可迭代的对象进行排序操作。

  1. sorted(iterable, key=None, reverse=False)

参数说明

  • iterable 可迭代对象。
  • key 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse 排序规则,reverse=True 降序, reverse=False 升序(默认)。

编程 在右侧编辑器中补充代码,根据用户输入,利用集合运算和这些文件数据输出以下内容:

  1. 涨幅和成交量均在前10名的股票
  2. 涨幅或成交量在前10名的股票
  3. 涨幅前10名,但成交量未进前10名的股票
  4. 涨幅和成交量不同时在前10名的股票 交易数据文件数据内容格

第1关:涨幅和成交量

import numpy as np
# 设置常量,对应各列数据的语义,方便索引  
HIGH = 0
LOW = 1
CLOSE = 3
VOLUME = 4

def statistics_of_all(code_list):
    """  
    @参数 code_list:股票代码列表,列表类型  
    接收股票数据文件名列表,逐个统计各股票数据文件涨跌幅、总成交量、最高价和最低价。  
    涨跌幅计算公式为:(最新记录收盘价-最早记录收盘价) / 最早记录收盘价 * 100  
    为方便处理,读入数据时,略过日期列。  
    """
    statistics_of_stock = []
    for code in code_list:
        data_of_code = np.genfromtxt('datas/' + code, dtype=None,
                                     usecols=[1, 2, 3, 4, 5, 6], delimiter=',',
                                     skip_header=1)
        # 计算当前股票涨跌幅、总成交量、最高价和最低价  
        uplift_or_fall = round((data_of_code[:, CLOSE][-1] - data_of_code[:, CLOSE][0]) / data_of_code[:, CLOSE][0] * 100, 2)
        volumes = round(sum(data_of_code[:, VOLUME]), 2)
        statistics_of_stock.append([code[:6], uplift_or_fall, volumes])
    return statistics_of_stock  # 每支股票涨跌幅、总成交量、最高价和最低价

def top_10_uplift(statistics_of_stock):
    """  
    @参数 statistics_of_stock:每支股票涨跌幅、总成交量、最高价和最低价统计信息,列表类型  
    按涨幅降序排序,涨幅相同时按股票代码降序排序,取排名前10的股票,  
    返回排名前10的股票代码,返回值为列表类型。  
    """
    # 补充你的代码
    statistics_of_stock1 = sorted(statistics_of_stock, key=lambda x : (x[1],x[0]), reverse=True)
    list1 = []
    for i in range(10): 
        list1.append(statistics_of_stock1[i][0])
    return list1


def top_10_volumes(statistics_of_stock):
    """  
    @参数 statistics_of_stock:每支股票涨跌幅、总成交量、最高价和最低价统计信息,列表类型  
    按成交量降序排序,成交量相同时,按股票代码降序排序,取成交量前10的股票代码,返回成交量  
    最大的10支股票代码列表。  
    """
    # 补充你的代码
    statistics_of_stock2 = sorted(statistics_of_stock, key=lambda x : (x[2],x[0]), reverse=True)
    list2 = []
    for i in range(10): 
        list2.append(statistics_of_stock2[i][0])
    return list2


def uplift_and_volumes(top_uplift, top_volumes):
    """
    @参数 top_high,最高价在前10名的股票代码,字符串
    @参数 top_volumes,成交量在前10名的股票代码,字符串
    返回一个列表,其元素依序为以下4个:
    涨幅和成交量均在前10名的股票,按股票代码升序,列表
    涨幅或成交量在前10名的股票,按股票代码升序,列表
    涨幅前10名,但成交量未进前10名的股票,按股票代码升序,列表
    涨幅和成交量不同时在前10名的股票,按股票代码升序,列表
    """
    # 补充你的代码
    u_and_v = []
    u_and_v0 = sorted([x for x in top_uplift if x in top_volumes])
    u_and_v.append(u_and_v0)
    u_and_v1 =sorted(top_volumes + [x for x in top_uplift if x not in top_volumes])
    u_and_v.append(u_and_v1)
    u_and_v2 = sorted([x for x in top_uplift if x not in top_volumes])
    u_and_v.append(u_and_v2)
    u_and_v3 = sorted([x for x in u_and_v1 if x not in u_and_v0])
    u_and_v.append(u_and_v3)
    return u_and_v

def operation():
    """接收一个字符串为参数,根据参数值调用不同函数完成任务"""
    statistics_of_list = statistics_of_all(stock_lst)  # 对获取的股票数据进行统计  
    uplift_set = top_10_uplift(statistics_of_list)  # 涨幅前10名集合
    volumes_set = top_10_volumes(statistics_of_list)  # 成交量前10名集合
    u_and_v = uplift_and_volumes(uplift_set, volumes_set)
    opt = input()
    if opt == '涨幅与成交量':
        print('涨幅和成交量均在前10名的股票:')
        print(u_and_v[0])  # 涨幅和成交量均在前10名的股票
        print('涨幅或成交量在前10名的股票:')
        print(u_and_v[1])  # 涨幅或成交量在前10名的股票
        print('涨幅前10名,但成交量未进前10名的股票:')
        print(u_and_v[2])  # 涨幅前10名,但成交量未进前10名的股票
        print('涨幅和成交量不同时在前10名的股票:')
        print(u_and_v[3])  # 涨幅和成交量均在前10名的股票
    else:
        print('输入错误')
if __name__ == '__main__':
    filename = 'datas/沪市股票top300.csv'              # 股票名称与代码文件
    stock_lst = ['600000.csv', '600004.csv', '600006.csv',
                 '600007.csv', '600008.csv', '600009.csv',
                 '600010.csv', '600011.csv', '600012.csv',
                 '600015.csv', '600016.csv', '600018.csv',
                 '600019.csv', '600020.csv', '600026.csv',
                 '600028.csv', '600029.csv', '600030.csv',
                 '600031.csv', '600033.csv', '600036.csv']
    operation()

 第2关 涨幅与最高价

import numpy as np
# 设置常量,对应各列数据的语义,方便索引
HIGH = 0
CLOSE = 3
VOLUME = 4


def statistics_of_all(code_list):
    """
    @参数 code_list:股票代码列表,列表类型
    接收股票数据文件名列表,逐个统计各股票数据文件涨幅、总成交量、最高价和最低价。
    涨幅计算公式为:(最新记录收盘价-最早记录收盘价) / 最早记录收盘价 * 100
    为方便处理,读入数据时,略过日期列。
    """
    statistics_of_stock = []
    for code in code_list:
        data_of_code = np.genfromtxt('datas/' + code, dtype=None,
                                     usecols=[1, 2, 3, 4, 5, 6], delimiter=',',
                                     skip_header=1)
        # 计算当前股票涨幅、总成交量、最高价和最低价
        uplift_or_fall = round((data_of_code[:, CLOSE][-1] - data_of_code[:, CLOSE][0]) / data_of_code[:, CLOSE][0] * 100, 2)
        volumes = round(sum(data_of_code[:, VOLUME]), 2)
        high = round(max(data_of_code[:, HIGH]), 2)
        statistics_of_stock.append([code[:6], uplift_or_fall, volumes, high])
    return statistics_of_stock  # 每支股票涨幅、总成交量、最高价和最低价

def top_10_uplift(statistics_of_stock):
    """
    @参数 statistics_of_stock:每支股票涨幅、总成交量、最高价和最低价统计信息,列表类型
    按涨幅降序排序,涨幅相同时按股票代码降序排序,取排名前10的股票,
    返回排名前10的股票代码,返回值为列表类型。
    """
    # 补充你的代码
    statistics_of_stock1 = sorted(statistics_of_stock, key=lambda x : x[1], reverse=True)
    list1 = []
    for i in range(10): 
        list1.append(statistics_of_stock1[i][0])
    return list1


def top_10_high(statistics_of_stock):
    """
    @参数 statistics_of_stock:每支股票涨幅、总成交量、最高价和最低价统计信息,列表类型
    按最高价降序排序,最高价相同时,按股票代码降序排序返回,取排名前10的股票,返回最高价最
    高的10支股票代码的列表。
    """
    # 补充你的代码
    statistics_of_stock2 = sorted(statistics_of_stock, key=lambda x : x[3], reverse=True)
    list2 = []
    for i in range(10): 
        list2.append(statistics_of_stock2[i][0])
    return list2


def high_and_uplift(top_uplift, top_high):
    """
    @参数 top_high,最高价在前10名的股票代码,字符串
    @参数 top_uplift,涨幅在前10名的股票代码,字符串
    返回一个列表,其元素依序为以下4个:
    涨幅和最高价均在前10名的股票代码,按股票代码升序,列表
    涨幅或最高价在前10名的股票代码,按股票代码升序,列表
    涨幅前10名,但最高价未进前10名的股票代码,按股票代码升序,列表
    涨幅和最高价不同时在前10名的股票,按股票代码升序,列表
    票代码。
    """
    # 补充你的代码
    u_and_v = []
    u_and_v0 = sorted([x for x in top_uplift if x in top_high])
    u_and_v.append(u_and_v0)
    u_and_v1 =sorted(top_high + [x for x in top_uplift if x not in top_high])
    u_and_v.append(u_and_v1)
    u_and_v2 = sorted([x for x in top_uplift if x not in top_high])
    u_and_v.append(u_and_v2)
    u_and_v3 = sorted([x for x in u_and_v1 if x not in u_and_v0])
    u_and_v.append(u_and_v3)
    return u_and_v

def operation():
    """接收一个字符串为参数,根据参数值调用不同函数完成任务"""
    statistics_of_list = statistics_of_all(stock_lst)  # 对获取的股票数据进行统计
    uplift_set = top_10_uplift(statistics_of_list)  # 涨幅前10名集合
    high_set = top_10_high(statistics_of_list)  # 最高价前10名集合
    u_and_v = high_and_uplift(uplift_set, high_set)
    opt = input()

    if opt == '涨幅与最高价':
        # 补充你的代码
        print('涨幅和最高价均在前10名的股票:')
        print(u_and_v[0])  # 涨幅和最高价均在前10名的股票
        print('涨幅或最高价在前10名的股票:')
        print(u_and_v[1])  # 涨幅或最高价在前10名的股票
        print('涨幅前10名,但最高价未进前10名的股票:')
        print(u_and_v[2])  # 涨幅前10名,但最高价未进前10名的股票
        print('涨幅和最高价不同时在前10名的股票:')
        print(u_and_v[3])  # 涨幅和最高价均在前10名的股票    
    else:
        print('输入错误')
if __name__ == '__main__':
    filename = 'datas/沪市股票top300.csv'              # 股票名称与代码文件
    stock_lst = ['600000.csv', '600004.csv', '600006.csv',
                 '600007.csv', '600008.csv', '600009.csv',
                 '600010.csv', '600011.csv', '600012.csv',
                 '600015.csv', '600016.csv', '600018.csv',
                 '600019.csv', '600020.csv', '600026.csv',
                 '600028.csv', '600029.csv', '600030.csv',
                 '600031.csv', '600033.csv', '600036.csv']
    operation()

 第3关 跌幅与最低价

import numpy as np

# 设置常量,对应各列数据的语义,方便索引
HIGH = 0
LOW = 1
CLOSE = 3
VOLUME = 4

def statistics_of_all(code_list):
    """
    @参数 code_list:股票代码列表,列表类型
    接收股票数据文件名列表,逐个统计各股票数据文件涨跌幅、总成交量、最高价和最低价。
    涨跌幅计算公式为:(最新记录收盘价-最早记录收盘价) / 最早记录收盘价 * 100
    为方便处理,读入数据时,略过日期列。
    """
    statistics_of_stock = []
    for code in code_list:
        data_of_code = np.genfromtxt('datas/' + code, dtype=None,
                                     usecols=[1, 2, 3, 4, 5, 6], delimiter=',',
                                     skip_header=1)
        # 计算当前股票跌幅、总成交量、最高价和最低价
        uplift_or_fall = round((data_of_code[:, CLOSE][-1] - data_of_code[:, CLOSE][0]) / data_of_code[:, CLOSE][0] * 100, 2)
        volumes = round(sum(data_of_code[:, VOLUME]), 2)
        high = round(max(data_of_code[:, HIGH]), 2)
        low = round(min(data_of_code[:, LOW]), 2)
        statistics_of_stock.append([code[:6], uplift_or_fall, volumes, high, low])
    return statistics_of_stock  # 每支股票涨跌幅、总成交量、最高价和最低价


def top_10_uplift(statistics_of_stock):
    """
    @参数 statistics_of_stock:每支股票涨跌幅、总成交量、最高价和最低价统计信息,列表类型
    按涨幅降序排序,涨幅相同时按股票代码降序排序,取排名前10的股票,
    返回排名前10的股票代码,返回值为列表类型。
    """
    # 补充你的代码
    statistics_of_stock1 = sorted(statistics_of_stock, key=lambda x : x[1], reverse=True)
    list1 = []
    for i in range(10): 
        list1.append(statistics_of_stock1[i][0])
    return list1

def top_10_fall(statistics_of_stock):
    """
    @参数 statistics_of_stock:每支股票涨跌幅、总成交量、最高价和最低价统计信息,列表类型
    按跌幅升序排序,跌幅相同时,按股票代码升序排序,取排名前10的股票,返回跌幅最大的10支股
    票代码的集合。
    """
    # 补充你的代码
    statistics_of_stock2 = sorted(statistics_of_stock, key=lambda x : x[1], reverse=False)
    list2 = []
    for i in range(10): 
        list2.append(statistics_of_stock2[i][0])
    return list2


def top_10_low(statistics_of_stock):
    """
    @参数 statistics_of_stock:每支股票涨跌幅、总成交量、最高价和最低价统计信息,列表类型
    按最低价升序排序,最低价相同时,按股票代码升序排序,取排名前10的股票,返回最低价最低的
    10支股票代码集合。
    """
    # 补充你的代码
    statistics_of_stock3 = sorted(statistics_of_stock, key=lambda x : x[4], reverse=False)
    list3 = []
    for i in range(10): 
        list3.append(statistics_of_stock3[i][0])
    return list3


def low_and_fall(top_fall, top_low):
    """
    @参数 top_low,最低价在前10名的股票代码,字符串
    @参数 top_fall,跌幅在前10名的股票代码,字符串
    返回一个列表,其元素依序为以下4个
    跌幅和最低价均在前10名的股票代码,按股票代码升序,列表
    跌幅或最低价在前10名的股票代码,按股票代码升序,列表
    跌幅前10名,但最低价未进前10名的股票代码,按股票代码升序,列表
    跌幅和最高价不同时在前10名的股票,按股票代码升序,列表
    """
    # 补充你的代码
    u_and_v = []
    u_and_v0 = sorted([x for x in top_fall if x in top_low])
    u_and_v.append(u_and_v0)
    u_and_v1 =sorted(top_low + [x for x in top_fall if x not in top_low])
    u_and_v.append(u_and_v1)
    u_and_v2 = sorted([x for x in top_fall if x not in top_low])
    u_and_v.append(u_and_v2)
    u_and_v3 = sorted([x for x in u_and_v1 if x not in u_and_v0])
    u_and_v.append(u_and_v3)
    return u_and_v


def operation():
    """接收一个字符串为参数,根据参数值调用不同函数完成任务"""
    statistics_of_list = statistics_of_all(stock_lst)  # 对获取的股票数据进行统计
    fall_set = top_10_fall(statistics_of_list)  # 跌幅前10名集合
    low_set = top_10_low(statistics_of_list)  # 最低价前10名集合
    u_and_v = low_and_fall(fall_set, low_set)
    opt = input()
    if opt == '跌幅与最低价':
        # 补充你的代码
        print('跌幅和最低价均在前10名的股票:')
        print(u_and_v[0])  # 涨幅和最高价均在前10名的股票
        print('跌幅或最低价在前10名的股票:')
        print(u_and_v[1])  # 涨幅或最高价在前10名的股票
        print('跌幅前10名,但最低价未进前10名的股票:')
        print(u_and_v[2])  # 涨幅前10名,但最高价未进前10名的股票
        print('跌幅和最低价不同时在前10名的股票:')
        print(u_and_v[3])  # 涨幅和最高价均在前10名的股票  
    else:
        print('输入错误')


if __name__ == '__main__':
    filename = 'datas/沪市股票top300.csv'              # 股票名称与代码文件
    stock_lst = ['600000.csv', '600004.csv', '600006.csv',
                 '600007.csv', '600008.csv', '600009.csv',
                 '600010.csv', '600011.csv', '600012.csv',
                 '600015.csv', '600016.csv', '600018.csv',
                 '600019.csv', '600020.csv', '600026.csv',
                 '600028.csv', '600029.csv', '600030.csv',
                 '600031.csv', '600033.csv', '600036.csv']
    operation()

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年12月13日
下一篇 2023年12月13日

相关推荐