Alpaca¶
Documentation¶
- class lumibot.brokers.alpaca.Alpaca(config, max_workers=20, chunk_size=100, connect_stream=True, data_source=None)¶
Bases:
Broker
A broker class that connects to Alpaca
- api¶
Alpaca API object
- Type:
tradeapi.REST
- get_timestamp()¶
Returns the current UNIX timestamp representation from Alpaca
- is_market_open()¶
Determines if the market is open.
- get_time_to_open()¶
How much time in seconds remains until the market next opens?
- get_time_to_close()¶
How much time in seconds remains until the market closes?
Examples
>>> # Connect to Alpaca >>> from lumibot.brokers import Alpaca >>> ALPACA_CONFIG = { ... # Put your own Alpaca key here: ... "API_KEY": "YOUR_API_KEY", ... # Put your own Alpaca secret here: ... "API_SECRET": "YOUR_API_SECRET", ... # If you want to go live, you must change this ... "ENDPOINT": "https://paper-api.alpaca.markets", ... } >>> alpaca = Alpaca(ALPACA_CONFIG) >>> print(alpaca.get_time_to_open()) >>> print(alpaca.get_time_to_close()) >>> print(alpaca.is_market_open())
>>> # Run a strategy on Alpaca >>> from lumibot.strategies import Strategy >>> from lumibot.brokers import Alpaca >>> from lumibot.traders import Trader >>> >>> ALPACA_CONFIG = { ... # Put your own Alpaca key here: ... "API_KEY": "YOUR_API_KEY", ... # Put your own Alpaca secret here: ... "API_SECRET": "YOUR_API_SECRET", ... # If you want to go live, you must change this ... "ENDPOINT": "https://paper-api.alpaca.markets", ... } >>> >>> class AlpacaStrategy(Strategy): ... def on_trading_interation(self): ... if self.broker.is_market_open(): ... self.create_order( ... asset=Asset(symbol="AAPL"), ... quantity=1, ... order_type="market", ... side="buy", ... ) >>> >>> alpaca = Alpaca(ALPACA_CONFIG) >>> strategy = AlpacaStrategy(broker=alpaca) >>> trader = Trader() >>> trader.add_strategy(strategy) >>> trader.run()
- ASSET_TYPE_MAP = {'forex': [], 'future': [], 'option': [], 'stock': ['us_equity']}¶
- cancel_order(order)¶
Cancel an order
- get_historical_account_value()¶
Get the historical account value of the account.
- get_time_to_close()¶
How much time in seconds remains until the market closes?
Return the remaining time for the market to closes in seconds
- Parameters:
None
- Returns:
Number of seconds until close.
- Return type:
int
Examples
If it is 1400 and the market closes at 1600, then there are 7,200 seconds until the market closes.
- get_time_to_open()¶
How much time in seconds remains until the market next opens?
Return the remaining time for the market to open in seconds
- Parameters:
None
- Returns:
Number of seconds until open.
- Return type:
int
Examples
If it is 0830 and the market next opens at 0930, then there are 3,600 seconds until the next market open.
>>> self.get_time_to_open()
- get_timestamp()¶
Returns the current UNIX timestamp representation from Alpaca
- Parameters:
None
- Returns:
Sample unix timestamp return value: 1612172730.000234
- Return type:
int
- is_market_open()¶
Determines if the market is open.
- Parameters:
None
- Returns:
True if market is open, false if the market is closed.
- Return type:
boolean
Examples
>>> self.is_market_open() True
- map_asset_type(type)¶
- Alpaca.get_time_to_close()
How much time in seconds remains until the market closes?
Return the remaining time for the market to closes in seconds
- Parameters:
None
- Returns:
Number of seconds until close.
- Return type:
int
Examples
If it is 1400 and the market closes at 1600, then there are 7,200 seconds until the market closes.
- Alpaca.get_time_to_open()
How much time in seconds remains until the market next opens?
Return the remaining time for the market to open in seconds
- Parameters:
None
- Returns:
Number of seconds until open.
- Return type:
int
Examples
If it is 0830 and the market next opens at 0930, then there are 3,600 seconds until the next market open.
>>> self.get_time_to_open()
- Alpaca.get_timestamp()
Returns the current UNIX timestamp representation from Alpaca
- Parameters:
None
- Returns:
Sample unix timestamp return value: 1612172730.000234
- Return type:
int
- Alpaca.is_market_open()
Determines if the market is open.
- Parameters:
None
- Returns:
True if market is open, false if the market is closed.
- Return type:
boolean
Examples
>>> self.is_market_open() True