Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Buy on open, sell on close #533

Answered by kernc
replacementAI asked this question in Q&A
Discussion options

Can this library allowed me to buy on open and then sell on close? I tried the trade on close = true parameter, but that causes both buys/sells to happen on close, and if that is set to false, then buys/sells happen on open.
EDIT: Is there an update on this?

You must be logged in to vote

Still not at the moment, sorry to say. 😞 You can only buy/sell on each open, or on each close if Backtest(..., trade_on_close=True).

Replies: 2 comments 3 replies

Comment options

Still not at the moment, sorry to say. 😞 You can only buy/sell on each open, or on each close if Backtest(..., trade_on_close=True).

You must be logged in to vote
1 reply
Comment options

I am currently trying to understand the process of closing an order.
Does it selps to overwrite the positions.close method for Market on Close orders?

Answer selected by replacementAI
Comment options

This is a very important capability in my opinion - these days, we have Market on Close orders. As such, if the open gaps below the stop, the stop will be triggered at the stop price which returns fake performance numbers. So ability to sell at open even why buying at close removes the fictitious performance numbers.

You must be logged in to vote
2 replies
Comment options

Hi,
Is there a solution now?
I need to do some tests:
purchase at the opening, and resale in the evening at the close (to have the price and test viability).
Is it possible ?
At worst, how can I sell the next morning at the open?
So keep the stock for a maximum of one day?

Comment options

You can make a change on your own; the proposed change will affect both/open and close.
Note also that the Open price is slightly different to the actual candle price as it is adjusted by commission.
In backtesting.py,
Search for
# Market-if-touched / market order
price = prev_close if self._trade_on_close else open
Change it to:
# Market-if-touched / market order
close = data.Close[-1]
price = close if self._trade_on_close else open # This is the line where a MARKET Order price is set.

IMHO the only viable price is close - not previous_close or open.
Using this change I am able to exactly match and execute the trades in live trading with the trades produced by backtesting.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /