code: def sharpe_ratio(
      returns, riskFreeRate, tradingPeriods, annualized=True
  ):
      ret = 0.0
  
      # From http://en.wikipedia.org/wiki/Sharpe_ratio: if Rf is a constant risk-free return throughout the period,
      # then stddev(R - Rf) = stddev(R).
      volatility = stats.stddev(returns, 1)
  
      if volatility != 0:
          rfPerReturn = riskFreeRate / float(tradingPeriods)
          avgExcessReturns = stats.mean(returns) - rfPerReturn
          ret = avgExcessReturns / volatility
  
          if annualized:
              ret = ret * math.sqrt(tradingPeriods)
      return ret
  
file_path: pyalgotrade/pyalgotrade/stratanalyzer/sharpe.py
imports: from pyalgotrade import stratanalyzer
  from pyalgotrade.stratanalyzer import returns
  from pyalgotrade.utils import stats
  import math
label_desc: Use image node_id 2 for calling a global function with example usage: sharpe_ratio(returns, riskFreeRate, tradingPeriods, annualized) and returns: ret