Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Code Not Working - Please Help

**Greetings Quantopian Community (any help you can provide me would be very much appreciated!) -
When I try to run the code below, I get a 'Syntax Error' message for line 154 - the line that says 'clf1 = DecisionTreeClassifier()'
I tried messing around with this to no avail. I dont understand the problem as I thought I had imported the sk library and its accompanying algorithms. Any thoughts?!
**

from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.filters.morningstar import Q1500US
import sys
import logbook
from datetime import datetime
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import scipy.stats as stats
import matplotlib.pyplot as plt
import seaborn as sns
import collections
from collections import Counter
import sklearn
from sklearn import preprocessing
from sklearn.datasets import load_iris
from sklearn.preprocessing import MinMaxScaler
from sklearn.feature_selection import SelectKBest
from sklearn import model_selection
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
from sklearn import linear_model
from sklearn.linear_model import LinearRegression, Logisticregression
from sklearn import svm
from sklearn.svm import SVC, LinearSVC, NuSVC
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
import pytz
import pyfolio as pf
import talib as tb
from talib.abstract import *
import translate
from translate import translator
import alphalens
import zipline
from zipline import TradingAlgorithm
from zipline.api import order_target, record, symbol

translator('en', 'zh-TW', 'Hello Traders. Welcome to Hua er Jie')

def initialize(context):

set_universe = fetch_csv('https://www.dropbox.com/s/v4jak4le814i33v/finviz%20%281%29.csv?dl=0')  
context.stocks = symbols('ABTX', 'AMH', 'BLDR','CCOI', 'CHCT', 'CNQ', 'CXP', 'DRE', 'E', 'ENLK', 'EXAR', 'GMS', 'GPT', 'GTN', 'GWGH', 'HBMD', 'INVA', 'INVH', 'JCAP', 'JD', 'KMT', 'LFL', 'MOMO', 'NEP', 'NEWR', 'NORD', 'NRG', 'OAK', 'OLBK', 'RGEN', 'RICK', 'RP', 'RYN', 'SBY', 'SSP', 'SUPN', 'SWC', 'SYX', 'TITN', 'TPC', 'TWOU', 'UMH', 'USAP', 'USFD', 'UTEK', 'VDTH', 'VRNS', 'WGP', 'WMGI', 'WWE')  
context.historical_bars = 100  
context.feature_window = 10


pipe = Pipeline(screen = set_universe)  
attach_pipeline(pipe, 'my_pipeline')  


# talib stuff here to add into pipeline  

sma = abstract.SMA     # Do i need this line?  
sma_ = SMA(context.stocks, timeperiod=100)  

rsi_ = RSI(context.stocks, timeperiod=100)  

morning_star = CDLMORNINGSTAR(context.stocks, timeperiod=100)   # do i need a ohlc here?  

technical_indicators = [sma_, rsi_, morning_star]  


# add a filter for signals you dont want (optional)  


# pipe.add()  

pipe.add(technical_indicators, 'technical_indicators')  

# pipe.set_screen(add filter here)  (optional)  

def before_trading_start(context, data):

output = pipeline_output('my_pipeline')  

# two lines from 'Pipeline API' doc  

context.my_universe = output.sort('technical_indicators', ascending=False)  

update_universe(context.my_universe.index)   # index?  


print output

def handle_data(context, data):

cash = context.portfolio.cash  
current_positions = context.portfolio.positions  

prices = history(bar_count = context.historical_bars, \  
                 frequency = '1d', field = 'price')  

for stock in context.my_universe:                  #  or 'output'  

    try:  
        ma1 = data[stock].mavg(50)  
        ma2 = data[stock].mavg(200)  
        start_bar = context.feature_window  
        price_list = prices[stock].tolist()  
        X = []  
        y = []  

        # feature creation  
        bar = start_bar  

        while bar < len(price_list) - 1:  
            try:  
                end_price = price_list[bar + 1]  
                begin_price = price_list[bar]  
                pricing_list = []  
                xx = 0  
                for _ in range(context.feature_window):  
                    price = price_list[bar - (context.feature_window -xx)]  
                    pricing_list.append(price)  
                    xx += 1  
                    features = np.around(np.diff(pricing_list) /  
                                         pricing_list[:-1] \  
                                 * 100.0, 1)  
                    print(features)  

                if end_price > begin_price:  
                    label = 1  

                else:  
                    label = -1  
                    bar += 1  
                    X.append(features)  
                    y.append(labels)  

            except Exception as e:  
                bar += 1  
                print (('feature creation', str(e))  


        clf1 = DecisionTreeClassifier()  
        clf2 = RandomForestClassifier()  
        clf3 = SVC()  
        clf4 = LinearSVC()  
        clf5 = LogisticRegression()  
        clf6 = LinearRegression()  


        last_prices = price_list[-context.feature_window:]  
        current_features = np.around(np.diff(last_prices) / last_prices[:-1] * 100.0, 1)   # other code from my sheet?  
        X.append(current_featurs)  
        X = preprocessing.scale(X)  

        current_features = X[-1]  
        X = X[:-1]  

        clf1.fit(X, y)  
        clf2.fit(X, y)  
        clf3.fit(X, y)  
        clf4.fit(X, y)  
        clf5.fit(X, y)  
        clf6.fit(X, y)  


        p1 = clf1.predict(current_features)[0]  
        p2 = clf2.predict(current_features)[0]  
        p3 = clf3.predict(current_features)[0]  
        p4 = clf4.predict(current_features)[0]  
        p5 = clf5.predict(current_features)[0]  
        p6 = clf6.predict(current_features)[0]  

        if Counter([p1, p2, p3, p4, p5, p6]).most_common(1)[0][1] > = 4:  
            p = Counter([p1, p2, p3, p4, p5, p6]).most_common(1)[0][1]  

        else:  
            p = 0  

        print(('Prediction', p))  

        if p == 1 and ma1 > ma2:  
            order_target_percent(stock, 0.11) #leverage set at 11x  

        elif p == -1 and ma1 < ma2:  
            order_target_percent(stock, -0.11)  




record('ma1', ma1)  
record('ma2', ma2)  
record('Leverage', context.account.leverage)  
2 responses

Can you press "full backtest" on your algorithm, and share that backtest instead? The cut-and-paste is pretty hard to debug.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Dan - Thank you so much for your response.

When I press the 'run full backtest' button - I get 'Error: There was a problem starting your backtest. Please try again.'...

...am I doing something wrong here?