def timerStart(self, TopStockList, TheNumberOfStocksPurchase, codeList, account): self.TopStockList = TopStockList self.TheNumberOfStocksPurchase = TheNumberOfStocksPurchase self.codeList = codeList self.account = account self.receiveRealDataHistory = [] sqlite3.connect("TradingProject.db") #Connect 'TradingProject.db' database which stores the stock's data now = time.localtime() today = CalculationTool.calculateTodayFormat(now.tm_year, now.tm_mon, now.tm_mday) kospiData = xingAPIClassModule.XAQuery("t1511", today) #Set initial index of KOSPI self.initialKospiValue = kospiData[0][4] #Make a string of code list to request the information of stock as multiple request. PLZ see a xing API manual ex)00593000596000010............. codeString = "" for stockName in TopStockList: codeString = codeString + codeList[stockName] self.requestCodeList = codeString #This code is for calculating the time! now! now = time.localtime() today = CalculationTool.calculateTodayFormat(now.tm_year, now.tm_mon, now.tm_mday) ### Algorithm 1 start ### self.algorithm1Timer_purchase() DBconnect = sqlite3.connect("TradingProject.db") dataBaseCursor = DBconnect.cursor() #Connect database cursor! ### Today's algroithm1 portfolio list updating ### for stockName in self.TopStockList: purchaseAmount = self.TheNumberOfStocksPurchase[stockName] #purchaseAmount = self.TheNumberOfStocksPurchase[stockName][0] valueString = "(" + "'" + stockName + "'" + "," + "'" + str(purchaseAmount) + "'" + "," + "'" + today + "'" + ")" dataBaseCursor.execute("INSERT INTO Algorithm1_TradingInfo VALUES" + valueString) DBconnect.commit() print("Today's algorithm1 portfolio list updated")
def algorithm1Timer_purchase(self): print("Algorithm1(purchase) START!!") today, buyDay = CalculationTool.calculateTimetoExecute(9,0) delta_t= buyDay - today secs = delta_t.seconds #Each thread will start after given 'secs' time purchaseTimer = Timer(secs, self.stockPurchase) receiveRealTimer = Timer(secs, self.realDataProcessing) purchaseTimer.start() receiveRealTimer.start()
def calculatingAverageValue(self,stockNameList, returnValue, WindowSize): averageValue = {} for name in stockNameList: ##Calculating average value that represents (return value(as many as WindowSize)) / WindowSize averageList = [] windowForCalculation = returnValue[name][0:WindowSize] average2 = CalculationTool.average(windowForCalculation) averageList.append(average2) averageValue[name] = averageList return averageValue
def calculatingStandardVariation(self,stockNameList, returnValue, WindowSize, criterion): standardDeviationValue = {} removeList = [] for name in stockNameList: ##Calculating standardDeviation that represents thet standard deviation of return value(as many as WindowSize) standardDeviationList = [] standardDeviation = CalculationTool.standardDeviation(returnValue[name][0:WindowSize]) standardDeviationList.append(standardDeviation) standardDeviationValue[name] = standardDeviationList if standardDeviation < criterion: removeList.append(name) else: standardDeviationList.append(standardDeviation) standardDeviationValue[name] = standardDeviationList return standardDeviationValue, removeList ############################################### #i = 2 #excel = win32com.client.Dispatch("Excel.Application") ##excel.Visible = True #wb = excel.Workbooks.Add() #for stockName in stockNameList: # ws = wb.Worksheets("Sheet1") # ws.Cells(1, i).Value = stockName # i += 1 #j = 2 #dayIndex.reverse() #for day in dayIndex: # ws = wb.Worksheets("Sheet1") # ws.Cells(j, 1).Value = day # j += 1 #i = 2 #j = 2 #index = 1 #print("stock's name list number: " + str(len(stockNameList))) #for stockName in stockNameList: # length = len(closingPriceData[stockName]) # if length <= 490: # continue # ws.Cells(1, j).Value = stockName # #i += 1 # print(str(index) + " " + stockName + " " + str(length)) # closingPriceData[stockName].reverse() # for index in range(length): # ws.Cells(i,j).Value = str(closingPriceData[stockName][index]) # i += 1 # i = 2 # j += 1 # closingPriceData[stockName].reverse() #excel.Visible = True #dayIndex.reverse() #WindowSize = int(otherWindowClass.investMentWindowSize['Algorithm1']) #Get the value of window's size from the global variable of 'otherWindowClass.py' #deposit = float(self.deposit) #Get the value of a deposit #investMentPercentage = float(otherWindowClass.investMentPercentage['Algorithm1']) #Get the value of total data"" #investMentSize = int(otherWindowClass.investMentSize['Algorithm1']) #closingPriceData = {} #This dictionary stores closing price data #openingPriceData = {} #stockNameList = [] #This list stores the stock's name #dayIndex = [] #This list stores day #Remainder = 0 #The remainder that made by subtracting the capital which will be used to purchase the stock from the investment #codeList = {} #This dictionary stores the code's list ##setting 'stockNameList', 'closingPriceData', 'dayIndex' data structures using 'setClosingData' function ##See below the 'AlgorithmPart.py' #stockNameList, closingPriceData, openingPriceData, dayIndex, codeList = self.setClosingPriceData_Algo() #dayIndex = dayIndex[-WindowSize:] #Extract the information of day from 'dayIndex' list as many as the number of 'TotalData' #print(DataFrame(openingPriceData, columns = stockNameList)) ##These data structures is for storing each data which refers in the name of variable itself #returnValue = {} #averageValue = {} #standardDeviationValue = {} #removeList = [] #AnValue = {} #WnValue = {} #TheNumberOfStocksPurchase = {} #profitValue = [] #rVIndex = dayIndex[1:] #This code functions as making the index of day to fit DataFrame data structure ##Calculating return value that represents (price of tommorrow - today's price) #returnValue = self.calculatingReturnValue(stockNameList, closingPriceData, openingPriceData, WindowSize) ##Calculating average value that represents (return value(as many as WindowSize)) / WindowSize #averageValue = self.calculatingAverageValue(stockNameList, returnValue, WindowSize) ##Calculating standardDeviation that represents thet standard deviation of return value(as many as WindowSize) ##And Add the stock whose standard variation value is below a certain value 'criterion' ##Return standardDeviationValue, removeList #criterion = 0.005 #standardDeviationValue, removeList = self.calculatingStandardVariation(stockNameList, returnValue, WindowSize, criterion) ########################################## 4.08 여기까지 함 그 다음에 나머지!! 하셈 ##Remove the stock which is contained in 'removeList' #for removeName in removeList: # stockNameList.remove(removeName) #for name in stockNameList: #Calculating An Value that represents the distribution of the stock's average value # AnList = [] # #criteria is that if this stock's average value is less than KOSPI's average value, this stock is excluded from the investment list # if float(standardDeviationValue[name][0]) != 0: # criteria = (float(averageValue[name][0]) - float(averageValue[stockNameList[0]][0]))/ (float(standardDeviationValue[name][0])) # if criteria < 0: #if criteria is less than 0, An = 0 # An = 0 # AnList.append(An) # else: #else An value is criteria # An = criteria # AnList.append(An) # else: # An = 0 # AnList.append(An) # AnValue[name] = AnList #Store the AnList to AnValue(dictionary) #InvestCandidateStock = [] #for name in stockNameList: # if AnValue[name][0] != 0: # InvestCandidateStock.append(name) ##Building Top Stock list which is to be invested #TopStockList = [] #for stockName in InvestCandidateStock: # if TopStockList == []: # TopStockList.append(stockName) # else: # listLength = len(TopStockList) # for index in range(200): # if index <= (listLength - 1): # if AnValue[TopStockList[index]][0] < AnValue[stockName][0]: # TopStockList.insert(index, stockName) # break # elif index == listLength: # TopStockList.append(stockName) # break #TopStockList = TopStockList[0:investMentSize] #Temp = AnValue #AnValue = {} ##Rearrange Anvalue as a list of the stock which will be invested #for stockName in TopStockList: # AnValue[stockName] = Temp[stockName] #WnIndex = dayIndex[WindowSize:] #Calculating Wn Value that represents the ratio of a certain stock's An value to total An value #SumOfAnArray = [] #This is the list of the total An value #try: # for name in TopStockList: # WnList = [] # if name == TopStockList[0]: # SumOfAn = 0 # for name2 in TopStockList: # SumOfAn = SumOfAn + AnValue[name2][0] # SumOfAnArray.append(SumOfAn) # print(SumOfAnArray[0]) # if float(SumOfAnArray[0]) != 0: # Wn = float(AnValue[name][0])/float(SumOfAnArray[0]) # else: # Wn = float(AnValue[name][0]) # WnList.append(Wn) # WnValue[name] = WnList #except ZeroDivisionError: # print("Wn ZeroError" + name + " "+ str(index)) #print(deposit) #investingAmount = investMentPercentage * deposit / 100.0 #Calculate a investment ##Initializing data structures that contains each value(the number of stock that have to purchase) #for name in TopStockList: # TheNumberOfStocksPurchase[name] = [] ##Calculating the start value of the capital #for stockName in TopStockList: # #Weight of a stock is not 0, distribute the capital depending on the value of weight of the stock # if WnValue[name][0] != 0: # theCapitalForPurchasingStock = float(WnValue[stockName][0]) * investingAmount # TheNumberOfStock = int(theCapitalForPurchasingStock / float(closingPriceData[stockName][WindowSize-1])) # TheNumberOfStocksPurchase[stockName].append(TheNumberOfStock) # #Weight of a stock is 0(It means it is no use of purchasing), assign 0 value to 'TheNumberOfStocksPurchase' # else: # TheNumberOfStocksPurchase[stockName].append(0) ##Print the result of the number of stock whcih have to be bought as a Data Frame structure #print("------------------- Stock Number Value -------------------------") #frame7 = DataFrame(TheNumberOfStocksPurchase, columns = TopStockList) #print("------------------- Stock Number Value -------------------------") #print(TheNumberOfStocksPurchase) ##Order the stock which is listed in 'TopSTockList' # def setClosingPriceData_Algo(self): #dataConnect = sqlite3.connect("TradingProject.db") #Connect 'TradingProject.db' database which stores the stock's data #dataBaseCursor = dataConnect.cursor() #Connect database cursor! #stockNameList = [] #Store Stock's name #closingPriceData = {} #Store closing price #openingPriceData = {} #Store opening price #dayIndex = [] #Store day index #codeList = {} #Store the code list #WindowSize = int(otherWindowClass.investMentWindowSize['Algorithm1']) #This stands for window size #selectQuery = "SELECT DISTINCT Name FROM kospiValues" #This query is that searching all stock's name without duplication #dataBaseCursor.execute(selectQuery) #Execute the query 'selectQuery' #ret = dataBaseCursor.fetchall() #This code is that fetching all of datas which is selected by the query #TheNumberOfstockNameList = len(ret) #Store the number of datas in a variable 'TheNumberOfstockNameList' #for index in range(int(TheNumberOfstockNameList)): #Construct 'stockNameList'. ret[index][0] means '종목명' # stockNameList.append(ret[index][0]) #conditionValue = "'" + stockNameList[0] + "'" #First, add '종합(KOSPI)' to string object 'condictionValue' #for stockName in stockNameList[1:]: #Making string object 'conditionValue' to be inserted in 'selectQuery' # conditionValue = conditionValue + " ,"+ "'" + stockName + "'" #whereCondition = "Name IN " + '(' + conditionValue + ')' #Making string object 'whereCondition' to inject 'selectQuery' #selectQuery = "SELECT Name, closingPrice, openingPrice, date FROM kospiValue WHERE " + whereCondition #This query is that selecting '종가' and '일수' #dataBaseCursor.execute(selectQuery) #Execute the query 'selectQuery' #ret = dataBaseCursor.fetchall() #This code is that fetching all of datas which is selected by the query #stockName = ret[0][0] #ret[0][0] means '종합(KOSPI)'. This is the first stock that Its closing price data will be stored in 'closingPriceList' #closingPriceList = [] #This list is for storing closing price data temporarily #openingPriceList = [] #This list is for storing opening price data temporarily #index = 0 ##This for loop is for storing each stock's closing price data in 'closingPriceData'(dictionary) ##If algorithm1 is now to execute, play below the 'if' line ##else, algorithm2 is now to execute, play below the 'elif' line #for stockName in stockNameList: # while ret[index][0] == stockName: # closingPriceList.append(ret[index][1]) # openingPriceList.append(ret[index][2]) # index += 1 # if len(ret) - 1 == index: # break # closingPriceData[stockName] = closingPriceList # openingPriceData[stockName] = openingPriceList # closingPriceList = [] # openingPriceList = [] #selectQuery = "SELECT date FROM kospiValue WHERE " + "Name IN ('삼성전자')" #This query is for selecting '일자' attribute to get full data list. why '삼성전자'? I think it never goes bankrupt #dataBaseCursor.execute(selectQuery) #Execute the query 'selectQuery' #ret = dataBaseCursor.fetchall() #This code is that fetching all of datas which is selected by the query #for index in range(len(ret)): #This code is for storing '일자' data # dayIndex.append(ret[index][0]) ######## EXTRACT DAILY CLOSING PRICE DATA ###### ################################################# ##Extract the necessary data as many as window size (Window size wil be updated at 'otherWindowClass.py' -> 'Algorithm_1_Option') #for stockName in stockNameList: # closingPriceData[stockName] = closingPriceData[stockName][-WindowSize:] # openingPriceData[stockName] = openingPriceData[stockName][-WindowSize:] #ListOfRemoveStockList = [] #Make a list of removing the stock datas #for stockName in stockNameList: #Remove the stock data which is less than 'otherWindowClass.TheNumberOfTotalData' # #Not handling these datas, It causes a fatal error 'outOfIndex' # TheNumberOfDataOfStock = len(closingPriceData[stockName]) # if TheNumberOfDataOfStock < WindowSize: # ListOfRemoveStockList.append(stockName) ##Remember 범양건영 #for stockName in ListOfRemoveStockList: #Remove the stock which The number of its closing index is less than 'TheNumberOfTotalData' from 'closingPrice' # del closingPriceData[stockName] # del openingPriceData[stockName] # stockNameList.remove(stockName) #selectQuery = "SELECT * FROM kospiCode" #This query is for selecting '일자' attribute. why '삼성전자'? I think it never goes bankrupt #dataBaseCursor.execute(selectQuery) #Execute the query 'selectQuery' #ret = dataBaseCursor.fetchall() ##Construct a dictionary which stores the code data #for index in range(len(ret)): # codeList[ret[index][1]] = ret[index][0] #return stockNameList, closingPriceData, openingPriceData, dayIndex, codeList #return the constructed data ## REAL ALGO ### ##removedList = {} #f = open("result.txt", 'w') #removedList = {} #tVolList = [] #yesterday = "20160722" #count = 0 #for i in range(len(ret)): # if ret[i][7] == yesterday: # stockName = ret[i][0] # #removedList.append(stockName) # tradingVol = ret[i][3] # tradingVal = ret[i][2] # closingVal = ret[i][1] # if tradingVol >= 200000 and tradingVol * tradingVal >= 5000000000 and closingVal >= 1100 and self.isCheatPattern() == False: # stockName = ret[i][0] # count+=1 # w = stockName + " : " + str(tradingVol) + "\n" # f.write(w) #print(count)
def realDataReceive(self): #print("start") dataConnect = sqlite3.connect("TradingProject.db") #Connect 'TradingProject.db' database which stores the stock's data time.sleep(2) while 1: pythoncom.CoInitialize() #After 10 secs, function is executed #Disconnected? Login xingAPILogin() now = datetime.datetime.today() timeNow = str(now) stringCount = len(timeNow) #Print current time on command line in place #text = "this time" + str(timeNow) + "\r" #print(text, end='') print("this time " + str(timeNow)) #Format current time for index in range(stringCount): char = timeNow[index] if char == '.': #timeNow = str(timeNow[0:index]) + "\r" #print(timeNow, end='') print(timeNow[0:index]) break #Count the number of stock list count = len(self.TopStockList) requestList = self.requestCodeList ####################################### <- This is needed to be modularization inXAQuery = winAPI.DispatchWithEvents("XA_DataSet.XAQuery", XAQueryEvents) inXAQuery.ResFileName = "C:\\eBEST\\xingAPI\\Res\\t1511.res" inXAQuery.SetFieldData("t1511InBlock", "upcode", 0, '001') while True: ret = inXAQuery.Request(0) #print("result: " + str(ret)) if ret == -21 or ret == -34 or ret == -23: time.sleep(0.8) #print(ret) xingAPILogin() else: break #Wait.... request... while XAQueryEvents.query_state == 0: pythoncom.PumpWaitingMessages() #Request is arrived! turn back the flag which represents "Oh, Requested data is arrived!!" ( "0" is not arrived yet(normal), "1" is arrived!!(SIGNAL occurs)) XAQueryEvents.query_state = 0 ########################################### nCount = inXAQuery.GetBlockCount("t1511OutBlock") temp = [] #At first, just get a deposit. I will get a lot of data in the end stockName = '종합(KOSPI)' #Get current price of KOSPI currentPrice = inXAQuery.GetFieldData('t1511OutBlock', 'pricejisu', 0) #At first, store the first value of kospi price in "kospiPrice" array #And then, calculate the loss profit of kopsi. #the ration of kospi's profit loss print("current:" + str(currentPrice)) print(type(currentPrice)) currentKospi = float(currentPrice) initialKospi = float(self.initialKospiValue) kospiLossProfit = ( ( currentKospi - initialKospi ) / initialKospi ) * 100 #self.kospiPrice.append(currentPrice) #text = "kospiloss profit : " + str(kospiLossProfit) + "\r" #print(text, end='') self.kospiProfit.append(kospiLossProfit) self.indexKospi +=1 #Execute 'insertQuery' dataBaseCursor = dataConnect.cursor() #Connect database cursor! valueStringOfkospiValue = "("+ "'" + stockName + "'" + "," + "'" + currentPrice + "'" + "," + "'" + timeNow + "'" + ")" insertQuery = "INSERT INTO kospiRealData VALUES" + valueStringOfkospiValue dataBaseCursor.execute(insertQuery) ################################################################################## #print("ACCOUNT :" + self.account) inXAQuery = winAPI.DispatchWithEvents("XA_DataSet.XAQuery", XAQueryEvents) inXAQuery.ResFileName = "C:\\eBEST\\xingAPI\\Res\\t0424.res" inXAQuery.SetFieldData("t0424InBlock", "accno", 0, self.account) inXAQuery.SetFieldData("t0424InBlock", "passwd", 0, "0000") inXAQuery.SetFieldData("t0424InBlock", "prcgb", 0, "1") inXAQuery.SetFieldData("t0424InBlock", "chegb", 0, "2") inXAQuery.SetFieldData("t0424InBlock", "dangb", 0, "0") inXAQuery.SetFieldData("t0424InBlock", "charge", 0, "1") inXAQuery.SetFieldData("t0424InBlock", "cts_expcode", 0, "") while True: ret = inXAQuery.Request(0) #print("result " + str(ret)) if ret == -21 or ret == -34: time.sleep(0.8) else: break #Wait.... request... while XAQueryEvents.query_state == 0: pythoncom.PumpWaitingMessages() #Request is arrived! turn back the flag which represents "Oh, Requested data is arrived!!" ( "0" is not arrived yet(normal), "1" is arrived!!(SIGNAL occurs)) XAQueryEvents.query_state = 0 nCount = inXAQuery.GetBlockCount("t0424OutBlock") ################################################################################## #print("----- Current position update -----\r", end='') print("----- Current position update -----") #dataConnect = sqlite3.connect("TradingProject.db") #Connect 'TradingProject.db' database which stores the stock's data for i in range(nCount): expectPosition = inXAQuery.GetFieldData('t0424OutBlock', 'sunamt', i) #현재 포지션 purchasePrice = inXAQuery.GetFieldData('t0424OutBlock', 'mamt', i) #매입금액 estimatedPrice = inXAQuery.GetFieldData('t0424OutBlock', 'tappamt', i) #추정자산 lossProfit = inXAQuery.GetFieldData('t0424OutBlock', 'tdtsunik', i) #수익률 ratioLossProfit = float(lossProfit)/float(purchasePrice) * 100 #UNIT (%) #추가추가추가 #print("----------------------KOSPI---------------------------\r", end='') print("----------------------KOSPI---------------------------") #text="expectPosition:" + str(expectPosition) + " " + "purchasePrice:" + str(purchasePrice) + " " + "estimatedPrice:" + str(estimatedPrice) + " " + "lossProfit:" + str(lossProfit) + " " + "ratioLossProfit:" + str(ratioLossProfit) + "\r" #print(text, end='') print("expectPosition:" + str(expectPosition) + " " + "purchasePrice:" + str(purchasePrice) + " " + "estimatedPrice:" + str(estimatedPrice) + " " + "lossProfit:" + str(lossProfit) + " " + "ratioLossProfit:" + str(ratioLossProfit)) #print("------------------------------------------------------\r", end='') print("------------------------------------------------------") nCount = inXAQuery.GetBlockCount("t0424OutBlock1") self.stockProfit.append(ratioLossProfit) dataBaseCursor = dataConnect.cursor() #Connect database cursor! valueStringOfkospiValue = "("+ "'" + str(expectPosition) + "'" + "," + "'" + str(purchasePrice) + "'" + "," + "'" + str(estimatedPrice) + "'" + "," + "'" + str(lossProfit) + "'" + "," + "'" + str(ratioLossProfit) + "'" + "," + "'" + str(timeNow) + "'" + ")" insertQuery = "INSERT INTO realTimeAccount VALUES" + valueStringOfkospiValue #Execute 'insertQuery' #dataConnect.commit() #print("RECEIVE nCount----OUTBLOCK1-------- " + str(nCount)) self.indexStock += 1 totalPosition = 0 #dataConnect = sqlite3.connect("TradingProject.db") #Connect 'TradingProject.db' database which stores the stock's data dataBaseCursor = dataConnect.cursor() #Connect database cursor! for i in range(nCount): #At first, just get a deposit. I will get a lot of data in the end stockCode = inXAQuery.GetFieldData('t0424OutBlock1', 'expcode', i) stockName = inXAQuery.GetFieldData('t0424OutBlock1', 'hname', i) amountRemain = inXAQuery.GetFieldData('t0424OutBlock1', 'janqty', i) currentPrice = inXAQuery.GetFieldData('t0424OutBlock1', 'price', i) purchasePrice = inXAQuery.GetFieldData('t0424OutBlock1', 'mamt', i) estimatedPrice = inXAQuery.GetFieldData('t0424OutBlock1', 'appamt', i) ratioLossProfit = inXAQuery.GetFieldData('t0424OutBlock1', 'sunikrt', i) #print("----------------------" + str(stockCode) + " " + str(stockName) + "---------------------------\r", end='') #print("amountRemain:" + str(amountRemain) + " "+ "currentPrice:" + str(currentPrice) + " " + "purchasePrice:" + str(purchasePrice) + " " + "estimatedPrice:" + str(estimatedPrice) + " " + "ratioLossProfit:" + str(ratioLossProfit) + "\r", end='') #print("------------------------------------------------------\r", end='') print("----------------------" + str(stockCode) + " " + str(stockName) + "---------------------------") print("amountRemain:" + str(amountRemain) + " "+ "currentPrice:" + str(currentPrice) + " " + "purchasePrice:" + str(purchasePrice) + " " + "estimatedPrice:" + str(estimatedPrice) + " " + "ratioLossProfit:" + str(ratioLossProfit)) print("------------------------------------------------------") dataBaseCursor.execute(insertQuery) valueStringOfkospiValue = "("+ "'" + stockCode + "'" + "," + "'" + stockName + "'" + "," + "'" + amountRemain + "'" + "," + "'" + currentPrice + "'" + "," + "'" + purchasePrice + "'" + "," + "'" + estimatedPrice + "'" + "," + "'" + ratioLossProfit + "'" + "," + "'" + timeNow + "'" + ")" insertQuery = "INSERT INTO stockRealData VALUES" + valueStringOfkospiValue dataBaseCursor.execute(insertQuery) after10Sec = CalculationTool.calculAfter10sec() dataConnect.commit() time.sleep(10) if ( after10Sec.hour == 15 and after10Sec.minute >= 15): break