import os import pandas as pd from datetime import datetime, timedelta from OrderType import OrderType class OrderChecker: saveFileName = None def __init__(self, stock_code): self.saveFileName = "./order/"+stock_code+"_"+datetime.today().strftime("%Y%m%d")+".csv" return def read(self): if os.path.isfile(self.saveFileName): order_df = pd.read_csv(self.saveFileName) else: order_df = pd.DataFrame(columns=["stock_code", "type", "orderNum", "count", "price", "datetime"]) if 'Unnamed: 0' in order_df.columns: order_df.drop(['Unnamed: 0'], axis=1, inplace=True) order_df = order_df.fillna(0) order_df = order_df.astype({"stock_code": str, "type": str, "orderNum": int, "count": int, "price": int}) order_df['datetime'] = pd.to_datetime(order_df['datetime']) return order_df def add(self, stock_code, type, orderNum, count, price, orderList): orderListToCancel = [] # 매수 요청인 경우만 두 시간 전 주문을 찾아서 제외한다. order_df = self.read() # 두 시간 전 주문을 찾는다. if orderList is not None and len(orderList) > 0: # 1) 종목 코드를 찾는다. stock_df = order_df.loc[order_df["stock_code"] == stock_code] # 2) 2 시간 전 계산 before_two_hour = datetime.now() - timedelta(hours=2) # 3) 만약 2 시간 전 주문을 찾는다. stock_twohour_df = stock_df.loc[stock_df["datetime"] <= before_two_hour] # 4) 2 시간 전 주문 중에서 매수 데이터만 찾는다. orderNum_df = stock_twohour_df.loc[stock_twohour_df["type"] == OrderType.buy.value] # 5) 2시간 전 매수 항목을 별도로 추출한다. orderNumSet = set(list(orderNum_df["orderNum"])) for item in orderList: if item.orderNum in orderNumSet: orderListToCancel.append(item) # 6) 추출된 항목을 제외시킨다. order_df = order_df.loc[order_df["orderNum"] != item.orderNum] # 7) 새로운 주문을 추가한다. order_df = order_df.append({"stock_code": stock_code, "type": type.value, "orderNum": orderNum, "count": count, "price": price, "datetime": datetime.now()}, ignore_index=True) # 8) 파일로 기록한다. order_df.to_csv(self.saveFileName) return orderListToCancel def remove(self, stock_code, type, orderList): order_df = self.read() orderListToCancel = [] if orderList is not None and len(orderList) > 0: # 1) 종목 코드를 찾는다. stock_df = order_df.loc[order_df["stock_code"] == stock_code] # 2) 매도 주문을 찾는다. orderNum_df = stock_df.loc[stock_df["type"] == type.value] # 3) 매도 주문 중에서 항목을 별도로 추출한다. orderNumSet = set(list(orderNum_df["orderNum"])) for item in orderList: if item.orderNum in orderNumSet: orderListToCancel.append(item) # 4) 해당 orderNum 제외하기 order_df = order_df.loc[order_df["orderNum"] != item.orderNum] order_df.to_csv(self.saveFileName) return orderListToCancel if __name__ == "__main__": a = pd.DataFrame(columns=["type", "orderNum", "count", "price", "datetime"]) a = a.astype({"type": str, "orderNum": int, "count": int, "price": int}) a['datetime'] = pd.to_datetime(a['datetime']) this_time = datetime.now() a = a.append({"stock_code": "a", "type": "0", "orderNum": 1, "count": 3, "price": 7, "datetime": this_time}, ignore_index=True) a = a.append({"stock_code": "a", "type": "1", "orderNum": 3, "count": 0, "price": 6, "datetime": this_time - timedelta(hours=2)}, ignore_index=True) a = a.append({"stock_code": "a", "type": "2", "orderNum": 3, "count": 0, "price": 3, "datetime": this_time + timedelta(hours=1)}, ignore_index=True) a = a.append({"stock_code": "a", "type": "1", "orderNum": 9, "count": 9, "price": 8, "datetime": this_time}, ignore_index=True) b = a.loc[a["orderNum"] != 3] print(b.tail()) print() b = a.loc[a["datetime"] < this_time] print(b.tail()) a.to_csv("test.csv") a = pd.read_csv("test.csv") a.drop(['Unnamed: 0'], axis=1, inplace=True) a = a.astype({"stock_code": str, "type": str, "orderNum": int, "count": int, "price": int}) a['datetime'] = pd.to_datetime(a['datetime']) print("done")