-
Notifications
You must be signed in to change notification settings - Fork 179
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
Backtesting broker should check cash before processing pending orders #439
Comments
We routinely test strategies that use margin so we'd need to add and check that too. Margin limits are usually not fixed but a function of the account's cleared assets. Perhaps a little more complex than you expected but totally worthwhile in my opinion. While it would be great to include modeling of the trade clearing process, we could approach margin in three stages:
Settlement used to be 3 biz days and is now 2 but I just found out it changes to 1 this month: For details on how to do this you can check out backtester code that supports margin like Backtrader: |
Couldn't agree more. We should make some tests for this |
If we want to add margin into backtesting then we will need to be able to set parameters around it |
This sounds like a good idea. At the very least it would be good to specify whether margin is possible for not. It might also make sense to go a little further and allow a "max margin" parameter or something. For example, most stock brokers only allow you to borrow maximum 30% of your account value. This would require more work but would be super valuable if you're up for the challenge. |
Challenge accepted, let's see if we have time ;) account = MarginAccount(max_margin=0.3) and then something like: class MarginAccount(Account) class Account(ABC): |
Awesome! Love your energy Ruben!
Margin can be tricky. I think if you want to add margin as a capability in
Backtesting it would be worthwhile to read/watch a bit about how margin
calls happen. This can be different for long/short positions and different
asset types.
One change I would recommend to what you outlined is to have a different
margin for longs, shorts and asset types. Eg. Your MarginAccount object
could have some way to specify whether it’s long or short, and what asset
it pertains to.
Just a suggestion. I know this can be hard to implement in practice
Robert Grzesik 347-635-3416
…On Wed, May 8, 2024 at 10:56 AM Ruben Cancho ***@***.***> wrote:
Challenge accepted, let's see if we have time ;)
As an exploratory idea we could pass an Account object to the broker,
something like this:
account = MarginAccount(max_margin=0.3)
broker = BacktestingBroker(data_source, account,...)
and then something like:
class MarginAccount(Account)
class CashAccount(Account)
class Account(ABC):
*init*(initial_cash):
def withdraw():
def deposit():
def calculate overdrafts():
etc....
—
Reply to this email directly, view it on GitHub
<#439 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIYQK22CD6SWXETWK5T5O3ZBI4LTAVCNFSM6AAAAABHHRJXLKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBQG44DAOBYGU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
In method process_pending_orders we should check if cash is enough to process order.
The text was updated successfully, but these errors were encountered: