The math is just addition, subtraction, multiplication, and division. The code I posted is standard perl. The logic in pseudocode is: Code: for loss_value_as_percent_of_target in 0%, 10%, 20%, ... 400% # guessing potential losses are percents of the profit target do max_profit = -999999999 # value lower than maximum possible loss max_profit_target = undefined max_profit_win_count = undefined for each potential_optimal_target in 8, 9, 10, ... 98 # i.e., from the smallest through largest "maximum profit per trade" value from post #1 of this thread do profit = 0 win_count = 0 for each target in 17, 34, 51, ... 33, 98, 15 # these are the "maximum profit per trade" values from post #1 of this thread do if target >= potential_optimal_target then add target to profit increment win_count else subtract loss_value_as_percent_of_target * potential_optimal_target from profit endif done if profit > max_profit then max_profit = profit max_profit_target = potential_optimal_target max_profit_win_count = win_count endif done mean_profit = max_profit / number_of_targets win_percent = max_profit_win_count / number_of_targets * 100 print loss_value_as_percent_of_target, max_profit_target, max_profit, mean_profit, win_percent done