I am new with Quantopian/Python and have looked at an algorithm to try to implement. I am for some reason getting Error 50: SyntaxError: expected an indented block when trying to 'build the algorithm' and an error when trying to run the backtes. If someone has the knowledge to help, it would be appreciated a million!
The code:
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
import quantopian.pipeline.factors as Factors
import quantopian.pipeline.filters as Filters
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
import numpy as pn
import pandas as pd
def initialize(context):
attach_pipeline(my_pipeline(context), name='my_pipe')
schedule_function(rebalance,
date_rule=date_rules.month_start(),
time_rule=time_rules.market_open())
def my_pipeline(context):
pass
market_cap = morningstar.valuation.market_cap.latest
roic = morningstar.operation_ratios.roic.latest
revenue_growth = morningstar.operation_ratios.revenue_growth.latest
shares_outstanding = morningstar.valuation.shares_outstanding.latest
ev_to_ebitda = morningstar.valuation_ratios.ev_to_ebitda.latest
market_cap_ok = market_cap > 100000000
roic_ok = roic > 0.1
revenue_growth_ok = revenue_growth > 0
shares_outstanding_ok = shares_outstanding.notnull()
my_mask = roic_ok & revenue_growth_ok & shares_outstanding_ok
return Pipeline(
columns={},
screen = ev_to_ebitda.bottom(20, mask = my_mask)
)
def before_trading_start(context, data):
context.output = pipeline_output('my_pipe')
record (cash = context.portfolio.cash, asset = context.portfolio_value)
def rebalance(context, data):
for stock in context.portfolio.positions:
if stock not in context.output.index:
order_target(stock, 0)
weight = create_weights(context, context.output.index, data)
for stock in context.output.index:
if data.can_trade(stock, weight);
def create_weights(context, stocks, data):
cantradecount = 0;
for stock in stocks:
if data.can_trade(stock):
cantradecount += 1
if cantradecount == 0:
return 0
else:
weight = 1.0/cantradecount
return weight