Exemple #1
0
def updateMACD():
    sql = unicode(
        "select code,date,macd,dif,dea from s_stock_tech where MACD=0 and dif=0 and dea=0 ORDER by code,date asc")
    data = select(sql)

    if len(data) <= 0:
        writeLog(unicode("没有获取到无效MACD指标数据"))
        return

    for item in data:
        code = item[0]
        date = item[1]
        try:
            data = getMACD(code, date)
            if data is None:
                continue

            updateSql = unicode(
                "update s_stock_tech set macd={0},dif={1},dea={2} where code='{3}' and date='{4}'").format(
                data[0],
                data[1],
                data[2],
                code,
                date)
            execute(updateSql)
        except Exception, e:
            writeErrorLog(unicode("更新MACD指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
def updateBiasData(startDate):
    stockList = select(
        unicode("SELECT code,name from s_stock_info")
    )  # loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/goodStockList.json")))
    for stock in stockList:
        code = stock[0]
        writeLog(unicode("开始更新股票:{0},名称:{1}的Bias数据").format(code, stock[1]))

        sql = unicode(
            "SELECT code,date,closePrice from s_stock where code='{0}' and date>'{1}' ORDER BY timestamp asc"
        ).format(code, startDate)
        data = select(sql)
        MA6 = calculateMA(6, data)
        MA12 = calculateMA(12, data)
        MA24 = calculateMA(24, data)

        for i in range(0, len(data)):
            date = data[i][1]
            closePrice = float(data[i][2])
            MA6data = float(MA6.get(date))
            MA12data = float(MA12.get(date))
            MA24data = float(MA24.get(date))

            bias1 = 0 if MA6data == 0 else round(
                (closePrice - MA6data) * 100 / MA6data, 2)
            bias2 = 0 if MA12data == 0 else round(
                (closePrice - MA12data) * 100 / MA12data, 2)
            bias3 = 0 if MA24data == 0 else round(
                (closePrice - MA24data) * 100 / MA24data, 2)

            updateSql = unicode(
                "update s_stock set bias1={0},bias2={1},bias3={2} where code='{3}' and date='{4}'"
            ).format(bias1, bias2, bias3, code, date)
            execute(updateSql)
Exemple #3
0
def saveStockInfoToDb(info, date):
    checkExistSql = unicode(
        "select count(*) from s_stock where code='{0}' and date='{1}'").format(
            info['code'], date)
    count = select(checkExistSql, False)[0]

    if count > 0:
        updateSql = unicode(
            "update s_stock set limitUp={2},limitDown={3},avgPrice={4},volume={5},amount='{6}',highPrice={7},lowPrice={8},openPrice={9},closePrice={10},changePercent={11},changeAmount={12},turnOverRatio={13},QRR={14}, totalValue={15},circulatedValue={16},PE={17},PTB={18},internalPan={19},externalPan={20} where code='{0}' and date='{1}'"
        ).format(info['code'], date, info['limitUp'], info['limitDown'],
                 info['avgPrice'], info['volume'], info['amount'],
                 info['highPrice'], info['lowPrice'], info['openPrice'],
                 info['closePrice'], info['changePercent'],
                 info['changeAmount'], info['turnOverRatio'], info['QRR'],
                 info['totalValue'], info['circulatedValue'], info['PE'],
                 info['PTB'], info['internalPan'], info['externalPan'])
        execute(updateSql)
    else:
        insertSql = unicode(
            "insert into s_stock(code,date,timestamp,limitUp,limitDown,avgPrice,volume,amount,highPrice,lowPrice,openPrice,closePrice,changePercent,changeAmount,turnOverRatio,QRR,totalValue,circulatedValue,PE,PTB,internalPan,externalPan) VALUES ('{0}','{1}',{2},{3},{4},{5},{6},'{7}',{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21})"
        ).format(info['code'], date,
                 int(time.mktime(time.strptime(date,
                                               '%Y-%m-%d'))), info['limitUp'],
                 info['limitDown'], info['avgPrice'], info['volume'],
                 info['amount'], info['highPrice'], info['lowPrice'],
                 info['openPrice'], info['closePrice'], info['changePercent'],
                 info['changeAmount'], info['turnOverRatio'], info['QRR'],
                 info['totalValue'], info['circulatedValue'], info['PE'],
                 info['PTB'], info['internalPan'], info['externalPan'])
        execute(insertSql)
Exemple #4
0
def updateMAData(startDate):
    stockList = select(
        unicode("SELECT code,name from s_stock_info")
    )  # loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/goodStockList.json")))
    for stock in stockList:
        code = stock[0]
        writeLog(unicode("开始更新股票:{0},名称:{1}的均线数据").format(code, stock[1]))
        sql = unicode(
            "SELECT code,date,closePrice from s_stock where code='{0}' and date>'{1}' ORDER BY timestamp asc"
        ).format(code, startDate)
        data = select(sql)
        MA5 = calculateMA(5, data)
        MA10 = calculateMA(10, data)
        MA20 = calculateMA(20, data)
        MA30 = calculateMA(30, data)
        MA60 = calculateMA(60, data)
        MA120 = calculateMA(120, data)
        MA250 = calculateMA(250, data)

        for i in range(0, len(data)):
            date = data[i][1]
            updateSql = unicode(
                "update s_stock set MA5={0},MA10={1},MA20={2},MA30={3},MA60={4},MA120={5},MA250={6} where code='{7}' and date='{8}'"
            ).format(MA5.get(date), MA10.get(date), MA20.get(date),
                     MA30.get(date), MA60.get(date), MA120.get(date),
                     MA250.get(date), code, date)
            execute(updateSql)
Exemple #5
0
def saveStockIndexToDb(stockIndexInfo, date):
    checkExistSql = unicode(
        "select count(*) from s_index where code='{0}' and date='{1}'").format(
            stockIndexInfo.code, date)
    count = select(checkExistSql, False)[0]
    if count > 0:
        updateSql = unicode(
            "update s_index set name='{0}',`index`={1},upNumber={2},downNumber={3},equalNumber={4},highIndex={7},lowIndex={8},openIndex={9},changePercent={10},changeAmount={11},volume={12},amount='{13}',turnOverRatio={14},internalPan={15},externalPan={16},upDownPercent={17},avgPE={18} where code='{5}' and date='{6}'"
        ).format(stockIndexInfo.name, stockIndexInfo.index,
                 stockIndexInfo.upNumber, stockIndexInfo.downNumber,
                 stockIndexInfo.equalNumber, stockIndexInfo.code, date,
                 stockIndexInfo.high, stockIndexInfo.low, stockIndexInfo.open,
                 stockIndexInfo.changePercent, stockIndexInfo.changeAmount,
                 stockIndexInfo.volume, stockIndexInfo.amount,
                 stockIndexInfo.turnOverRatio, stockIndexInfo.internalPan,
                 stockIndexInfo.externalPan, stockIndexInfo.upDownPercent,
                 stockIndexInfo.avgPE)
        execute(updateSql)
    else:
        insertSql = unicode(
            "insert into s_index(code,date,name,`index`,upNumber,downNumber,equalNumber,highIndex,lowIndex,openIndex,changePercent,changeAmount,volume,amount,turnOverRatio,internalPan,externalPan,upDownPercent,avgPE,timestamp) VALUES (\'{0}\',\'{1}\',\'{2}\',{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},\'{13}\',{14},{15},{16},{17},{18},{19})"
        ).format(stockIndexInfo.code, date, stockIndexInfo.name,
                 stockIndexInfo.index, stockIndexInfo.upNumber,
                 stockIndexInfo.downNumber, stockIndexInfo.equalNumber,
                 stockIndexInfo.high, stockIndexInfo.low, stockIndexInfo.open,
                 stockIndexInfo.changePercent, stockIndexInfo.changeAmount,
                 stockIndexInfo.volume, stockIndexInfo.amount,
                 stockIndexInfo.turnOverRatio, stockIndexInfo.internalPan,
                 stockIndexInfo.externalPan, stockIndexInfo.upDownPercent,
                 stockIndexInfo.avgPE,
                 int(time.mktime(time.strptime(date, '%Y-%m-%d'))))
        execute(insertSql)
Exemple #6
0
def updateAllMACD():
    sql = unicode("select code from s_stock_info order by code asc")
    stockList = select(sql)

    if stockList is None:
        return

    for stock in stockList:
        sql = unicode(
            "select code,date,macd,dif,dea from s_stock_tech where code='{0}' ORDER by code,date asc limit 1,60000").format(
            stock[0])
        data = select(sql)

        if len(data) <= 0:
            # writeLog(unicode("没有获取到无效MACD指标数据,code:{0}").format(stock[0]))
            continue

        for item in data:
            code = item[0]
            date = item[1]
            try:
                data = getMACD(code, date)
                if data is None:
                    continue

                updateSql = unicode(
                    "update s_stock_tech set macd={0},dif={1},dea={2} where code='{3}' and date='{4}'").format(
                    data[0],
                    data[1],
                    data[2],
                    code,
                    date)
                execute(updateSql)
            except Exception, e:
                writeErrorLog(unicode("更新MACD指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
Exemple #7
0
def updateEXPMA():
    sql = unicode(
        "select code,date from s_stock_tech where EXPMA1=0 and EXPMA2=0 ORDER by code,date asc")
    data = select(sql)

    if len(data) <= 0:
        writeLog(unicode("没有获取到无效EXPMA指标数据"))
        return

    for item in data:
        code = item[0]
        date = item[1]
        try:
            data = getEXPMA(code, date)
            if data is None:
                continue

            updateSql = unicode(
                "update s_stock_tech set EXPMA1={0},EXPMA2={1} where code='{2}' and date='{3}'").format(
                data[0],
                data[1],
                code,
                date)
            execute(updateSql)
        except Exception, e:
            writeErrorLog(unicode("更新EXPMA指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
def updateTodayBiasData():
    stockList = select(
        unicode("SELECT code,name from s_stock_info")
    )  # loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/goodStockList.json")))
    today = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
    for stock in stockList:
        code = stock[0]
        writeLog(unicode("开始更新股票:{0},名称:{1} 今日的Bias数据").format(code, stock[1]))

        sql = unicode(
            "select code,closePrice from s_stock where code='{0}' and date='{1}'"
        ).format(code, today)
        data = select(sql)
        if (len(data) <= 0):
            writeLog(
                unicode("没有获取到今日的收盘价数据, code: {0}, date: {1}").format(
                    code, today))
            continue

        closePrice = float(data[0][1])
        MA6 = float(getMAData(6, code, today))
        MA12 = float(getMAData(12, code, today))
        MA24 = float(getMAData(24, code, today))

        bias1 = 0 if MA6 == 0 else round((closePrice - MA6) * 100 / MA6, 2)
        bias2 = 0 if MA12 == 0 else round((closePrice - MA12) * 100 / MA12, 2)
        bias3 = 0 if MA24 == 0 else round((closePrice - MA24) * 100 / MA24, 2)

        updateSql = unicode(
            "update s_stock set bias1={0},bias2={1},bias3={2} where code='{3}' and date='{4}'"
        ).format(bias1, bias2, bias3, code, today)
        execute(updateSql)
Exemple #9
0
def saveStockTechToDb(stockTechInfo, date, updateOp):
    if updateOp:
        updateSql = unicode(
            "update s_stock_tech set MACD={2},DIF={3},DEA={4},ADX={5},ADXR={6},INCDI={7},DECDI={8},DMA={9},AMA={10},EXPMA1={11},EXPMA2={12},EMV={13},TRIX={14}, TMA={15},WVAD={16},VR={17},CR={18},AR={19},BR={20},PSY={21},K={22},D={23},J={24},RSI1={25},RSI2={26},MTM={27},MA={28},WR1={29},CCI={30},OBV={31} where code='{0}' and date='{1}'"
        ).format(stockTechInfo.code, date, stockTechInfo.MACD,
                 stockTechInfo.DIF, stockTechInfo.DEA, stockTechInfo.ADX,
                 stockTechInfo.ADXR, stockTechInfo.INCDI, stockTechInfo.DECDI,
                 stockTechInfo.DMA, stockTechInfo.AMA, stockTechInfo.EXPMA1,
                 stockTechInfo.EXPMA2, stockTechInfo.EMV, stockTechInfo.TRIX,
                 stockTechInfo.TMA, stockTechInfo.WVAD, stockTechInfo.VR,
                 stockTechInfo.CR, stockTechInfo.AR, stockTechInfo.BR,
                 stockTechInfo.PSY, stockTechInfo.K, stockTechInfo.D,
                 stockTechInfo.J, stockTechInfo.RSI1, stockTechInfo.RSI2,
                 stockTechInfo.MTM, stockTechInfo.MA, stockTechInfo.WR,
                 stockTechInfo.CCI, stockTechInfo.OBV)
        execute(updateSql)
    else:
        insertSql = unicode(
            "insert into s_stock_tech(code,date,timestamp,MACD,DIF,DEA,ADX,ADXR,INCDI,DECDI,DMA,AMA,EXPMA1,EXPMA2,EMV,TRIX,TMA,WVAD,VR,CR,AR,BR,PSY,K,D,J,RSI1,RSI2,MTM,MA,WR1,CCI,OBV) VALUES (\'{0}\',\'{1}\',{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32})"
        ).format(stockTechInfo.code, date,
                 int(time.mktime(time.strptime(date, '%Y-%m-%d'))),
                 stockTechInfo.MACD, stockTechInfo.DIF, stockTechInfo.DEA,
                 stockTechInfo.ADX, stockTechInfo.ADXR, stockTechInfo.INCDI,
                 stockTechInfo.DECDI, stockTechInfo.DMA, stockTechInfo.AMA,
                 stockTechInfo.EXPMA1, stockTechInfo.EXPMA2, stockTechInfo.EMV,
                 stockTechInfo.TRIX, stockTechInfo.TMA, stockTechInfo.WVAD,
                 stockTechInfo.VR, stockTechInfo.CR, stockTechInfo.AR,
                 stockTechInfo.BR, stockTechInfo.PSY, stockTechInfo.K,
                 stockTechInfo.D, stockTechInfo.J, stockTechInfo.RSI1,
                 stockTechInfo.RSI2, stockTechInfo.MTM, stockTechInfo.MA,
                 stockTechInfo.WR, stockTechInfo.CCI, stockTechInfo.OBV)
        execute(insertSql)
Exemple #10
0
def updateStockMA(code, data, n):
    for i in range(n - 1, len(data)):
        j = i
        sum = 0
        while (i - j < n):
            sum = sum + data[j][1]
            j = j - 1
        avg = round(sum / n, 2)
        sql = unicode(
            "update s_stock set MA{0}={1} where code='{2}' and date='{3}'"
        ).format(n, avg, code, data[i][0])
        execute(sql)
Exemple #11
0
def updateStockChangePercent(code, data):
    for i in range(1, len(data)):
        try:
            changeAmount = data[i][1] - data[i - 1][1]
            changePercent = round(changeAmount * 100 / data[i - 1][1], 2)
            updateSql = unicode(
                "update s_stock set changePercent={0},changeAmount={1} where code='{2}' and date='{3}'"
            ).format(changePercent, changeAmount, code, data[i][0])
            execute(updateSql)
        except Exception, e:
            writeErrorLog(
                unicode("更新涨幅数据失败: code:{0}, i:{1}, date:{2}, closePrice:{3}").
                format(code, i, data[i][0], data[i][1]))
Exemple #12
0
def updataStockBias(code, data, n):
    for i in range(n - 1, len(data)):
        j = i
        sum = 0
        while (i - j < n):
            sum = sum + data[j][1]
            j = j - 1
        avg = round(sum / n, 2)

        todayClosePrice = float(data[i][1])
        bias = 0 if avg == 0 else round((todayClosePrice - avg) * 100 / avg, 2)
        number = 1 if n == 6 else (2 if n == 12 else 3)
        sql = unicode(
            "update s_stock set BIAS{0}={1} where code='{2}' and date='{3}'"
        ).format(number, bias, code, data[i][0])
        execute(sql)
Exemple #13
0
def updateKDJ():
    sql = unicode("SELECT code,date,k,d,j from s_stock_tech where K=0 and d=0 and j=0 ORDER by code,date asc")
    data = select(sql)

    if len(data) <= 0:
        writeLog(unicode("没有获取到无效KDJ指标数据"))
        return

    for item in data:
        code = item[0]
        date = item[1]
        try:
            rsv = getRSV(code, date)
            if rsv is None:
                continue

            techSql = unicode(
                "SELECT k,d,j from s_stock_tech where code='{0}' and date<'{1}' ORDER by date desc limit 1").format(
                code,
                date)
            data = select(techSql)
            yesterdayK = 50
            yesterdayD = 50

            if len(data) > 0:
                yesterdayK = data[0][0]
                yesterdayD = data[0][1]

            todayK = getFloat((rsv + 2 * float(yesterdayK)) / 3)
            todayD = getFloat((todayK + 2 * float(yesterdayD)) / 3)
            todayJ = getFloat(3 * todayK - 2 * todayD)
            writeLog(unicode("更新KDJ指标数据,code:{0},date:{1},todayK:{2},todayD:{3},todayJ:{4},rsv:{5}").format(code, date,
                                                                                                            todayK,
                                                                                                            todayD,
                                                                                                            todayJ,
                                                                                                            rsv))
            print code, date, todayK, todayD, todayJ, rsv

            updateSql = unicode("update s_stock_tech set k={0},d={1},j={2} where code='{3}' and date='{4}'").format(
                todayK,
                todayD,
                todayJ,
                code,
                date)
            execute(updateSql)
        except Exception, e:
            writeErrorLog(unicode("更新KDJ指标数据异常,code:{0}, date:{1}, e:{2}").format(code, date, str(e)))
Exemple #14
0
def saveStockTechForecastToDb(stockTechStatus, forecastInfo, date):
    checkExistSql = unicode(
        "select count(*) from s_stock_forecast where code='{0}' and date='{1}'"
    ).format(stockTechStatus.code, date)
    count = select(checkExistSql, False)[0]
    if count > 0:
        updateSql = unicode(
            "update s_stock_forecast set MACD={2},DMI={3},DMA={4},EXPMA={5},EMV={6},TRIX={7},WVAD={8},VR={9},CR={10},AR={11},PSY={12},KDJ={13},RSI={14}, MTM={15},WR={16},CCI={17},OBV={18},bulls={19},bears={20},notsure={21},BGB={22},BGB_DIFF4={23},TrendMax={24},EnergyMax={25},OBOSMax={26},TrendEnergyShow={27},TrendOBOSShow={28},EnergyOBOSShow={29},TrendEnergyOBOSShow={30},TEOBOS_BGBShow={31},TE_BGBShow={32},EOBOS_BGBShow={33},TOBOS_BGBShow={34},AllBulls={35},AllBulls_DIFF4={36} where code='{0}' and date='{1}'"
        ).format(
            stockTechStatus.code, date, stockTechStatus.MACD,
            stockTechStatus.DMI, stockTechStatus.DMA, stockTechStatus.EXPMA,
            stockTechStatus.EMV, stockTechStatus.TRIX, stockTechStatus.WVAD,
            stockTechStatus.VR, stockTechStatus.CR, stockTechStatus.AR,
            stockTechStatus.PSY, stockTechStatus.KDJ, stockTechStatus.RSI,
            stockTechStatus.MTM, stockTechStatus.WR, stockTechStatus.CCI,
            stockTechStatus.OBV, stockTechStatus.bulls, stockTechStatus.bears,
            stockTechStatus.notsure, forecastInfo.BGB, forecastInfo.BGB_DIFF4,
            forecastInfo.TrendMax, forecastInfo.EnergyMax,
            forecastInfo.OBOSMax, forecastInfo.TrendEnergyShow,
            forecastInfo.TrendOBOSShow, forecastInfo.EnergyOBOSShow,
            forecastInfo.TrendEnergyOBOSShow, forecastInfo.TEOBOS_BGBShow,
            forecastInfo.TE_BGBShow, forecastInfo.EOBOS_BGBShow,
            forecastInfo.TOBOS_BGBShow, forecastInfo.AllBulls,
            forecastInfo.AllBulls_DIFF4)
        execute(updateSql)
    else:
        insertSql = unicode(
            "insert into s_stock_forecast(code,date,timestamp,MACD,DMI,DMA,EXPMA,EMV,TRIX,WVAD,VR,CR,AR,PSY,KDJ,RSI,MTM,WR,CCI,OBV,bulls,bears,notsure,BGB,BGB_DIFF4,TrendMax,EnergyMax,OBOSMax,TrendEnergyShow,TrendOBOSShow,EnergyOBOSShow,TrendEnergyOBOSShow,TEOBOS_BGBShow,TE_BGBShow,EOBOS_BGBShow,TOBOS_BGBShow,AllBulls,AllBulls_DIFF4) VALUES (\'{0}\',\'{1}\',{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33},{34},{35},{36},{37})"
        ).format(
            stockTechStatus.code, date,
            int(time.mktime(time.strptime(date, '%Y-%m-%d'))),
            stockTechStatus.MACD, stockTechStatus.DMI, stockTechStatus.DMA,
            stockTechStatus.EXPMA, stockTechStatus.EMV, stockTechStatus.TRIX,
            stockTechStatus.WVAD, stockTechStatus.VR, stockTechStatus.CR,
            stockTechStatus.AR, stockTechStatus.PSY, stockTechStatus.KDJ,
            stockTechStatus.RSI, stockTechStatus.MTM, stockTechStatus.WR,
            stockTechStatus.CCI, stockTechStatus.OBV, stockTechStatus.bulls,
            stockTechStatus.bears, stockTechStatus.notsure, forecastInfo.BGB,
            forecastInfo.BGB_DIFF4, forecastInfo.TrendMax,
            forecastInfo.EnergyMax, forecastInfo.OBOSMax,
            forecastInfo.TrendEnergyShow, forecastInfo.TrendOBOSShow,
            forecastInfo.EnergyOBOSShow, forecastInfo.TrendEnergyOBOSShow,
            forecastInfo.TEOBOS_BGBShow, forecastInfo.TE_BGBShow,
            forecastInfo.EOBOS_BGBShow, forecastInfo.TOBOS_BGBShow,
            forecastInfo.AllBulls, forecastInfo.AllBulls_DIFF4)
        execute(insertSql)
Exemple #15
0
def updateTodayMAData():
    stockList = select(
        unicode("SELECT code,name from s_stock_info")
    )  # loadJsonConfig(os.path.abspath(os.path.join(os.getcwd(), "../config/goodStockList.json")))
    for stock in stockList:
        code = stock[0]
        writeLog(unicode("开始更新股票:{0},名称:{1} 今日的均线数据").format(code, stock[1]))
        MA5 = getMAData(5, code)
        MA10 = getMAData(10, code)
        MA20 = getMAData(20, code)
        MA30 = getMAData(30, code)
        MA60 = getMAData(60, code)
        MA120 = getMAData(120, code)
        MA250 = getMAData(250, code)

        updateSql = unicode(
            "update s_stock set MA5={0},MA10={1},MA20={2},MA30={3},MA60={4},MA120={5},MA250={6} where code='{7}' and date='{8}'"
        ).format(MA5, MA10, MA20, MA30, MA60, MA120, MA250, code,
                 datetime.now().strftime('%Y-%m-%d'))
        execute(updateSql)
Exemple #16
0
def updateStockHistoryInfoByTHS(stockList):
    for stock in stockList:
        code = stock[0]
        i = 2010
        thisYear = datetime.now().year
        while (i <= thisYear):
            # time.sleep(1)
            infos = getStockInfos(code, i)
            if infos is None:
                continue

            for date in infos:
                open = infos.get(date).get('open')
                close = infos.get(date).get('close')
                high = infos.get(date).get('high')
                low = infos.get(date).get('low')
                volume = infos.get(date).get('volume')
                amount = infos.get(date).get('amount')

                checkExistSql = unicode(
                    "select count(*) from s_stock where code='{0}' and date='{1}'"
                ).format(code, date)
                count = select(checkExistSql, False)[0]
                if count > 0:
                    updateSql = unicode(
                        "update s_stock set volume={2},highPrice={3},lowPrice={4},openPrice={5},closePrice={6},amount='{7}' where code='{0}' and date='{1}'"
                    ).format(code, date, volume, high, low, open, close,
                             amount)
                    execute(updateSql)
                    print code, date, updateSql
                else:
                    insertSql = unicode(
                        "insert into s_stock(code,date,timestamp,volume,highPrice,lowPrice,openPrice,closePrice,amount) VALUES ('{0}','{1}',{2},{3},{4},{5},{6},{7},'{8}')"
                    ).format(code, date,
                             int(time.mktime(time.strptime(date, '%Y-%m-%d'))),
                             volume, high, low, open, close, amount)
                    execute(insertSql)
                    print code, date, insertSql
            i = i + 1
Exemple #17
0
def saveStockTechToDb(code, date, macd_kdj_dmi_expmaDic, emv_trix_wvadDic,
                      vr_cr_arbrDic, psy_rsi_mtmDic, wr_cci_obvDic,
                      dma_boll_biasDic):
    checkExistSql = unicode(
        "select count(*) from s_stock_tech where code='{0}' and date='{1}'"
    ).format(code, date)
    count = select(checkExistSql, False)[0]

    if count > 0:
        updateSql = unicode(
            "update s_stock_tech set MACD={2},DIF={3},DEA={4},ADX={5},ADXR={6},INCDI={7},DECDI={8},DMA={9},AMA={10},EXPMA1={11},EXPMA2={12},EMV={13},TRIX={14}, TMA={15},WVAD={16},VR={17},CR={18},AR={19},BR={20},PSY={21},K={22},D={23},J={24},RSI1={25},RSI2={26},MTM={27},MA={28},WR1={29},CCI={30},OBV={31},RSI3={32},WR2={33},MID={34},UPP={35},LOW={36},BIAS1={37},BIAS2={38},BIAS3={39} where code='{0}' and date='{1}'"
        ).format(code, date, getDicValue(macd_kdj_dmi_expmaDic, 'MACD'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'DIF'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'DEA'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'ADX'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'ADXR'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'PDI'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'MDI'),
                 getDicValue(dma_boll_biasDic, 'DMA'),
                 getDicValue(dma_boll_biasDic, 'AMA'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'EXP1'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'EXP2'),
                 getDicValue(emv_trix_wvadDic, 'EMV'),
                 getDicValue(emv_trix_wvadDic, 'TRIX'),
                 getDicValue(emv_trix_wvadDic, 'TMA'),
                 getDicValue(emv_trix_wvadDic, 'WVAD'),
                 getDicValue(vr_cr_arbrDic, 'VR'),
                 getDicValue(vr_cr_arbrDic, 'CR'),
                 getDicValue(vr_cr_arbrDic, 'AR'),
                 getDicValue(vr_cr_arbrDic, 'BR'),
                 getDicValue(psy_rsi_mtmDic, 'PSY'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'K'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'D'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'J'),
                 getDicValue(psy_rsi_mtmDic, 'RSI1'),
                 getDicValue(psy_rsi_mtmDic, 'RSI2'),
                 getDicValue(psy_rsi_mtmDic, 'MTM'),
                 getDicValue(psy_rsi_mtmDic, 'MTMMA'),
                 getDicValue(wr_cci_obvDic, 'WR1'),
                 getDicValue(wr_cci_obvDic, 'CCI'),
                 getDicValue(wr_cci_obvDic, 'OBV'),
                 getDicValue(psy_rsi_mtmDic, 'RSI3'),
                 getDicValue(wr_cci_obvDic, 'WR2'),
                 getDicValue(dma_boll_biasDic, 'MID'),
                 getDicValue(dma_boll_biasDic, 'UPP'),
                 getDicValue(dma_boll_biasDic, 'LOW'),
                 getDicValue(dma_boll_biasDic, 'BIAS1'),
                 getDicValue(dma_boll_biasDic, 'BIAS2'),
                 getDicValue(dma_boll_biasDic, 'BIAS3'))
        # execute(updateSql)
    else:
        insertSql = unicode(
            "insert into s_stock_tech(code,date,timestamp,MACD,DIF,DEA,ADX,ADXR,INCDI,DECDI,DMA,AMA,EXPMA1,EXPMA2,EMV,TRIX,TMA,WVAD,VR,CR,AR,BR,PSY,K,D,J,RSI1,RSI2,MTM,MA,WR1,CCI,OBV,RSI3,WR2,MID,UPP,LOW,BIAS1,BIAS2,BIAS3) VALUES (\'{0}\',\'{1}\',{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26},{27},{28},{29},{30},{31},{32},{33},{34},{35},{36},{37},{38},{39},{40})"
        ).format(code, date, int(time.mktime(time.strptime(date, '%Y-%m-%d'))),
                 getDicValue(macd_kdj_dmi_expmaDic, 'MACD'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'DIF'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'DEA'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'ADX'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'ADXR'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'PDI'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'MDI'),
                 getDicValue(dma_boll_biasDic, 'DMA'),
                 getDicValue(dma_boll_biasDic, 'AMA'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'EXP1'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'EXP2'),
                 getDicValue(emv_trix_wvadDic, 'EMV'),
                 getDicValue(emv_trix_wvadDic, 'TRIX'),
                 getDicValue(emv_trix_wvadDic, 'TMA'),
                 getDicValue(emv_trix_wvadDic, 'WVAD'),
                 getDicValue(vr_cr_arbrDic, 'VR'),
                 getDicValue(vr_cr_arbrDic, 'CR'),
                 getDicValue(vr_cr_arbrDic, 'AR'),
                 getDicValue(vr_cr_arbrDic, 'BR'),
                 getDicValue(psy_rsi_mtmDic, 'PSY'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'K'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'D'),
                 getDicValue(macd_kdj_dmi_expmaDic, 'J'),
                 getDicValue(psy_rsi_mtmDic, 'RSI1'),
                 getDicValue(psy_rsi_mtmDic, 'RSI2'),
                 getDicValue(psy_rsi_mtmDic, 'MTM'),
                 getDicValue(psy_rsi_mtmDic, 'MTMMA'),
                 getDicValue(wr_cci_obvDic, 'WR1'),
                 getDicValue(wr_cci_obvDic, 'CCI'),
                 getDicValue(wr_cci_obvDic, 'OBV'),
                 getDicValue(psy_rsi_mtmDic, 'RSI3'),
                 getDicValue(wr_cci_obvDic, 'WR2'),
                 getDicValue(dma_boll_biasDic, 'MID'),
                 getDicValue(dma_boll_biasDic, 'UPP'),
                 getDicValue(dma_boll_biasDic, 'LOW'),
                 getDicValue(dma_boll_biasDic, 'BIAS1'),
                 getDicValue(dma_boll_biasDic, 'BIAS2'),
                 getDicValue(dma_boll_biasDic, 'BIAS3'))
        execute(insertSql)
Exemple #18
0
sys.path.insert(0, os.path.abspath(os.path.join(os.getcwd(), "..")))

from db.MysqlUtil import select, initMysql, execute
from stock.StockInfo import getPE
'''os.system("python AvgLinePriceCheckTask.py &")  # 获取每日均线数据,工作日20点以后获取
os.system("python StockIndexTask.py &")  # 获取每日各个主要指数数据,工作日19点以后获取
os.system("python StockTechTask.py &")  # 获取每日各个股票的技术指标数据,工作日22点以后获取
os.system("python StockInfoTask.py &")  # 获取每日各个股票的行情数据,工作日19点以后获取
os.system("python UpdateMADataTask.py &") #更新今日均线数据,工作日21点以后获取
os.system("python UpdateBiasDataTask.py &") #更新今日bias数据,工作日21点以后获取
os.system("python RealTimeRemindTask.py &") #启动每日提醒任务,工作日9点以后启动
os.system("python DownPercentRemindTask.py &") #启动每日涨跌幅提醒任务,工作日9点以后启动
os.system("python StockChooseTask.py &")  # 每周更新PEG数据
'''

# 更新股票名称

initMysql()
sql = unicode("SELECT code from s_stock_tech GROUP by code order by code asc")
data = select(sql)

for item in data:
    code = item[0]
    name = getPE(code)[0]
    print code, name
    addSql = unicode(
        "insert into s_stock_info(code,name) values('{0}','{1}')").format(
            code, name)
    execute(addSql)
Exemple #19
0
def updateForecastResult(date):
    writeLog(unicode("beginUpdateForecastResult, date:{0}").format(date))
    selectSql = unicode(
        "select f.*,s.changePercent from s_stock_forecast as f INNER JOIN s_stock as s ON (s.code=f.code and s.date=f.date) where f.date='{0}'"
    ).format(date)
    data = select(selectSql)

    weekday = datetime.today().weekday()
    diff = 1 if weekday >= 1 and weekday <= 4 else (
        3 if weekday == 0 else weekday - 3)
    lastDay = (datetime.now() - timedelta(days=diff)).strftime('%Y-%m-%d')

    indexType = [
        '', '', 'MACD', 'DMI', 'DMA', 'EXPMA', 'EMV', 'TRIX', 'WVAD', 'VR',
        'CR', 'AR', 'PSY', 'KDJ', 'RSI', 'MTM', 'WR', 'CCI', 'OBV', '', '', '',
        '', 'BGB'
    ]
    upIndexType = [
        'BGB_DIFF4', 'TrendMax', 'EnergyMax', 'OBOSMax', 'TrendEnergyShow',
        'TrendOBOSShow', 'EnergyOBOSShow', 'TrendEnergyOBOSShow',
        'TEOBOS_BGBShow', 'TE_BGBShow', 'EOBOS_BGBShow', 'TOBOS_BGBShow',
        'AllBulls', 'AllBulls_DIFF4'
    ]

    forecastCountField = ''
    forecastCountValue = ''
    dictCount = {}
    for row in data:
        dictIndex = {}
        upDictIndex = {}
        changePercent = row[len(row) - 1]

        for i in range(0, 24):
            if (indexType[i] == ''):
                continue

            if (row[i] == 0):
                if (changePercent > 0):
                    # 预测上涨,实际上涨
                    dictIndex[indexType[i]] = 1
                elif (changePercent < 0):
                    # 预测上涨,实际下跌
                    dictIndex[indexType[i]] = 2
                else:
                    # 预测上涨,实际持平
                    dictIndex[indexType[i]] = 3
            elif (row[i] == 1):
                if (changePercent > 0):
                    # 预测下跌,实际上涨
                    dictIndex[indexType[i]] = 4
                elif (changePercent < 0):
                    # 预测下跌,实际下跌
                    dictIndex[indexType[i]] = 5
                else:
                    # 预测下跌,实际持平
                    dictIndex[indexType[i]] = 6

        for i in range(24, len(row) - 1):
            if (row[i] == 0):
                j = i - 24
                if (changePercent > 0):
                    # 预测上涨,实际上涨
                    upDictIndex[upIndexType[j]] = 1
                elif (changePercent < 0):
                    # 预测上涨,实际下跌
                    upDictIndex[upIndexType[j]] = 2
                else:
                    # 预测上涨,实际持平
                    upDictIndex[upIndexType[j]] = 3

        forecastResultField = ''
        forecastResultValue = ''

        stockForecastCountField = ''
        stockForecastCountValue = ''

        lastDayStockForecastCountData = select(
            unicode(
                "select * from s_stock_forecast_count where code='{0}' and date='{1}'"
            ).format(row[0], lastDay))

        i = 1
        for index in indexType:
            if (index == ''):
                continue
            forecastResultField = forecastResultField + index + ','
            forecastResultValue = forecastResultValue + str(
                dictIndex[index] if dictIndex.has_key(index) else -1) + ','
            stockForecastCountField = "{0}{1}_up_right,{1}_up_total,{1}_down_right,{1}_down_total,".format(
                stockForecastCountField, index)
            if len(lastDayStockForecastCountData) > 0:
                up_right = lastDayStockForecastCountData[0][
                    3 +
                    (i - 1) * 4 + 1] + (1 if dictIndex.has_key(index)
                                        and dictIndex.get(index) == 1 else 0)
                up_total = lastDayStockForecastCountData[0][
                    3 +
                    (i - 1) * 4 + 2] + (1 if dictIndex.has_key(index)
                                        and dictIndex.get(index) <= 3 else 0)
                down_right = lastDayStockForecastCountData[0][
                    3 +
                    (i - 1) * 4 + 3] + (1 if dictIndex.has_key(index)
                                        and dictIndex.get(index) == 5 else 0)
                down_total = lastDayStockForecastCountData[0][
                    3 +
                    (i - 1) * 4 + 4] + (1 if dictIndex.has_key(index)
                                        and dictIndex.get(index) > 3 else 0)
                stockForecastCountValue = "{0}{1},{2},{3},{4},".format(
                    stockForecastCountValue, up_right, up_total, down_right,
                    down_total)
            else:
                up_right = (1 if dictIndex.has_key(index)
                            and dictIndex.get(index) == 1 else 0)
                up_total = (1 if dictIndex.has_key(index)
                            and dictIndex.get(index) <= 3 else 0)
                down_right = (1 if dictIndex.has_key(index)
                              and dictIndex.get(index) == 5 else 0)
                down_total = (1 if dictIndex.has_key(index)
                              and dictIndex.get(index) > 3 else 0)
                stockForecastCountValue = "{0}{1},{2},{3},{4},".format(
                    stockForecastCountValue, up_right, up_total, down_right,
                    down_total)

            forecastCount_up_right = (1 if dictIndex.has_key(index)
                                      and dictIndex.get(index) == 1 else 0)
            forecastCount_up_total = (1 if dictIndex.has_key(index)
                                      and dictIndex.get(index) <= 3 else 0)
            forecastCount_down_right = (1 if dictIndex.has_key(index)
                                        and dictIndex.get(index) == 5 else 0)
            forecastCount_down_total = (1 if dictIndex.has_key(index)
                                        and dictIndex.get(index) > 3 else 0)
            if (dictCount.has_key(index)):
                dictCount[index].up_right = dictCount[
                    index].up_right + forecastCount_up_right
                dictCount[index].up_total = dictCount[
                    index].up_total + forecastCount_up_total
                dictCount[index].down_right = dictCount[
                    index].down_right + forecastCount_down_right
                dictCount[index].down_total = dictCount[
                    index].down_total + forecastCount_down_total
            else:
                dictCount[index] = IndexResult(forecastCount_up_right,
                                               forecastCount_up_total,
                                               forecastCount_down_right,
                                               forecastCount_down_total)

            i = i + 1

        j = 1
        for index in upIndexType:
            if (index == ''):
                continue
            forecastResultField = forecastResultField + index + ','
            forecastResultValue = forecastResultValue + str(
                upDictIndex[index] if upDictIndex.has_key(index) else -1) + ','
            stockForecastCountField = "{0}{1}_up_right,{1}_up_total,".format(
                stockForecastCountField, index)
            if len(lastDayStockForecastCountData) > 0:
                up_right = lastDayStockForecastCountData[0][
                    75 +
                    (j - 1) * 2 + 1] + (1 if upDictIndex.has_key(index)
                                        and upDictIndex.get(index) == 1 else 0)
                up_total = lastDayStockForecastCountData[0][
                    75 +
                    (j - 1) * 2 + 2] + (1 if upDictIndex.has_key(index) else 0)
                stockForecastCountValue = "{0}{1},{2},".format(
                    stockForecastCountValue, up_right, up_total)
            else:
                up_right = (1 if upDictIndex.has_key(index)
                            and upDictIndex.get(index) == 1 else 0)
                up_total = (1 if upDictIndex.has_key(index) else 0)
                stockForecastCountValue = "{0}{1},{2},".format(
                    stockForecastCountValue, up_right, up_total)

            forecastCount_up_right = (1 if upDictIndex.has_key(index)
                                      and upDictIndex.get(index) == 1 else 0)
            forecastCount_up_total = (1 if upDictIndex.has_key(index) else 0)
            if (dictCount.has_key(index)):
                dictCount[index].up_right = dictCount[
                    index].up_right + forecastCount_up_right
                dictCount[index].up_total = dictCount[
                    index].up_total + forecastCount_up_total
            else:
                dictCount[index] = IndexResult(forecastCount_up_right,
                                               forecastCount_up_total, 0, 0)

            j = j + 1

        forecastResultSql = unicode(
            "insert into s_stock_forecast_result(code,date,timestamp,{0}) values('{1}','{2}',{3},{4})"
        ).format(forecastResultField[:-1], row[0], row[1], row[22],
                 forecastResultValue[:-1])
        stockForecastCountSql = unicode(
            "insert into s_stock_forecast_count(code,date,timestamp,updateTS,{0}) values('{1}','{2}',{3},{4},{5})"
        ).format(stockForecastCountField[:-1], row[0], row[1], row[22],
                 int(time.time()), stockForecastCountValue[:-1])

        execute(forecastResultSql)
        execute(stockForecastCountSql)

    lastDayForecastCountData = select(
        unicode("select * from s_forecast_count where date='{0}'").format(
            lastDay))
    i = 1
    for index in indexType:
        if (index == ''):
            continue

        forecastCountField = "{0}{1}_up_right,{1}_up_total,{1}_down_right,{1}_down_total,".format(
            forecastCountField, index)
        if len(lastDayForecastCountData) > 0:
            forecastCount_up_right = lastDayForecastCountData[0][
                2 + (i - 1) * 4 + 1] + (dictCount.get(index).up_right
                                        if dictCount.has_key(index) else 0)
            forecastCount_up_total = lastDayForecastCountData[0][
                2 + (i - 1) * 4 + 2] + (dictCount.get(index).up_total
                                        if dictCount.has_key(index) else 0)
            forecastCount_down_right = lastDayForecastCountData[0][
                2 + (i - 1) * 4 + 3] + (dictCount.get(index).down_right
                                        if dictCount.has_key(index) else 0)
            forecastCount_down_total = lastDayForecastCountData[0][
                2 + (i - 1) * 4 + 4] + (dictCount.get(index).down_total
                                        if dictCount.has_key(index) else 0)
            forecastCountValue = "{0}{1},{2},{3},{4},".format(
                forecastCountValue, forecastCount_up_right,
                forecastCount_up_total, forecastCount_down_right,
                forecastCount_down_total)
        else:
            forecastCount_up_right = (dictCount.get(index).up_right
                                      if dictCount.has_key(index) else 0)
            forecastCount_up_total = (dictCount.get(index).up_total
                                      if dictCount.has_key(index) else 0)
            forecastCount_down_right = (dictCount.get(index).down_right
                                        if dictCount.has_key(index) else 0)
            forecastCount_down_total = (dictCount.get(index).down_total
                                        if dictCount.has_key(index) else 0)
            forecastCountValue = "{0}{1},{2},{3},{4},".format(
                forecastCountValue, forecastCount_up_right,
                forecastCount_up_total, forecastCount_down_right,
                forecastCount_down_total)
        i = i + 1

    j = 1
    for index in upIndexType:
        if (index == ''):
            continue

        forecastCountField = "{0}{1}_up_right,{1}_up_total,".format(
            forecastCountField, index)
        if len(lastDayForecastCountData) > 0:
            forecastCount_up_right = lastDayForecastCountData[0][
                74 + (j - 1) * 2 + 1] + (dictCount.get(index).up_right
                                         if dictCount.has_key(index) else 0)
            forecastCount_up_total = lastDayForecastCountData[0][
                74 + (j - 1) * 2 + 2] + (dictCount.get(index).up_total
                                         if dictCount.has_key(index) else 0)
            forecastCountValue = "{0}{1},{2},".format(forecastCountValue,
                                                      forecastCount_up_right,
                                                      forecastCount_up_total)
        else:
            forecastCount_up_right = (dictCount.get(index).up_right
                                      if dictCount.has_key(index) else 0)
            forecastCount_up_total = (dictCount.get(index).up_total
                                      if dictCount.has_key(index) else 0)
            forecastCountValue = "{0}{1},{2},".format(forecastCountValue,
                                                      forecastCount_up_right,
                                                      forecastCount_up_total)
        j = j + 1

    forecastCountSql = unicode(
        "insert into s_forecast_count(date,timestamp,updateTs,{0}) values('{1}',{2},{3},{4})"
    ).format(forecastCountField[:-1], date,
             int(time.mktime(time.strptime(date, '%Y-%m-%d'))),
             int(time.time()), forecastCountValue[:-1])
    execute(forecastCountSql)
    writeLog(unicode("endUpdateForecastResult, date:{0}").format(date))
Exemple #20
0
def updateOBOS(index, timestamp):
    selectSql = unicode(
        "update s_index set obos=(select count from (SELECT SUM(b.upNumber)-Sum(b.downNumber) as count FROM s_index as b WHERE b.timestamp<={1} and b.code='{0}' ORDER by b.timestamp desc limit 10) as c) where code='{0}' and timestamp={1}"
    ).format(index, timestamp)
    execute(selectSql)