Hello,
Does anyone know of a more efficient way to calculate a security's annual returns in Quantopian?
Right now, I have the function below run on a daily basis. It simply records the returns of a security everyday and adds it to a running list of daily returns stored in a dictionary keyed to that security. However, I have to wait a year in the backtest (and live trading) in order to determine the security's annual returns!
def returns_recorder(context, data):
for security in context.securities:
daily_return = data[security].returns()
try:
context.sec_returns[security] = context.sec_returns[security][-260:]
except KeyError:
context.sec_returns[security] = []
context.sec_returns[security].append(daily_return)
Surely there's a better way to do this ?