Exemplo n.º 1
0
def loadStockList(fileName):
    dictData = {}
    for line in open(fileName):
        line = line.strip().zfill(6)
        if line not in dictData:
            dictData[line] = 1

    saveFileName = os.path.abspath(
        os.path.join(os.getcwd(), "../config/goodStockList.json"))
    backupFileName = os.path.abspath(
        os.path.join(
            os.getcwd(), "../backup/goodStockList_" +
            datetime.now().strftime('%Y-%m-%d') + ".json"))
    if os.path.exists(saveFileName):
        shutil.move(saveFileName, backupFileName)

    writeFile(saveFileName, json.dumps(dictData.keys()))
Exemplo n.º 2
0
def updateNewList():
    oldList = loadJsonConfig(
        os.path.abspath(
            os.path.join(
                os.getcwd(), "../backup/goodStockList_" +
                datetime.now().strftime('%Y-%m-%d') + ".json")))
    dic = {}
    for item in oldList:
        dic[item[0]] = 1

    stockList = select(
        unicode("SELECT code,name from s_stock_info")
    )  # loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/goodStockList.json")))
    newList = []
    for item in stockList:
        if item[0] not in dic:
            newList.append([item[0], item[1]])

    saveFileName = os.path.abspath(
        os.path.join(os.getcwd(), "../config/newStockList.json"))
    writeFile(saveFileName, json.dumps(newList))
Exemplo n.º 3
0
def updateLatestGoodStockListToConfig():
    selectSql = unicode(
        "SELECT  DISTINCT code,name from b_peg where date=(SELECT max(date) FROM b_peg)"
    )
    result = select(selectSql)

    dictData = {}
    data = []
    for row in result:
        code = row[0].zfill(6)
        if code not in dictData:
            dictData[code] = 1
            data.append([code, row[1]])

    saveFileName = os.path.abspath(
        os.path.join(os.getcwd(), "../config/goodStockList.json"))
    backupFileName = os.path.abspath(
        os.path.join(
            os.getcwd(), "../backup/goodStockList_" +
            datetime.now().strftime('%Y-%m-%d') + ".json"))
    if os.path.exists(saveFileName):
        shutil.move(saveFileName, backupFileName)

    writeFile(saveFileName, json.dumps(data))
