|
import pandas as pd |
|
import streamlit as st |
|
import gspread |
|
from google.oauth2.service_account import Credentials |
|
import ast |
|
import requests |
|
from datetime import datetime |
|
import boto3 |
|
import json |
|
|
|
start = False |
|
starting_position = [] |
|
tradeHistory_positions = [] |
|
|
|
s3 = boto3.resource( |
|
service_name = 's3', |
|
region_name = 'ap-south-1', |
|
aws_access_key_id = 'AKIA3TD2SOLYZML62HJR', |
|
aws_secret_access_key ='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' |
|
) |
|
|
|
bucket_name = 'usdsmcoinmdata' |
|
|
|
|
|
file_mapping = { |
|
"usdm": "usdm_trade_history.csv", |
|
"coinm": "coinm_trade_history.csv", |
|
"copyLeaderboard": "copyLeaderboard_trade_history.csv", |
|
"okx": "okx_history.csv" |
|
} |
|
|
|
|
|
st.title("Trade History Viewer") |
|
|
|
|
|
option = st.selectbox("Choose the trade history to display:", list(file_mapping.keys())) |
|
|
|
|
|
|
|
if option: |
|
file_key = file_mapping[option] |
|
try: |
|
|
|
obj = s3.Bucket(bucket_name).Object(file_key).get() |
|
|
|
|
|
df = pd.read_csv(obj['Body'], index_col=False) |
|
|
|
|
|
st.write(f"Displaying data for: **{option}**") |
|
except Exception as e: |
|
st.error(f"Error fetching the file: {str(e)}") |
|
|
|
|
|
|
|
|
|
df2 = pd.read_csv('df.csv') |
|
|
|
|
|
def convert_str_to_list_or_keep(value): |
|
if isinstance(value, str): |
|
try: |
|
return ast.literal_eval(value) |
|
except (SyntaxError, ValueError): |
|
return value |
|
else: |
|
return value |
|
|
|
df = df.apply(lambda col: col.map(convert_str_to_list_or_keep)) |
|
df2 = df2.apply(lambda col: col.map(convert_str_to_list_or_keep)) |
|
|
|
df['positionClosed'] = False |
|
|
|
if option =="copyLeaderboard": |
|
uid_input = int(st.text_input("Enter U_IDs to filter")) |
|
|
|
else: |
|
uid_input = str.upper(st.text_input("Enter U_IDs to filter")) |
|
|
|
|
|
option2 = st.radio("Choose an option:", ["Show Position History", "Show Live Positions"]) |
|
|
|
if df is not None and uid_input: |
|
|
|
if option2 == "Show Position History": |
|
st.title("Position History Viewer") |
|
|
|
st.header("Starting Positions") |
|
|
|
|
|
filtered_df = df[df['U_IDs'] == uid_input].copy() |
|
|
|
if not filtered_df.empty: |
|
trade_list = filtered_df['trade_history'].iloc[0] |
|
else: |
|
st.write("No data found for the provided U_ID.") |
|
|
|
|
|
unique_lists = [] |
|
|
|
def get_amounts_from_positions_and_closed_trades(data): |
|
|
|
|
|
|
|
if 'Modified' in data: |
|
modified_positions = data['Modified'] |
|
|
|
if isinstance(modified_positions, dict) and 'amount' in modified_positions: |
|
amount = modified_positions.get('amount') |
|
if isinstance(amount, (int, float)): |
|
amounts =amount |
|
|
|
if 'ClosedTrades' in data: |
|
closed_trades = data['ClosedTrades'] |
|
closed_trades =closed_trades[0] |
|
if isinstance(closed_trades, dict) and 'amount' in closed_trades: |
|
amount = closed_trades.get('amount') |
|
if isinstance(amount, (int, float)): |
|
amounts = amount |
|
|
|
return amounts |
|
|
|
def get_symbols_from_positions_and_closed_trades(data): |
|
|
|
|
|
|
|
if 'Modified' in data: |
|
modified_positions = data['Modified'] |
|
|
|
if isinstance(modified_positions, dict) and 'symbol' in modified_positions: |
|
symbol = modified_positions['symbol'] |
|
|
|
|
|
if 'ClosedTrades' in data: |
|
closed_trades = data['ClosedTrades'] |
|
closed_trades =closed_trades[0] |
|
if isinstance(closed_trades, dict) and 'symbol' in closed_trades: |
|
symbol = closed_trades['symbol'] |
|
|
|
return symbol |
|
|
|
for i in range(len(trade_list)): |
|
|
|
|
|
if trade_list[i]=="none": |
|
continue |
|
|
|
if not trade_list: |
|
st.header("No data found, this may not be in the leaderboard") |
|
|
|
if start ==False: |
|
st.subheader(f"Data is from {datetime.now()}") |
|
start =True |
|
foundCLosed = False |
|
changeInAmount = 0 |
|
|
|
if 'symbol' in trade_list[i]: |
|
symbol = trade_list[i]['symbol'] |
|
side ="buy" if trade_list[i]['amount']>0 else "sell" |
|
amount = trade_list[i]['amount'] |
|
symbol = trade_list[i]['symbol'] |
|
trade_list[i]['side'] =side |
|
trade_list[i]['changeInAmount'] = changeInAmount |
|
trade_list[i]['i'] = i |
|
unique_lists.append({"position":trade_list[i]}) |
|
trade_list[i] = "none" |
|
|
|
|
|
else: |
|
if 'positions' in trade_list[i]: |
|
reached = False |
|
|
|
for k, v in list(trade_list[i].items()): |
|
for entry in v: |
|
if 'NewPosition' in entry: |
|
new_position = entry.get('NewPosition', {}) |
|
|
|
symbol = new_position.get('symbol') |
|
amount = new_position.get('amount') |
|
if option != "copyLeaderBoard": |
|
if start==False: |
|
start_time = new_position.get('updateTime') |
|
year = start_time[0] |
|
month = start_time[1] |
|
day = start_time[2] |
|
hour =start_time[3] |
|
minute =start_time[4] |
|
seconds = start_time[5] |
|
dt = datetime(year, month, day, hour, minute, seconds) |
|
human_readable_format = dt.strftime('%B %d, %Y, %I:%M:%S %p') |
|
st.subheader(f"Data from {human_readable_format}") |
|
start=True |
|
|
|
|
|
|
|
side = "buy" if amount > 0 else "sell" |
|
new_position['side'] = side |
|
new_position['changeInAmount'] = changeInAmount |
|
new_position['i'] = i |
|
|
|
entry['NewPosition'] = new_position |
|
|
|
|
|
unique_lists.append(trade_list[i]) |
|
|
|
reached = True |
|
|
|
|
|
if reached: |
|
trade_list[i] = "none" |
|
|
|
|
|
|
|
|
|
for j in range(i+1, len(trade_list)): |
|
if trade_list[j] == "none": |
|
continue |
|
|
|
if 'positions' in trade_list[j] and isinstance(trade_list[j]['positions'], list): |
|
for position in trade_list[j]['positions']: |
|
|
|
if 'Modified' in position and isinstance(position['Modified'], dict): |
|
if option!="copyLeaderboard": |
|
if start==False: |
|
for k,v in position.items(): |
|
start_time = v['updateTime'] |
|
|
|
year = start_time[0] |
|
month = start_time[1] |
|
day = start_time[2] |
|
hour =start_time[3] |
|
minute =start_time[4] |
|
seconds = start_time[5] |
|
dt = datetime(year, month, day, hour, minute, seconds) |
|
human_readable_format = dt.strftime('%d-%m-%Y %H:%M:%S') |
|
st.subheader(f"Data from {human_readable_format}") |
|
start=True |
|
modified_amount = get_amounts_from_positions_and_closed_trades(position) |
|
modified_symbol = get_symbols_from_positions_and_closed_trades(position) |
|
|
|
if modified_amount > 0: |
|
modified_side = "buy" |
|
else: |
|
modified_side = "sell" |
|
|
|
if symbol == modified_symbol and side == modified_side: |
|
if start ==False: |
|
st.header(f"Data is from {datetime.now}") |
|
start =True |
|
position['Modified']['side'] = modified_side |
|
position['Modified']['changeInAmount'] = amount - modified_amount if modified_amount < 0 else modified_amount - amount |
|
position['Modified']['i'] = i |
|
amount = modified_amount |
|
unique_lists.append(trade_list[j]) |
|
trade_list[j] = "none" |
|
|
|
|
|
if 'ClosedTrades' in position and isinstance(position['ClosedTrades'], tuple): |
|
if start ==False: |
|
st.header(f"Data is from {datetime.now}") |
|
start =True |
|
foundCLosed = False |
|
closed_trades_tuple = position['ClosedTrades'] |
|
closed_trades_dict = { |
|
'trade_info': closed_trades_tuple[0], |
|
'side': closed_trades_tuple[1] |
|
} |
|
|
|
closed_amount = get_amounts_from_positions_and_closed_trades(position) |
|
closed_symbol = get_symbols_from_positions_and_closed_trades(position) |
|
|
|
if closed_amount > 0: |
|
closed_side = "buy" |
|
else: |
|
closed_side = "sell" |
|
|
|
if symbol == closed_symbol and side == closed_side: |
|
if option!= "copyLeaderboard": |
|
if start==False: |
|
for k,v in position.items(): |
|
start_time = v['updateTime'] |
|
start =True |
|
|
|
closed_trades_dict['side'] = closed_side |
|
trade_info = closed_trades_dict['trade_info'] |
|
trade_info['changeInAmount'] = amount - closed_amount if closed_amount < 0 else closed_amount - amount |
|
amount = closed_amount |
|
closed_trades_dict['trade_info']['i'] = i |
|
closed_trades_dict['trade_info']['closed'] = True |
|
|
|
|
|
unique_lists.append(trade_list[j]) |
|
trade_list[j] = "none" |
|
foundCLosed = True |
|
break |
|
|
|
|
|
if foundCLosed: |
|
break |
|
|
|
|
|
|
|
|
|
for k in range(len(unique_lists)): |
|
data = unique_lists[k] |
|
|
|
|
|
if k ==0: |
|
|
|
|
|
if 'positions' in data: |
|
if isinstance(data['positions'], list): |
|
for a in data['positions']: |
|
if 'NewPosition' in a: |
|
|
|
position_data = a['NewPosition'] |
|
starting_position.append(position_data) |
|
tradeHistory_positions.append(position_data) |
|
|
|
|
|
else: |
|
if 'position' in data: |
|
position_data =data['position'] |
|
starting_position.append(position_data) |
|
tradeHistory_positions.append(position_data) |
|
|
|
|
|
|
|
if 'positions' in data: |
|
if isinstance(data['positions'],list): |
|
for a in data['positions']: |
|
if 'ClosedTrades' in a: |
|
position_data = a['ClosedTrades'][0] |
|
tradeHistory_positions.append(position_data) |
|
|
|
|
|
|
|
if 'positions' in data: |
|
if isinstance(data['positions'],list): |
|
for a in data['positions']: |
|
if 'Modified' in a: |
|
position_data = a['Modified'] |
|
tradeHistory_positions.append(position_data) |
|
|
|
|
|
|
|
|
|
unique_lists =[] |
|
|
|
elif option2 == "Show Live Positions": |
|
filtered_df2 = df2[df2['U_IDs'] == uid_input] |
|
|
|
if not filtered_df2.empty: |
|
|
|
positions_list = filtered_df2['Positions'].iloc[0] |
|
|
|
|
|
if isinstance(positions_list, list) and positions_list: |
|
positions_df = pd.DataFrame(positions_list) |
|
st.subheader("Live Positions") |
|
st.dataframe(positions_df) |
|
else: |
|
st.write("No live positions data available for the given U_ID.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def show_position_history(selected_position): |
|
st.header(f"History for {selected_position}") |
|
|
|
|
|
position_history = [pos for pos in tradeHistory_positions if pos['i'] == selected_position] |
|
|
|
|
|
|
|
if position_history: |
|
df_history = pd.DataFrame(position_history) |
|
|
|
if option =="copyLeaderboard": |
|
df_history['changeInAmount'] = pd.to_numeric(df_history['changeInAmount'], errors='coerce') |
|
df_history['markPrice'] = pd.to_numeric(df_history['markPrice'], errors='coerce') |
|
df_history['entryPrice'] = pd.to_numeric(df_history['entryPrice'], errors='coerce') |
|
|
|
df_history['amount'] = pd.to_numeric(df_history['amount'],errors='coerce') |
|
|
|
|
|
df_history.fillna(0, inplace=True) |
|
|
|
|
|
|
|
columns_to_check = [ |
|
'symbol', 'side', 'amount', 'changeInAmount', 'markPrice', |
|
'entryPrice', 'pnl', 'roe', 'leverage', 'updateTime', |
|
'tradeType', 'stopLossPrice', 'takeProfitPrice', 'weightedScoreRatio' |
|
] |
|
|
|
|
|
for column in columns_to_check: |
|
if column not in df_history.columns: |
|
df_history[column] = None |
|
|
|
|
|
df_transformed = pd.DataFrame({ |
|
'Pair/Asset': df_history['symbol'], |
|
'is long': df_history['side'], |
|
'Current size after change': df_history['amount'], |
|
'Change in size in Asset': df_history['changeInAmount'], |
|
'Change in size in USDT': df_history['changeInAmount'] * -(df_history['markPrice']), |
|
'Entry price': df_history['entryPrice'], |
|
'Exit price': df_history['markPrice'], |
|
'pnl in usdt': df_history['pnl'], |
|
'pnl in %': df_history['roe'], |
|
'Leverage': df_history['leverage'], |
|
'updatedTime': df_history['updateTime'], |
|
'Trade Type': df_history['tradeType'], |
|
'Stop Loss Price': df_history['stopLossPrice'], |
|
'Take Profit Price': df_history['takeProfitPrice'], |
|
'Weighted Score Ratio': df_history['weightedScoreRatio'], |
|
'Transaction Value in USDT': df_history['amount'] * df_history['markPrice'], |
|
'Profit/Loss Ratio': (df_history['markPrice'] - df_history['entryPrice']) / df_history['entryPrice'] |
|
}) |
|
|
|
if 'closed' in df_history.columns: |
|
df_transformed['Position closed'] = df_history['closed'] |
|
|
|
st.dataframe(df_transformed) |
|
|
|
|
|
|
|
else: |
|
st.write("No history found for this position.") |
|
|
|
def lastUpdated(selected_position): |
|
position_history = [pos for pos in tradeHistory_positions if pos['i'] == selected_position] |
|
return position_history[-1]['updateTime'] |
|
|
|
def isClosed(selected_position): |
|
|
|
position_history = [pos for pos in tradeHistory_positions if pos['i'] == selected_position] |
|
|
|
|
|
if not position_history: |
|
return False |
|
|
|
|
|
last_entry = position_history[-1] |
|
|
|
|
|
return last_entry.get('closed', False) |
|
|
|
|
|
|
|
|
|
def main(): |
|
df_starting = pd.DataFrame(starting_position) |
|
|
|
for index, row in df_starting.iterrows(): |
|
side = True if row['amount'] > 0 else False |
|
is_closed = isClosed(row['i']) |
|
|
|
|
|
button_key = f"position_{row['i']}" |
|
|
|
|
|
if option == "copyLeaderboard": |
|
if st.button( |
|
f"{row['symbol']} : Long: {side}, Entry Price: {row['entryPrice']}, " |
|
f"Market Price: {row['markPrice']}, Amount: {row['amount']}, " |
|
f"Leverage: {row['leverage']}, " |
|
f" isClosed: {is_closed}", |
|
key=button_key |
|
): |
|
show_position_history(row['i']) |
|
else: |
|
if st.button( |
|
f"{row['symbol']} : Long: {side}, Entry Price: {row['entryPrice']}, " |
|
f"Market Price: {row['markPrice']}, Amount: {row['amount']}, " |
|
f"Leverage: {row['leverage']}, TradeTakenAt: {row['updateTime']}, " |
|
f"lastUpdated: {lastUpdated(row['i'])}, isClosed: {is_closed}", |
|
key=button_key |
|
): |
|
show_position_history(row['i']) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|