Comments (1)
Solved it with this function. If there is a better way to calculate PnL (ideally a direct API call) please comment below.
def get_last_trade_pnl_and_percentage(self):
# Fetch the most recent trades for the futures account
pnl_data = self.client.futures_account_trades(symbol=self.symbol, limit=1)
# Fetch the current available balance
futures_balances = self.client.futures_account_balance(asset=self.config.BASE_ASSET)
futures_balance_df = pd.DataFrame(futures_balances)
current_balance = round(float(futures_balance_df[futures_balance_df['asset'] == self.config.BASE_ASSET]['availableBalance'].values[0]), 2)
last_trade_pnl = 0
last_trade_pnl_percentage = 0
if pnl_data:
# Convert the trade data into a DataFrame
df = pd.DataFrame(pnl_data)
# Ensure the 'realizedPnl' column is treated as numeric for calculations
df['realizedPnl'] = pd.to_numeric(df['realizedPnl'])
if not df.empty:
# Get the PnL of the last (most recent) trade
last_trade_pnl = round(df['realizedPnl'].iloc[-1], 2)
# Calculate the balance before the last trade
balance_before_last_trade = current_balance - last_trade_pnl
# Calculate PnL percentage based on the balance before the last trade
if balance_before_last_trade != 0:
last_trade_pnl_percentage = round((last_trade_pnl / balance_before_last_trade) * 100, 2)
# Return both the PnL of the very last trade and its percentage
return last_trade_pnl, last_trade_pnl_percentage
from python-binance.
Related Issues (20)
- Correct way of implementing daily PnL query? HOT 6
- Portfolio margin support HOT 1
- Binance Testnet user_socket() problem.
- API for leaderboard not work
- margin API will change by March 2024 HOT 1
- python-binance not working with Python 3.10? HOT 1
- Support order cancelReplace
- Authentication using Ed25519 API keys
- Yo
- Binance is upgrading futures websocket HOT 2
- GET /fapi/v1/positionRisk is retired, please use GET /fapi/v2/positionRisk HOT 1
- pip install lws
- Testnet
- ThreadedWebsocketManager()
- Failed when try to place an order
- Binance API issue
- API KEY Error when fetching Wallet address
- get_historical_klines() takes forever and gives no output
- Is the project dead?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-binance.