def get_equities(self): assets = [] t = normalize_date(pd.Timestamp('now', tz=NY)) raw_assets = self._api.list_assets(asset_class='us_equity') for raw_asset in raw_assets: asset = Equity( raw_asset.id, raw_asset.exchange, symbol=raw_asset.symbol, asset_name=raw_asset.symbol, ) asset.start_date = t - one_day_offset if raw_asset.status == 'active' and raw_asset.tradable: asset.end_date = t + end_offset else: # if asset is not tradable, set end_date = day before asset.end_date = t - one_day_offset asset.auto_close_date = asset.end_date assets.append(asset) # register all unseen exchange name as # alias of NYSE (e.g. AMEX, ARCA, NYSEARCA.) if not default_calendar.has_calendar(raw_asset.exchange): register_calendar_alias(raw_asset.exchange, 'NYSE', force=True) return assets
def __init__(self, start=None, end=None, assets=None, exchange='NYSE'): self.start = normalize_date(pd.Timestamp(start or '2018-08-13')) self.end = normalize_date(pd.Timestamp(end or '2018-08-14')) self._exchange = exchange self._calendar = get_calendar(exchange) self.assets = assets or ['asset-0', 'asset-1', 'asset-2'] minutes = self._calendar.minutes_for_sessions_in_range( self.start, self.end) self._minutely_bars = {} for i, asset in enumerate(self.get_equities()): bars = create_bars(minutes, i) self._minutely_bars[asset] = bars days = self._calendar.sessions_in_range(self.start, self.end) self._daily_bars = {} for i, asset in enumerate(self.get_equities()): bars = create_bars(days, i) self._daily_bars[asset] = bars
def get_equities(self): assets = [] t = normalize_date(pd.Timestamp('now', tz=NY)) raw_assets = self._api.list_assets(asset_class='us_equity') for raw_asset in raw_assets: asset = Equity( raw_asset.id, raw_asset.exchange, symbol=raw_asset.symbol, asset_name=raw_asset.symbol, ) asset.start_date = t - one_day_offset if raw_asset.status == 'active' and raw_asset.tradable: asset.end_date = t + end_offset else: # this is an experimental change, if an asset is not active or # tradable, don't include it in the asset list. why? # first the logical thing - if it's not tradable - we don't # need it. now why bother? # some symbols are included more than once in the Alpaca list. # e.g VXX. one is tradable, one is not. as it happens, the # first one we iterate on is the not tradable asset. which # means when someone tries to trade it, even though there's a # tradable asset, it rejects it because we test it against the # untradable one. by doing this - we avoid this issue. # but, it fear it may cause issues (e.g if an asset was # tradable yesterday, but not tradable today. just a thought) # so I do this with caution. continue # if asset is not tradable, set end_date = day before asset.end_date = t - one_day_offset asset.auto_close_date = asset.end_date assets.append(asset) # register all unseen exchange name as # alias of NYSE (e.g. AMEX, ARCA, NYSEARCA.) if not default_calendar.has_calendar(raw_asset.exchange): register_calendar_alias(raw_asset.exchange, 'NYSE', force=True) return assets
def _can_order_asset(self, asset): if not isinstance(asset, Asset): raise UnsupportedOrderParameters( msg="Passing non-Asset argument to 'order()' is not supported." " Use 'sid()' or 'symbol()' methods to look up an Asset.") if asset.auto_close_date: day = normalize_date(self.get_datetime()) if day > min(asset.end_date, asset.auto_close_date): # If we are after the asset's end date or auto close date, warn # the user that they can't place an order for this asset, and # return None. log.warn("Cannot place order for {0}" ", as it is not tradable.".format(asset.symbol)) return False return True
def _is_stale_for_asset(self, asset, dt, adjusted_dt, data_portal): session_label = normalize_date(dt) # FIXME if not asset.is_alive_for_session(session_label): return False current_volume = data_portal.get_spot_value(asset, "volume", adjusted_dt, self.data_frequency) if current_volume > 0: # found a current value, so we know this asset is not stale. return False else: # we need to distinguish between if this asset has ever traded # (stale = True) or has never traded (stale = False) last_traded_dt = \ data_portal.get_spot_value(asset, "last_traded", adjusted_dt, self.data_frequency) return not (last_traded_dt is pd.NaT)
def get_equities(self): assets = [] t = normalize_date(pd.Timestamp('now', tz='America/New_York')) raw_assets = self._api.list_assets(asset_class='us_equity') for raw_asset in raw_assets: asset = Equity( raw_asset.id, raw_asset.exchange, symbol=raw_asset.symbol, asset_name=raw_asset.symbol, ) asset.start_date = t - one_day_offset if raw_asset.status == 'active' and raw_asset.tradable: asset.end_date = t + end_offset else: # if asset is not tradable, set end_date = day before asset.end_date = t - one_day_offset asset.auto_close_date = asset.end_date assets.append(asset) return assets