Back to Community
Can somebody help me on this please?

Hello,

I don't know what is wrong with my algorithms, but it tells me there is a syntax error on line 73 where 'History' is. I can't find it though, could somebody please tell me what I did wrong ?

Thanks in advance !

from quantopian.algorithm import attach_pipeline, pipeline_output, order_optimal_portfolio
from quantopian.pipeline import Pipeline
from quantopian.pipeline.factors import CustomFactor, SimpleMovingAverage, AverageDollarVolume, RollingLinearRegressionOfReturns
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters.morningstar import IsPrimaryShare
from quantopian.pipeline.classifiers.morningstar import Sector

import numpy as np
import pandas as pd

from quantopian.pipeline.filters import Q1500US
import quantopian.experimental.optimize as opt

def initialize(context):
# Here we set our slippage and commisions. Set slippage
# and commission to zero to evaulate the signal-generating
# ability of the algorithm independent of these additional
# costs.

set_commission(commission.PerShare(cost=0.005, min_trade_cost=1))  
set_slippage(slippage.VolumeShareSlippage(volume_limit=1, price_impact=0))

attach_pipeline(make_pipeline(), 'long_short_equity_template')

# Schedule my rebalance function  
schedule_function(func=rebalance,  
                  date_rule=date_rules.month_start(),  
                  time_rule=time_rules.market_open(hours=0, minutes=30),  
                  half_days=True)  
# record my portfolio variables at the end of day  
schedule_function(func=recording_statements,  
                  date_rule=date_rules.every_day(),  
                  time_rule=time_rules.market_close(),  
                  half_days=True)

def before_trading_start(context, data):
# Call pipeline_output to get the output
# Note: this is a dataframe where the index is the SIDs for all
# securities to pass my screen and the columns are the factors
# added to the pipeline object above
context.pipeline_data = pipeline_output('long_short_equity_template')

def recording_statements(context, data):
# Plot the number of positions over time.
record(num_positions=len(context.portfolio.positions))

record(leverage = context.account.leverage)

# Called at the start of every month in order to rebalance  
# the longs and shorts lists  

def rebalance(context, data):

stocks = symbols('DBC', 'EFA', 'GLD', 'SPY', 'TLT', 'VB', 'VWO', 'ZIV')  

hist = data.history(stocks, "price", 120, "1d")  

60dgn = hist[-60:].mean()  
120dgn = hist.mean()  

60dgn50 = stocks.sort_values(by='60dgn',ascending=True).head(4)  
120dgn50 = stocks.sort_values(by='120dgn',ascending=True).head(4)  
close_price = stocks.close()  

if close_price > 120dgn:  
    order_target_percent(60dgn50, 0.5);  
    order_target_percent(120dgn50, 0.5)  

else :  
    order_target_percent(60dgn, 0);  
    order_target_percent(120dgn50, 0)  
1 response

i added comments throughout the attached to help you follow ...

gluck --- still tonnes of room for improvements to be made

Clone Algorithm
4
Loading...
Backtest from to with initial capital
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
# Backtest ID: 59a1275d9c1fcd5546df3081
There was a runtime error.