2

This is my First Question Here, Excuse me for my mistakes I have been reading Andreas Clenow's Trading Evolved, a Book about Backtesting and Finance using Python Here is the Code that and error that I receive

%matplotlib inline

# Import Zipline functions that we need
from zipline import run_algorithm
from zipline.api import order_target_percent, symbol

# Import date and time zone libraries
from datetime import datetime
import pytz
import pandas as pd

# Import visualization
import matplotlib.pyplot as plt


def initialize(context):
    # Which stock to trade
    context.stock = symbol('AAPL')
    
    # Moving average window
    context.index_average_window = 100
    
def handle_data(context, data):
    # Request history for the stock
    equities_hist = data.history(context.stock, "close", 
                                 context.index_average_window, "1d")
    
    # Check if price is above moving average
    if equities_hist[-1] > equities_hist.mean():
        stock_weight = 1.0
    else:
        stock_weight = 0.0
    
    # Place order
    order_target_percent(context.stock, stock_weight)

def analyze(context, perf):
    fig = plt.figure(figsize=(12, 8))
    
    # First chart
    ax = fig.add_subplot(311)
    ax.set_title('Strategy Results')
    ax.semilogy(perf['portfolio_value'], linestyle='-', 
                label='Equity Curve', linewidth=3.0)
    ax.legend()
    ax.grid(False)
    
    # Second chart
    ax = fig.add_subplot(312)
    ax.plot(perf['gross_leverage'], 
            label='Exposure', linestyle='-', linewidth=1.0)
    ax.legend()
    ax.grid(True)

    # Third chart
    ax = fig.add_subplot(313)
    ax.plot(perf['returns'], label='Returns', linestyle='-.', linewidth=1.0)
    ax.legend()
    ax.grid(True)

# Set start and end date
#start_date = datetime(1996, 1, 1, tzinfo=pytz.UTC)
#end_date = datetime(2018, 12, 31, tzinfo=pytz.UTC)
#start_date = pd.to_datetime('1996-1-1', utc=True)
#end_date = pd.to_datetime('2018-12-31', utc=True)
start_date = pd.to_datetime('1996-1-1', utc=True)
end_date = pd.to_datetime('2018-12-31', utc=True)


# Fire off the backtest
results = run_algorithm(
    start=start_date, 
    end=end_date, 
    initialize=initialize, 
    analyze=analyze, 
    handle_data=handle_data, 
    capital_base=10000, 
    data_frequency = 'daily', bundle='quandl' 
) 

The error I receive is this

NoBenchmark Traceback (most recent call last) _RunAlgoError: No benchmark_spec was provided, and zipline.api.set_benchmark was not called in initialize.

Thanks in Advance

2 Answers 2

10

I had the same problem, what worked for me was setting the benchmark in initialize() to False (you have to import set_benchmark from zipline.api)

def initialize(context):
    # Which stock to trade
    context.stock = symbol('AAPL')
    
    # Moving average window
    context.index_average_window = 100

    set_benchmark(False)
    
0

I ran into similar problem (was running zipline in jupyter using the following code

%load_ext zipline
from zipline.api import order, record, symbol, set_benchmark

def initiatlize(context):
    context.stock = symbol('AAPL')
    set_benchmark(False)
        

def handle_data(context, data):
    order(symbol('AAPL'), 10)
    record(APPL=data.current(symbol('AAPL'), "price"))


%zipline --bundle quantopian-quandl --start 2008-1-1 --end 2012-1-1 -o strat.pickle

Adding "--no-benchmark" option to the execution command fixed the problem. I am still interested in figuring out what that option does, but that's for another time.

 %zipline --bundle quantopian-quandl --no-benchmark  --start 2008-1-1 --end 2012-1-1 -o strat.pickle

Note

The code I have referred was through a tutorial at https://www.youtube.com/watch?v=J4LTIixReMA

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.