Now that you know a little about me, let me tell you about the issue I am
having:
Description of Issue
What did you expect to happen?
What happened instead?
Here is how you can reproduce this issue on your machine:
Reproduction Steps
Create two py scripts to ingest a custom bundle:
ingestion py script:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Anon'
from datetime import date, datetime, timedelta
from pathlib import Path
from os import getenv
import numpy as np
import pandas as pd
import alpaca_api as alpaca
pd.set_option('display.expand_frame_repr', False)
np.random.seed(42)
# Create a dictionary of symbols and company names
ASSETS = {
1: ('AAPL', 'Apple Inc.'),
2: ('TSLA', 'Tesla Inc.'),
}
# Create a list of symbols from the asset dictionary
SYMBOLS_TEST = [value[0] for value in ASSETS.values()]
ZIPLINE_ROOT = getenv('ZIPLINE_ROOT')
if not ZIPLINE_ROOT:
custom_data_path = Path('~/.zipline/custom_data').expanduser()
else:
custom_data_path = Path(ZIPLINE_ROOT, 'custom_data')
def ticker_generator():
"""
Lazily return (sid, ticker) tuple
"""
return ((key, value[0]) for key, value in ASSETS.items())
def data_generator():
minute_bars_results = alpaca.minute_bars(SYMBOLS_TEST)
for symbol, asset_name in ticker_generator():
df = minute_bars_results[minute_bars_results['symbol'] == str(symbol)]
df.tz_localize('Pacific/Auckland').tz_convert('UTC')
start_date = df.index[0]
end_date = df.index[-1]
first_traded = start_date.date()
auto_close_date = end_date + pd.Timedelta(days=1)
exchange = 'Alpaca'
yield (symbol, df), asset_name, start_date, end_date, first_traded, auto_close_date, exchange
def metadata_frame():
dtype = [
('symbol', 'object'),
('asset_name', 'object'),
('start_date', 'datetime64[ns]'),
('end_date', 'datetime64[ns]'),
('first_traded', 'datetime64[ns]'),
('auto_close_date', 'datetime64[ns]'),
('exchange', 'object'), ]
# Create an empty DataFrame with the number of rows equal to the length of ASSETS
df = pd.DataFrame(index=range(len(ASSETS)))
# Set the data type for each column
for col_name, col_type in dtype:
df[col_name] = pd.Series(dtype=col_type)
return df
def alpaca_to_bundle(interval='1m'):
def ingest(environ,
asset_db_writer,
minute_bar_writer,
daily_bar_writer,
adjustment_writer,
calendar,
start_session,
end_session,
cache,
show_progress,
output_dir
):
metadata = metadata_frame()
def minute_data_generator():
# could try without ths sid/symbol as it is in the bar data
return (sid_df for (sid_df, *metadata.iloc[sid_df[0]]) in data_generator())
minute_bar_writer.write(minute_data_generator(), show_progress=True)
metadata.dropna(inplace=True)
asset_db_writer.write(equities=metadata)
# adjustment_writer.write(splits=pd.read_hdf(custom_data_path / 'algoseek.h5', 'splits'))
# dividends do not work
# adjustment_writer.write(dividends=pd.read_hdf(custom_data_path / 'algoseek.h5', 'dividends'))
return ingest
api script to return data to script above
import pandas as pd
import getpass
from alpaca_trade_api.rest import REST, TimeFrame
from zipline.data.bundles import register
def get_bars(symbols):
API_KEY = getpass.getpass('Enter your API key: ')
SECRET_KEY = getpass.getpass('Enter your secret key: ')
# Create an instance of the REST API class
api = REST(key_id=API_KEY, secret_key=SECRET_KEY, base_url="https://paper-api.alpaca.markets")
# time frame dates
start_time = pd.Timestamp('2023-11-15', tz='UTC').isoformat()
end_time = pd.Timestamp('2023-11-16', tz='UTC').isoformat()
# Get bars for the time period
minute_bars = api.get_bars(symbols, TimeFrame.Minute, start_time, end_time).df
minute_bars
return minute_bars
Create an extension.py script in a .zipline folder with the following content
import sys
from pathlib import Path
sys.path.append(Path('~', '.zipline').expanduser().as_posix())
from zipline.data.bundles import register
from .zipline.ingest_alpaca_bundle import alpaca_to_bundle
from datetime import time
from pytz import timezone
register('alpaca',
alpaca_to_bundle(),
calendar_name='NYSE',
)
Run zipline ingest -b alpaca
Get error
/Users/anon/opt/anaconda3/envs/env_zipline/lib/python3.10/site-packages/zipline/__main__.py:60: UserWarning: Failed to load extension: '/Users/anon/opt/anaconda3/envs/env_zipline/.zipline/extension.py'
"'__name__' not in globals"
load_extensions(
...
What steps have you taken to resolve this already?
I have tried to import my .py script into another python notebook successfully, but from that .zipline folder, so was wondering if it might be a context issue as the main script is being run for a different location. Initially it wasn't finding the ingest_alpaca_bundle module, but I think I resolved that by adding .zipline.ingest_alpaca_bundle. Also I wasn't sure if having another script dependency was causing issues. It's hard to tell at the moment if it is an issue with the .py script or an environmental issue
Dear Zipline Maintainers,
Before I tell you about my issue, let me describe my environment:
Environment
Now that you know a little about me, let me tell you about the issue I am having:
Description of Issue
Here is how you can reproduce this issue on your machine:
Reproduction Steps
ingestion py script:
api script to return data to script above
zipline ingest -b alpaca
...
What steps have you taken to resolve this already?
I have tried to import my .py script into another python notebook successfully, but from that .zipline folder, so was wondering if it might be a context issue as the main script is being run for a different location. Initially it wasn't finding the ingest_alpaca_bundle module, but I think I resolved that by adding .zipline.ingest_alpaca_bundle. Also I wasn't sure if having another script dependency was causing issues. It's hard to tell at the moment if it is an issue with the .py script or an environmental issue
...
Anything else?
Any help would be greatly appreciated! ...
Sincerely, John.