|
樓主 |
發表於 14-11-29 00:53
|
顯示全部樓層
本帖最後由 wldtw2008 於 14-11-29 01:30 編輯
剛剛花了一點時間,寫了一支py,透過小弟的DDE2TickQuote or D2TQMini 收到報價後直接灌到KairosDB。
Ins2KairosDB.py- import time
- import sys
- import time
- from socket import socket
- #Read one line
- def readlines(sock, recv_buffer=4096, delim='\n'):
- buffer = ''
- data = True
- while data:
- data = sock.recv(recv_buffer)
- buffer += data
- while buffer.find(delim) != -1:
- line, buffer = buffer.split('\n', 1)
- yield line
- return
- #get tick from linedata
- def parseTickQuote(linedata):
- iBeg = linedata.find("01_ID=")
- if iBeg >= 0:
- #print linedata
- iBeg += 6;
- iEnd = linedata.find(",", iBeg);
- symbol = linedata[iBeg:iEnd]
- allKeyVals = linedata[iEnd+1:].split(",")
- dbPrice=0.0
- for oneKeyVal in allKeyVals:
- keyVal = oneKeyVal.split("=")
- if (len(keyVal)!=2):
- continue
- key=keyVal[0].upper()
- if key == "C":
- dbPrice=float(keyVal[1])
- break
- return symbol, dbPrice, -999, 1
- iBeg = linedata.find("00_ID=")
- if iBeg >= 0:
- #print linedata
- iBeg += 6
- iEnd = linedata.find(",", iBeg);
- symbol = linedata[iBeg:iEnd]
- allKeyVals = linedata[iEnd+1:].split(",")
- dbAsk=0.0;
- dbBid=0.0;
- for oneKeyVal in allKeyVals:
- keyVal = oneKeyVal.split("=")
- if (len(keyVal)!=2):
- continue
- key=keyVal[0].upper()
- if key == "BID":
- dbBid=float(keyVal[1])
- elif key == "ASK":
- dbAsk=float(keyVal[1])
- return symbol, dbBid, dbAsk, 0
- return "N/A", -999, -999, -999
- def loopClient(sreverIp, serverPort):
- sockRcv = socket()
- sockRcv.connect((sreverIp, serverPort))
- str2KairosDB = ""
- for line in readlines(sockRcv):
- symbol, price1, price2, torq = parseTickQuote(line)
- if (symbol == "N/A"):
- continue
- #print line
- curTime_m = int(round(time.time() * 1000))
- #put wtx.c 1417096644358 9002
- if (torq==1):
- str2KairosDB="put %s.c %d %f\n"%(symbol,curTime_m,price1)
- else:
- str2KairosDB="put %s.b %d %f\n"%(symbol,curTime_m,price1)
- str2KairosDB+="put %s.a %d %f\n"%(symbol,curTime_m,price2)
- #if (symbol == SymbolA or symbol == SymbolB):
- # newTickString = getDiffTickStr(SymbolOutput, SymbolA, SymbolB)
- if (str2KairosDB != ""):
- sys.stdout.write(str2KairosDB)
- sys.stdout.flush()
- sockRcv.close()
- szQuoteIP = sys.argv[1]
- szQuotePort = sys.argv[2]
- loopClient(szQuoteIP, int(szQuotePort));
複製代碼 只要報價有來就全部自動存到KairosDB,效果不錯,查詢出來的摩台(我只收了1.5hr)Bid/Ask/Last圖如下
PS.我是在Linux 搭配 pipe netcat 執行這個py,命令如下
python Ins2KairosDB.py 192.168.1.1 4568 | netcat 192.168.1.217 4242 |
|