Beispiel #1
0
def get_total_csv(rightStock, dfname, stockArgX):
    rightStocklist = [dfname]
    totalProfit = pd.DataFrame({'rule': rightStocklist})

    if (stockArgX.strategy == True):
        #复制一份原始stock,用于策略买卖收益
        rightStockStrategy = copy.deepcopy(rightStock)
        totalProfit = rule.add_strategy_income(totalProfit, rightStockStrategy,
                                               stockArgX, dfname)

    #复制一份原始stock,用于计算原价卖收益
    # rightStockH = copy.deepcopy(rightStock)
    #计算原价卖收益
    # totalProfit = add_final_income(totalProfit, rightStockH,stockArgX,dfname)
    totalProfit = add_right_count(totalProfit, rightStock)
    rightStock = group_by_date(rightStock)
    # 是否生产中间表
    if (stockArgX.mean):
        rightStock.to_csv(con.detailPath + str(dateUtil.get_date_date()) +
                          dateUtil.get_hour_and_minute_str() + dfname +
                          'mean.csv',
                          index=False)
    # 增加筛选规则对应的样本数量和百分百显示
    totalProfit = add_right_count_by_date(totalProfit, rightStock)
    # 计算并且增加 评价收益
    totalProfit = add_income_mean(totalProfit, rightStock)
    totalProfit = add_days_income_percent(totalProfit, rightStock)
    # totalProfit = add_high_income_mean(totalProfit,rightStockH)
    # totalProfit = add_max_price_day_percent(totalProfit, rightStock)

    # 清除临时detail缓存
    del rightStock
    # 马上重置垃圾清除器
    gc.collect()
    return totalProfit
Beispiel #2
0
def generate_detail_csv(detailTempList, stockArgX, ruleName):
    # 8.如果规则筛选出了样本则进行生成csv等
    if (len(detailTempList) > 0):
        # 连接所有的detail数据
        tempDetail = pd.concat(detailTempList)
        # 转换为pandas的dataframe格式
        tempDetail = pd.DataFrame(tempDetail)
        # 按日期逆向排序
        tempDetail = tempDetail.sort_values(by=['date'], ascending=False)
        # 10.是否生成detail表
        if (stockArgX.detail and len(tempDetail) > 0):
            tempDetail.to_csv(con.detailPath + str(dateUtil.get_date_date()) +
                              dateUtil.get_hour_and_minute_str() + ruleName +
                              'detail.csv',
                              index=False)
        #为邮件保存临时detail
        stockArgX.tempDetail = tempDetail
        return tempDetail
