def getStockDayData(stockCode, date, flag): stockHq = [] count = 0 url = stockHistoryUrl%(stockCode[2:], stockCode[:2].upper(), (str)((int)(date[4:6])-1), date[6:], date[:4], flag) debugPrint(url) try: reqAccess = urllib.request.urlopen(url) except urllib.error.HTTPError as e: print('Warning !!! can not access %s'%url) print('Error code:', e.code) return stockHq stockDayHq = reqAccess.read().decode('gbk','ignore').split('\n') for data in stockDayHq: try: debugPrint(data) Date, Open, High, Low, Close, Volumn, Adj = data.split(',') if ( Volumn == 'Volume' or Volumn == '000' ): pass else: count += 1 stockHq.append((Date, Open, High, Low, Close, Volumn, Adj)) except ValueError: pass return stockHq
def getVerboseStockHq(stockCode): url = verboseStockURL + stockCode stockHq = urllib.request.urlopen(url).read().decode('gbk','ignore') tmp, stockHq, tmp = stockHq.split('"') stockHq = stockHq.split(',') debugPrint(stockHq) return stockHq
def fetchAllItems(conn): sql = '''SELECT * FROM stockList''' if sql is not None and sql != '': debugPrint(sql) cu = getCursor(conn) cu.execute(sql) return cu.fetchall() else: print("fetchAllItems Error")
def deleteItem(conn, sql, data): if sql is not None and sql != '': if data is not None: cu = getCursor(conn) debugPrint(sql, data) cu.execute(sql, data) conn.commit() else: print("deleteItem Error")
def deleteTable(conn, table): if table is not None and table != '': sql = 'DROP TABLE IF EXISTS ' + table debugPrint(sql) cu = getCursor(conn) cu.execute(sql) conn.commit() else: print("deleteTable Error")
def updateItem(conn, data): sql = '''UPDATE stockList SET sellTime = ?, sellPrice = ?,sellColumn = ?,inhandFlag = ? WHERE id = ?''' if sql is not None and sql != '': if data is not None: cu = getCursor(conn) debugPrint(sql, data) cu.execute(sql, data) conn.commit() else: print("updateItem Error")
def insertItem(conn, data): sql = '''INSERT INTO stockList values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''' if sql is not None and sql != '': if data is not None: cu = getCursor(conn) debugPrint(sql, data) cu.execute(sql, data) conn.commit() else: print("insertItem2Table Error")
def monitorInHandStockHqDB(handStock): for stockCode in handStock: inHandStockHq = getVerboseStockHq(stockCode) if( len(inHandStockHq) < 2): print("monitorInHandStockHqDB: Can't get the verbose %s hq"%stockCode) continue if( (float)(inHandStockHq[hq.price]) == 0.0): debugPrint("monitorInHandStockHqDB: hq.price is not right !") continue noticeFlag = lzStockSell.zstdFunction(stockCode, inHandStockHq) if (noticeFlag == 1 and monitorStates[stockCode][mn.state] == 1): smUpdateStockToDb(inHandStockHq[hq.price], handStock[stockCode][3]) return
def monitorInHandStockHq(handStock): for stockCode in handStock: inHandStockHq = getVerboseStockHq(stockCode) if( len(inHandStockHq) < 2): print("monitorInHandStockHq: Can't get the verbose %s hq"%stockCode) continue if( (float)(inHandStockHq[hq.price]) == 0.0): debugPrint("monitorInHandStockHqDB: hq.price is not right !") continue noticeFlag = lzStockSell.zstdFunction(stockCode, inHandStockHq) if (noticeFlag == 1 and monitorStates[stockCode][mn.state] == 1): noticeString = stockCode + ' ' + inHandStockHq[hq.name] + ' ' + inHandStockHq[hq.price] humanNoticeProcess(noticeString) return
def zstdFunction(stockCode, monStockHq): debugPrint("zstdFunction processing...") zstdPrice = (float)(zstdData[stockCode]) currentPrice = (float)(monStockHq[hq.price]) debugPrint("zstdFunction: stockCode %s zstdPrice %f, currentPrice %f"%(stockCode, zstdPrice, currentPrice)) if( zstdPrice > currentPrice): tmp = ((zstdPrice - currentPrice)/zstdPrice)*100 if(tmp >= zstdPoint): monitorStates[stockCode][mn.price] = currentPrice monitorStates[stockCode][mn.state] += 1 return 1 else: return 0
def zstdInitialize(handStock): print("zstdInitialize...") for stockCode in handStock: tmpStockCode = stockCode.replace('h','s') debugPrint(tmpStockCode, stockCode) zstdPrice = handStock[stockCode][0] stockHq = getStockDayData(tmpStockCode, handStock[stockCode][2], 'd') print(' ', 'buyPrice:', zstdPrice) if(len(stockHq) != 0): for dayData in stockHq : print(' ', dayData[0], dayData[4]) if ( zstdPrice < (float)(dayData[4])): zstdPrice = (float)(dayData[4]) zstdData[stockCode] = zstdPrice for stockCode in zstdData: print(stockCode, zstdData[stockCode])
def createTable(conn): sql = '''create table if not exists `stockList` ( `id` int(11) NOT NULL, `name` varchar(10) NOT NULL, `code` varchar(10) NOT NULL, `buyTime` varchar(10) NOT NULL, `buyPrice` float(10) NOT NULL, `buyColumn` int(10) NOT NULL, `sellTime` varchar(10), `sellPrice` float(10), `sellColumn` int(10), `inhandFlag` varchar(2) NOT NULL, `simuReal` varchar(2) NOT NULL, PRIMARY KEY (`id`) )''' if sql is not None and sql != '': cu = getCursor(conn) debugPrint(sql) cu.execute(sql) conn.commit() else: print("createTable Error")
def simulater(n,arrflag): while True: handStock = smGetStockFromDb() print("Monitor simulater stocks ...") if( len(handStock) == 0 ): print(" No simulater stocks, please buy ...") time.sleep(120) else: lzStockProcess.initializeMonitorStructure(handStock) while True: if(arrflag[3] == 0): debugPrint(" Start Monitor simulater stocks ...") lzStockProcess.simulaterInterestStock(handStock) time.sleep(1) elif(arrflag[3] == 1): arrflag[3] = 0 break elif(arrflag[3] == 2): print(" Create daily report ...") smStatisticData() arrflag[3] = 9 else: time.sleep(120)
# def getStockDayData(stockCode, date, flag): stockHq = [] count = 0 url = stockHistoryUrl%(stockCode[2:], stockCode[:2].upper(), (str)((int)(date[4:6])-1), date[6:], date[:4], flag) debugPrint(url) try: reqAccess = urllib.request.urlopen(url) except urllib.error.HTTPError as e: print('Warning !!! can not access %s'%url) print('Error code:', e.code) return stockHq stockDayHq = reqAccess.read().decode('gbk','ignore').split('\n') for data in stockDayHq: try: debugPrint(data) Date, Open, High, Low, Close, Volumn, Adj = data.split(',') if ( Volumn == 'Volume' or Volumn == '000' ): pass else: count += 1 stockHq.append((Date, Open, High, Low, Close, Volumn, Adj)) except ValueError: pass return stockHq if __name__ == '__main__': stockHq = getStockDayData('ss600415', '20151118', 'd') debugPrint(stockHq)