Some research says that "mean variance portfolio optimization" can
give good results. I discussed this in a message
To implement this approach, a needed input is the covariance matrix of
returns, which requires historical stock prices, which one can obtain
using "Python quote grabber" http://www.openvest.org/Databases/ovpyq .
For expected returns -- hmmm. One of the papers I cited found that
assuming equal expected returns of all stocks can give reasonable
results.
Then one needs a "quadratic programming" solver, which appears to be
handled by the CVXOPT Python package.
If someone implements the approach in Python, I'd be happy to hear
about it.
There is a "backtest" package in R (open source stats package callable
from Python) http://cran.r-project.org/web/packages/backtest/index.html
"for exploring portfolio-based hypotheses about financial instruments
(stocks, bonds, swaps, options, et cetera)."