Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Custom CSV File loading has different market close time than Quantopian history function

I am loading a custom CSV File with success (the data prints on screen). The problem is that the market close time in the file is 17.00 p.m. whereas history function (for a 30 days history) in Quantopian expects per-daily close data at 21.00. I have converted the data to UTC but still no result: It prints NaN values.

I would appreciate any advice.

2 responses

It's hard to troubleshoot it without seeing it. Do you think you could share a sample algo with a link to the sample data file so we can look at it?

The quickest way may be to, in your pre_func(), change it from a datetime into a date and solve it that way. (https://www.quantopian.com/help#overview-fetcher)

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.

Here is my sample code:

if __name__ == '__main__':  
import numpy as np  
import pytz  
import pandas as pd  
import pylab as pl  
import matplotlib.pyplot as plt  
import os  
import sys  
import collections  
import csv  

data1 = pd.read_csv('SPY_20150101_20151202.csv', index_col=['Date'], usecols=['Symbol','Date', 'Open', 'High','Low','Close','Volume'])  

data1.columns = ['open','high','low','close','volume','price']  
data1.index = data1.index.to_datetime().tz_localize("Europe/London").tz_convert("UTC")

trading.environment = TradingEnvironment(bm_symbol='SPY', exchange_tz='Europe/London')  
trading.environment.trading_days = pd.date_range(start=data1.index.normalize()[0],  
                                             end=data1.index.normalize()[-1],  
                                             freq=pd.tseries.offsets.CDay(holidays=tradingcalendar_lse.non_trading_days))

trading.environment.open_and_closes = pd.DataFrame(index=trading.environment.trading_days,columns=["market_open","market_close"])  
trading.environment.open_and_closes.market_open = (trading.environment.open_and_closes.index + pd.to_timedelta(60*7,unit="T")).to_pydatetime()  
trading.environment.open_and_closes.market_close = (trading.environment.open_and_closes.index + pd.to_timedelta(60*15+30,unit="T")).to_pydatetime()

data1 = data1.dropna()

data = {'SPY' : data1.to_dict()}

panel = pd.Panel.from_dict(data, orient='minor')

data = pd.Panel(data)

sim_params = factory.create_simulation_parameters(  
   start = pd.to_datetime("2015-02-02 09:00:00").tz_localize("UTC"),  
   end = pd.to_datetime("2015-12-02 17:00:00").tz_localize("UTC"),  
   data_frequency = "minute",  
   emission_rate = "minute")  

algo_obj = TradingAlgorithm(data_frequency = "minute", initialize=initialize, handle_data=handle_data, sim_params=sim_params)  
results = algo_obj.run(panel['close'], overwrite_sim_params=False)



def initialize(context):

sym = symbol('SPY')

add_history(29, '1d', 'price')  
add_history(1, '1d', 'price')  

context.i = 0

pass

def handle_data(context, data):  

sym = symbol('SPY')

context.i += 1

if context.i < 30:  
     return

close_prices = history(29, '1d', 'price')  
yesterday_prices = history(1, '1d', 'price')

And here is a sample output when I print close_prices (as you can see due to this end market date-time mismatch between the file and history function of Quantopian
it has NaN values):

2015-01-20 21:00:00+00:00 NaN
2015-01-21 21:00:00+00:00 NaN
2015-01-22 21:00:00+00:00 NaN
2015-01-23 21:00:00+00:00 NaN
2015-01-26 21:00:00+00:00 NaN
2015-01-27 21:00:00+00:00 NaN
2015-01-28 21:00:00+00:00 NaN
2015-01-29 21:00:00+00:00 NaN
2015-01-30 21:00:00+00:00 NaN
2015-02-02 21:00:00+00:00 NaN
2015-02-03 21:00:00+00:00 NaN
2015-02-04 21:00:00+00:00 NaN
2015-02-05 21:00:00+00:00 NaN
2015-02-06 21:00:00+00:00 NaN
2015-02-09 21:00:00+00:00 NaN
2015-02-10 21:00:00+00:00 NaN
2015-02-11 21:00:00+00:00 NaN
2015-02-12 21:00:00+00:00 NaN
2015-02-13 21:00:00+00:00 NaN
2015-02-17 21:00:00+00:00 NaN
2015-02-18 21:00:00+00:00 NaN
2015-02-19 21:00:00+00:00 NaN
2015-02-20 21:00:00+00:00 NaN
2015-02-23 21:00:00+00:00 NaN
2015-02-24 21:00:00+00:00 NaN
2015-02-25 21:00:00+00:00 NaN
2015-02-26 21:00:00+00:00 NaN
2015-02-27 21:00:00+00:00 NaN
2015-03-02 16:45:00+00:00 211.95

The format of the CSV file (Per-Minute resolution):

Symbol,Date,Open,High,Low,Close,Volume,OpenInt
SPY,02-Jan-2015 09:00,206.44,206.44,206.38,206.39,12200
SPY,02-Jan-2015 09:01,206.39,206.4,206.38,206.38,5600
SPY,02-Jan-2015 09:02,206.38,206.41,206.38,206.41,6115
SPY,02-Jan-2015 09:03,206.4,206.4,206.38,206.38,9590
SPY,02-Jan-2015 09:04,206.38,206.41,206.38,206.4,7300
SPY,02-Jan-2015 09:05,206.42,206.45,206.42,206.45,21621
SPY,02-Jan-2015 09:06,206.43,206.52,206.43,206.43,37770
SPY,02-Jan-2015 09:07,206.52,206.56,206.51,206.53,31882
SPY,02-Jan-2015 09:08,206.54,206.57,206.54,206.54,26868
SPY,02-Jan-2015 09:09,206.55,206.58,206.55,206.56,35707
SPY,02-Jan-2015 09:10,206.56,206.59,206.56,206.59,9300

I would appreciate any help!