I am trying to get familiar with quantopian. I cloned an algorithm and I would like to understand differences between cash and returns. Why in this algorithm cash is almost 0 and returns goes up?
Clone Algorithm
9
Backtest from
to
with
initial capital
Cumulative performance:
Algorithm
Benchmark
Custom data:
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns | 1 Month | 3 Month | 6 Month | 12 Month |
Alpha | 1 Month | 3 Month | 6 Month | 12 Month |
Beta | 1 Month | 3 Month | 6 Month | 12 Month |
Sharpe | 1 Month | 3 Month | 6 Month | 12 Month |
Sortino | 1 Month | 3 Month | 6 Month | 12 Month |
Volatility | 1 Month | 3 Month | 6 Month | 12 Month |
Max Drawdown | 1 Month | 3 Month | 6 Month | 12 Month |
# P/E Ratio mean reversion from collections import deque import pandas as pd def initialize(context): # Import a CSV containing the reported and operational earnings per share of the S&P 500 # This is only updated quarterly since that's when earnings announcements occur # I'm just using reported EPS, but I've included operational EPS for those interested # 45 day delay to account for announcement delay fetch_csv('https://gist.githubusercontent.com/gusgordon/eda583be659f6f38c243/raw/110c1955cd864b2edd652b897922eeb691325f25/gistfile1.txt', symbol='eps') context.stock = sid(8554) # S&P 500 # Store past PE ratios, have some finite limit on the amount of past day's ratios to keep context.recent_PE_ratios = deque(maxlen=2000) def handle_data(context, data): # Get the most recent earnings per share try: eps = data['eps']['eps'].split('|') recent_reported_EPS = float(eps[0][1:]) #recent_operational_EPS = float(eps[1][1:]) active = 1 except: active = 0 if active == 1: # Calculate price/earnings PE_ratio = data[context.stock].price / recent_reported_EPS # Calculate the average past P/E ratio to use for reference avg_PE_ratio = pd.Series(context.recent_PE_ratios).mean() # Store today's PE ratio to use in future averages context.recent_PE_ratios.append(PE_ratio) # Buy if the PE ratio is low, sell if the PE ratio is high # Use a leverage of 1 if PE_ratio < avg_PE_ratio: order_target_percent(context.stock, 1) # Long elif PE_ratio > avg_PE_ratio: order_target_percent(context.stock, -1) # Short #record(avg_PE_ratio=avg_PE_ratio, #current_PE_ratio=PE_ratio) record(cash = context.portfolio.cash) # If we don't have an EPS, don't take any position else: order_target_percent(context.stock, 0)