Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -33,34 +33,33 @@ trade_type= "PERPETUAL" # perpetual or delivery
|
|
33 |
# Authenticate using the service account info
|
34 |
creds = Credentials.from_service_account_info(service_account_info, scopes=scopes)
|
35 |
client = gspread.authorize(creds)
|
36 |
-
sheet_mapping = {
|
37 |
-
"usdm": "usdmHistory",
|
38 |
-
"coinm": "coinmHistory",
|
39 |
-
"copyTraders": "copyTradersHistory",
|
40 |
-
"okx": "okx"
|
41 |
-
}
|
42 |
-
|
43 |
-
# Streamlit UI
|
44 |
-
st.title("Google Sheets Selector")
|
45 |
|
46 |
-
#
|
47 |
-
|
48 |
-
"Select a Sheet",
|
49 |
-
("usdm", "coinm", "copyTraders", "okx")
|
50 |
-
)
|
51 |
|
52 |
-
|
53 |
-
# Get the selected sheet name
|
54 |
-
sheet_name = sheet_mapping[option]
|
55 |
-
|
56 |
-
# Open the Google Sheet and select the worksheet
|
57 |
-
sheet_id = "1I_PuAeWTaRC4OhS5BA5gv0XQCA17VlIjpA1MvOlzVA8" # Replace with your actual Sheet ID
|
58 |
workbook = client.open_by_key(sheet_id)
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
|
66 |
|
@@ -110,7 +109,10 @@ df = df.apply(lambda col: col.map(convert_str_to_list_or_keep))
|
|
110 |
|
111 |
df['positionClosed'] = False
|
112 |
|
113 |
-
|
|
|
|
|
|
|
114 |
|
115 |
option = st.radio("Choose an option:", ["Show Position History", "Show Live Positions"])
|
116 |
|
@@ -190,7 +192,10 @@ if df is not None and uid_input:
|
|
190 |
|
191 |
if 'symbol' in trade_list[i]:
|
192 |
symbol = trade_list[i]['symbol']
|
193 |
-
|
|
|
|
|
|
|
194 |
amount = trade_list[i]['amount']
|
195 |
symbol = trade_list[i]['symbol']
|
196 |
trade_list[i]['side'] =side
|
@@ -211,18 +216,19 @@ if df is not None and uid_input:
|
|
211 |
# Extract symbol and amount
|
212 |
symbol = new_position.get('symbol')
|
213 |
amount = new_position.get('amount')
|
214 |
-
if
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
226 |
# if start==False:
|
227 |
#
|
228 |
# start =True
|
@@ -253,21 +259,21 @@ if df is not None and uid_input:
|
|
253 |
for position in trade_list[j]['positions']:
|
254 |
# Check if 'Modified' is in the position and is a dict
|
255 |
if 'Modified' in position and isinstance(position['Modified'], dict):
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
modified_amount = get_amounts_from_positions_and_closed_trades(position)
|
272 |
modified_symbol = get_symbols_from_positions_and_closed_trades(position)
|
273 |
|
@@ -281,7 +287,10 @@ if df is not None and uid_input:
|
|
281 |
st.header(f"Data is from {datetime.now}")
|
282 |
start =True
|
283 |
position['Modified']['side'] = modified_side
|
284 |
-
|
|
|
|
|
|
|
285 |
position['Modified']['i'] = i
|
286 |
amount = modified_amount
|
287 |
unique_lists.append(trade_list[j])
|
@@ -308,14 +317,19 @@ if df is not None and uid_input:
|
|
308 |
closed_side = "sell"
|
309 |
|
310 |
if symbol == closed_symbol and side == closed_side:
|
311 |
-
if
|
312 |
-
|
313 |
-
|
314 |
-
|
|
|
315 |
|
316 |
closed_trades_dict['side'] = closed_side
|
317 |
trade_info = closed_trades_dict['trade_info']
|
318 |
-
|
|
|
|
|
|
|
|
|
319 |
amount = closed_amount
|
320 |
closed_trades_dict['trade_info']['i'] = i # Store index 'i' inside 'ClosedTrades'
|
321 |
closed_trades_dict['trade_info']['closed'] = True
|
@@ -414,25 +428,46 @@ if df is not None and uid_input:
|
|
414 |
|
415 |
if position_history:
|
416 |
df_history = pd.DataFrame(position_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
417 |
|
418 |
# Update the global timestamp with the last update from history
|
419 |
|
420 |
|
421 |
# Create a transformed DataFrame for display
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
436 |
if 'closed' in df_history.columns:
|
437 |
df_transformed['Position closed'] = df_history['closed']
|
438 |
|
@@ -468,21 +503,34 @@ if df is not None and uid_input:
|
|
468 |
df_starting = pd.DataFrame(starting_position)
|
469 |
|
470 |
for index, row in df_starting.iterrows():
|
471 |
-
|
|
|
|
|
|
|
472 |
is_closed = isClosed(row['i'])
|
473 |
|
474 |
# Generate a unique key for the button
|
475 |
button_key = f"position_{row['i']}"
|
476 |
|
477 |
# Display a button for each trade position
|
478 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
479 |
f"{row['symbol']} : Long: {side}, Entry Price: {row['entryPrice']}, "
|
480 |
f"Market Price: {row['markPrice']}, Amount: {row['amount']}, "
|
481 |
-
f"Leverage: {row['leverage']},
|
482 |
-
f"lastUpdated: {lastUpdated(row['i'])}, isClosed: {is_closed}",
|
483 |
key=button_key
|
484 |
-
|
485 |
-
|
486 |
|
487 |
if __name__ == "__main__":
|
488 |
main()
|
|
|
33 |
# Authenticate using the service account info
|
34 |
creds = Credentials.from_service_account_info(service_account_info, scopes=scopes)
|
35 |
client = gspread.authorize(creds)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# The ID of the Google Sheet (found in the URL of the sheet)
|
38 |
+
sheet_id = "1I_PuAeWTaRC4OhS5BA5gv0XQCA17VlIjpA1MvOlzVA8"
|
|
|
|
|
|
|
39 |
|
40 |
+
# Open the Google Sheet by sheet ID
|
|
|
|
|
|
|
|
|
|
|
41 |
workbook = client.open_by_key(sheet_id)
|
42 |
+
|
43 |
+
# Select the specific sheet in the workbook'
|
44 |
+
|
45 |
+
options = ["Select an option", "usdmHistory", "coinmHistory", "copyLeaderBoard"]
|
46 |
+
|
47 |
+
# Dropdown menu to select the sheet
|
48 |
+
selected_sheet = st.selectbox("Select a worksheet:", options)
|
49 |
+
|
50 |
+
# Ensure a valid selection is made
|
51 |
+
if selected_sheet != "Select an option":
|
52 |
+
sheet = workbook.worksheet(selected_sheet)
|
53 |
+
st.write(f"Selected sheet: {selected_sheet}")
|
54 |
+
else:
|
55 |
+
st.write("Please select a worksheet.")
|
56 |
+
|
57 |
+
|
58 |
+
# Extract the data from the Google Sheet into a pandas DataFrame
|
59 |
+
data = sheet.get_all_values()
|
60 |
+
|
61 |
+
headers = data.pop(0)
|
62 |
+
df = pd.DataFrame(data, columns=headers)
|
63 |
|
64 |
|
65 |
|
|
|
109 |
|
110 |
df['positionClosed'] = False
|
111 |
|
112 |
+
if selected_sheet == "copyLeaderBoard":
|
113 |
+
uid_input = int(st.text_input("Enter U_IDs to filter"))
|
114 |
+
else:
|
115 |
+
uid_input = str.upper(st.text_input("Enter U_IDs to filter"))
|
116 |
|
117 |
option = st.radio("Choose an option:", ["Show Position History", "Show Live Positions"])
|
118 |
|
|
|
192 |
|
193 |
if 'symbol' in trade_list[i]:
|
194 |
symbol = trade_list[i]['symbol']
|
195 |
+
if selected_sheet == "copyLeaderBoard":
|
196 |
+
side ="buy" if float(trade_list[i]['amount'])>0 else "sell"
|
197 |
+
else:
|
198 |
+
side ="buy" if trade_list[i]['amount']>0 else "sell"
|
199 |
amount = trade_list[i]['amount']
|
200 |
symbol = trade_list[i]['symbol']
|
201 |
trade_list[i]['side'] =side
|
|
|
216 |
# Extract symbol and amount
|
217 |
symbol = new_position.get('symbol')
|
218 |
amount = new_position.get('amount')
|
219 |
+
if selected_sheet != "copyLeaderBoard":
|
220 |
+
if start==False:
|
221 |
+
start_time = new_position.get('updateTime')
|
222 |
+
year = start_time[0]
|
223 |
+
month = start_time[1]
|
224 |
+
day = start_time[2]
|
225 |
+
hour =start_time[3]
|
226 |
+
minute =start_time[4]
|
227 |
+
seconds = start_time[5]
|
228 |
+
dt = datetime(year, month, day, hour, minute, seconds)
|
229 |
+
human_readable_format = dt.strftime('%B %d, %Y, %I:%M:%S %p')
|
230 |
+
st.subheader(f"Data from {human_readable_format}")
|
231 |
+
start=True
|
232 |
# if start==False:
|
233 |
#
|
234 |
# start =True
|
|
|
259 |
for position in trade_list[j]['positions']:
|
260 |
# Check if 'Modified' is in the position and is a dict
|
261 |
if 'Modified' in position and isinstance(position['Modified'], dict):
|
262 |
+
if selected_sheet != "copyLeaderBoard":
|
263 |
+
if start==False:
|
264 |
+
for k,v in position.items():
|
265 |
+
start_time = v['updateTime']
|
266 |
+
|
267 |
+
year = start_time[0]
|
268 |
+
month = start_time[1]
|
269 |
+
day = start_time[2]
|
270 |
+
hour =start_time[3]
|
271 |
+
minute =start_time[4]
|
272 |
+
seconds = start_time[5]
|
273 |
+
dt = datetime(year, month, day, hour, minute, seconds)
|
274 |
+
human_readable_format = dt.strftime('%d-%m-%Y %H:%M:%S')
|
275 |
+
st.subheader(f"Data from {human_readable_format}")
|
276 |
+
start=True
|
277 |
modified_amount = get_amounts_from_positions_and_closed_trades(position)
|
278 |
modified_symbol = get_symbols_from_positions_and_closed_trades(position)
|
279 |
|
|
|
287 |
st.header(f"Data is from {datetime.now}")
|
288 |
start =True
|
289 |
position['Modified']['side'] = modified_side
|
290 |
+
if selected_sheet =="copyLeaderBoard":
|
291 |
+
position['Modified']['changeInAmount'] = float(amount) - modified_amount if modified_amount < 0 else modified_amount - float(amount)
|
292 |
+
else:
|
293 |
+
position['Modified']['changeInAmount'] = amount - modified_amount if modified_amount < 0 else modified_amount - amount
|
294 |
position['Modified']['i'] = i
|
295 |
amount = modified_amount
|
296 |
unique_lists.append(trade_list[j])
|
|
|
317 |
closed_side = "sell"
|
318 |
|
319 |
if symbol == closed_symbol and side == closed_side:
|
320 |
+
if selected_sheet != "copyLeaderBoard":
|
321 |
+
if start==False:
|
322 |
+
for k,v in position.items():
|
323 |
+
start_time = v['updateTime']
|
324 |
+
start =True
|
325 |
|
326 |
closed_trades_dict['side'] = closed_side
|
327 |
trade_info = closed_trades_dict['trade_info']
|
328 |
+
if selected_sheet =="copyLeaderBoard":
|
329 |
+
trade_info['changeInAmount'] = float(amount) - closed_amount if closed_amount < 0 else closed_amount - float(amount)
|
330 |
+
else:
|
331 |
+
|
332 |
+
trade_info['changeInAmount'] = amount - closed_amount if closed_amount < 0 else closed_amount - amount
|
333 |
amount = closed_amount
|
334 |
closed_trades_dict['trade_info']['i'] = i # Store index 'i' inside 'ClosedTrades'
|
335 |
closed_trades_dict['trade_info']['closed'] = True
|
|
|
428 |
|
429 |
if position_history:
|
430 |
df_history = pd.DataFrame(position_history)
|
431 |
+
|
432 |
+
if selected_sheet == "copyLeaderBoard":
|
433 |
+
df_history['changeInAmount'] = pd.to_numeric(df_history['changeInAmount'], errors='coerce')
|
434 |
+
df_history['markPrice'] = pd.to_numeric(df_history['markPrice'], errors='coerce')
|
435 |
+
df_history['entryPrice'] = pd.to_numeric(df_history['entryPrice'], errors='coerce')
|
436 |
+
# df_history['amount'] = pd.to_numeric(df['amount'],errors='coerce')
|
437 |
+
|
438 |
+
# Replace NaN with 0 or handle as required
|
439 |
+
df_history.fillna(0, inplace=True)
|
440 |
|
441 |
# Update the global timestamp with the last update from history
|
442 |
|
443 |
|
444 |
# Create a transformed DataFrame for display
|
445 |
+
if selected_sheet != "copyLeaderBoard":
|
446 |
+
df_transformed = pd.DataFrame({
|
447 |
+
'Pair/Asset': df_history['symbol'],
|
448 |
+
'is long': df_history['side'],
|
449 |
+
'Current size after change': df_history['amount'],
|
450 |
+
'Change in size in Asset': df_history['changeInAmount'],
|
451 |
+
'Change in size in USDT': df_history['changeInAmount'] * -(df_history['markPrice']),
|
452 |
+
'Entry price': df_history['entryPrice'],
|
453 |
+
'Exit price': df_history['markPrice'],
|
454 |
+
'pnl in usdt': df_history['pnl'],
|
455 |
+
'pnl in %': df_history['roe'],
|
456 |
+
'Leverage': df_history['leverage'],
|
457 |
+
'updatedTime': df_history['updateTime']
|
458 |
+
})
|
459 |
+
else:
|
460 |
+
df_transformed = pd.DataFrame({
|
461 |
+
'Pair/Asset': df_history['symbol'],
|
462 |
+
'is long': df_history['side'],
|
463 |
+
'Current size after change': df_history['amount'],
|
464 |
+
'Change in size in Asset': df_history['changeInAmount'],
|
465 |
+
'Change in size in USDT': df_history['changeInAmount'] * -(df_history['markPrice']),
|
466 |
+
'Entry price': df_history['entryPrice'],
|
467 |
+
'Exit price': df_history['markPrice'],
|
468 |
+
'Leverage': df_history['leverage'],
|
469 |
+
})
|
470 |
+
|
471 |
if 'closed' in df_history.columns:
|
472 |
df_transformed['Position closed'] = df_history['closed']
|
473 |
|
|
|
503 |
df_starting = pd.DataFrame(starting_position)
|
504 |
|
505 |
for index, row in df_starting.iterrows():
|
506 |
+
if selected_sheet =="copyLeaderBoard":
|
507 |
+
side = True if float(row['amount']) > 0 else False
|
508 |
+
else:
|
509 |
+
side = True if row['amount'] > 0 else False
|
510 |
is_closed = isClosed(row['i'])
|
511 |
|
512 |
# Generate a unique key for the button
|
513 |
button_key = f"position_{row['i']}"
|
514 |
|
515 |
# Display a button for each trade position
|
516 |
+
if selected_sheet != "copyLeaderBoard":
|
517 |
+
if st.button(
|
518 |
+
f"{row['symbol']} : Long: {side}, Entry Price: {row['entryPrice']}, "
|
519 |
+
f"Market Price: {row['markPrice']}, Amount: {row['amount']}, "
|
520 |
+
f"Leverage: {row['leverage']}, TradeTakenAt: {row['updateTime']}, "
|
521 |
+
f"lastUpdated: {lastUpdated(row['i'])}, isClosed: {is_closed}",
|
522 |
+
key=button_key
|
523 |
+
):
|
524 |
+
show_position_history(row['i'])
|
525 |
+
|
526 |
+
else:
|
527 |
+
if st.button(
|
528 |
f"{row['symbol']} : Long: {side}, Entry Price: {row['entryPrice']}, "
|
529 |
f"Market Price: {row['markPrice']}, Amount: {row['amount']}, "
|
530 |
+
f"Leverage: {row['leverage']}, isClosed: {is_closed}",
|
|
|
531 |
key=button_key
|
532 |
+
):
|
533 |
+
show_position_history(row['i'])
|
534 |
|
535 |
if __name__ == "__main__":
|
536 |
main()
|