@@ -710,32 +710,32 @@ def previous_high_low(cls, ohlc: DataFrame, time_frame: str = "1D") -> Series:
710
710
broken_high = np .zeros (len (ohlc ), dtype = np .int32 )
711
711
broken_low = np .zeros (len (ohlc ), dtype = np .int32 )
712
712
713
+ resampled_ohlc = ohlc .resample (time_frame ).agg (
714
+ {
715
+ "open" : "first" ,
716
+ "high" : "max" ,
717
+ "low" : "min" ,
718
+ "close" : "last" ,
719
+ "volume" : "sum" ,
720
+ }
721
+ ).dropna ()
722
+
713
723
currently_broken_high = False
714
724
currently_broken_low = False
715
725
last_broken_time = None
716
726
for i in range (len (ohlc )):
717
- resampled_ohlc = (
718
- ohlc [:i ]
719
- .resample (time_frame )
720
- .agg (
721
- {
722
- "open" : "first" ,
723
- "high" : "max" ,
724
- "low" : "min" ,
725
- "close" : "last" ,
726
- "volume" : "sum" ,
727
- }
728
- )
729
- )
730
- if len (resampled_ohlc ) >= 1 :
731
- if last_broken_time != resampled_ohlc .index [- 1 ]:
732
- currently_broken_high = False
733
- currently_broken_low = False
734
- last_broken_time = resampled_ohlc .index [- 1 ]
735
727
# remove rows with nan values (ignoring weekends)
736
- resampled_ohlc = resampled_ohlc .dropna ()
737
- previous_high [i ] = resampled_ohlc ["high" ].iloc [- 2 ] if len (resampled_ohlc ) > 1 else np .nan
738
- previous_low [i ] = resampled_ohlc ["low" ].iloc [- 2 ] if len (resampled_ohlc ) > 1 else np .nan
728
+ resampled_previous_index = np .where (
729
+ resampled_ohlc .index < ohlc .index [i ]
730
+ )[0 ]
731
+ if len (resampled_previous_index ) <= 1 :
732
+ previous_high [i ] = np .nan
733
+ previous_low [i ] = np .nan
734
+ continue
735
+ resampled_previous_index = resampled_previous_index [- 1 ]
736
+
737
+ previous_high [i ] = resampled_ohlc ["high" ].iloc [resampled_previous_index ]
738
+ previous_low [i ] = resampled_ohlc ["low" ].iloc [resampled_previous_index ]
739
739
currently_broken_high = ohlc ["high" ].iloc [i ] > previous_high [i ] or currently_broken_high
740
740
currently_broken_low = ohlc ["low" ].iloc [i ] < previous_low [i ] or currently_broken_low
741
741
broken_high [i ] = 1 if currently_broken_high else 0
0 commit comments