Trading/Ranking According to Correlations

Hello,

Is there a way to calculate the correlation between a stock/ETF and your current holdings and trade accordingly?

For instance, suppose I have a portfolio P. I am considering adding stock A or stock B to the portfolio. Though A scores slightly higher on my ranking system, B’s past correlation with the portfolio is less, so I want the trading tool to prefer B.

Or suppose my screen returns 30 stocks. Instead of the highest ranking 20 stocks according to my ranker, I want to pick the 20 that have the least past correlation. For instance, I wouldn’t want the tool to buy AAPL and AAPL suppliers, since these are likely to be correlated.

I realize that past correlation may not always indicate future correlation. I also realize that sector/industry could serve as a surrogate for this.

Anyway - apologies if this was asked before.

Can be done in portfolios - function is MaxCorrel. Can’t be done in screens.