Here's the article about this logic by Szymbon Chlebowski and Andrzej Gajda: AQAS_CPL.pdf
aqas_main_WITH_PRINTS.py is less elegant but has print() functions all over the place for debugging. Also it's not as up to date as the file without prints.
I tried to document most of the steps using comments in the aqas_main.py file. Reach to me in case of uncertainty.