Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
CSV Fetch not working

Hello I am trying to import a CSV file of prices from Google Drive and then plot the closing prices on a plot save the values in the csv columns as a list.
Something is wrong the code below isn't working like that

import pandas, talib  
from pytz import timezone  
def rename_col(df):  
    df = df.rename(columns={'Close': 'Closing_Data'})  
    df = df.fillna(method='ffill')  
    return df  
#---------------------------------------------------------------------#  
def initialize(context):  
    context.stocks = {}  
    url_x = 'https://drive.google.com/file/d/0B8sToPDpAlKpYVl5SEUzNFRXUjA/view?usp=sharing'  
    #url_x = 'http://www.nseindia.com/content/indices/histdata/CNX%20NIFTY12-04-2014-10-04-2015.csv'  
    fetch_csv(url_x,  
              date_column='Date',  
              symbol = 'NIFTY',  
              usecols = ['Close'],  
              post_func=rename_col ,  
              date_format='%d-%mmm-%Y',  
              )  
    #context.stock = symbol('NIFTY')  
#----------------------------------------------------------------------#  
def handle_data(context, data):  
    if 'Closing_Data' in data['NIFTY']:  
        record(Day_Closes = data['NIFTY'].Closing_Data)  
6 responses

For a file to be fetched into your algo, it needs to be a "pure" CSV, without extra HTML added to the file. Google Drive is tricky to setup, I would recommend to use the Public folder in your Dropbox or to use www.copy.com another free hosting service to get the public URL.

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.

Hello Alisa
Even quandl is not working in this code.I am not able to detect the error as the Build algorithm works fine but there are no values recorded though I am using record function

Thanks for the reply though.

I moved the CSV over to www.copy.com and made a couple fixes to get the code to run. Namely,

  • The CSV needs a symbol or a signal column. This is either the stock(s) you want to trade or the data you're using to signal other trades. In your case, you are using NIFTY values as a signal to purchase another security. I added a column name with the signal NIFTY.
  • The algo needs a US security to trade. I added SPY to initialize(), which you can change to any other basket of securities.
  • The close prices in the CSV are accessed via the syntax "data['NIFTY']['Closing_Data']".

And voila! Here's the algo, you can continue to tweak the code.

Thanks alisa for replying
Can you show how to save all the columns in the csv in this sample csv in python lists to process.The code is working in case of just single column not in case of multiple columns

Sure, if you comment out the line "usecols" your algo will access all of the columns in the CSV.

from the logs,

2014-04-15PRINTOpen  
2014-04-15PRINTLow  
2014-04-15PRINTHigh  
2014-04-16PRINTOpen  
2014-04-16PRINTLow  
2014-04-16PRINTHigh  

Thanks alisa
if 'Closing_Data' in data['NIFTY']:
record(Day_Closes = data['NIFTY']['Closing_Data'])
if 'Open' in data['NIFTY']:
print "Open"
if 'Low' in data['NIFTY']:
print "Low"
if 'High' in data['NIFTY']:
print "High"

Here the values are only getting displayed not getting stored.How to store it in python lists.If the variable Day_Closes is not able to be processed.
Also using something like Close_p = data['NIFTY'][Closing_Data'] is not working