-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Buy on open, sell on close #533
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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)
.
Beta Was this translation helpful? Give feedback.
All reactions
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.