Exemplo n.º 4
0
def reminder():
    reminderCodeList = open(
        os.path.abspath(os.path.join(os.getcwd(), "../config/reminder.csv")),
        'r').readlines()

    dictReminder = {}
    for i in range(1, len(reminderCodeList)):
        infos = reminderCodeList[i].split(',')
        code = infos[0].strip().zfill(6)
        name = getPE(code)[0]
        maxPrice = float(infos[1])
        minPrice = float(infos[2])
        upPercent = float(infos[3])
        downPercent = float(infos[4])
        receiver = int(infos[5])
        dictReminder[code] = ReminderInfo(maxPrice, minPrice, upPercent,
                                          downPercent, receiver, code, name)

    checkDict = {}  # 用于检查今天是否已经发送过某类信息
    date = datetime.now().strftime('%Y-%m-%d')
    result = []
    result.append(
        unicode("{0},{1},{2},{3},{4},{5}\n").format(
            '股票代码', '股票涨到多少元', '股票跌到多少元', '股票涨幅超过多少(%)', '股票跌幅超过多少(%)',
            '发给谁(0:期待,1:踏雪)').encode('gbk'))

    while 1 == 1:
        if datetime.now().hour >= 16:
            # 更新提醒数据
            for i in range(1, len(reminderCodeList)):
                infos = reminderCodeList[i].split(',')
                code = infos[0].strip().zfill(6)
                name = getPE(code)[0]
                maxPrice = float(infos[1])
                minPrice = float(infos[2])
                upPercent = float(infos[3])
                downPercent = float(infos[4])
                receiver = int(infos[5])

                stockInfo = getStockInfo(code)
                if stockInfo is None:
                    continue

                highPrice = float(stockInfo['highPrice'])
                closePrice = float(stockInfo['closePrice'])
                changePercent = float(stockInfo['changePercent'])
                newMinPrice = getAboveNumber(closePrice * 0.97)
                minPrice = minPrice if minPrice == -1 or changePercent <= 0 or newMinPrice <= minPrice else newMinPrice
                maxPrice = maxPrice if maxPrice == -1 or changePercent <= 0 else getAboveNumber(
                    highPrice * 1.03)
                result.append(
                    unicode("{0},{1},{2},{3},{4},{5}\n").format(
                        code, maxPrice, minPrice, upPercent, downPercent,
                        receiver).encode('gbk'))

            saveFileName = os.path.abspath(
                os.path.join(os.getcwd(), "../config/reminder.csv"))
            writeFile(saveFileName, result)
            writeLog(unicode("止损表已经修正为以下值:{0}").format(result))
            sendMessageToMySelf(unicode("止损表已经修正为以下值:{0}").format(result))
            break

        for key in dictReminder:
            df = ts.get_realtime_quotes(key)
            reminderInfo = dictReminder[key]

            price = float(df['price'].values[0])
            pre_close = float(df['pre_close'].values[0])
            currentUpPercent = 0 if price <= pre_close else round(
                (price - pre_close) * 100 / pre_close, 2)
            currentDownPercent = 0 if price >= pre_close else round(
                (price - pre_close) * 100 / pre_close, 2)

            print key, price, reminderInfo.maxPrice, reminderInfo.minPrice, currentUpPercent, currentDownPercent
            if (price != 0 and reminderInfo.maxPrice != -1
                    and price >= reminderInfo.maxPrice):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'max')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{3},当前价格为:{1},已经超过您预设的{2}').format(
                            key, price, reminderInfo.maxPrice,
                            reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            if (price != 0 and reminderInfo.minPrice != -1
                    and price <= reminderInfo.minPrice):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'min')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{3},当前价格为:{1},已经低于您预设的{2}').format(
                            key, price, reminderInfo.minPrice,
                            reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            if (price != 0 and reminderInfo.upPercent != -1
                    and currentUpPercent >= reminderInfo.upPercent):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'up')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{4},当前价格为:{1},涨幅为:{2}%,已经超过您预设的{3}%'
                    ).format(key, price, currentUpPercent,
                             reminderInfo.upPercent, reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            if (price != 0 and reminderInfo.downPercent != -1
                    and currentDownPercent <= -reminderInfo.downPercent):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'down')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{4},当前价格为:{1},跌幅为:{2}%,已经低于您预设的{3}%'
                    ).format(key, price, currentDownPercent,
                             -reminderInfo.downPercent, reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

        time.sleep(1)
Exemplo n.º 5
0
def reminder():
    reminderCodeList = open(
        os.path.abspath(os.path.join(os.getcwd(), "../config/myreminder.csv")),
        'r').readlines()

    dictReminder = {}
    for i in range(1, len(reminderCodeList)):
        infos = reminderCodeList[i].split(',')
        code = infos[0].strip().zfill(6)
        name = getPE(code)[0]
        maxPrice = float(infos[1])
        minPrice = float(infos[2])
        buyPrice = float(infos[3])
        upPercent = float(infos[4])
        downPercent = float(infos[5])
        receiver = int(infos[6])
        sql = unicode(
            "SELECT changePercent,closePrice,highPrice from s_stock where code='{0}' ORDER by date desc limit 30"
        ).format(code)
        data = select(sql)

        totalChangePercentBefore = 0
        yesterdayChangePercent = 0
        yesterdayHighPrice = 0
        if data is not None:
            yesterdayChangePercent = data[0][0]
            yesterdayHighPrice = float(data[0][2])
            for i in range(1, len(data)):
                changePercent = data[i][0]
                if (changePercent < 0):
                    break
                totalChangePercentBefore = totalChangePercentBefore + changePercent
            totalChangePercentBefore = totalChangePercentBefore + yesterdayChangePercent

        dictReminder[code] = ReminderInfo(maxPrice, minPrice, upPercent,
                                          downPercent, receiver, code, name,
                                          buyPrice, yesterdayHighPrice,
                                          float(yesterdayChangePercent),
                                          float(totalChangePercentBefore))

    checkDict = {}  # 用于检查今天是否已经发送过某类信息
    date = datetime.now().strftime('%Y-%m-%d')
    result = []
    result.append(
        unicode("{0},{1},{2},{3},{4},{5},{6}\n").format(
            '股票代码', '股票涨到多少元', '股票跌到多少元', '买入价', '股票涨幅超过多少(%)', '股票跌幅超过多少(%)',
            '发给谁(0:期待,1:踏雪)').encode('gbk'))

    while 1 == 1:
        if datetime.now().hour >= 16:
            # 更新提醒数据
            for i in range(1, len(reminderCodeList)):
                infos = reminderCodeList[i].split(',')
                code = infos[0].strip().zfill(6)
                maxPrice = float(infos[1])
                minPrice = float(infos[2])
                buyPrice = float(infos[3])
                upPercent = float(infos[4])
                downPercent = float(infos[5])
                receiver = int(infos[6])

                stockInfo = getStockInfo(code)
                if stockInfo is None:
                    continue

                highPrice = float(stockInfo['highPrice'])
                closePrice = float(stockInfo['closePrice'])
                changePercent = float(stockInfo['changePercent'])
                newMinPrice = getAboveNumber(closePrice * 0.958)
                minPrice = minPrice if minPrice == -1 or changePercent <= 0 or newMinPrice <= minPrice else newMinPrice
                maxPrice = maxPrice if maxPrice == -1 or changePercent <= 0 else getAboveNumber(
                    highPrice * 1.03)
                result.append(
                    unicode("{0},{1},{2},{3},{4},{5},{6}\n").format(
                        code, maxPrice, minPrice, buyPrice, upPercent,
                        downPercent, receiver).encode('gbk'))

            saveFileName = os.path.abspath(
                os.path.join(os.getcwd(), "../config/myreminder.csv"))
            writeFile(saveFileName, result)
            sendMessageToMySelf(unicode("止损表已经修正为以下值:{0}").format(result))
            break

        for key in dictReminder:
            df = ts.get_realtime_quotes(key)
            reminderInfo = dictReminder[key]

            price = float(df['price'].values[0])
            highPrice = float(df['high'].values[0])
            pre_close = float(df['pre_close'].values[0])
            todayUpPercent = 0 if price <= pre_close else round(
                (price - pre_close) * 100 / pre_close, 2)
            todayDownPercent = 0 if price >= pre_close else round(
                (price - pre_close) * 100 / pre_close, 2)
            highPercent = round((highPrice - pre_close) * 100 / pre_close, 2)
            currentPercent = round((price - pre_close) * 100 / pre_close, 2)

            if (price <= reminderInfo.buyPrice * 0.88):  # 跌破12%,止损
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'sell12')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{1},成本价:{2}, 当前价格为:{3},跌幅:{4}%').format(
                            key, reminderInfo.name, reminderInfo.buyPrice,
                            price, (price - reminderInfo.buyPrice) /
                            reminderInfo.buyPrice)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            if (price <= reminderInfo.yesterdayHighPrice * 0.95
                    and price > reminderInfo.buyPrice):  # 最高收益回落5%
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'sell5')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{1},成本价:{2}, 当前价格为:{3},昨日最高价:{4}%'
                    ).format(key, reminderInfo.name, reminderInfo.buyPrice,
                             price, reminderInfo.yesterdayHighPrice)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            # 今日最高价与当前价,跌幅超过了4.5
            if (highPercent > 0 and highPercent - currentPercent > 4.5):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'highdown')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{1},今日最高价:{2}, 当前价格为:{3},最高涨幅:{4}%,当前涨幅为:{5}%,两者跌幅已经您预设的{6}%'
                    ).format(key, reminderInfo.name, highPrice, price,
                             highPercent, currentPercent, 4.5)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            # 当前涨幅已经超过预设的涨幅,目前为8%
            if (reminderInfo.upPercent != -1
                    and todayUpPercent >= reminderInfo.upPercent):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'cup')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{4},当前价格为:{1},涨幅为:{2}%,已经超过您预设的{3}%'
                    ).format(key, price, todayUpPercent,
                             reminderInfo.upPercent, reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            # 昨天和当前总跌幅已经超过25%
            if (todayUpPercent + reminderInfo.totalChangePercentLast30Days >=
                    25):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'tup')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{4},当前价格为:{1},累计涨幅为:{2}%,已经超过您预设的{3}%'
                    ).format(
                        key, price, todayUpPercent +
                        reminderInfo.totalChangePercentLast30Days, 25,
                        reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            # 当前跌幅已经超过预设的跌幅,目前为4.2%
            if (reminderInfo.downPercent != -1
                    and todayDownPercent <= -reminderInfo.downPercent):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'cdown')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{4},当前价格为:{1},跌幅为:{2}%,已经低于您预设的{3}%'
                    ).format(key, price, todayDownPercent,
                             -reminderInfo.downPercent, reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

            # 昨天和当前总跌幅已经超过4.2%
            if (todayDownPercent + reminderInfo.yesterdayChangePercent <=
                    -4.2):
                checkKey = unicode('{0}_{1}_{2}').format(date, key, 'tdown')
                if checkDict.has_key(checkKey) == False:
                    message = unicode(
                        '股票代码:{0},名称:{4},当前价格为:{1},昨日和当前总跌幅为:{2}%,已经低于您预设的{3}%'
                    ).format(
                        key, price,
                        todayDownPercent + reminderInfo.yesterdayChangePercent,
                        str(-4.2), reminderInfo.name)
                    writeInfoLog(message)
                    sendMessageToMySelf(
                        message
                    ) if reminderInfo.receiver == 1 else sendMessageToBaby(
                        message)
                    checkDict[checkKey] = True

        time.sleep(1)