-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
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 self.buy(size=.02) that means "2% of current available liquidity". And liquidity is "cash plus [Position.pl] minus used margin". Putting aside the margin part, what does that mean? To me, it could mean:
- It's cash (which is a value that never changes after you set initial cash) plus p/l of closed positions.
- It's cash (which is a value that gets updated after positions close).
- It's cash (which is a value that gets updated after positions close) plus p/l of open positions.
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 backtesing.py and found line 788:
@property
def equity(self) -> float:
return self._cash + sum(trade.pl for trade in self.trades)
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 self.trades?". Well I think that is in line 718:
self.trades: List[Trade] = []
This has me confused. Can I see what goes in this list?
There is also this part:
trade.pl
I tried to look for this and I think I found it in line 637:
@property
def pl(self):
"""Trade profit (positive) or loss (negative) in cash units."""
price = self.__exit_price or self.__broker.last_price
return self.__size * (price - self.__entry_price)
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.
Beta Was this translation helpful? Give feedback.