Используйте нарезку списка с enumerate()
и all()
проверкой на срез ваших данных, который включает 5 индексов перед вашим текущим индексом:
Этот код предоставит вывод для скользящего 5-дневного окна по всему вашему списку данных:
DAILY_PRICE = [ 1,2,3,6,5,6,7,8,5,6,7,6,7,5]
# ^ window starts here, before it is less then 5 days of data
for index,price in enumerate(DAILY_PRICE):
if index < 5 :
continue # no comparison for first 5 prices
if all(p <= price for p in DAILY_PRICE[index-5:index]):
print(f"All prices {DAILY_PRICE[index-5:index]} lower/equal then {price}")
else:
print(f"Not all prices {DAILY_PRICE[index-5:index]} lower/equal then {price}")
Выход:
All prices [1, 2, 3, 6, 5] lower/equal then 6
All prices [2, 3, 6, 5, 6] lower/equal then 7
All prices [3, 6, 5, 6, 7] lower/equal then 8
Not all prices [6, 5, 6, 7, 8] lower/equal then 5
Not all prices [5, 6, 7, 8, 5] lower/equal then 6
Not all prices [6, 7, 8, 5, 6] lower/equal then 7
Not all prices [7, 8, 5, 6, 7] lower/equal then 6
Not all prices [8, 5, 6, 7, 6] lower/equal then 7
Not all prices [5, 6, 7, 6, 7] lower/equal then 5 # this is the one your code tries to do
# all(p <= DAILY_PRICE[-1] for p
# in DAILY_PRICE[-6:-1])