To prepare for live trading with Alpaca, the following steps are required:
To place orders with Alpaca, it is necessary to collect securities master listings from Alpaca. It is not sufficient to have collected the listings from another vendor; specific Alpaca fields must be present in the securities master database in order to allow QuantRocket to communite with the Alpaca API.
from quantrocket.master import collect_alpaca_listings
collect_alpaca_listings()
{'status': 'success', 'msg': 'successfully loaded alpaca securities'}
Next, create the real-time database for collecting data from Alpaca. These steps simply create the database. Real-time data collection will be initiated from the Zipline strategy code. First, create the tick database. Note that we are collecting minute-level data instead of ticks to reduce the data volume and increase the potential universe size. See the usage guide for more information.
from quantrocket.realtime import create_alpaca_tick_db
create_alpaca_tick_db(
"us-stk-realtime",
# specifying a universe is required, but we will override this when initiating data
# collection in the Zipline strategy, so the universe need not exist
universes="us-stk",
fields=["MinuteOpen",
"MinuteHigh",
"MinuteLow",
"MinuteClose",
"MinuteVolume"])
{'status': 'successfully created tick database us-stk-realtime'}
Then create the 1-minute aggregate database derived from the tick database:
from quantrocket.realtime import create_agg_db
create_agg_db(
"us-stk-realtime-1min",
tick_db_code="us-stk-realtime",
bar_size="1m",
fields={
"MinuteOpen":["Open"],
"MinuteHigh": ["High"],
"MinuteLow": ["Low"],
"MinuteClose": ["Close"],
"MinuteVolume": ["Sum"]})
{'status': 'successfully created aggregate database us-stk-realtime-1min from tick database us-stk-realtime'}