Backtester Development (Part 3)
Posted by Mark on November 7, 2022 at 07:07 | Last modified: June 22, 2022 08:36In moving forward to eliminate the lower bound, I encountered a bug described at the end of Part 2. Today I want to finish discussing my debugging effort.
I tried all kinds of things during the next three hours. I can’t remember the exact order of what I tried or even the results of everything I did.
The bug occurred before the dataframe was printed to .csv file, which prevented me from viewing in Excel. I was able to display in JN, and that helped immensely. This revealed a long option with 28 DTE at trade inception. Of course that’s going to be a problem because it means the short option would be 0 DTE and missing upon subsequent update. I later noticed many missing short options, which I never understood.
Before looking at the dataframe, I added lines in the program to print different things as a way to track footprints. I needed to better understand what branches the program traveled through. In order to see the log file, I had to enter in JN:
> strike_file.close()
Because the ValueError occurred before the program completed, the results file remained open in Python. Closing it manually with this line made viewing the log file easier. After studying the log file and making changes, I learned to close the log file to avoid an error upon subsequently running the program again (PermissionError: [Errno 13] Permission denied).
Printing out different variables helped to determine where the bug arose and what steps the program took to get there. The list of variables I looked at grew long:
I even had to create some new variables to better interpret the old ones. raw_trade_date_list is one example because the data file—something I studied to understand on what row the program stopped due to error—shows the raw date rather than a more familiar format (see end of this post for explanation).
In many places, I inserted counters to determine how many times the program executed particular branches. This line proved particularly useful because it creates a detailed log of variables upon every iteration:
My debugging effort had many fits and starts because I would find something that seemed like a smoking gun only to make changes and see the bug remain. I became saddled with contradictory information. For example, assignment to current_date meant the program executed one branch while so-and-so flag True meant the program couldn’t have executed said branch.
I eventually noticed an instance of == when it should have been = . This led me to do a search for equals signs and verify every single one of over 350 in the code. This revealed:
- L142: control_flag == ‘find_long’ #NEEDS TO BE SINGLE = NOT ==
- L183: wait_until_next_day == True #NEEDS TO BE SINGLE = NOT ==
- L265: control_flag == ‘find_long’ #NEEDS TO BE SINGLE = NOT ==
- L266: wait_until_next_day == True #NEEDS TO BE SINGLE = NOT ==
>
See a pattern here? Clearly I need to improve with regard to usage of = versus == .
With these all fixed, the program runs as expected even without the lower bound.
Mission [finally] accomplished!
Categories: Python | Comments (0) | Permalink