Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do portfolio simulation and backtest with the original stock prices instead of the adjusted ones #1833

Open
tonduy opened this issue Jul 24, 2024 · 1 comment
Labels
question Further information is requested

Comments

@tonduy
Copy link

tonduy commented Jul 24, 2024

Dear Qlib Team,

Thank you very much for providing this great framework!

I understand based on the documentation that the prices of the stocks are adjusted and the original price can be get via $close / $factor.

But now I wanted to ask if it is possible to run the whole portfolio simulation and backtest with the original prices instead of the adjusted ones so that the amount of stocks to buy also represents a real life scenario.

I would highly appreciate an answer of you. I tried to modify the exchange.py file to return for the deal price the original one, but this messes with the whole backtesting:

def get_deal_price(
        self,
        stock_id: str,
        start_time: pd.Timestamp,
        end_time: pd.Timestamp,
        direction: OrderDir,
        method: Optional[str] = "ts_data_last",
    ) -> Union[None, int, float, bool, IndexData]:
        if direction == OrderDir.SELL:
            pstr = self.sell_price
        elif direction == OrderDir.BUY:
            pstr = self.buy_price
        else:
            raise NotImplementedError(f"This type of input is not supported")
        deal_price = self.quote.get_data(stock_id, start_time, end_time, field=pstr, method=method)
        if method is not None and (deal_price is None or np.isnan(deal_price) or deal_price <= 1e-08):
            self.logger.warning(f"(stock_id:{stock_id}, trade_time:{(start_time, end_time)}, {pstr}): {deal_price}!!!")
            self.logger.warning(f"setting deal_price to close price")
            deal_price = self.get_close(stock_id, start_time, end_time, method)
        factor = self.get_factor(stock_id, start_time, end_time)
        original_price = deal_price / factor
        return original_price
@tonduy tonduy added the question Further information is requested label Jul 24, 2024
@SunsetWolf
Copy link
Collaborator

The data used by qlib are all post-weighted, why do you use post-weighted data? Post-weighting ensures that the historical price remains constant and adjusts the current stock price after each equity event. How to post weight? The reason for using the post-weighting factor is so that all stocks are at 1 on the first trading day of the calculation, and subsequent data is added or subtracted from 1 for easy comparison. If the original price is used to replace the post-weighting price, the price difference of thousands of times will make the model calculation very difficult.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants