Sell all value in the end
Andreas Lapueros last edited by
I'm using a custom strategy and, when I execute cerebro.broker.setcash(), it adds me cash PLUS value (what is value, actually? amount of stocks I'm trading?).
In the end, I would really like to get the pure amount in cash. If I get it right, I have to sell all the value right before Cerebro stops.
How can I do this?
ab_trader last edited by
For stocks in case if you don't use leverage:
Broker Value = Cash + Open Positions
Value at the end = Cash + Open Positions based on last close price
It is unclear why and when you are using
setcash. Trying to read between the lines it would seem you do it during the backtesting phase and not before starting it.
During backtesting you control the cash reserves with
add_cash. See Docs - Broker
Selling all at the end is tricky. See why:
The framework doesn't know in advance that backtesting is going to end. It is unknown at which point a data feed will state that it has come to the end. This is so, because some data feeds are dynamic and some others hook up to live data
Once you have hit the end ... you cannot sell. Because when you see a bar, the bar has already happened and you cannot progress to the next bar for broker evaluation.
Wit that in mind:
- You need to issue a
sellorder during the last bar before the actual last one.
You can use:
self.close(See Docs - Strategy and the right stake will be calculated automatically
Andreas Lapueros last edited by Andreas Lapueros
@backtrader thank you for the reply.
I'm executing "setcash" before backtest starts.
So this is how it looks like:
bot says I have 1000 cash AND 1000 "value" (why? I did not add any value manually)
in the end of the period, I see some cash AND value
The thing is - to determine the profit rate, I have to calculate things in cash. Because, in the end of the backtest, I may have 100 in cash but 2000 in "value". And, if I change some params, I may have 1500 in cash and 500 in "value".
How can I know what backtest was more profitable? The first one where cash was low but value was high? Or the second one where I had more cash and less value?
Honestly, this is confusing. If I (as investor) had put some cash into business, I would like to know the profit in cash (that probably means, I have to sell all "value" at the end?). So there has to be a way to determine the pure cash value. I'm pretty sure there is, but I'm just missing something.
- bot says I have 1000 cash AND 1000 "value" (why? I did not add any value manually)
The value of your portfolio is
1000(all in cash)
- in the end of the period, I see some cash AND value
Yes and as pointed out by @ab_trader, your total value will include the cash reserves you haven't invested in any asset.
How can I know what backtest was more profitable?
The one with a higher value. But it may still have open positions which may end up losing.
Honestly, this is confusing. If I (as investor) had put some cash into business, I would like to know the profit in cash
Apparently nobody else sees a problem when comparing the final value with the starting value to understand if a system is profitable (which doesn't preclude the use of the many other ways of seeing if a strategy is delivering profit as expected, including ratios like Sharpe, Calmar or Profit Factor)
The value includes in both cases your cash and the monetary value of any position (which in practical terms is like cash)
If you really want to do it with cash, sell everything and compare the starting cash amounts and ending cash amounts.
So there has to be a way to determine the pure cash value
If you really don't like value (which includes monetary value at a given point in time), then no, unless you sell everything and your liquidity is 100%.