Profit Factor is based on trade entries and exits. Why are you trying to calculate it with Open and Close prices? Are you trying to calculate a different metric that is similar to Profit Factor but actually is not?

Hi mradtke, thank you for answer. This is just simple example system, where we buy on close of day and sell on open next day. For this I would need calculate Profit Factor. This Profit Factor I would use as Position Score for rotational backtest.
I would need calculate:
gross profits = this is sum of positive trades for N bars
gross losses = this is sum of negative trades for N bars

Please do you know how can I calculate it?
Thank you

Hi mradtke, thank you for answer again. Your code is great. I have change it little bit according my purpose….It looks very good, but there is problem in "pf" column. There is wrong division and it looks like that it is rounded, do you know how to solve it please?

code:
lookbackBars = 5;
tradeProfit = O - Ref(C,-1);