In [1]:
# Imports

%matplotlib inline
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Jupyter")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Market import TradeBar, QuoteBar
from QuantConnect.Jupyter import *
from QuantConnect.Indicators import *
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# import sklearn
# import keras

# Create an instance
qb = QuantBook()
print("imports ready.")
imports ready.
In [2]:
# initialize securities

spy = qb.AddEquity("SPY")
my_symbols = ['CGIX', 'UNB', 'PCF', 'NID', 'VDSI', 'UCFC', 'TEAM', 'LOB', 'NHC', 'XRAY']
my_securities = [qb.AddEquity(symbol, Resolution.Minute) for symbol in my_symbols]

# get large stocks

start_date = datetime(2015,1,1)
day_delta = timedelta(days=7)
start_date_5 = datetime(2015,1,5)
end_date = datetime(2015,6,1)
min_volume = 2*10**5

large_symbols = []
history_d = qb.History(qb.Securities.Keys, start_date, start_date_5, Resolution.Daily)
for symbol in history_d.index.levels[0]:
    volume = np.min(history_d.loc[symbol]["volume"]*history_d.loc[symbol]["low"])
    if volume > min_volume:
        date = history_d.index.levels[1][0]
        large_symbols.append(symbol)
print(large_symbols)

history_m = qb.History(large_symbols,start_date, start_date + day_delta, Resolution.Minute)
print(len(history_m))
['NHC', 'PCF', 'SPY', 'UCFC', 'VDSI', 'XRAY']
9360
In [3]:
# create data
number_of_input_days = 10
Data = []
# day = timedelta(days=1)
day = np.timedelta64(1, 'D')
In [ ]:
history_d = qb.History(large_symbols, start_date, end_date, Resolution.Daily)
for symbol in large_symbols:
    symbol_df = history_d.loc[symbol]
    dates = symbol_df.index
    
    ### The raw data code is breaking because of mismatching indexing and array size
    # raw_data = symbol_df.values
    # raw_data_3D = np.array([raw_data[i:i+number_of_input_days,:] for i in range(raw_data.shape[0]+1-number_of_input_days)])
    # raw_data_3D = raw_data_3D[:-1,:,:]
    ###
    
    ### In the for loop, the number of input days for the first symbol in large_symbols
    ### is 1, but 'dates' is a signle value DatetimeIndex, and so calling dates[1] is invalid -- it needs to be dates[0]
    ### Because of this, the iterator np_date is invalid and so the loop will break.
    for np_date in dates[number_of_input_days:].values: 
        history_m = qb.History([symbol], np_date, np_date+day, Resolution.Minute)
        print(symbol,np_date, len(history_m))
In [16]:
## The code below shows the error with the for-loop
symbol = 'NHC'
symbol_df = history_d.loc[symbol]
dates = symbol_df.index

for np_date in dates[number_of_input_days:].values:
    history_m = qb.History([symbol], np_date, np_date+day, Resolution.Minute)
    print(symbol,np_date, len(history_m))
    

print(dates[number_of_input_days:])
print(dates)