def SetPerOrder(): ############################### # 신규 가입시 실행 DB_InserTacker() ############################### balances = upbit.get_balances() for b in balances: if b['currency'] == "KRW": continue tabkerName = "KRW-" + b['currency'] #orders = upbit.get_order(tabkerName) # DB 에 있는 평균가 조회 sql = """SELECT * FROM tacker WHERE id = %s AND currency = %s; """ cursor.execute(sql, (id ,tabkerName)) result = cursor.fetchall() # DB 평균가와 Upbit 평균가가 다를때 if float(result[0]['avgprice']) != float(b['avg_buy_price']) and float(result[0]['selper1']) > 0: print(tabkerName) # 기존 판매 예약 취소 oders = upbit.get_order(tabkerName) for oder in oders: if oder['side'] == 'ask': upbit.cancel_order(oder['uuid']) time.sleep(1) # 평균 매수가 fAvgPrice = float(b['avg_buy_price']) # 보유수량 fCount = float(b['balance']) + float(b['locked']) # 신규 판매 예약 등록 p1 = float(pyupbit.get_tick_size(fAvgPrice + (fAvgPrice * 0.01 * float(result[0]['selper1'])))) p2 = float(pyupbit.get_tick_size(fAvgPrice + (fAvgPrice * 0.01 * float(result[0]['selper2'])))) p3 = float(pyupbit.get_tick_size(fAvgPrice + (fAvgPrice * 0.01 * float(result[0]['selper3'])))) p1_1 = (fCount * 0.01 * float(result[0]['selper1_1'])) p2_1 = (fCount * 0.01 * float(result[0]['selper2_1'])) p3_1 = float(fCount - p1_1 - p2_1) print(str(p1) + " -> " + str(p1_1)) print(str(p2) + " -> " + str(p2_1)) print(str(p3) + " -> " + str(p3_1)) # 현제 평균가 다시 등록 rt = upbit.sell_limit_order(tabkerName, p1, p1_1 ,True) time.sleep(1) #print(rt) rt = upbit.sell_limit_order(tabkerName, p2, p2_1,True) time.sleep(1) #print(rt) rt = upbit.sell_limit_order(tabkerName, p3, p3_1,True) time.sleep(1) #print(rt) sql = """UPDATE tacker SET avgprice = %s WHERE id = %s AND currency = %s """ cursor.execute(sql, (b['avg_buy_price'] ,id ,tabkerName)) time.sleep(1) time.sleep(1)
def SetPerOrder(): balances = upbit.get_balances() for b in balances: if b['currency'] == "KRW": continue tabkerName = "KRW-" + b['currency'] #orders = upbit.get_order(tabkerName) print(tabkerName) # 평균 매수가 fAvgPrice = float(b['avg_buy_price']) # 보유수량 fCount = float(b['balance']) + float(b['locked']) # 신규 판매 예약 등록 p1 = float(pyupbit.get_tick_size(fAvgPrice + (fAvgPrice * 0.01 * 10))) iCancleOrderCount = 1 # 기존 판매 예약 취소 oders = upbit.get_order(tabkerName) for oder in oders: if oder['side'] == 'ask': iCancleOrderCount = 0 # DB 평균가와 Upbit 평균가가 다를때 if float(p1) != float(oder['price']): upbit.cancel_order(oder['uuid']) iCancleOrderCount = 1 # 현제 평균가 다시 등록 if iCancleOrderCount == 1: rt = upbit.sell_limit_order(tabkerName, p1, fCount ,True) time.sleep(1)
def get_Totalbalance(): balances = upbit.get_balances() arbalances = [] for b in balances: if b['currency'] != "KRW": #print(b) if b['balance'] is not None and float(b['balance']) > 0: bKrw = b['unit_currency'] + "-" + b['currency'] hopePrice = pyupbit.get_tick_size( float(b['avg_buy_price']) + float(b['avg_buy_price']) * 0.01 * iHopePer) upbit.sell_limit_order(bKrw, hopePrice, b['balance']) arbalances.append([bKrw, hopePrice, float(b['balance'])]) return arbalances
def SetPerOrder(tabker): balances = upbit.get_balances() for b in balances: if "KRW-" + b['currency'] == tabker: balance = (float(b['balance']) + float(b['locked'])) # 평균 매수가 fAvgPrice = float(b['avg_buy_price']) # 보유수량 fCount = float(b['balance']) + float(b['locked']) # 신규 판매 예약 등록 time.sleep(0.1) p1 = float(pyupbit.get_tick_size(fAvgPrice + (fAvgPrice * 0.02))) time.sleep(0.1) print(upbit.sell_limit_order(tabker, p1, fCount, True))
def SetPerOrder(): balances = upbit.get_balances() for b in balances: if b['currency'] == "KRW": continue # print(b) tabkerName = "KRW-" + b['currency'] balance = (float(b['balance']) + float(b['locked'])) # DB 평균가와 Upbit 평균가가 다를때 # if float(result[0]['avgprice']) != float(b['avg_buy_price']) and float(result[0]['selper1']) > 0: print(tabkerName) logging.info("--> " + tabkerName) # 평균 매수가 fAvgPrice = float(b['avg_buy_price']) # 보유수량 fCount = float(b['balance']) + float(b['locked']) # 신규 판매 예약 등록 p1 = float(pyupbit.get_tick_size(fAvgPrice * 1.018)) print(upbit.sell_limit_order(tabkerName, p1, fCount, True))
def run(self): price_curr = None hold_flag = False wait_flag = False # 로그인 upbit = pyupbit.Upbit(access, secret) cash = upbit.get_balance() print("autotrade start") print("보유현금", cash) post_message(myToken, "#test", "autotrade start") post_message(myToken, "#test", "Now Balance : " + str(cash)) i = 0 while True: try: if not self.q.empty(): if price_curr != None: self.ma15.append(price_curr) self.ma50.append(price_curr) self.ma120.append(price_curr) curr_ma15 = sum(self.ma15) / len(self.ma15) curr_ma50 = sum(self.ma50) / len(self.ma50) curr_ma120 = sum(self.ma120) / len(self.ma120) price_open = self.q.get() if hold_flag == False: price_buy = price_open * 1.01 price_sell = price_open * 1.02 wait_flag = False price_curr = pyupbit.get_current_price(self.ticker) if price_curr == None: continue if hold_flag == False and wait_flag == False and \ price_curr >= price_buy and curr_ma15 >= curr_ma50 and \ curr_ma15 <= curr_ma50 * 1.03 and curr_ma120 <= curr_ma50 : # 0.05% ret = upbit.buy_market_order(self.ticker, cash * 0.9995) print("매수 주문", ret) if ret == None or "error" in ret: print("매수 주문 이상") continue while True: order = upbit.get_order(ret['uuid']) if order != None and len(order['trades']) > 0: print("매수 주문 처리 완료", order) post_message(myToken, "#test", "ADA 매수 완료") break else: print("매수 주문 대기 중") time.sleep(0.5) while True: volume = upbit.get_balance(self.ticker) if volume != None: break time.sleep(0.5) while True: price_sell = pyupbit.get_tick_size(price_sell) ret = upbit.sell_limit_order(self.ticker, price_sell, volume) if ret == None or 'error' in ret: print("매도 주문 에러") time.sleep(0.5) else: print("매도주문", ret) hold_flag = True break # print(price_curr, curr_ma15, curr_ma50, curr_ma120) if hold_flag == True: uncomp = upbit.get_order(self.ticker) if uncomp != None and len(uncomp) == 0: cash = upbit.get_balance() if cash == None: continue print("매도완료", cash) post_message(myToken, "#test", "ADA 매도 완료") hold_flag = False wait_flag = True # 3 minutes if i == (5 * 60 * 3): print( f"[{datetime.datetime.now()}] 현재가 {price_curr}, 목표가 {price_buy}, ma {curr_ma15:.2f}/{curr_ma50:.2f}/{curr_ma120:.2f}, hold_flag {hold_flag}, wait_flag {wait_flag}" ) i = 0 i += 1 except: print("error") time.sleep(0.2)
def run(self): price_curr = None hold_flag = False wait_flag = False # 로그인 upbit = pyupbit.Upbit(access, secret) cash = upbit.get_balance() print("autotrade start") print("보유현금", cash) post_message(myToken, "#test", "autotrade start") post_message(myToken, "#test", "Now Balance : " + str(cash)) i = 0 while True: try: if not self.q.empty(): if price_curr != None: self.ma15.append(price_curr) self.ma50.append(price_curr) self.ma120.append(price_curr) curr_ma15 = sum(self.ma15) / len(self.ma15) curr_ma50 = sum(self.ma50) / len(self.ma50) curr_ma120 = sum(self.ma120) / len(self.ma120) price_open = self.q.get() if hold_flag == False: price_buy = int(price_open * 1.01) price_sell = int(price_open * 1.02) wait_flag = False price_curr = pyupbit.get_current_price(self.ticker) if hold_flag == False and wait_flag == False and \ price_curr >= price_buy and curr_ma15 >= curr_ma50 and \ curr_ma15 <= curr_ma50 * 1.03 and curr_ma120 <= curr_ma50: # 0.05% ret = upbit.buy_market_order(self.ticker, cash * 0.9995) print("매수주문", ret) time.sleep(1) volume = upbit.get_balance(self.ticker) price_sell = pyupbit.get_tick_size(price_sell) ret = upbit.sell_limit_order(self.ticker, price_sell, volume) print("매도주문", ret) hold_flag = True # print(price_curr, curr_ma15, curr_ma50, curr_ma120) if hold_flag == True: uncomp = upbit.get_order(self.ticker) if len(uncomp) == 0: cash = upbit.get_balance() print("매도완료", cash) hold_flag = False wait_flag = True # 3 minutes if i == (5 * 60 * 3): print( f"[{datetime.datetime.now()}] 현재가 {price_curr}, 목표가 {price_buy}, ma {curr_ma15:.2f}/{curr_ma50:.2f}/{curr_ma120:.2f}, hold_flag {hold_flag}, wait_flag {wait_flag}" ) i = 0 i += 1 except: print("error") time.sleep(0.2)