0

I am testing the performance of a stock trading strategy. For a basket of stocks, I want to loop through each stock, get their returns, volatility, and Sharpe ratio, then average these 3 metrics out across all the stocks.

In summing up the results from each stock, should I add them to a variable in every iteration or put them in a list and call sum()? It probably works either way, but each time there is a similar choice, I wonder which way is better.

I wrote out the non-list version:

# test the average performance on a basket of stocks
def trial_on_basket(stock_list, start, end, short_MA, long_MA):
    total_returns = 0
    total_volatility = 0
    total_sharpe = 0
    for i in stock_list:
        returns, volatility, sharpe = trial(i, start, end, short_MA, long_MA) #this function tests the performance on one stock
        total_returns += returns
        total_volatility += volatility
        total_sharpe += sharpe
    average_returns = total_returns / len(stock_list)
    average_volatility = total_volatility / len(stock_list)
    average_sharpe = total_sharpe / len(stock_list)
    return average_returns, average_volatility, average_sharpe

General suggestions welcomed on how to improve the code. Thanks!

Casey
  • 1
  • 1

0 Answers0