Beispiel #3
0
def add_final_income(df, rightStock, stockArgX, dfname):
    rightStockGood = rightStock[rightStock.day2closeIncome >= 0]
    rightStockNormal = rightStock[(rightStock.day2closeIncome < 0)
                                  & ((rightStock.day3highIncome >= 0)
                                     | (rightStock.day4highIncome >= 0)
                                     | (rightStock.day5highIncome >= 0)
                                     | (rightStock.day6highIncome >= 0)
                                     | (rightStock.day7highIncome >= 0)
                                     | (rightStock.day8highIncome >= 0)
                                     | (rightStock.day9highIncome >= 0)
                                     | (rightStock.day11highIncome >= 0)
                                     | (rightStock.day12highIncome >= 0)
                                     | (rightStock.day13highIncome >= 0)
                                     | (rightStock.day14highIncome >= 0)
                                     | (rightStock.day15highIncome >= 0)
                                     | (rightStock.day16highIncome >= 0)
                                     | (rightStock.day17highIncome >= 0)
                                     | (rightStock.day18highIncome >= 0)
                                     | (rightStock.day19highIncome >= 0)
                                     | (rightStock.day20highIncome >= 0))]
    rightStockLoss = rightStock[(rightStock['day2highIncome'] < 0)
                                & (rightStock['day3highIncome'] < 0)
                                & (rightStock['day4highIncome'] < 0)
                                & (rightStock['day5highIncome'] < 0)
                                & (rightStock['day6highIncome'] < 0)
                                & (rightStock['day7highIncome'] < 0)
                                & (rightStock['day8highIncome'] < 0)
                                & (rightStock['day9highIncome'] < 0)
                                & (rightStock['day11highIncome'] < 0)
                                & (rightStock['day12highIncome'] < 0)
                                & (rightStock['day13highIncome'] < 0)
                                & (rightStock['day14highIncome'] < 0)
                                & (rightStock['day15highIncome'] < 0)
                                & (rightStock['day16highIncome'] < 0)
                                & (rightStock['day17highIncome'] < 0)
                                & (rightStock['day18highIncome'] < 0)
                                & (rightStock['day19highIncome'] < 0)
                                & (rightStock['day20highIncome'] < 0)]
    # 是否生产保本表
    if (stockArgX.save):
        rightStockGood.to_csv(con.detailPath + str(dateUtil.get_date_date()) +
                              dateUtil.get_hour_and_minute_str() + dfname +
                              'day2收盘卖.csv',
                              index=False)
        rightStockNormal.to_csv(
            con.detailPath + str(dateUtil.get_date_date()) +
            dateUtil.get_hour_and_minute_str() + dfname + '原价卖.csv',
            index=False)
        rightStockLoss.to_csv(con.detailPath + str(dateUtil.get_date_date()) +
                              dateUtil.get_hour_and_minute_str() + dfname +
                              '割肉卖.csv',
                              index=False)

    #进行日期分组
    rightStockGood = group_by_date(rightStockGood)
    rightStockNormal = group_by_date(rightStockNormal)
    rightStockLoss = group_by_date(rightStockLoss)

    #分组后计算收益和概率
    totalCount = len(rightStockGood) + len(rightStockNormal) + len(
        rightStockLoss)
    df['day2收盘卖掉概率'] = round(len(rightStockGood) / totalCount, 3)
    df['原价卖概率'] = round(len(rightStockNormal) / totalCount, 3)
    df['割肉卖概率'] = round(len(rightStockLoss) / totalCount, 3)

    df['day2收盘卖掉平均收益'] = round(rightStockGood.day2closeIncome.mean(), 3)
    df['原价卖收益'] = 0
    df['割肉卖平均亏损'] = round(rightStockLoss.day20closeIncome.mean(), 3)

    df['保本卖法收益'] = round((df['day2收盘卖掉概率'] * (df['day2收盘卖掉平均收益'] + 1)) +
                         df['原价卖概率'] + (df['割肉卖概率'] * (df['割肉卖平均亏损'] + 1)), 3)

    return df
Beispiel #4
0
def get_total_csv_by_year(rightStock, ruleName, stockArgX, totalReportForm):
    rightStockList = []
    '''
    先进行按年划分
    '''

    rightStock1 = rightStock[rightStock['date'] >= '2010-01-01']
    rightStock1 = rightStock1[rightStock1['date'] < '2011-01-01']

    rightStock2 = rightStock[rightStock['date'] >= '2011-01-01']
    rightStock2 = rightStock2[rightStock2['date'] < '2012-01-01']

    rightStock3 = rightStock[rightStock['date'] >= '2012-01-01']
    rightStock3 = rightStock3[rightStock3['date'] < '2013-01-01']

    rightStock4 = rightStock[rightStock['date'] >= '2013-01-01']
    rightStock4 = rightStock4[rightStock4['date'] < '2014-01-01']

    rightStock5 = rightStock[rightStock['date'] >= '2014-01-01']
    rightStock5 = rightStock5[rightStock5['date'] < '2015-01-01']

    rightStock6 = rightStock[rightStock['date'] >= '2015-01-01']
    rightStock6 = rightStock6[rightStock6['date'] < '2016-01-01']

    rightStock7 = rightStock[rightStock['date'] >= '2016-01-01']
    rightStock7 = rightStock7[rightStock7['date'] < '2017-01-01']

    rightStock8 = rightStock[rightStock['date'] >= '2017-01-01']
    rightStock8 = rightStock8[rightStock8['date'] < '2018-01-01']

    rightStockList.append(rightStock)
    rightStockList.append(rightStock1)
    rightStockList.append(rightStock2)
    rightStockList.append(rightStock3)
    rightStockList.append(rightStock4)
    rightStockList.append(rightStock5)
    rightStockList.append(rightStock6)
    rightStockList.append(rightStock7)
    rightStockList.append(rightStock8)

    year = -2
    '''
    再循环添加对应的数据
    '''
    for rightStockX in rightStockList:
        year = year + 1
        if len(rightStockX) > 0:
            ruleNameyear = ruleName + 'year201' + str(year)
            # 如果是全部的rightStock 则重新命名为 year
            if (year == -1):
                ruleNameyear = ruleName + 'year'
            ruleNamelist = [ruleNameyear]
            totalProfit = pd.DataFrame({'rule': ruleNamelist})

            totalProfit = add_right_count(totalProfit, rightStockX)
            rightStockX = group_by_date(rightStockX)
            # 是否生产中间表
            if (stockArgX.mean):
                rightStockX.to_csv(
                    con.detailPath + str(dateUtil.get_date_date()) +
                    dateUtil.get_hour_and_minute_str() + ruleNameyear +
                    'mean.csv',
                    index=False)
            # 增加筛选规则对应的样本数量和百分百显示
            totalProfit = add_right_count_by_date(totalProfit, rightStockX)
            # 计算并且增加 评价收益
            totalProfit = add_income_mean(totalProfit, rightStockX)
            totalProfit = add_days_income_percent(totalProfit, rightStockX)
            totalReportForm = totalReportForm.append(totalProfit)
            # 清除临时detail缓存
            del rightStockX
            # 马上重置垃圾清除器
            gc.collect()
    del rightStock
    # 马上重置垃圾清除器
    gc.collect()
    return totalReportForm
