Back testing fundamental investment strategies

Hello Everyone!! I want to do historical analysis for some value investing strategies like P/E ratio or like back testing Magic formula strategy. I want to know using the algo Z I can write a code or something like that so that i can do the analysis for the specified time periods with specific strategical constraints( Mostly using financial ratio’s).