Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using Eclipse and Pydev, getting "unresolved import: order_target" error

I've been using IPython Notebook, and it works pretty well. But, I'd prefer to use Eclipse with PyDev.

I have PyDev's Python interpreter configured to use the correct python instance (version 2.7.9) and zipline is installed in the instance. I can run python code from the command line using the 2.7.9 python instance. But, when I try to run the exact same code from Eclipse I get "unresolved import: order_target" and "unresolved import: symbol".

Has anyone else run into this and hopefully know how to resolve this issue? I've been trying everything I can think of for 2 days. I think my perspective is becoming a bit warped from the "can't see the forest for the trees" phenomenon. lol

Thanks for any help!!

Mike

4 responses

some additional information would be useful:
- can you post your code?
- can you see the reference to zipline in eclipse?

Hi Ueli, thanks for the reply. I'm using the the last example in the Zipline tutorial (http://www.zipline.io/tutorial/).

Here are the settings for PyDev in eclipse. I made sure python 2.7.9 is the interpreter.
http://s25.postimg.org/z3wtc1nzz/eclipse_pydev.png

Here's the code I'm trying to run.

import matplotlib.pyplot as plt  
from zipline.api import order_target, record, symbol, history, add_history  
import numpy as np

def initialize(context):  
    # Register 2 histories that track daily prices,  
    # one with a 100 window and one with a 300 day window  
    add_history(100, '1d', 'price')  
    add_history(300, '1d', 'price')

    context.i = 0

def handle_data(context, data):  
    # Skip first 300 days to get full windows  
    context.i += 1  
    if context.i < 300:  
        return

    # Compute averages  
    # history() has to be called with the same params  
    # from above and returns a pandas dataframe.  
    short_mavg = history(100, '1d', 'price').mean()  
    long_mavg = history(300, '1d', 'price').mean()

    # Trading logic  
    if short_mavg[0] > long_mavg[0]:  
        # order_target orders as many shares as needed to  
        # achieve the desired number of shares.  
        order_target(symbol('AAPL'), 100)  
    elif short_mavg[0] < long_mavg[0]:  
        order_target(symbol('AAPL'), 0)

    # Save values for later inspection  
    record(AAPL=data[symbol('AAPL')].price,  
           short_mavg=short_mavg[0],  
           long_mavg=long_mavg[0])

def analyze(context, perf):  
    fig = plt.figure()  
    ax1 = fig.add_subplot(211)  
    perf.portfolio_value.plot(ax=ax1)  
    ax1.set_ylabel('portfolio value in $')

    ax2 = fig.add_subplot(212)  
    perf['AAPL'].plot(ax=ax2)  
    perf[['short_mavg', 'long_mavg']].plot(ax=ax2)

    perf_trans = perf.ix[[t != [] for t in perf.transactions]]  
    buys = perf_trans.ix[[t[0]['amount'] > 0 for t in perf_trans.transactions]]  
    sells = perf_trans.ix[  
        [t[0]['amount'] < 0 for t in perf_trans.transactions]]  
    ax2.plot(buys.index, perf.short_mavg.ix[buys.index],  
             '^', markersize=10, color='m')  
    ax2.plot(sells.index, perf.short_mavg.ix[sells.index],  
             'v', markersize=10, color='k')  
    ax2.set_ylabel('price in $')  
    plt.legend(loc=0)  
    plt.show()  

mmh... how do you start your algorithm in eclipse? do you use TradingAlgorithm.run() (also demonstrated in the tutorial)? because just from this code, it is not even clear during which time period the stratey runs....

btw: i also used to love eclipse for doing everything and still consider it the best free ide for java and c++. also for python, i started using pydev but soon switched to pycharm from jetbrains which imho is far superior (and free).

Wow, your comment totally pointed me in the right direction! It's working now. I used a more simple program to test with. I was being impatient and didn't fully read how to run it "Manually" on the tutorial. But, your comment jogged my brain, so thanks for that!

For you gee-whiz file, I got the following code to run successfully. Thanks for your help!!!

import pytz  
from datetime import datetime

from zipline.api import order_target, record, symbol, order  
from zipline.algorithm import TradingAlgorithm  
from zipline.utils.factory import load_bars_from_yahoo

# Load data manually from Yahoo! finance  
start = datetime(2000, 1, 1, 0, 0, 0, 0, pytz.utc)  
end = datetime(2012, 1, 1, 0, 0, 0, 0, pytz.utc)  
data = load_bars_from_yahoo(stocks=['AAPL'], start=start, end=end)

# Define algorithm  
def initialize(context):  
    pass

def handle_data(context, data):  
    order(symbol('AAPL'), 10)  
    record(AAPL=data[symbol('AAPL')].price)

# Create algorithm object passing in initialize and  
# handle_data functions  
algo_obj = TradingAlgorithm(initialize=initialize, handle_data=handle_data)

# Run algorithm  
perf_manual = algo_obj.run(data)