@melvina
Here is a simple implementation of computing Parabolic SAR (Stop and Reverse) in Ruby:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
def parabolic_sar(high_values, low_values, acceleration_factor = 0.02, max_acceleration_factor = 0.2) sar = 0 sar_values = [] trend = 1 acceleration = acceleration_factor max_high = high_values.max min_low = low_values.min high_values.each_with_index do |high, i| low = low_values[i] if i == 0 sar = low sar_values << sar else prev_sar = sar_values.last if trend == 1 if high > max_high max_high = high acceleration = [acceleration + acceleration_factor, max_acceleration_factor].min end sar = prev_sar + acceleration * (max_high - prev_sar) if low < sar sar = max_high trend = -1 acceleration = acceleration_factor max_high = high_values[0..i].max min_low = low_values[0..i].min end else if low < min_low min_low = low acceleration = [acceleration + acceleration_factor, max_acceleration_factor].min end sar = prev_sar + acceleration * (min_low - prev_sar) if high > sar sar = min_low trend = 1 acceleration = acceleration_factor max_high = high_values[0..i].max min_low = low_values[0..i].min end end sar_values << sar end end return sar_values end # Example usage high_values = [10, 12, 14, 16, 18] low_values = [8, 9, 10, 12, 14] sar_values = parabolic_sar(high_values, low_values) puts sar_values |
In this implementation, we define a parabolic_sar
function that takes in arrays of high and low values as input. It computes the Parabolic SAR values based on the high and low values, acceleration factor, and max acceleration factor. The function returns an array of SAR values corresponding to the input data.
You can customize the input high and low values as well as the acceleration factors to suit your specific requirements.