mhallsmoore / qstrader

QuantStart.com - QSTrader backtesting simulation engine.
https://www.quantstart.com/qstrader/
MIT License
2.84k stars 851 forks source link

why inspect is used in update_position func #328

Closed chloe-wang closed 4 years ago

chloe-wang commented 4 years ago

hi, just want to know why we are using inspect in update_position func in position_handler.py? Any response will be appreciated!

mhallsmoore commented 4 years ago

Hi @chloe-wang,

Thank you for your question. Here is the particular code in question: https://github.com/mhallsmoore/qstrader/blob/master/qstrader/broker/portfolio/position_handler.py#L45-L68

From what I can recall it was coded this way to avoid a lot of repetitive code of the following type, especially as more attributes were added to the Position:

if quantity is not None:
    position.quantity = quantity
if current_price is not None:
    position.current_price = current_price
..
..

However, in retrospect the code intent is probably a little unclear. It may be worth a refactor in the future!

Kind regards,

Mike.

chloe-wang commented 4 years ago

hi @mhallsmoore :

Thanks for the quick reply. Yes I kind of getting the idea of using inspect here. Like you said, it may be worth a refactor since the task here is basically quite straightforward, but inspect is making it mysterious. Thanks again for your kind explanations, and it really helps a lot!

mhallsmoore commented 4 years ago

Hi @chloe-wang,

Just to let you know that this issue has now been fixed in the latest 0.1.4 release. Here is the appropriate commit: https://github.com/mhallsmoore/qstrader/pull/330/commits/7faf70c619ec17f28d893bce045f86de3fe4aba6

Kind regards,

Mike.

chloe-wang commented 4 years ago

@mhallsmoore thanks a lot.