File size: 23,797 Bytes
f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 1af2ab2 510a7ae 116a154 f1ac821 116a154 f1ac821 1500a29 510a7ae 1500a29 116a154 1500a29 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 510a7ae f1ac821 510a7ae f1ac821 116a154 f1ac821 510a7ae f1ac821 510a7ae 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 510a7ae f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 f1ac821 116a154 510a7ae 1500a29 510a7ae 116a154 f1ac821 |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
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
# Define the scope
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 for options
file_mapping = {
"usdm": "usdm_trade_history.csv",
"coinm": "coinm_trade_history.csv",
"copyLeaderboard": "copyLeaderboard_trade_history.csv",
"okx": "okx_history.csv",
"buybit": "buybit.csv"
}
# Streamlit App
st.title("Trade History Viewer")
# Dropdown to select the trade history
option = st.selectbox("Choose the trade history to display:", list(file_mapping.keys()))
# Fetch and display the corresponding trade history
if option:
file_key = file_mapping[option]
try:
# Fetch the file from S3
obj = s3.Bucket(bucket_name).Object(file_key).get()
# Read the CSV into a DataFrame
df = pd.read_csv(obj['Body'], index_col=False)
# Display the DataFrame in Streamlit
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 =="buybit":
uid_input = (st.text_input("Enter U_IDs to filter"))
elif 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")
# Display starting positions with clickable rows
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):
# Check if 'Modified' key exists and extract amounts
if 'Modified' in data:
modified_positions = data['Modified']
# modified_positions = modified_positions[0]
if isinstance(modified_positions, dict) and 'amount' in modified_positions:
if option =="okx":
print("reached")
amount = modified_positions.get('amount')/modified_positions.get('markPrice')
else:
amount = modified_positions.get('amount')
if isinstance(amount, (int, float)): # Check if amount is a number
amounts =amount
# Check if 'ClosedTrades' key exists and extract amounts
if 'ClosedTrades' in data:
closed_trades = data['ClosedTrades']
closed_trades =closed_trades[0]
if isinstance(closed_trades, dict) and 'amount' in closed_trades:
if option == "okx":
amount = closed_trades.get('amount')/closed_trades.get('markPrice')
else:
amount = closed_trades.get('amount')
if isinstance(amount, (int, float)): # Check if amount is a number
amounts = amount
return amounts
def get_symbols_from_positions_and_closed_trades(data):
# Check if 'Modified' key exists and extract symbols
if 'Modified' in data:
modified_positions = data['Modified']
# modified_positions =modified_positions
if isinstance(modified_positions, dict) and 'symbol' in modified_positions:
symbol = modified_positions['symbol']
# Check if 'ClosedTrades' key exists and extract symbols
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: # Check if the trade_list is empty
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']
if option =="okx":
amount = trade_list[i]['amount']/trade_list[i]['markPrice']
else:
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
# Collect necessary data first before modifying the dictionary
for k, v in list(trade_list[i].items()): # Convert to a list to avoid modifying during iteration
for entry in v:
if 'NewPosition' in entry:
new_position = entry.get('NewPosition', {})
# Extract symbol and amount
symbol = new_position.get('symbol')
if option =="okx":
amount = new_position.get('amount')/new_position.get('markPrice')
else:
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
# if start==False:
#
# start =True
side = "buy" if amount > 0 else "sell"
new_position['side'] = side
new_position['changeInAmount'] = changeInAmount
new_position['i'] = i
# Update the entry with the modified 'NewPosition'
entry['NewPosition'] = new_position
# Append the updated trade_list[i] to unique_lists
unique_lists.append(trade_list[i])
reached = True
# Now safely modify the dictionary after iteration is complete
if reached:
trade_list[i] = "none"
# Now safely modify the dictionary after iteration is complete
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']:
# Check if 'Modified' is in the position and is a dict
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"
# Check if 'ClosedTrades' is in the position and is a tuple
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 # Store index 'i' inside 'ClosedTrades'
closed_trades_dict['trade_info']['closed'] = True
# Append the updated trade_list[j] to unique_lists
unique_lists.append(trade_list[j])
trade_list[j] = "none"
foundCLosed = True
break
# Break the inner loop if a closed trade was found
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] # Extract the first match
# Convert the list of dictionaries to a DataFrame
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.")
# data3 = sheet3.get_all_values()
# headers3 = data3.pop(0)
# df3 = pd.DataFrame(data3, columns=headers3)
# filtered_df3 = df3[df3['U_IDs'] == uid_input]
# st.subheader("Performace")
# st.dataframe(filtered_df3)
def show_position_history(selected_position):
st.header(f"History for {selected_position}")
# Filter trade history for the 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')
# Replace NaN with 0 or handle as required
df_history.fillna(0, inplace=True)
# Update the global timestamp with the last update from history
columns_to_check = [
'symbol', 'side', 'amount', 'changeInAmount', 'markPrice',
'entryPrice', 'pnl', 'roe', 'leverage', 'updateTime',
'tradeType', 'stopLossPrice', 'takeProfitPrice', 'weightedScoreRatio'
]
# Adding missing columns with None as default
for column in columns_to_check:
if column not in df_history.columns:
df_history[column] = None
# Create a transformed DataFrame for display
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'], # New field
'Stop Loss Price': df_history['stopLossPrice'], # New field
'Take Profit Price': df_history['takeProfitPrice'], # New field
'Weighted Score Ratio': df_history['weightedScoreRatio'], # New field
'Transaction Value in USDT': df_history['amount'] * df_history['markPrice'], # New calculation
'Profit/Loss Ratio': (df_history['markPrice'] - df_history['entryPrice']) / df_history['entryPrice'] # New calculation
})
if option == "okx":
df_transformed = pd.DataFrame({
'Pair/Asset': df_history['symbol'],
'is long': df_history['side'],
'Current size after change': df_history['usdAmount'],
'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']
})
if 'closed' in df_history.columns:
df_transformed['Position closed'] = df_history['closed']
st.dataframe(df_transformed)
# Add the update timestamp to the transformed DataFrame
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):
# Filter trade history for the selected position
position_history = [pos for pos in tradeHistory_positions if pos['i'] == selected_position]
# Check if there are any records for the selected position
if not position_history:
return False
# Get the most recent entry for the selected position
last_entry = position_history[-1]
# Check if the 'closed' key exists and if it indicates the position is closed
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'])
# Generate a unique key for the button
button_key = f"position_{row['i']}"
# Display a button for each trade position
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'])
elif option =="okx":
if st.button(
f"{row['symbol']} : Long: {side}, Entry Price: {row['entryPrice']}, "
f"Market Price: {row['markPrice']}, Amount: {row['usdAmount']}, "
f"Leverage: {row['leverage']}, TradeTakenAt: {row['updateTime']}, "
f"lastUpdated: {lastUpdated(row['i'])}, 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()
|