반응형

 안녕하세요. 이번 포스팅에서는 tkinter의 위젯을 배치하는 함수에 대해 포스팅하겠습니다.

 

1. 함수 소개

위젯을 배치하는 함수는 pack, place, grid 이렇게 세 가지 종류가 있습니다. 간략한 설명은 아래와 같습니다.

place - 절대 위치

grid - 격자 배치

pack - 상대 위치

 place는 위젯을 놓을 위치의 절대좌표(x, y 좌표)를 입력해서 사용할 수 있습니다. 직접 좌표를 입력해서 배치하기 때문에 정확한 위치에 배치할 수 있습니다.

 grid는 위젯을 놓을 위치를 행(column)과 열(row)을 입력하여 배치합니다. 바둑판이나 엑셀과 같다고 생각하면 좋을 것 같습니다.

 pack은 상대 위치로 윈도우의 상하좌우 등에 배치할 수 있으며, 윈도우의 크기를 변경하면, 변하는 크기에 따라 움직입니다.

 

2. place

 윈도우 창의 좌상단을 원점으로(0,0) 좌우를 x축, 상하를 y축으로 구성되어 있습니다. 각각 우측과 아래 방향이 양(+)의 방향입니다. x와 y에 숫자를 입력해, 원하는 위치에 위젯을 배치할 수 있습니다.

import tkinter

window = tkinter.Tk()
window.geometry('300x300')

lb_1 = tkinter.Label(text='1번 라벨', background='green')
lb_2 = tkinter.Label(text='2번 라벨', background='green')
lb_3 = tkinter.Label(text='3번 라벨', background='green')

lb_1.place(x=220, y=50)
lb_2.place(x=20, y=100)
lb_3.place(x=150, y=210)

window.mainloop()

 

tkinter place

 

3. grid

 grid는 격자구조로 행과 열에 맞추어 배치할 수 있습니다. 행은 column에, 열은 row에 원하는 숫자를 대입해 위젯을 배치할 수 있습니다. 좌우가 row이고, 상하가 column입니다.

import tkinter

window = tkinter.Tk()
window.geometry('300x300')

lb_1 = tkinter.Label(text='1번 라벨', background='green')
lb_2 = tkinter.Label(text='2번 라벨', background='green')
lb_3 = tkinter.Label(text='3번 라벨', background='green')
lb_4 = tkinter.Label(text='4번 라벨', background='green')
lb_5 = tkinter.Label(text='5번 라벨', background='green')
lb_6 = tkinter.Label(text='6번 라벨', background='green')
lb_7 = tkinter.Label(text='7번 라벨', background='green')
lb_8 = tkinter.Label(text='8번 라벨', background='green')
lb_9 = tkinter.Label(text='9번 라벨', background='green')

lb_1.grid(row=1,column=1)
lb_2.grid(row=1,column=2)
lb_3.grid(row=1,column=3)
lb_4.grid(row=2,column=1)
lb_5.grid(row=2,column=2)
lb_6.grid(row=2,column=3)
lb_7.grid(row=3,column=1)
lb_8.grid(row=3,column=2)
lb_9.grid(row=3,column=3)

window.mainloop()

 

tkinter grid

 

4. pack

 pack은 윈도우 상대적인 위치에 배치할 수 있습니다. 추가 요소를 주지 않고 pack()만 사용하게 되면 아래와 같이 중앙 상단부터 내려가게 됩니다.

import tkinter

window = tkinter.Tk()
window.geometry('300x300')

lb_1 = tkinter.Label(text='1번 라벨', background='green')
lb_2 = tkinter.Label(text='2번 라벨', background='green')
lb_3 = tkinter.Label(text='3번 라벨', background='green')
lb_4 = tkinter.Label(text='4번 라벨', background='green')
lb_5 = tkinter.Label(text='5번 라벨', background='green')

lb_1.pack()
lb_2.pack()
lb_3.pack()
lb_4.pack()
lb_5.pack()

window.mainloop()

 

tkinter pack

 

