Download modules/portfolio.py from deanpowers/blind-monkeys: direct link, hf CLI and curl.
- Browser
- Download file 2.73 kB
-
https://huggingface.co/deanpowers/blind-monkeys/resolve/main/modules/portfolio.py
- Command line
-
hf download hf://deanpowers/blind-monkeys/modules/portfolio.py
-
curl -L -o portfolio.py https://huggingface.co/deanpowers/blind-monkeys/resolve/main/modules/portfolio.py
2.73 kB
| from modules.bingx_client import get_bingx_balance, get_bingx_positions | |
| from modules.market_data import get_market_data | |
| def calculate_portfolio_allocation(balance_data, prices): | |
| """Calculate portfolio allocation percentages""" | |
| total_value = 0 | |
| allocations = {} | |
| if balance_data and isinstance(balance_data, dict) and not balance_data.get('error'): | |
| balances = balance_data.get('balances', []) | |
| for item in balances: | |
| asset = item.get('asset', '') | |
| free = float(item.get('free', 0)) | |
| if free > 0: | |
| price = prices.get(asset, 1) | |
| usdt_value = free * price | |
| allocations[asset] = { | |
| "available": free, | |
| "price": price, | |
| "usdt_value": round(usdt_value, 2) | |
| } | |
| total_value += usdt_value | |
| if total_value > 0: | |
| for asset in allocations: | |
| allocations[asset]["percentage"] = round((allocations[asset]["usdt_value"] / total_value) * 100, 2) | |
| return { | |
| "total_value": round(total_value, 2), | |
| "allocations": allocations | |
| } | |
| def calculate_hedging_percentage(spot_balance, swap_positions): | |
| """Calculate hedging percentage based on spot and swap positions""" | |
| total_spot_value = 0 | |
| total_swap_value = 0 | |
| # Spot value (stablecoins only for simplicity) | |
| if spot_balance and isinstance(spot_balance, dict) and not spot_balance.get('error'): | |
| balances = spot_balance.get('balances', []) | |
| for item in balances: | |
| free = float(item.get('free', 0)) | |
| asset = item.get('asset', '') | |
| if asset in ('USDT', 'USDC', 'BUSD', 'DAI'): | |
| total_spot_value += free | |
| # Swap positions value (returns a list, not a dict) | |
| if swap_positions and isinstance(swap_positions, list): | |
| for position in swap_positions: | |
| total_swap_value += float(position.get('positionValue', 0)) | |
| if total_spot_value == 0: | |
| return {"hedge_percentage": 0, "message": "No spot balance to hedge"} | |
| hedge_percentage = (total_swap_value / total_spot_value) * 100 | |
| return { | |
| "total_spot_value": round(total_spot_value, 2), | |
| "total_swap_value": round(total_swap_value, 2), | |
| "hedge_percentage": round(hedge_percentage, 2) | |
| } | |
| def get_portfolio(): | |
| """Get full portfolio allocation""" | |
| spot = get_bingx_balance() | |
| prices = get_market_data() | |
| return calculate_portfolio_allocation(spot, prices) | |
| def get_hedge(): | |
| """Get hedging percentage""" | |
| spot = get_bingx_balance() | |
| positions = get_bingx_positions() | |
| return calculate_hedging_percentage(spot, positions) | |