|
In the docs there is a part that explains order sizing under Class Order > var size (https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html#gsc.tab=0) : "Order size (negative for short orders). If size is a value between 0 and 1, it is interpreted as a fraction of current available liquidity (cash plus Position.pl minus used margin). A value greater than or equal to 1 indicates an absolute number of units." Ok, so if I were to this this code
Concerns: My concern at this point is that I do NOT want available liquidity to use my open positions, because I do not trade that way. When I size my trades, I size it as "2% of my account balance / cash". If I have open positions, I ignore them when calculating my trade size. My account balance / cash / availably liquidity should change only when positions are closed. If this is not the way the backtester works, then I need to modify the code for my own purposes. Also, the equity / available liquidity may also use margin in its calculation. If this is the case I need to take that out. Also, can someone please clarify how the program defines "positions" and "trades"? Could a trade be an open position? Are trades simply positions that have been closed? For me, positions get added when I place a trade or order. A position shows it's current p/l. If it gets closed, it becomes part of my trade history / journal. Also, in the eyes of the program, what is the difference between "equity", "cash", and "available liquidity"? To try and understand more clearly, I went to the code in Ok so here we see that equity is defined as "cash plus the sum of some p/l figures that I don't know". This doesn't clarify my three questions so I ask myself, "What is This has me confused. Can I see what goes in this list? There is also this part: I tried to look for this and I think I found it in line 637: At this point I'm still wondering whether these trades are closed or open positions, and I don't know where to look in the code to answer that. So again my main question is: How is equity/liquidity calculated? If it's cash plus open positions, that's wrong for my use case and I need to figure out a way to change it in the code. I'm trying to size my positions in terms of a percentage of cash or account balance, which is something that changes after positions close. |
Replies: 1 comment
|
My reading of that docs sentence is:
Available liquidity is roughly:
So open positions can affect it in two ways:
But the market value of an existing open position is not simply treated as free cash that can be reused. If you already have a position open and submit another For example, if you want only one long position at a time, gate the entry: if not self.position.is_long:
self.buy(size=0.02)Also, if your intention is "risk 2% if the stop-loss is hit", |
My reading of that docs sentence is:
size=0.02means "use 2% of current available liquidity for this order", not "risk 2% of account equity" and not necessarily "buy 2% of starting cash".Available liquidity is roughly:
cash + current unrealized P/L - margin already usedSo open positions can affect it in two ways:
But the market value of an existing open position is not simply treated as free cash that can be reused. If you already have a position open and submit another
self.buy(size=0.02), Backtesting.py will size that new orde…