Skip to content

How to get notified when order can't be opened? #1216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ironhak opened this issue Feb 11, 2025 · 3 comments
Closed

How to get notified when order can't be opened? #1216

ironhak opened this issue Feb 11, 2025 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@ironhak
Copy link

ironhak commented Feb 11, 2025

Hi, currently when I set a stop/limit order and the sizing of the order exceed the equity, then the order simply gets deleted when price reaches the stop/limit level, without giving any error.

... I spent like one hour trying to understand why orders gets randomly deleted when price reaches the entry levels... Some kind of alarm would be good to have, specially for those that use dynamic position sizing.

Thank's.

@kernc
Copy link
Owner

kernc commented Feb 17, 2025

It's not random. It's only when the specified relative size sums to be not even a single unit.

# If order size was specified proportionally,
# precompute true size in units, accounting for margin and spread/commissions
size = order.size
if -1 < size < 1:
size = copysign(int((self.margin_available * self._leverage * abs(size))
// adjusted_price_plus_commission), size)
# Not enough cash/margin even for a single unit
if not size:
# XXX: The order is canceled by the broker?
self.orders.remove(order)
continue

It's kind of like the broker refusing the order straight. I'm not sure this is in line with orders being GTC as mentioned in the docs. 🤔

What alternative do you suggest?

@kernc kernc added the help wanted Extra attention is needed label Feb 19, 2025
@ironhak
Copy link
Author

ironhak commented Feb 20, 2025

It's not random. It's only when the specified relative size sums to be not even a single unit.

backtesting.py/backtesting/backtesting.py

Lines 945 to 955 in 1dfe96b

If order size was specified proportionally,

precompute true size in units, accounting for margin and spread/commissions

size = order.size
if -1 < size < 1:
size = copysign(int((self.margin_available * self._leverage * abs(size))
// adjusted_price_plus_commission), size)
# Not enough cash/margin even for a single unit
if not size:
# XXX: The order is canceled by the broker?
self.orders.remove(order)
continue

It's kind of like the broker refusing the order straight. I'm not sure this is in line with orders being GTC as mentioned in the docs. 🤔

What alternative do you suggest?

The logic is good, I was just saying that it would be nice to have some kind of feedback, like:

 # If order size was specified proportionally, 
 # precompute true size in units, accounting for margin and spread/commissions 
 size = order.size 
 if -1 < size < 1: 
     size = copysign(int((self.margin_available * self._leverage * abs(size)) 
                         // adjusted_price_plus_commission), size) 
     # Not enough cash/margin even for a single unit 
     if not size: 
         # XXX: The order is canceled by the broker? 
         print(f'WARNING: order removed due to not sufficient margin')
         self.orders.remove(order)
         continue 

This is useful when someone is trying to backtest using limit/stop orders with a dynamic position sizing. Maybe the logic is not good and some orders does not get opened, it would be good to inform the user about that.

@kernc kernc closed this as completed in 5e72e7b Feb 20, 2025
@ironhak
Copy link
Author

ironhak commented Feb 24, 2025

Thank you so much! This is amazing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants