본문 바로가기
증권주식경제/파이썬

바이낸스 리벨런싱 봇

by 영혼의 길 2024. 2. 18.
반응형

흠..

 

최근은 아니지만(블로그를 요즘 잘 안 해서) 바이낸스에 리벨런싱 봇이 나왔다.

 

정확히 내가 만든 그대로인데 ㅋㅋㅋㅋ

 

뭐 ETF처럼 일정비율을 유지하는 것이고 리벨런싱은 2, 5% 등 차이가 나는 퍼센트 or 시간으로 세팅할 수 있다.

 

이것도 정말 기본적인 기능이다.

 

그래서 최근의 버전을 올려보려고 한다.

 

내 수준으로  이 이상으로 깔끔하게 만드는 것을 불가능 할 것이다.

(비율설정하는 것도 만들까 하다가... 귀찮아서 포기ㅠ)

 

import ccxt
import requests
import time
import math

def post_message(token, channel, text):
    response = requests.post("https://slack.com/api/chat.postMessage",
        headers={"Authorization": "Bearer "+token},
        data={"channel": channel,"text": text}
    )
    print(response)
 
myToken = "SLACK 토큰"

binance = ccxt.binance({
    'apiKey': 'APIKEY',
    'secret': '비번',
})



coins_of_interest = ['PEPE', 'SOL', 'ETH', 'BTC']

# Define buy and sell thresholds for each coin
thresholds = {
    'PEPE': {'buy': 0.095, 'sell': 0.105},
    'SOL': {'buy': 0.19, 'sell': 0.21},
    'ETH': {'buy': 0.29, 'sell': 0.31},
    'BTC': {'buy': 0.38, 'sell': 0.42},
}
def get_coin_value(coin):
    balance = binance.fetch_balance()[coin]['free']
    ticker = binance.fetch_ticker(f'{coin}/USDT')
    return balance * ticker['close']

total = sum(get_coin_value(coin) for coin in coins_of_interest) + binance.fetch_balance()['USDT']['free']
total = 0.97*total

for coin in coins_of_interest:
    balance = binance.fetch_balance()[coin]['free']
    ticker = binance.fetch_ticker(f'{coin}/USDT')
    value = balance * ticker['close']
    buy_threshold = thresholds[coin]['buy']
    sell_threshold = thresholds[coin]['sell']
    k=value/total
    if value / total <= buy_threshold:
        k=value/total
        amount= total*(((buy_threshold+ sell_threshold)/2)-k)/ticker['close']
        if ticker['close']  < 1 :
            amount=round(amount)
        print(amount)
        binance.create_market_buy_order(f'{coin}/USDT',amount)
        print(f"you are buying {coin}",amount)
        time.sleep(1)
        msg = str(f"you are buying{coin}") + str(amount) + str(round(amount/ticker['close'],2))
        post_message(myToken,"ㅎ",msg)

    elif value / total >= sell_threshold:
        k=value/total
        amount= total*(((buy_threshold+ sell_threshold)/2)-k)/ticker['close']
        if ticker['close']  < 1 :
            amount=round(amount)
        binance.create_market_sell_order(f'{coin}/USDT',amount)
        print(f"you are selling {coin}",amount)
        time.sleep(1)
        msg = str(f"you are selling{coin}") + str(amount) + str(round(amount/ticker['close'],2))
        post_message(myToken,"ㅎ",msg)

 

threshold즉 어느 일정범위를 벗어나면 팔게 만들거나 사게 만드는 것인데 각각 설정해 줘야 한다.

 

BTC는 4 ETH 3 SOL 2 PEPE는 1로 정하고 있고, slack 메신저를 이용해 만약 거래가 일어난다면 핸드폰으로 전송이 되도록 만들었다.

 

뭐 리벨런싱 봇을 정확하게 확인하지는 못 했는데 아마 내가 처음에 생각한 문제들이 존재할지도 모른다고 생각한다.

 

예를 들어서 2%로 설정했을 때(위 코인 기준) 어떤 조건에서 거래가 일어나는가 이다.

 

1. BTC

40%인 btc기준에서 2%는 적은 숫자이다.

정확한 계산은 아니지만 40-> 42가 되면 즉 5%가 상승하면 약 42%가 된다.

 

2. PEPE

10%인 pepe의 기준에서는 2%는 큰 숫자이다.

10-> 12가 되면 20%가 상승해야 바뀌는 숫자인 것이다.

 

즉 BTC와 PEPE는 같은 퍼센트가 아니라 다른 퍼센트에 거래가 된다는 것이다.

 

그래서 5%로 설정해 놓으면 btc는 42 퍼 pepe는 10.5에 거래가 돼야 할 것이다.

 

리벨런싱의 기본은 5%로 알고있는데 이게 코인에도 적용되는지는 또 의문이고 뭐 그렇다.

 

전공이 아니라 백테이터는 포트폴리오 비주얼라이져에서만 백테스팅이 가능하고 ㅋㅋ

 

배우기는 귀찮고 ㅋㅋㅋㅋ

 

끝 은 아니고 블로그에 또 사진을 넣어야 되서 요즘 simple earn에 usdt가 이자를 많이줘서 가져와 봤다.

 

무려 10.21%이다. 

보너스는 7%로 약 500$까지만 적용되니 참고!

 

500$까지는 17%니 안하는게 손해일지도... /참고로 김프 3.5프로로 수수료 3.5뗀다고 생각하는게 좋음.

-대신 다시 한국돈으로 옮길 때 그대로라면 손해는 1xrp정도

반응형

'증권주식경제 > 파이썬' 카테고리의 다른 글

자료정리를 위한 딕션어리  (0) 2021.05.29
파이썬 if, else, elif에 대하여(예시)  (0) 2021.05.02

댓글