Debugging the Missing Time Spread Backtrades (Part 2)
Posted by Mark on January 12, 2023 at 06:22 | Last modified: June 27, 2022 13:16By way of review, the idea is to log relevant variables in places where spreads could or should be found and aren’t. I left off discussing where in the program I need to insert code to do this.
In case I want to log in the ‘find_spread’ branch, let’s review the sub-branches:
- if int(float(stats[2])) > 200:
I don’t want time spreads with options > 200 DTE. Skipping these rows is expected and no reason to log variables. - elif len(dte_list) == 0 and if int(float(stats[6])) % 25 == 0 and if int(float(stats[6])) > float(stats[3]) and int(float(stats[6])) – float(stats[3]) < 26:
This sub-branch appends data for the option with first matching strike price. It’s always possible that no match will be found in any row for a particular historical date, which to me would suggest an incomplete data file. - elif int(float(stats[2])) == dte_list[0]: continue
This skips rows [on same current_date and] with DTE equal to a previously-matched DTE. Nothing to log here. - elif int(float(stats[1])) == current_date and if int(float(stats[6])) == strike_price:
This sub-branch records DTE for subsequent strike price matches. The different DTE represents a different expiration month. If the previously-matched strike price is not matched here, then it may be evidence of an incomplete data file. - else:
This sub-branch should be executed only after the program has gone through all options on current_date. If no spread has been found, then a skipped trading day will result and len(dte_list) should be < 2. Alternatively, a found spread(s) may not selected in this sub-branch. Either way, logging relevant variables here seems to make good sense.
>
>
>
>
>
What are the relevant variables to log?
I increasingly suspect len(dte_list) should be looked at first. On any given day it should be > 2 for at least two matching options (one potential spread). As a first attempt, I will append current_date to a new list dates_without_enough_matches at the top of the ‘find_spread’ ELSE sub-branch for any day that does not meet this criterion.
Surprisingly, the list is empty! This suggests failure to match strike price is not the issue.
Just to be transparent, I am now moving the following line to the end of the ‘find_spread’ ELSE sub-branch:
current_date = int(float(stats[1])) #updating
Since the previous elif executes on the same current_date (and strike price match), by implication this sub-branch takes place on the next day. Updating current_date at the beginning of this sub-branch forced me to use “current_date – 1” in following lines, which may not be accurate in case of weekends, holidays, etc. Updating at the end eliminates this problem. While not part of the current debugging effort, in scrutinizing this part of the program I saw opportunity for improvement.
I will continue next time.