Back to Community
Problem with fetch_csv when loading multiple signals.

I'm new to this community and I'm having trouble using the fetcher to import multiple signals from one single csv file.
My data are located at :
https://dl.dropboxusercontent.com/u/503052837/FinalDataFrame.csv
or:
https://copy.com/0F3y2N9kfaatlyzY
They are the same so you can use either one.

I have several columns signals for each of Dow Jones Stocks
And I seem to getting this error every time i run this.
Can someone please explain for this?

def initialize(context):

    fetch_csv("https://copy.com/0F3y2N9kfaatlyzY",date_column ='date', date_format = '%m-%d-%Y')

    set_symbol_lookup_date('2015-11-01')  
    context.stocks = symbols('MMM', 'DIS', 'NKE', 'GE', 'AAPL', 'GS', 'MSFT', 'PFE', 'UTX',  
       'INTC', 'HD', 'PG', 'JPM', 'V', 'TRV', 'DD', 'CSCO', 'BA', 'AXP',  
       'CVX', 'IBM', 'MRK', 'MCD', 'UNH', 'WMT', 'XOM', 'CAT', 'VZ', 'KO',  
       'JNJ')  
def handle_data(context, data):  
    for stock in data:  
        if ("rate" and "retweet") in data[stock]:  
            indicator1 = data[stock]["rate"]  
            indicator2 = data[stock]["retweet"]  
        try:  
            current_price = data[stock].price  
            current_position = context.portfolio.positions[stock].amount  
            #cash = context.portfolio.cash  
            if (indicator1 < -1) and (indicator2 > 10) and (current_position == 0):  
                number_of_shares = int((33000 - current_position*current_price)/current_price)  
                order(stock, number_of_shares)  
                log.info("Buying shares")

            elif  (indicator1 > 5) and (indicator2 > 10) and (current_position != 0):  
                order_target(stock, 0)  
                log.info("Selling share")  
        except:  
            pass  
KeyError: 'symbol'  
There was a runtime error on line 2.

Thank you very much!

11 responses

I cloned your code and the algorithm runs using the dropbox link. Perhaps the CSV changed since you tried running the code?

Cheers,
Alisa

Clone Algorithm
13
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: 567afebb499ebf117746c3a9
There was a runtime error.
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.. Zhuite Si is this leverage?

It is, yes, in the sense that leverage is very low, so the profit vs. dollars put into play are lower than what you see on the chart, in fact, lower than the benchmark. Thank you chan john for sniffing out another one. Explanation.

Hover over the custom chart and compare PvR (Profit vs. Risk) to Algorithm and Benchmark values.

Clone Algorithm
2
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: 567b9e21a5f1f611979625c1
There was a runtime error.

how to take out this leverage effect...? so that I can see... whether.. his algo really.. bet the index... without the leverage anyone..? thanks...

Hey guys, Merry Christmas! Thank you for all the explanations. I fixed it by changing column name from "tickers"to "symbol", then everything works fine.

pls, post your corrected algo. thanks ;)

Hi, chan. I mean the code itself is correct, problem is about the CSV file that I need to fetch, the column name that contains the tickers must be "symbol", not others. Previously the column in my CSV was called "tickers". The code above was just a scratch, I was learning to use the fetcher method.

@ Zhuite Si whats the logic.. behind your algo...? and why is it returning 900 percent... in short amount of time...aside from leverage...? is it base on retweet?

Clone Algorithm
0
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: 567bba57423031116ef1df14
There was a runtime error.

I would change that original error message to -- Expected csv header 'symbol' not found.

Clarifying, Chan John ran the identical code as above, extending the algo's date range.
The CSV includes data from 2014-01-02 (where it starts trading) to 2015-12-04.
Headers ... date,symbol,rate,rate_tfidf,follower,retweet,follower_per_tweet,5rate,5rate_tfidf,5follower,5retweet,5follower_per_tweet,20rate, 20rate_tfidf,20follower,20retweet,20follower_per_tweet,60rate,60rate_tfidf,60follower,60retweet,60follower_per_tweet

The custom chart shows leverage over 6000%, negative cash (margin) over $800K. No shorting.
PvR (Profit vs. Risk), the amount of profit for each dollar put into play (including negative cash) is about 11 cents.
One of the advantages of PvR is that no matter how much leverage it still measures actual profitability.

Below, I added a line, the second line here, keeping the code from doubling up on orders:

    for stock in data:  
        if get_open_orders(stock): continue  

Max leverage is now .86 and an extra penny PvR, well over half of the benchmark.
A starting point. Now it is something a software developer can sink their teeth into.

Clone Algorithm
2
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: 567ce5c5237b0c116e1932ed
There was a runtime error.

The basic idea is to construct some signals by using NLP stuff on twits, very basic sentiment analysis actually(buy if the sentiment is positive, sell if the sentiment is negative). So far the signals are poor as you can see, if you set a limit on the leverage.

Have you normalized the data? 200 tweets for AAPL might be small compared to usual and extraordinary for ACME.
Also attention can drop off the next day or two and I don't think you really want to sell immediately just because things are quiet, it could be rising.

This is some simple code anyone can use to see what a value tends to be doing. Here, it was run twice, once for indicator1, another time for indicator2, and the results added as comments. c = context ... set elsewhere.

        indicator1 = data[stock]['rate']  
        indicator2 = data[stock]['retweet']  
        #track_val(c, indicator1)  # now 0.00  avg  0.18  lo -31.00  hi    70.00  
        #track_val(c, indicator2)  # now 0.00  avg 48.15  lo   0.00  hi 12895.00

def track_val(c, var):    # Value diagnostics  
    if 'val' not in c:  
        c.val = {  
            'hi' : var,  
            'lo' : var,  
            'avg': [0, 0],  
        }  
    avg, num     = c.val['avg']  
    avg_new      = ((avg * num) + var) / (num + 1)  
    c.val['avg'] = [avg_new, num + 1]  
    if var > c.val['hi']: c.val['hi'] = var  
    if var < c.val['lo']: c.val['lo'] = var  
    log.info('now {}  avg {}  lo {}  hi {} '.format(  
        '%.2f' % var, '%.2f' % avg_new, '%.2f' % c.val['lo'], '%.2f' % c.val['hi']))