side 값을 주게 된다면 그에 해당하는 위치에 배치할 수 있습니다. pack은 앞서 말했듯이 상대적인 위치에 배치하기 때문에 윈도우의 크기를 변경해도, 변하는 윈도우에 맞춰 각각의 위치에 있게 됩니다.

import tkinter

window = tkinter.Tk()
window.geometry('300x300')

lb_1 = tkinter.Label(text='1번 라벨', background='green')
lb_2 = tkinter.Label(text='2번 라벨', background='green')
lb_3 = tkinter.Label(text='3번 라벨', background='green')
lb_4 = tkinter.Label(text='4번 라벨', background='green')

lb_1.pack(side='right')
lb_2.pack(side='left')
lb_3.pack(side='top')
lb_4.pack(side='bottom')

window.mainloop()

 

 

 이번 포스팅은 tkinter의 위젯들을 배치하는 방법에 대한 포스팅을 작성해봤습니다. 읽어주셔서 감사합니다.

반응형
반응형

 안녕하세요. 이번 포스팅에서는 'tkinter' 패키지를 이용해 GUI를 만들어보겠습니다.

 GUI란? 그래픽 사용자 인터페이스(graphical user interface)의 약자로, 컴퓨터를 사용하면서 화면 위의 틀이나 색상과 같은 그래픽 요소들을 기능과 용도를 나타내기 위해 고안된 컴퓨터 인터페이스라고 합니다. 

 tkinter을 이용해 새로운 윈도우 창을 만들고 몇 가지 기능들을 넣어보겠습니다. 이번 포스팅에서는 간단한 기능들만 다루고 추가적인 기능은 차후에 포스팅하도록 하겠습니다.

 

1. 윈도우 생성 및 출력

import tkinter

window = tkinter.Tk()
window.title("Bitcoding's window")
window.geometry('400x400')

window.mainloop()

 윈도우의 제목과 크기에 대해 설정하였습니다. 다른 것들은 넣은게 없기 때문에 텅 비어있는 모습입니다.

 

tkinter window

 

2. 레이블을 이용한 텍스트 출력

 레이블은 tkinter에서 텍스트를 배치하기 위해 사용합니다. 간단한 레이블을 배치해보겠습니다.

import tkinter

window = tkinter.Tk()
window.title("Bitcoding's window")
window.geometry('400x400')

lb_1 = tkinter.Label(text='안녕하세요. Bitcoding 입니다.')
lb_1.pack()

window.mainloop()

 

 '안녕하세요. Bitcoding 입니다' 라는 텍스트가 정상적으로 출력된 모습입니다. 

tkinter Label

 

3. 클릭시 작동하는 버튼 생성

 클릭시 window를 닫는 버튼을 생성해 보겠습니다.

import tkinter

def close():
    window.destroy()

window = tkinter.Tk()
window.title("Bitcoding's window")
window.geometry('400x400')

lb_1 = tkinter.Label(text='안녕하세요. Bitcoding 입니다.')
lb_1.pack()

bt_1 = tkinter.Button(text='창닫기', command = close)
bt_1.pack()

window.mainloop()

 

 close를 정의하고 command에 연결해, 버튼을 클릭하면 창이 닫히도록 했습니다. 

close window

4. 윈도우에서 입력 받기

 지금까지는 출력하는 것이었다면, 이번에는 윈도우 창에서 간단한 수식을 입력받아 계산해보겠습니다.

import tkinter

def close():
    window.destroy()

def calculate():
    lb_2.configure(text='결과: {}'.format(str(eval(entry_1.get()))))

window = tkinter.Tk()
window.title("Bitcoding's window")
window.geometry('400x400')

lb_1 = tkinter.Label(text='안녕하세요. Bitcoding 입니다.')
lb_1.pack()

bt_1 = tkinter.Button(text='창닫기', command = close)
bt_1.pack()

entry_1 = tkinter.Entry()
entry_1.pack()

bt_2 = tkinter.Button(text='계산', command = calculate)
bt_2.pack()

