The sliding window method is a road network screening approach commonly used for identifying black spots. Previous studies have indicated that the selection of window length significantly impacts the black spot identification process. This research proposes a new method that optimizes the sliding window framework by examining its characteristics. The optimization methodology employed in this study is as follows: Firstly, the road is segmented, and for each segment, different scenarios of window lengths are chosen using the Density-Based Spatial Clustering of Applications with Noise algorithm. Next, a Safety Performance Function is developed to calculate the predicted and expected number of crashes, as well as the Potential Safety Improvement, for each window movement across all selected scenarios within the segment. Subsequently, the average differences are calculated using the analysis of variance, and the window length with the lowest dispersion of difference values from the mean is identified as the optimal length for each segment. The case study yielded noteworthy results, indicating that the utilization of the sliding window with optimal lengths led to the identification of 122 high-risk black spot-candidates. These points exhibit a higher crash density, effective length, and greater value in quantitative evaluation tests compared to the results obtained using windows with common fixed lengths.