7.3 Stock analysis (project)

Table of contents

Level 1: Gains and volume

 Level 2 Increase and highest price

 Level 3 Decline and Lowest Price


The task of this level: complete the analysis of stock price increase and trading volume.

related information

1. sorted () function 2. set operation

sorted() function

sorted()The function sorts all iterable objects.

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

Parameter Description

  • iterable iterable object.
  • key is mainly an element used for comparison, and has only one parameter. The parameter of the specific function is taken from the iterable object, and an element in the iterable object is specified for sorting.
  • reverse collation, reverse=True descending, reverse=False ascending (default).

Programming Supplement the code in the editor on the right, and output the following content based on user input using set operations and these file data:

  1. Top 10 Stocks by Growth and Volume
  2. Top 10 Stocks by Gain or Volume
  3. The top 10 stocks with the largest gains, but the trading volume did not enter the top 10 stocks
  4. The data content grid of the top 10 stock transaction data files with different price increases and trading volumes

Level 1: Gains and volume

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()

 Level 2  increase and highest price

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()

 Level 3  drop and lowest price

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()

Guess you like

Origin blog.csdn.net/m0_70456205/article/details/130417809