lb_2 = tkinter.Label()
lb_2.pack()

window.mainloop()

 

 entry 를 이용해 수식을 입력받은 뒤, 계산 버튼을 클릭해 결과를 출력합니다.

tkinter Entry

 

 이번 포스팅에서는 tkinter 패키지를 이용해 새로운 윈도우 창을 만들어서 간단하게 출력하고 수식을 입력받아 계산하여 출력하는 코드를 작성해 봤습니다. 다른 기능이나 구체적인 설정 등 추가적인 부분은 향후 포스팅 할 계획입니다. 읽어주셔서 감사합니다.

반응형

'Python > GUI - tkinter' 카테고리의 다른 글

파이썬 tkinter을 이용해 GUI 만들기 - 위젯 배치  (0) 2022.11.02
반응형

 

안녕하세요. 이번 포스팅에서는 바이낸스의 usds-m의 선물 포지션을 조회하고, 새로운 창을 열어서 출력해보겠습니다. 포지션을 조회하는 데는 'ccxt'라는 패키지를, 새로운 창에 출력하는 것은 'tkinter'을 사용해 코딩해보겠습니다. 향후 다른 것들도 추가적으로 출력하는 것을 포스팅할 계획입니다.

 

1. ccxt를 이용한 바이낸스 선물 포지션 조회

import ccxt
import os
import dotenv

dotenv_file = dotenv.find_dotenv()
dotenv.load_dotenv(dotenv_file)
apikey = os.environ['bn_apikey']
apisecret = os.environ['bn_secret']

binance = ccxt.binance(config={'apiKey': apikey, 'secret': apisecret})
balance = binance.fetch_balance(params={"type": "future"})
positions = balance['info']['positions']

print(positions)

dotenv를 이용해 환경변수 저장하는 방법은 아래 포스팅을 참고해주세요.

2022.10.05 - [Python] - 파이썬 dotenv를 이용한 환경변수 저장 및 호출

 

파이썬 dotenv를 이용한 환경변수 저장 및 호출

 블로그 만든 뒤에 처음 작성하는 글이네요. 블로그에 공유하기 위해 중요한 정보(암호) 등을 환경변수로 지정해 다른 파일에 저장하고, 공유시에 다른 사람들에게 노출되는 것을 막고자 합니

bitcoding.tistory.com

 

 

 필요한 패키지들을 import 해주고, 환경변수를 불러온 뒤, 바이낸스의 모든 usds-m의 선물 포지션을 조회하고 출력하는 코드입니다. 해당 코드에서 positions을 출력하면 아래와 같이 usds-m의 모든 페어의 포지션을 출력하게 됩니다.

