Exemplo n.º 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
Exemplo n.º 2
0
def create_stock_csv(codeStr):
    log.info("当前开始生成代码" + codeStr + "的CSV文件!")
    stock_data = ts.get_k_data(codeStr,
                               start='2010-01-01',
                               end=str(dateUtil.get_date_date()))
    stock_data.to_csv(con.csvPath + codeStr + '.csv',
                      index=False,
                      encoding='gbk')
Exemplo n.º 3
0
def create_all_index_csv():
    for codeStr in con.baseIndexList:
        log.info("当前开始生成指数" + codeStr + "的CSV文件!")
        index_data = ts.get_k_data(codeStr,
                                   index=True,
                                   start='2010-01-01',
                                   end=str(dateUtil.get_date_date()))
        index_data.to_csv(con.indexCsvPath + codeStr + '.csv',
                          index=False,
                          encoding='gbk')
Exemplo n.º 4
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
Exemplo n.º 5
0
def start_stock_generator():
    '''
    启动股票生成器
    '''

    # 定义参数类
    class stockArg:
        # total报表命名
        totalCsvName = ''
        # code信息
        codeInfo = ''

    stockArgX = stockArg()
    '''股票类型过滤'''
    stockArgX.stockTypeTF = False
    '''
    '''
    stockArgX.stockTypeList = []
    ''' 基础参数 '''
    # total表名称
    stockArgX.totalCsvName = '今日筛选'
    # 是否生成明细表开关
    stockArgX.detail = True
    # 是否生成平均表开关
    stockArgX.mean = False
    # 是否生成保本表开关
    stockArgX.save = False
    # 是否分年开关
    stockArgX.groupByDaysTF = False

    # 是否使用开始日期参数开关
    stockArgX.dateBeginTF = False
    # 开始日期参数
    stockArgX.dateBeginRange = str(dateUtil.get_date_date())
    # stockArgX.dateBeginRange = '2018-05-21'
    # 是否使用结束日期参数开关
    stockArgX.dateEndTF = False
    # 结束日期参数
    stockArgX.dateEndRange = '2018-05-21'
    ''' 规则参数 '''
    #成熟规则获取开关,开启时下面定义规则无效
    stockArgX.mustByCsvTF = False
    stockArgX.mustBmustByCsvTFyCsvName = 'gongshiv2'
    stockArgX.mustByCsvRule = [1, 2, 3, 5, 6, 7, 8, 10, 11, 13, 19, 21]
    stockArgX.ruleNumListChoose = [10]
    stockArgX.ruleNumListMust = [1, 8, 11, 13, 21]

    # 大盘规则开关
    stockArgX.indexOpen = False
    ''' 策略参数开始 '''
    # 策略开关
    stockArgX.strategy = False
    '''买入策略'''
    #买入策略二
    # 买入线 开关
    stockArgX.buyLineTF = False
    # 买入线 等待天数
    stockArgX.buyLineWaitDays = 3
    # 买入线 为day0close的加n个百分点
    stockArgX.buyLine = -2
    # 买入线期望修正
    stockArgX.buyLineExpect = 0
    '''卖出策略'''
    # 按收盘卖开关
    stockArgX.sellIncomeByCloseTF = False
    # 按收盘卖day几c
    stockArgX.sellCloseDay = 2

    # 固定收益卖出开关
    stockArgX.sellIncomeTF = False
    # 固定收益卖出参数(百分点)
    stockArgX.sellIncome = 10

    # 按low卖出收益开关
    stockArgX.sellIncomeByLowTF = False
    # 按low卖出收益参数
    stockArgX.sellIncomeByLow = 10

    # 策略止损线开关
    stockArgX.stopLineTF = False
    # 策略止损线(百分点)
    stockArgX.stopLine = -10
    # 策略止损线期望修正(百分点)
    stockArgX.stopLineExpect = 0
    # 策略失败割肉交易日参数(day几 收盘割肉卖)
    stockArgX.cutMeatDay = 15
    ''' 策略参数结束 '''
    ''' 是否发送邮件开关'''
    stockArgX.TFmail = False
    #是否成功筛选
    stockArgX.TFHaveResult = False
    #是否已经赋值第一排名信息
    stockArgX.TFHaveHighStock = False
    startDate = dateUtil.print_date()
    # 执行规则数据生成器方法!
    rule.make_stockData(stockArgX)
    dateUtil.print_end_date(startDate)
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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