File size: 2,926 Bytes
53568ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import requests
import json
import streamlit as st
import pandas as pd
import datetime
from st_aggrid import AgGrid, GridOptionsBuilder, JsCode

url_pos = "https://www.binance.com/bapi/futures/v1/public/future/leaderboard/getOtherPosition"
url = "https://www.binance.com/fr/futures-activity/leaderboard/user?encryptedUid=EE56F412D7DAB7DBAFCEC2147FA2D223"
url_perf = "https://www.binance.com/bapi/futures/v2/public/future/leaderboard/getOtherPerformance"
url_name = "https://www.binance.com/bapi/futures/v2/public/future/leaderboard/getOtherLeaderboardBaseInfo"
headers = {
  'Content-Type': 'application/json',
}

def get_data(id: str):
    payload = json.dumps({
      "encryptedUid": id, 
      "tradeType": "PERPETUAL"
    })

    response = requests.request("POST", url_pos, headers=headers, data=payload)

    x = response.json()["data"]["otherPositionRetList"]
    itog = []
    for i in x:
        if 'updateTime' in i.keys():
            # breakpoint()
            i.pop('updateTime') 
            i.pop('yellow') 
            i.pop('tradeBefore') 
            i["updateTimeStamp"] = datetime.datetime.fromtimestamp(i["updateTimeStamp"]//1000)
            i["roe"] = i["roe"]*100
            i["roe"] = round(i["roe"], 2)
            i["entryPrice"] = round(i["entryPrice"], 3)
            i["markPrice"] = round(i["markPrice"], 3)
            i["pnl"] = round(i["pnl"], 2)

            itog.append(pd.DataFrame.from_dict([i]))
    itog = pd.concat(itog , axis=0, ignore_index=True)[["symbol", "amount", "leverage","entryPrice","markPrice", "pnl", "roe","updateTimeStamp"]]
    return itog


def get_name(id: str):
    payload = json.dumps({
      "encryptedUid": id 
    })

    response = requests.request("POST", url_name, headers=headers, data=payload)
    x = response.json()["data"]["nickName"]
    return x
    

def get_table(id, name):
    df = get_data(id)
    gd= GridOptionsBuilder.from_dataframe(df)
    cellstyle_jscode = JsCode("""
    function(params){
        if (params.value < '0') {
            return{
                'color': 'red'
            }
        }
        if (params.value > '0') {
            return{
                'color': 'green'
            }
        }
    }
    """)
    gd.configure_default_column(min_column_width=1)
    gd.configure_columns(column_names= ["pnl", "roe", "amount"], cellStyle= cellstyle_jscode)
    grid_options = gd.build()
    st.header(name)
    return AgGrid(df, gridOptions=grid_options, allow_unsafe_jscode=True, fit_columns_on_grid_load=True)

with open("./links.txt", 'r') as f:
    links = f.readlines()
    links = [i.strip().replace("https://www.binance.com/fr/futures-activity/leaderboard/user?encryptedUid=", "") for i in links]
    links = {get_name(i):i for i in links}

print(links)


st.set_page_config(layout="wide")

op = st.multiselect("traders", options=links.keys())
print(op)
for i, name in enumerate(op):
    get_table(links[name], name)