[{'symbol': 'RAYUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'API3USDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'SUSHIUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'CVCUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'BTSUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'FTMBUSD', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'LINKBUSD', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'SANDBUSD', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'INJUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'HOTUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'TRXBUSD', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'ZRXUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'QTUMUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'IOTAUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'BTCBUSD', 'initialMargin': '111.56959712', 'maintMargin': '0.39084988', 'unrealizedProfit': '-0.25647121', 'positionInitialMargin': '9.77124712', 'openOrderInitialMargin': '101.79835000', 'leverage': '10', 'isolated': True, 'entryPrice': '19491.2', 'maxNotional': '7500000', 'positionSide': 'BOTH', 'positionAmt': '-0.005', 'notional': '-97.71247121', 'isolatedWallet': '9.74774889', 'updateTime': '1666022402680', 'bidNotional': '0', 'askNotional': '1017.98350000'}, {'symbol': 'WAVESUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'SPELLUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'ADAUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '250000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'LITUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'XTZUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '250000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'BNBUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '250000', 'positionSide': 'BOTH', 'positionAmt': '0.00', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'UNIBUSD', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'DARUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 
'HNTUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'ETCUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': True, 'entryPrice': '0.0', 'maxNotional': '250000', 'positionSide': 'BOTH', 'positionAmt': '0.00', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '1647929253005', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'XMRUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '250000', 'positionSide': 'BOTH', 'positionAmt': '0.000', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'YFIUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.000', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'FTTBUSD', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '100000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'ETHUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '10', 'isolated': True, 'entryPrice': '0.0', 'maxNotional': '10000000', 'positionSide': 'BOTH', 'positionAmt': '0.000', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '1663227945832', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'ALICEUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'ALPHAUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'WOOUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'SFPUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'REEFUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'BATUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'DOGEUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '250000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'TRXUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '250000', 'positionSide': 'BOTH', 'positionAmt': '0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'}, {'symbol': 'RLCUSDT', 'initialMargin': '0', 'maintMargin': '0', 'unrealizedProfit': '0.00000000', 'positionInitialMargin': '0', 'openOrderInitialMargin': '0', 'leverage': '20', 'isolated': False, 'entryPrice': '0.0', 'maxNotional': '25000', 'positionSide': 'BOTH', 'positionAmt': '0.0', 'notional': '0', 'isolatedWallet': '0', 'updateTime': '0', 'bidNotional': '0', 'askNotional': '0'},

 상당히 길고 복잡한 결과가 나오게 되는데, 여기서 현재 내가 오픈한 포지션만 조회하도록 추가해보겠습니다.

 

2. 오픈된 포지션 조회

import ccxt
import os
import dotenv

dotenv_file = dotenv.find_dotenv()
dotenv.load_dotenv(dotenv_file)
apikey = os.environ['bn_apikey']
apisecret = os.environ['bn_secret']

binance = ccxt.binance(config={'apiKey': apikey, 'secret': apisecret})
balance = binance.fetch_balance(params={"type": "future"})
positions = balance['info']['positions']

symbol = []
size = []
pnl = []
for position in positions:
    if position['initialMargin'] != '0':
        symbol.append(position['symbol'])
        size.append(position['notional'])
        pnl.append(float(position['unrealizedProfit']))

print(symbol)
print(size)
print(pnl)

 앞선 코드에서 받아온 positions의 initialMargin이 없는 경우를 제외하고, 해당 position의 symbol, size, pnl을 각각의 변수에 저장하고 출력하는 코드입니다. 결과는 아래와 같습니다.

['BTCBUSD', 'ETHBUSD']
['-97.67867686', '5.33319399']
[-0.22267686, 0.06211399]

 이것은 현재 오픈되어 있는 제 포지션입니다. BTCBUSD페어를 97.678 $의 숏포지션을 보유 중이며, pnl은 -0.2226$로 손실 중입니다. ETHBUSD페어는 5.333 $의 롱포지션을 보유 중이며 0.06211$의 수익을 보고 있습니다.

 다른 정보들도 조회 가능하지만, Symbol과 Size, pnl만 출력하고자 합니다.

 

3. tkinter를 이용한 윈도우 창 생성

import tkinter

win = tkinter.Tk ()
win.title("Traiding Tools")
win.configure(background='black')
win.geometry('200x200+165+200')

win.mainloop()

먼저 tkinter을 import 해주고 윈도우 창에 대한 설정들을 추가해줍니다. 창의 이름을 'Trading Tools'로 지정하고, 배경색을 검정으로 지정했습니다. 코드의 6번째 줄의 "win.geometry('200x200+165+200')"를 통해서 창의 크기와 위치를 지정할 수 있습니다. 200x200은 창의 크기이고, 뒤의 165+200은 코드 실행 시 창이 나타나는 위치를 지정한 것입니다. 코드를 실행하면 아래와 같이 검은 배경의 창이 실행되는 것을 볼 수 있습니다.

tkinter을 이용한 윈도우 창 출력

 

 이번 포스팅에서는 ccxt를 이용해 오픈된 포지션을 조회하고. tkinter를 이용해 윈도우 창을 출력해보았습니다. 다음 포스팅에서는 조회한 오픈된 포지션과 현재시간을 tkinter를 이용해 출력하는 것에 대해 이어서 포스팅하겠습니다. 읽어주셔서 감사합니다.

반응형

+ Recent posts