Portfolio value lagged.
-
Hi, I've been searching for a while and didn't get a good answer for what I'm going through. I'm not very familiar with the details, which I'm trying to improve rapidly.
I have turned on the
coc
andcoo
function here and am trying to keep records of every day's closing prices and daily portfolio value withself.dataclose[0] self.stats.broker.value[0]
Aftering printing out everything, this is what happens it seems:
- it creates an order when a strategy signals it to
- it executes the order created the day before using the closing price from the previous day
- the portfolio value seems to be lagged. Say my cost base is 100 at day 1, the trade is executed on day 2. Closing price on day 2 is 99, but the portfolio value remains at 100.
- Only on day 3, the portfolio value shows up as 99.
I'm wondering
- Is it possible to create and execute trades on the same day?
- If a trade does have to execute on two separate days Is it possibly to use the open price of the next day?
- Is it possible to align those portfolio values so that the changes in portfolio value happens on the same day as the changes in closing prices, as opposed to the following day?
Thanks!
-
@yjy said in Portfolio value lagged.:
- Is it possible to create and execute trades on the same day?
Yes. You are getting the
close
price withcheat-on-close
. But what you cannot (and won't get with backtrader) is creating an order with for exampleself.buy
is an immediate execution. The platform will queue the order in the broker just like any other order and execute it as soon as possible. Thevalue
you are collecting doesn't take the order execution into account, because the order is simply queued. Thevalue
isn't lagging.Your expectation is to get matched in an unrealistic instant scenario with prices that are already gone (you may have good reasons for it), but backtrader tries to approximate as much as possible the reality of trading, even if you activate
cheat-on-close
- If a trade does have to execute on two separate days Is it possibly to use the open price of the next day?
That's the standard default behavior with
Market
orders and for exampletimeframe=bt.TimeFrame.Days
. TheMarket
order is matched against the next incoming price which is theopen
price of the next bar.See:
- Is it possible to align those portfolio values so that the changes in portfolio value happens on the same day as the changes in closing prices, as opposed to the following day?
No. As explained above, that would be completely unrealistic..