microsoft/qlib · error · NotImplementedError
Please implement the `get_stock_amount_dict` method
Error message
Please implement the `get_stock_amount_dict` method
What it means
Error "Please implement the `get_stock_amount_dict` method" thrown in microsoft/qlib.
Source
Thrown at qlib/backtest/position.py:157
Default: not include those unavailable cash
Returns
-------
float:
the available(tradable) cash in position
"""
raise NotImplementedError(f"Please implement the `get_cash` method")
def get_stock_amount_dict(self) -> dict:
"""
generate stock amount dict {stock_id : amount of stock}
Returns
-------
Dict:
{stock_id : amount of stock}
"""
raise NotImplementedError(f"Please implement the `get_stock_amount_dict` method")
def get_stock_weight_dict(self, only_stock: bool = False) -> dict:
"""
generate stock weight dict {stock_id : value weight of stock in the position}
it is meaningful in the beginning or the end of each trade step
- During execution of each trading step, the weight may be not consistent with the portfolio value
Parameters
----------
only_stock : bool
If only_stock=True, the weight of each stock in total stock will be returned
If only_stock=False, the weight of each stock in total assets(stock + cash) will be returned
Returns
-------
Dict:
{stock_id : value weight of stock in the position}
"""View on GitHub (pinned to 79633dd950)
Solutions
- Implement the `get_stock_amount_dict` method in your Position subclass before calling it.
- Use the built-in Position class which already implements `get_stock_amount_dict`.
When it happens
Trigger: This error is raised at qlib/backtest/position.py:157 when the guard condition fails: "Please implement the `get_stock_amount_dict` method". It triggers when the code path receives input or a state that violates the precondition checked at this point — e.g. an unimplemented abstract method being called, an unsupported argument type/value, or an invalid configuration. To avoid it, satisfy the documented precondition before this call: implement the required method in your subclass, or pass a supported value of the expected type as described by the message.
Common situations: See trigger scenarios.
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/43f4d57f2b80c9b6.
Report an issue: GitHub.