code: def sharpe_ratio_2(
      returns,
      riskFreeRate,
      firstDateTime,
      lastDateTime,
      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:
          # We use 365 instead of 252 becuase we wan't the diff from 1/1/xxxx to 12/31/xxxx to be 1 year.
          yearsTraded = days_traded(firstDateTime, lastDateTime) / 365.0
  
          riskFreeRateForPeriod = riskFreeRate * yearsTraded
          rfPerReturn = riskFreeRateForPeriod / float(len(returns))
  
          avgExcessReturns = stats.mean(returns) - rfPerReturn
          ret = avgExcessReturns / volatility
          if annualized:
              ret = ret * math.sqrt(len(returns) / yearsTraded)
      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 3 for calling a global function with example usage: sharpe_ratio_2(returns, riskFreeRate, firstDateTime, lastDateTime, annualized) and returns: ret