By default, an order decided in next() executes at the open of the next bar: you decide with the known close and execute later, as in real life.
cerebro.broker.set_coc(True) enables cheat-on-close: the order executes at the close price of the very bar on which it was decided. Handy to simulate an order placed in the closing auction, but dangerous if the decision uses that very close.
A more honest alternative: self.buy(exectype=bt.Order.Close) executes the order at the close of the next bar. The decision is still made with data already known.
There is also cheat-on-open: with bt.Cerebro(cheat_on_open=True), the next_open() method is called before execution, when the bar’s open is already known. It is used, for example, to compute a position size from the opening price.
Examples
TAU-X (fictional market) : The vertical line marks the close of the current bar. With cheat-on-close, the order is executed exactly at that price; without it, at the next open, which may be higher or lower.
Euronext Paris: the 17:35 closing auction : On Euronext Paris, orders build up from 17:30 to 17:35 before an auction that sets the closing price. An order “at the close” is therefore executed at a price unknown when you send it: deciding while knowing that price, as cheat-on-close allows, is impossible in real life.
Key terms
Cheat-on-close
Execution at the close price of the bar on which the order is decided
set_coc
Broker method that enables cheat-on-close
Look-ahead
Bias that uses information not yet known at decision time