Goal: Compute average true range using ATR()
in TA-Lib
Big O notation: Execution time and space trade-off when computing average true range
Solution 1:
- Call data.history()
and data.current()
in handle_data()
- Combine data
- Compute ATR()
Solution 1 saves space at the expense of execution time because data.history()
returns the same results for each call to handle_data()
.
Solution 2:
- Call data.history()
once before market open or using schedule_function()
- Call data.current()
in handle_data()
- Combine data
- Compute ATR()
Solution 2 saves execution time at the expense of space because the results of data_history()
need to be stored in memory.
Solution 2 is not feasible because context
has limitations:
- Scalar value saved to context in initialize()
is accessible in handle_data()
- Scalar value saved to context in before_trading_start()
or schedule_function()
is not accessible in handle_data()
- 3-dimensional array saved to context in before_trading_start()
or schedule_function()
is not accessible in handle_data()
Uncommenting line 121 throws KeyError: 'prices'.