Backtester Development (Part 4)
Posted by Mark on November 15, 2022 at 06:54 | Last modified: June 22, 2022 08:36Despite my best efforts described near the end of this post and the beginning of that, the DTE combinations I was getting from the short and long legs were not what I wanted. Another mitigating factor is that depending on the cycle, options may not be available in the 120-150 DTE range. Today I will discuss what I have done to revamp the logic.
One thing I found is despite check_second, sometimes I actually needed to check a third. This made me realize I may not know exactly how many I need to check, which suggests a need for dynamically-created variables.
I’ve run up against this issue a number of times so it’s worth trying to describe a more general case and look for a solution.
Suppose I have a large data file and I don’t know how often a pattern will repeat. Each time it occurs, though, I need to capture details and later evaluate to select which one I’ll need.
What is the best way to name variables in this case?
One approach would be to define as many or more sets than I’ll ever need. For example:
> dte0 = dte1 = dte2 = dte3 = … = dte50 = 0
> row_num1 = row_num2 = row_num3 = … = row_num50 = 0
That seems like a pain.
I’d rather define and initialize the variables with a loop like:
> variable_set = [‘dte’+ str(i) for i in range(50)]
> print(variable_set)
Now I’ve got the variable names in a list… could I then initialize them as a loop?
This doesn’t work:
> for i in len(variable_set):
> int(variable_set[i]) = 0
>
> print(variable_set)
Even if that did work, I might have just changed the list to all zeros, which destroys variable names I might otherwise be able to use later in the program. I don’t know how to keep the strings as variable names for later use.
I think the solution is to use a list or dictionary and add values as needed. I can’t address particular values by unique names, but the logic in the program can dictate what values to retrieve by index/slicing and where to insert/delete the same way.
Here’s what I know:
- Short option should have at least 60 DTE.
- Long option should be next month out (for now).
- If I can identify the short option first then the long option will come easy.
- Iterating down the file means going from longer-term to short-term options, which makes this more complex.
>
The new solution is to encode any necessary data for options 200 DTE or lower at the proper strike price and put them in lists (e.g. dte_list, orig_price_list, orig_delta list, etc.).
Each time DTE changes, as long as historical date remains the same I will encode new data for the proper strike price. Instead of appending values to the end of lists, I will insert at the beginning.
The time to identify the spread is once historical date changes. dte_list then includes DTE of all available options on that historical date in ascending order. I can then iterate through the dte_list from beginning to end. If the value > 60 then I have found my short option and the long option will be next (for width = 1). I can then assign the appropriate values from each list to specific variable names like those listed in the key.
Categories: Python | Comments (0) | Permalink