feat: support percent-of-portfolio order sizing#20
Open
subhajitlucky wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds optional “percent-of-portfolio” sizing for Binance Futures alerts by computing order quantity from available quote-currency balance and a reference price, while keeping existing qty behavior when qty_percent is omitted. Also fixes Binance Futures config key casing/route indentation and documents the new alert field.
Changes:
- Introduce
order_sizing.pyhelpers to derive quote currency, read available balance, and compute base-asset qty fromqty_percent. - Update Binance Futures bot to support
qty_percentand to use correctedBINANCE-FUTURESconfig casing. - Fix Binance Futures webhook route indentation and document
qty_percentin README; add basic unit tests for sizing helpers.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
order_sizing.py |
New sizing utilities for converting % of quote balance into order quantity. |
binanceFutures.py |
Uses sizing utilities when qty_percent is provided; fixes config key casing usage. |
app.py |
Fixes Binance Futures webhook routing/indentation and returns clearer error response when disabled. |
README.md |
Documents new qty_percent alert field. |
test_order_sizing.py |
Adds unit tests for quote-currency parsing and percent-based sizing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return 0 | ||
| if quote_currency in balance: | ||
| if isinstance(balance[quote_currency], dict): | ||
| return float(balance[quote_currency].get('free') or balance[quote_currency].get('total') or 0) |
Comment on lines
162
to
168
| if data['order_mode'] == 'Both': | ||
| take_profit_percent = float(data['take_profit_percent']) / 100 | ||
| stop_loss_percent = float(data['stop_loss_percent']) / 100 | ||
| current_price = exchange.fetch_ticker(data['symbol'])['last'] | ||
| reference_price = price if data['type'] == 'Limit' and price else current_price | ||
| qty = self.get_order_quantity(data, reference_price) | ||
| if data['side'] == 'Buy': |
Comment on lines
+173
to
+184
| if data['exchange'] == 'binance-futures': | ||
| if use_binance_futures: | ||
| bot = Bot() | ||
| bot.run(data) | ||
| return { | ||
| "status": "error", | ||
| "message": "Invalid Exchange, Please Try Again!" | ||
| "status": "success", | ||
| "message": "Binance Futures Webhook Received!" | ||
| } | ||
| return { | ||
| "status": "error", | ||
| "message": "Binance Futures is not enabled or API validation failed." | ||
| } |
|
|
||
| qty = calculate_qty_from_percent(balance, 'BTC/USDT', 25000, 10) | ||
|
|
||
| self.assertEqual(qty, 0.004) |
Comment on lines
195
to
200
| elif data['order_mode'] == 'Profit': | ||
| take_profit_percent = float(data['take_profit_percent']) / 100 | ||
| current_price = exchange.fetch_ticker(data['symbol'])['last'] | ||
| reference_price = price if data['type'] == 'Limit' and price else current_price | ||
| qty = self.get_order_quantity(data, reference_price) | ||
|
|
2f59b10 to
bd566c4
Compare
Author
|
Updated after automated review:
Verification:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9.
Summary
qty_percentsupport for Binance Futures alerts.qtybehavior whenqty_percentis not provided.qty_percentin the alert settings table.Verification
python3 -m unittest test_order_sizing -vpython3 -m py_compile app.py binanceFutures.py order_sizing.pygit diff --check