I know this isn't an exact solution to what you described above, but hopefully it will help guide in the right direction.
When you initialize your algo, you can add the below Custom Factor:
# predeclare your inputs and window length
inputs = [USEquityPricing.close]
# return out the montly return value
def compute(self, today, assets, out, close):
out[:] = close[-1]/close
You could set your pipeline to update once per month, that way once per month you are adding 21 day returns to your pipeline (i.e. one month of trading days).
Once you have added this factor to the pipeline with something like:
monthly_returns = MonthlyReturns()
In your order function, you can maybe implement an if statement on the return column for your algo to go long on stocks that are > 0 in this regard and short stock that are < 0 monthly returns. In the attached, I just went long on the top 100 returning stocks each month.