Beispiel #5
0
def generate_report_form(ChooseCombinations, ruleNumListMust, ruleList,
                         stockCashList, stockArgX):
    # 声明最终的total报表
    totalReportForm = pd.DataFrame()

    # 规则实例数量
    exampleCount = 0
    # 1.循环所有的规则数组组合
    for inList in ChooseCombinations:
        # 2.循环规则数组中的每一个,
        for brackets in inList:
            # 将必选规则添加到规则中
            brackets = list(brackets) + ruleNumListMust
            # 对形成的规则排序
            brackets.sort()
            # 为规则前面加rule前缀
            ruleName = 'rule'
            # 3.循环每一个数组中的每一个规则
            for ruleNum in brackets:
                # 4.循环规则列表中的每一个规则属性和数组中的规则匹配,来生成规则命名
                for rule in ruleList:
                    if (ruleNum == rule.num):
                        # 如果规则定义为Ture增加+号标记
                        if (rule.tf == True):
                            ruleName = ruleName + '+' + str(ruleNum)
                        # 如果规则定义为Ture增加-号标记
                        if (rule.tf == False):
                            ruleName = ruleName + '-' + str(ruleNum)
            # 规则实例数量+1
            exampleCount = exampleCount + 1
            print('开始生成规则' + ruleName + '的数据!当前是第' + str(exampleCount) +
                  '种规则.')
            # 打印当前时间
            dateUtil.print_date()
            '''
            开始生成复合规则的股票数据,根据股票标记与规则一一对应筛选
            '''
            detailTempList = get_TF_stock(stockCashList, brackets, ruleList)
            if (len(detailTempList) <= 0):
                continue
            '''
            开始生成detail.csv报表
            '''
            tempDetail = view.generate_detail_csv(detailTempList, stockArgX,
                                                  ruleName)

            if (stockArgX.groupByDaysTF):
                '''
                开始生成某规则的收益total数据
                '''
                totalReportForm = view.get_total_csv_by_year(
                    tempDetail, ruleName, stockArgX, totalReportForm)
            else:
                '''
                开始生成某规则的收益total数据
                '''
                totalProfit = view.get_total_csv(tempDetail, ruleName,
                                                 stockArgX)
                '''
                开始按年生成某规则的收益total数据
                '''
                # 将某规则的收益total数据添加到total报表中
                totalReportForm = totalReportForm.append(totalProfit)

    if (len(totalReportForm) > 0):
        print('筛选成功,生成totalcsv文件开始!')
        dateUtil.print_date()
        # 最终生成csv命名
        totalCsvName = stockArgX.totalCsvName
        totalReportForm.to_csv(con.detailPath + str(dateUtil.get_date_date()) +
                               dateUtil.get_hour_and_minute_str() + 'total' +
                               totalCsvName + '.csv',
                               index=False)
        print('生成csv文件结束!')
        stockArgX.TFHaveResult = True
    if (len(totalReportForm) == 0):
        stockArgX.TFHaveResult = False