def test_order(market="KRW-DOGE"): data = pyupbit.get_coin_investablity(market) account_money = float(data['bid_account']['balance']) - 5000 coin_price = float(pyupbit.view_candle_min(market)[0]['trade_price']) coin_count = account_money / coin_price print(account_money, coin_price, round(coin_count, 2)) pyupbit.order_coin(market, coin_price, coin_count, 'bid')
def test_init(): print('init test') order_money = 20_000 my_coin_dict = pyupbit.get_my_coin_info() coin_name = '' for key, value in my_coin_dict.items(): coin_name = key unit_price = my_coin_dict[coin_name][0] quantity = my_coin_dict[coin_name][1] coin_value = float(unit_price) * float(quantity) print( f'코인 단가 : {unit_price}, 수량 : {quantity}, 가치 : {round(float(unit_price) * float(quantity), 2)}' ) # 분단위 캔들 coin_info = pyupbit.view_candle_min(coin_name) # 내가 매수 한 코인 단가 buy_unit_price = pyupbit.get_my_coin_unit_price(my_coin_dict) # 현재 코인 단가 current_unit_price = pyupbit.get_current_coin_price(coin_info) # 수익률(100%가 매수 시점 단가) profit_rate = pyupbit.get_profit_rate(current_unit_price, buy_unit_price) print( f'매수시 코인 단가 : {buy_unit_price}, 현재코인단가 : {current_unit_price}, 수익률 : {profit_rate}' ) if profit_rate > 100: print('팔기') else: print('버티기')
def sell_all(): # config.json 자동 매도 기능 허용 여부 확인 if pyupbit.get_auto_sell() == 'YES': myinfo_map = pyupbit.get_my_coin_info() if myinfo_map is not None: # 코인명 market = pyupbit.get_my_coin_name(myinfo_map) # 내가 구매 한 코인 수 my_coin_amount = pyupbit.get_my_coin_total_amount(myinfo_map) # 분단위 캔들 coin_info = pyupbit.view_candle_min(market) # 코인의 현재 단가(분단위 캔들로 조회) current_my_coin_price = pyupbit.get_current_coin_price(coin_info) order_price = current_my_coin_price order_volume = my_coin_amount order_type = 'ask' # 전량 매도! order_coin(market_name=market, order_money=order_price, order_volume=order_volume, type=order_type) else: pyupbit.send_message( pyupbit.get_slack_channel(), '자동 매도 기능을 허용하지 않았습니다. \ninvest_helper에게 요청 하세요.')
def test_profit_rate(): print('test start') my_investment = pyupbit.get_my_coin_info() current_coin = pyupbit.view_candle_min(list(my_investment.keys())[0]) current_coin_price = pyupbit.get_current_coin_price(current_coin) print( f'my_investment ::: {my_investment} / current_coin_info ::: {current_coin_price}' )
def order_10000(market_name="KRW-BTC", order_volume=0, order_type='bid'): if order_type == 'bid': order_money = 10000 / order_volume else: print(f'대상코인현재정보 ::: {pyupbit.view_candle_min(market_name)}') order_money = pyupbit.get_current_coin_price( pyupbit.view_candle_min(market_name)) return order_coin(market_name, order_money, order_volume, order_type)
def order_best_coin(best_coin='', order_amount=0): coin_info = pyupbit.view_candle_min(best_coin) order_volume = pyupbit.get_possible_order_volume(coin_info, order_amount) order_money = (order_amount / order_volume) print( f'잘 될 것 같은 코인 구매 ::: unit_price : {order_money}, amount : {order_volume}' ) # 50,000원 어치 매수 return pyupbit.order_coin(market_name=best_coin, order_money=order_money, order_volume=order_volume, type='bid')
def working(market='', my_investment={}, prev_profit_rate=100, score=0, has_minus_exp=False): # 해당 코인의 현재 상태(분 캔들) 조회 coin_candle = pyupbit.view_candle_min(market) # 내가 매수 한 코인 단가 buy_unit_price = pyupbit.get_my_coin_unit_price(my_investment) # 내 계좌에 남은 현금 #krw_balance = pyupbit.get_my_krw_balance(my_investment) # 내 계좌에 남은 코인 수 #my_coin_balance = pyupbit.get_my_coin_total_amount(my_investment) # 현재 코인 단가 current_unit_price = pyupbit.get_current_coin_price(coin_candle) # 수익률(100%가 매수 시점 단가) profit_rate = pyupbit.get_profit_rate(current_unit_price, buy_unit_price) # 스코어(매도시점용) score = calc_profit_score(score, prev_profit_rate, profit_rate) slack_message1 = f"코인명 ::: {market}(현재빡침점수 : {round(score, 2)}), 매수단가 ::: {buy_unit_price}, 현재단가 ::: {current_unit_price}, 수익률 ::: {str(profit_rate)}%" print(slack_message1) if profit_rate < 100: has_minus_exp = True # 수익률 한번이라도 100% 미만인 경우 수익률 기준으로 매도 결정 if has_minus_exp and profit_rate >= 100: pyupbit.sell_all() pyupbit.send_message(pyupbit.get_slack_channel(), f'[구사일생으로 팔았음.-{str(datetime.today())}]' + slack_message1) print('sell!!') else: # 매수할 만 하고 코인 단가가 내가 샀을때 보다 살짝 떨어져 있을 때 추가 매수 -> 일단 막기!! # if target_price >= current_unit_price and 99 >= profit_rate >= 97: # if krw_balance >= 10000: # 추가 매수 기능 막음 # available_coin_amount = pyupbit.get_possible_order_volume(coin_candle, 10000) # pyupbit.order_10000(market, available_coin_amount, 'bid') # pyupbit.send_message('#myinvestment', f'[Buying!!-{str(datetime.today())}]' + slack_message1) # print('buy!!') # 매도 매수 시점 판단 빡침 스코어 기준으로 변경! if score > 5: pyupbit.sell_all() pyupbit.send_message(pyupbit.get_slack_channel(), f'[빡쳐서 팔았음!!-{str(datetime.today())}]' + slack_message1) print('sell!!') # 수익률이 너무 떨어질 것 같을때 매도 elif profit_rate < 99: pyupbit.sell_all() pyupbit.send_message(pyupbit.get_slack_channel(), f'[하락해서 팔았음... -{str(datetime.today())}]' + slack_message1) print('sell...') # 그 외 상태일 경우 else: print('thinking...') # 수익률, 스코어 반환 return [profit_rate, score, has_minus_exp]
def check_my_investment(): profit_rate = 0 myinfo_map = pyupbit.get_my_coin_info() if myinfo_map is not None: # 코인명 market = pyupbit.get_my_coin_name(myinfo_map) # 내가 매수 한 코인 단가 buy_unit_price = pyupbit.get_my_coin_unit_price(myinfo_map) # 분단위 캔들 coin_info = pyupbit.view_candle_min(market) # 코인의 현재 단가(분단위 캔들로 조회) current_my_coin_price = pyupbit.get_current_coin_price(coin_info) # 현재 수익률 profit_rate = pyupbit.get_profit_rate(current_my_coin_price, buy_unit_price) return profit_rate <= 100
def test_candle_min_loop(): market_codes = pyupbit.all_market_names.view_market_codes() for code in market_codes: candle_data = pyupbit.view_candle_min(code) print(candle_data) time.sleep(5)
def get_current_coin_price(candle): return pyupbit.view_candle_min(candle[0]['market'])[0]['trade_price']