So, I've uploaded the data, all checks out, clicked on the name, placed the example in a new algorithm and whoa:
Runtime exception: ImportError: No module named 'quantopian.research'
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.data.user_5f38f7672471750011004615 import spy_test2
# Date,Symbol,Open,High,Low,Close,AdjClose,Volume
pipe = Pipeline(
columns={
'volume': spy_test2.volume.latest,
'close': spy_test2.close.latest,
'low': spy_test2.low.latest,
'high': spy_test2.high.latest,
'open': spy_test2.open_.latest,
'date': spy_test2.asof_date.latest
},
screen=spy_test2.open_.latest.notnull()
)
df = run_pipeline(pipe, '2002-01-02', '2020-08-06')
df.head()
Managed to get the above example running @ notebook, noticed that the values for each date are actually a day ahead, for example:
close date high low open volume
2002-01-03 00:00:00+00:00 Equity(8554 [SPY]) 115.529999 2002-01-02 115.750000 113.809998 115.110001 18651900.0
The initial date is 2002-01-03 00:00:00+00:00, but the values are for 2002-01-02 (when comparing against the csv data)
So, how do we use/load our own data in an algorithm in place of:
context.ticker = sid(xxxx) ?
Tried a few examples but none worked, reballance function expects a context.spy, but I can't find how to "add" it from the custom dataset.
import datetime
import pandas as pd
from quantopian.pipeline import Pipeline
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.data.user_5f38f7672471750011004615 import spy_test2
def my_pipeline(context):
pipe = Pipeline()
return Pipeline(
columns={
'volume': spy_test2.volume.latest,
'close': spy_test2.close.latest,
'low': spy_test2.low.latest,
'high': spy_test2.high.latest,
'open': spy_test2.open_.latest,
},
)
return pipe
def before_trading_start(context, data):
context.output = pipeline_output('my_pipeline').dropna()
df = pd.DataFrame(context.output)
df = df.reset_index()
#print(df)
def initialize(context):
context.FirstDateOfBacktest = get_environment('start').date()
attach_pipeline(my_pipeline(context), 'my_pipeline')
#context.spy = sid(8554) # Gets quantopian SPY values
#context.spy = pipeline_output(my_pipeline) # Doesnt work
#set_benchmark(sid(8554))
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open())
def my_rebalance(context, data):
#context.spy = pipeline_output('my_pipeline').dropna() # Doesnt work
# Print the current values from the custom dataset
p_open = data.history(context.spy, 'open', 1, '1d')
high = data.history(context.spy, 'high', 1, '1d')
low = data.history(context.spy, 'low', 1, '1d')
close = data.history(context.spy, 'close', 1, '1d')
volume = data.history(context.spy, 'volume', 1, '1d')
log.info('%.6f,%.6f,%.6f,%.6f,%d' % (p_open, high, low, close, volume))
Also tried using the fetcher, no luck.
The data in the csv is in the following format:
date,symbol,open,high,low,close,adjclose,volume
1993-01-29,SPY,43.968750,43.968750,43.750000,43.937500,26.184059,1003200
Supposedly fetcher would "internally map" the SPY to symbol('SPY'), however, it doesn't seem so. The code bellow does not get the data from my CSV, but rather the data from the default quantopian SPY, which is something I don't want :)
fetch_csv('https://www.mydomain.com/spy-to-q.csv',
date_column = 'date',
date_format = '%Y-%m-%d', timezone='UTC')
context.spy = symbol('SPY')
So... again.. how do I access my "custom SPY" data within the algo ?