text
stringlengths
81
112k
Set the date for which symbols will be resolved to their assets (symbols may map to different firms or underlying assets at different times) Parameters ---------- dt : datetime The new symbol lookup date. def set_symbol_lookup_date(self, dt): """Set the date for which symbols will be resolved to their assets (symbols may map to different firms or underlying assets at different times) Parameters ---------- dt : datetime The new symbol lookup date. """ try: self._symbol_lookup_date = pd.Timestamp(dt, tz='UTC') except ValueError: raise UnsupportedDatetimeFormat(input=dt, method='set_symbol_lookup_date')
Place an order in the specified asset corresponding to the given percent of the current portfolio value. Parameters ---------- asset : Asset The asset that this order is for. percent : float The percentage of the portfolio value to allocate to ``asset``. This is specified as a decimal, for example: 0.50 means 50%. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_value` def order_percent(self, asset, percent, limit_price=None, stop_price=None, style=None): """Place an order in the specified asset corresponding to the given percent of the current portfolio value. Parameters ---------- asset : Asset The asset that this order is for. percent : float The percentage of the portfolio value to allocate to ``asset``. This is specified as a decimal, for example: 0.50 means 50%. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_value` """ if not self._can_order_asset(asset): return None amount = self._calculate_order_percent_amount(asset, percent) return self.order(asset, amount, limit_price=limit_price, stop_price=stop_price, style=style)
Place an order to adjust a position to a target number of shares. If the position doesn't already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target number of shares and the current number of shares. Parameters ---------- asset : Asset The asset that this order is for. target : int The desired number of shares of ``asset``. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- ``order_target`` does not take into account any open orders. For example: .. code-block:: python order_target(sid(0), 10) order_target(sid(0), 10) This code will result in 20 shares of ``sid(0)`` because the first call to ``order_target`` will not have been filled when the second ``order_target`` call is made. See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_target_percent` :func:`zipline.api.order_target_value` def order_target(self, asset, target, limit_price=None, stop_price=None, style=None): """Place an order to adjust a position to a target number of shares. If the position doesn't already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target number of shares and the current number of shares. Parameters ---------- asset : Asset The asset that this order is for. target : int The desired number of shares of ``asset``. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- ``order_target`` does not take into account any open orders. For example: .. code-block:: python order_target(sid(0), 10) order_target(sid(0), 10) This code will result in 20 shares of ``sid(0)`` because the first call to ``order_target`` will not have been filled when the second ``order_target`` call is made. See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_target_percent` :func:`zipline.api.order_target_value` """ if not self._can_order_asset(asset): return None amount = self._calculate_order_target_amount(asset, target) return self.order(asset, amount, limit_price=limit_price, stop_price=stop_price, style=style)
Place an order to adjust a position to a target value. If the position doesn't already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target value and the current value. If the Asset being ordered is a Future, the 'target value' calculated is actually the target exposure, as Futures have no 'value'. Parameters ---------- asset : Asset The asset that this order is for. target : float The desired total value of ``asset``. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- ``order_target_value`` does not take into account any open orders. For example: .. code-block:: python order_target_value(sid(0), 10) order_target_value(sid(0), 10) This code will result in 20 dollars of ``sid(0)`` because the first call to ``order_target_value`` will not have been filled when the second ``order_target_value`` call is made. See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_target` :func:`zipline.api.order_target_percent` def order_target_value(self, asset, target, limit_price=None, stop_price=None, style=None): """Place an order to adjust a position to a target value. If the position doesn't already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target value and the current value. If the Asset being ordered is a Future, the 'target value' calculated is actually the target exposure, as Futures have no 'value'. Parameters ---------- asset : Asset The asset that this order is for. target : float The desired total value of ``asset``. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- ``order_target_value`` does not take into account any open orders. For example: .. code-block:: python order_target_value(sid(0), 10) order_target_value(sid(0), 10) This code will result in 20 dollars of ``sid(0)`` because the first call to ``order_target_value`` will not have been filled when the second ``order_target_value`` call is made. See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_target` :func:`zipline.api.order_target_percent` """ if not self._can_order_asset(asset): return None target_amount = self._calculate_order_value_amount(asset, target) amount = self._calculate_order_target_amount(asset, target_amount) return self.order(asset, amount, limit_price=limit_price, stop_price=stop_price, style=style)
Place an order to adjust a position to a target percent of the current portfolio value. If the position doesn't already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target percent and the current percent. Parameters ---------- asset : Asset The asset that this order is for. target : float The desired percentage of the portfolio value to allocate to ``asset``. This is specified as a decimal, for example: 0.50 means 50%. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- ``order_target_value`` does not take into account any open orders. For example: .. code-block:: python order_target_percent(sid(0), 10) order_target_percent(sid(0), 10) This code will result in 20% of the portfolio being allocated to sid(0) because the first call to ``order_target_percent`` will not have been filled when the second ``order_target_percent`` call is made. See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_target` :func:`zipline.api.order_target_value` def order_target_percent(self, asset, target, limit_price=None, stop_price=None, style=None): """Place an order to adjust a position to a target percent of the current portfolio value. If the position doesn't already exist, this is equivalent to placing a new order. If the position does exist, this is equivalent to placing an order for the difference between the target percent and the current percent. Parameters ---------- asset : Asset The asset that this order is for. target : float The desired percentage of the portfolio value to allocate to ``asset``. This is specified as a decimal, for example: 0.50 means 50%. limit_price : float, optional The limit price for the order. stop_price : float, optional The stop price for the order. style : ExecutionStyle The execution style for the order. Returns ------- order_id : str The unique identifier for this order. Notes ----- ``order_target_value`` does not take into account any open orders. For example: .. code-block:: python order_target_percent(sid(0), 10) order_target_percent(sid(0), 10) This code will result in 20% of the portfolio being allocated to sid(0) because the first call to ``order_target_percent`` will not have been filled when the second ``order_target_percent`` call is made. See :func:`zipline.api.order` for more information about ``limit_price``, ``stop_price``, and ``style`` See Also -------- :class:`zipline.finance.execution.ExecutionStyle` :func:`zipline.api.order` :func:`zipline.api.order_target` :func:`zipline.api.order_target_value` """ if not self._can_order_asset(asset): return None amount = self._calculate_order_target_percent_amount(asset, target) return self.order(asset, amount, limit_price=limit_price, stop_price=stop_price, style=style)
Place a batch market order for multiple assets. Parameters ---------- share_counts : pd.Series[Asset -> int] Map from asset to number of shares to order for that asset. Returns ------- order_ids : pd.Index[str] Index of ids for newly-created orders. def batch_market_order(self, share_counts): """Place a batch market order for multiple assets. Parameters ---------- share_counts : pd.Series[Asset -> int] Map from asset to number of shares to order for that asset. Returns ------- order_ids : pd.Index[str] Index of ids for newly-created orders. """ style = MarketOrder() order_args = [ (asset, amount, style) for (asset, amount) in iteritems(share_counts) if amount ] return self.blotter.batch_order(order_args)
Retrieve all of the current open orders. Parameters ---------- asset : Asset If passed and not None, return only the open orders for the given asset instead of all open orders. Returns ------- open_orders : dict[list[Order]] or list[Order] If no asset is passed this will return a dict mapping Assets to a list containing all the open orders for the asset. If an asset is passed then this will return a list of the open orders for this asset. def get_open_orders(self, asset=None): """Retrieve all of the current open orders. Parameters ---------- asset : Asset If passed and not None, return only the open orders for the given asset instead of all open orders. Returns ------- open_orders : dict[list[Order]] or list[Order] If no asset is passed this will return a dict mapping Assets to a list containing all the open orders for the asset. If an asset is passed then this will return a list of the open orders for this asset. """ if asset is None: return { key: [order.to_api_obj() for order in orders] for key, orders in iteritems(self.blotter.open_orders) if orders } if asset in self.blotter.open_orders: orders = self.blotter.open_orders[asset] return [order.to_api_obj() for order in orders] return []
Lookup an order based on the order id returned from one of the order functions. Parameters ---------- order_id : str The unique identifier for the order. Returns ------- order : Order The order object. def get_order(self, order_id): """Lookup an order based on the order id returned from one of the order functions. Parameters ---------- order_id : str The unique identifier for the order. Returns ------- order : Order The order object. """ if order_id in self.blotter.orders: return self.blotter.orders[order_id].to_api_obj()
Cancel an open order. Parameters ---------- order_param : str or Order The order_id or order object to cancel. def cancel_order(self, order_param): """Cancel an open order. Parameters ---------- order_param : str or Order The order_id or order object to cancel. """ order_id = order_param if isinstance(order_param, zipline.protocol.Order): order_id = order_param.id self.blotter.cancel(order_id)
DEPRECATED: use ``data.history`` instead. def history(self, bar_count, frequency, field, ffill=True): """DEPRECATED: use ``data.history`` instead. """ warnings.warn( "The `history` method is deprecated. Use `data.history` instead.", category=ZiplineDeprecationWarning, stacklevel=4 ) return self.get_history_window( bar_count, frequency, self._calculate_universe(), field, ffill )
Register a new AccountControl to be checked on each bar. def register_account_control(self, control): """ Register a new AccountControl to be checked on each bar. """ if self.initialized: raise RegisterAccountControlPostInit() self.account_controls.append(control)
Set a limit on the minimum leverage of the algorithm. Parameters ---------- min_leverage : float The minimum leverage for the algorithm. grace_period : pd.Timedelta The offset from the start date used to enforce a minimum leverage. def set_min_leverage(self, min_leverage, grace_period): """Set a limit on the minimum leverage of the algorithm. Parameters ---------- min_leverage : float The minimum leverage for the algorithm. grace_period : pd.Timedelta The offset from the start date used to enforce a minimum leverage. """ deadline = self.sim_params.start_session + grace_period control = MinLeverage(min_leverage, deadline) self.register_account_control(control)
Register a new TradingControl to be checked prior to order calls. def register_trading_control(self, control): """ Register a new TradingControl to be checked prior to order calls. """ if self.initialized: raise RegisterTradingControlPostInit() self.trading_controls.append(control)
Set a limit on the number of shares and/or dollar value held for the given sid. Limits are treated as absolute values and are enforced at the time that the algo attempts to place an order for sid. This means that it's possible to end up with more than the max number of shares due to splits/dividends, and more than the max notional due to price improvement. If an algorithm attempts to place an order that would result in increasing the absolute value of shares/dollar value exceeding one of these limits, raise a TradingControlException. Parameters ---------- asset : Asset, optional If provided, this sets the guard only on positions in the given asset. max_shares : int, optional The maximum number of shares to hold for an asset. max_notional : float, optional The maximum value to hold for an asset. def set_max_position_size(self, asset=None, max_shares=None, max_notional=None, on_error='fail'): """Set a limit on the number of shares and/or dollar value held for the given sid. Limits are treated as absolute values and are enforced at the time that the algo attempts to place an order for sid. This means that it's possible to end up with more than the max number of shares due to splits/dividends, and more than the max notional due to price improvement. If an algorithm attempts to place an order that would result in increasing the absolute value of shares/dollar value exceeding one of these limits, raise a TradingControlException. Parameters ---------- asset : Asset, optional If provided, this sets the guard only on positions in the given asset. max_shares : int, optional The maximum number of shares to hold for an asset. max_notional : float, optional The maximum value to hold for an asset. """ control = MaxPositionSize(asset=asset, max_shares=max_shares, max_notional=max_notional, on_error=on_error) self.register_trading_control(control)
Set a limit on the number of shares and/or dollar value of any single order placed for sid. Limits are treated as absolute values and are enforced at the time that the algo attempts to place an order for sid. If an algorithm attempts to place an order that would result in exceeding one of these limits, raise a TradingControlException. Parameters ---------- asset : Asset, optional If provided, this sets the guard only on positions in the given asset. max_shares : int, optional The maximum number of shares that can be ordered at one time. max_notional : float, optional The maximum value that can be ordered at one time. def set_max_order_size(self, asset=None, max_shares=None, max_notional=None, on_error='fail'): """Set a limit on the number of shares and/or dollar value of any single order placed for sid. Limits are treated as absolute values and are enforced at the time that the algo attempts to place an order for sid. If an algorithm attempts to place an order that would result in exceeding one of these limits, raise a TradingControlException. Parameters ---------- asset : Asset, optional If provided, this sets the guard only on positions in the given asset. max_shares : int, optional The maximum number of shares that can be ordered at one time. max_notional : float, optional The maximum value that can be ordered at one time. """ control = MaxOrderSize(asset=asset, max_shares=max_shares, max_notional=max_notional, on_error=on_error) self.register_trading_control(control)
Set a limit on the number of orders that can be placed in a single day. Parameters ---------- max_count : int The maximum number of orders that can be placed on any single day. def set_max_order_count(self, max_count, on_error='fail'): """Set a limit on the number of orders that can be placed in a single day. Parameters ---------- max_count : int The maximum number of orders that can be placed on any single day. """ control = MaxOrderCount(on_error, max_count) self.register_trading_control(control)
Set a restriction on which assets can be ordered. Parameters ---------- restricted_list : Restrictions An object providing information about restricted assets. See Also -------- zipline.finance.asset_restrictions.Restrictions def set_asset_restrictions(self, restrictions, on_error='fail'): """Set a restriction on which assets can be ordered. Parameters ---------- restricted_list : Restrictions An object providing information about restricted assets. See Also -------- zipline.finance.asset_restrictions.Restrictions """ control = RestrictedListOrder(on_error, restrictions) self.register_trading_control(control) self.restrictions |= restrictions
Register a pipeline to be computed at the start of each day. Parameters ---------- pipeline : Pipeline The pipeline to have computed. name : str The name of the pipeline. chunks : int or iterator, optional The number of days to compute pipeline results for. Increasing this number will make it longer to get the first results but may improve the total runtime of the simulation. If an iterator is passed, we will run in chunks based on values of the iterator. Default is True. eager : bool, optional Whether or not to compute this pipeline prior to before_trading_start. Returns ------- pipeline : Pipeline Returns the pipeline that was attached unchanged. See Also -------- :func:`zipline.api.pipeline_output` def attach_pipeline(self, pipeline, name, chunks=None, eager=True): """Register a pipeline to be computed at the start of each day. Parameters ---------- pipeline : Pipeline The pipeline to have computed. name : str The name of the pipeline. chunks : int or iterator, optional The number of days to compute pipeline results for. Increasing this number will make it longer to get the first results but may improve the total runtime of the simulation. If an iterator is passed, we will run in chunks based on values of the iterator. Default is True. eager : bool, optional Whether or not to compute this pipeline prior to before_trading_start. Returns ------- pipeline : Pipeline Returns the pipeline that was attached unchanged. See Also -------- :func:`zipline.api.pipeline_output` """ if chunks is None: # Make the first chunk smaller to get more immediate results: # (one week, then every half year) chunks = chain([5], repeat(126)) elif isinstance(chunks, int): chunks = repeat(chunks) if name in self._pipelines: raise DuplicatePipelineName(name=name) self._pipelines[name] = AttachedPipeline(pipeline, iter(chunks), eager) # Return the pipeline to allow expressions like # p = attach_pipeline(Pipeline(), 'name') return pipeline
Get the results of the pipeline that was attached with the name: ``name``. Parameters ---------- name : str Name of the pipeline for which results are requested. Returns ------- results : pd.DataFrame DataFrame containing the results of the requested pipeline for the current simulation date. Raises ------ NoSuchPipeline Raised when no pipeline with the name `name` has been registered. See Also -------- :func:`zipline.api.attach_pipeline` :meth:`zipline.pipeline.engine.PipelineEngine.run_pipeline` def pipeline_output(self, name): """Get the results of the pipeline that was attached with the name: ``name``. Parameters ---------- name : str Name of the pipeline for which results are requested. Returns ------- results : pd.DataFrame DataFrame containing the results of the requested pipeline for the current simulation date. Raises ------ NoSuchPipeline Raised when no pipeline with the name `name` has been registered. See Also -------- :func:`zipline.api.attach_pipeline` :meth:`zipline.pipeline.engine.PipelineEngine.run_pipeline` """ try: pipe, chunks, _ = self._pipelines[name] except KeyError: raise NoSuchPipeline( name=name, valid=list(self._pipelines.keys()), ) return self._pipeline_output(pipe, chunks, name)
Internal implementation of `pipeline_output`. def _pipeline_output(self, pipeline, chunks, name): """ Internal implementation of `pipeline_output`. """ today = normalize_date(self.get_datetime()) try: data = self._pipeline_cache.get(name, today) except KeyError: # Calculate the next block. data, valid_until = self.run_pipeline( pipeline, today, next(chunks), ) self._pipeline_cache.set(name, data, valid_until) # Now that we have a cached result, try to return the data for today. try: return data.loc[today] except KeyError: # This happens if no assets passed the pipeline screen on a given # day. return pd.DataFrame(index=[], columns=data.columns)
Compute `pipeline`, providing values for at least `start_date`. Produces a DataFrame containing data for days between `start_date` and `end_date`, where `end_date` is defined by: `end_date = min(start_date + chunksize trading days, simulation_end)` Returns ------- (data, valid_until) : tuple (pd.DataFrame, pd.Timestamp) See Also -------- PipelineEngine.run_pipeline def run_pipeline(self, pipeline, start_session, chunksize): """ Compute `pipeline`, providing values for at least `start_date`. Produces a DataFrame containing data for days between `start_date` and `end_date`, where `end_date` is defined by: `end_date = min(start_date + chunksize trading days, simulation_end)` Returns ------- (data, valid_until) : tuple (pd.DataFrame, pd.Timestamp) See Also -------- PipelineEngine.run_pipeline """ sessions = self.trading_calendar.all_sessions # Load data starting from the previous trading day... start_date_loc = sessions.get_loc(start_session) # ...continuing until either the day before the simulation end, or # until chunksize days of data have been loaded. sim_end_session = self.sim_params.end_session end_loc = min( start_date_loc + chunksize, sessions.get_loc(sim_end_session) ) end_session = sessions[end_loc] return \ self.engine.run_pipeline(pipeline, start_session, end_session), \ end_session
Return a list of all the TradingAlgorithm API methods. def all_api_methods(cls): """ Return a list of all the TradingAlgorithm API methods. """ return [ fn for fn in itervalues(vars(cls)) if getattr(fn, 'is_api_method', False) ]
Format a bulleted list of values. def bulleted_list(items, max_count=None, indent=2): """Format a bulleted list of values. """ if max_count is not None and len(items) > max_count: item_list = list(items) items = item_list[:max_count - 1] items.append('...') items.append(item_list[-1]) line_template = (" " * indent) + "- {}" return "\n".join(map(line_template.format, items))
Checks for the presence of an extra to the argument list. Raises expections if this is unexpected or if it is missing and expected. def _expect_extra(expected, present, exc_unexpected, exc_missing, exc_args): """ Checks for the presence of an extra to the argument list. Raises expections if this is unexpected or if it is missing and expected. """ if present: if not expected: raise exc_unexpected(*exc_args) elif expected and expected is not Argument.ignore: raise exc_missing(*exc_args)
Checks the callable_ to make sure that it satisfies the given expectations. expected_args should be an iterable of Arguments in the order you expect to receive them. expect_starargs means that the function should or should not take a *args param. expect_kwargs says the callable should or should not take **kwargs param. If expected_args, expect_starargs, or expect_kwargs is Argument.ignore, then the checks related to that argument will not occur. Example usage: callable_check( f, [Argument('a'), Argument('b', 1)], expect_starargs=True, expect_kwargs=Argument.ignore ) def verify_callable_argspec(callable_, expected_args=Argument.ignore, expect_starargs=Argument.ignore, expect_kwargs=Argument.ignore): """ Checks the callable_ to make sure that it satisfies the given expectations. expected_args should be an iterable of Arguments in the order you expect to receive them. expect_starargs means that the function should or should not take a *args param. expect_kwargs says the callable should or should not take **kwargs param. If expected_args, expect_starargs, or expect_kwargs is Argument.ignore, then the checks related to that argument will not occur. Example usage: callable_check( f, [Argument('a'), Argument('b', 1)], expect_starargs=True, expect_kwargs=Argument.ignore ) """ if not callable(callable_): raise NotCallable(callable_) expected_arg_list = list( expected_args if expected_args is not Argument.ignore else [] ) args, starargs, kwargs = Argument.parse_argspec(callable_) exc_args = callable_, args, starargs, kwargs # Check the *args. _expect_extra( expect_starargs, starargs, UnexpectedStarargs, NoStarargs, exc_args, ) # Check the **kwargs. _expect_extra( expect_kwargs, kwargs, UnexpectedKwargs, NoKwargs, exc_args, ) if expected_args is Argument.ignore: # Ignore the argument list checks. return if len(args) < len(expected_arg_list): # One or more argument that we expected was not present. raise NotEnoughArguments( callable_, args, starargs, kwargs, [arg for arg in expected_arg_list if arg not in args], ) elif len(args) > len(expected_arg_list): raise TooManyArguments( callable_, args, starargs, kwargs ) # Empty argument that will not match with any actual arguments. missing_arg = Argument(object(), object()) for expected, provided in zip_longest(expected_arg_list, args, fillvalue=missing_arg): if not expected.matches(provided): raise MismatchedArguments( callable_, args, starargs, kwargs )
Takes a callable and returns a tuple with the list of Argument objects, the name of *args, and the name of **kwargs. If *args or **kwargs is not present, it will be None. This returns a namedtuple called Argspec that has three fields named: args, starargs, and kwargs. def parse_argspec(callable_): """ Takes a callable and returns a tuple with the list of Argument objects, the name of *args, and the name of **kwargs. If *args or **kwargs is not present, it will be None. This returns a namedtuple called Argspec that has three fields named: args, starargs, and kwargs. """ args, varargs, keywords, defaults = getargspec(callable_) defaults = list(defaults or []) if getattr(callable_, '__self__', None) is not None: # This is a bound method, drop the self param. args = args[1:] first_default = len(args) - len(defaults) return Argspec( [Argument(arg, Argument.no_default if n < first_default else defaults[n - first_default]) for n, arg in enumerate(args)], varargs, keywords, )
An asset is restricted for all dts if it is in the static list. def is_restricted(self, assets, dt): """ An asset is restricted for all dts if it is in the static list. """ if isinstance(assets, Asset): return assets in self._restricted_set return pd.Series( index=pd.Index(assets), data=vectorized_is_element(assets, self._restricted_set) )
Returns whether or not an asset or iterable of assets is restricted on a dt. def is_restricted(self, assets, dt): """ Returns whether or not an asset or iterable of assets is restricted on a dt. """ if isinstance(assets, Asset): return self._is_restricted_for_asset(assets, dt) is_restricted = partial(self._is_restricted_for_asset, dt=dt) return pd.Series( index=pd.Index(assets), data=vectorize(is_restricted, otypes=[bool])(assets) )
Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list A list of splits. Each split is a tuple of (asset, ratio). Returns ------- int: The leftover cash from fractional shares after modifying each position. def handle_splits(self, splits): """Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list A list of splits. Each split is a tuple of (asset, ratio). Returns ------- int: The leftover cash from fractional shares after modifying each position. """ total_leftover_cash = 0 for asset, ratio in splits: if asset in self.positions: self._dirty_stats = True # Make the position object handle the split. It returns the # leftover cash from a fractional share, if there is any. position = self.positions[asset] leftover_cash = position.handle_split(asset, ratio) total_leftover_cash += leftover_cash return total_leftover_cash
Given a list of dividends whose ex_dates are all the next trading day, calculate and store the cash and/or stock payments to be paid on each dividend's pay date. Parameters ---------- cash_dividends : iterable of (asset, amount, pay_date) namedtuples stock_dividends: iterable of (asset, payment_asset, ratio, pay_date) namedtuples. def earn_dividends(self, cash_dividends, stock_dividends): """Given a list of dividends whose ex_dates are all the next trading day, calculate and store the cash and/or stock payments to be paid on each dividend's pay date. Parameters ---------- cash_dividends : iterable of (asset, amount, pay_date) namedtuples stock_dividends: iterable of (asset, payment_asset, ratio, pay_date) namedtuples. """ for cash_dividend in cash_dividends: self._dirty_stats = True # only mark dirty if we pay a dividend # Store the earned dividends so that they can be paid on the # dividends' pay_dates. div_owed = self.positions[cash_dividend.asset].earn_dividend( cash_dividend, ) try: self._unpaid_dividends[cash_dividend.pay_date].append(div_owed) except KeyError: self._unpaid_dividends[cash_dividend.pay_date] = [div_owed] for stock_dividend in stock_dividends: self._dirty_stats = True # only mark dirty if we pay a dividend div_owed = self.positions[ stock_dividend.asset ].earn_stock_dividend(stock_dividend) try: self._unpaid_stock_dividends[stock_dividend.pay_date].append( div_owed, ) except KeyError: self._unpaid_stock_dividends[stock_dividend.pay_date] = [ div_owed, ]
Returns a cash payment based on the dividends that should be paid out according to the accumulated bookkeeping of earned, unpaid, and stock dividends. def pay_dividends(self, next_trading_day): """ Returns a cash payment based on the dividends that should be paid out according to the accumulated bookkeeping of earned, unpaid, and stock dividends. """ net_cash_payment = 0.0 try: payments = self._unpaid_dividends[next_trading_day] # Mark these dividends as paid by dropping them from our unpaid del self._unpaid_dividends[next_trading_day] except KeyError: payments = [] # representing the fact that we're required to reimburse the owner of # the stock for any dividends paid while borrowing. for payment in payments: net_cash_payment += payment['amount'] # Add stock for any stock dividends paid. Again, the values here may # be negative in the case of short positions. try: stock_payments = self._unpaid_stock_dividends[next_trading_day] except KeyError: stock_payments = [] for stock_payment in stock_payments: payment_asset = stock_payment['payment_asset'] share_count = stock_payment['share_count'] # note we create a Position for stock dividend if we don't # already own the asset if payment_asset in self.positions: position = self.positions[payment_asset] else: position = self.positions[payment_asset] = Position( payment_asset, ) position.amount += share_count return net_cash_payment
The current status of the positions. Returns ------- stats : PositionStats The current stats position stats. Notes ----- This is cached, repeated access will not recompute the stats until the stats may have changed. def stats(self): """The current status of the positions. Returns ------- stats : PositionStats The current stats position stats. Notes ----- This is cached, repeated access will not recompute the stats until the stats may have changed. """ if self._dirty_stats: calculate_position_tracker_stats(self.positions, self._stats) self._dirty_stats = False return self._stats
Add a transaction to ledger, updating the current state as needed. Parameters ---------- transaction : zp.Transaction The transaction to execute. def process_transaction(self, transaction): """Add a transaction to ledger, updating the current state as needed. Parameters ---------- transaction : zp.Transaction The transaction to execute. """ asset = transaction.asset if isinstance(asset, Future): try: old_price = self._payout_last_sale_prices[asset] except KeyError: self._payout_last_sale_prices[asset] = transaction.price else: position = self.position_tracker.positions[asset] amount = position.amount price = transaction.price self._cash_flow( self._calculate_payout( asset.price_multiplier, amount, old_price, price, ), ) if amount + transaction.amount == 0: del self._payout_last_sale_prices[asset] else: self._payout_last_sale_prices[asset] = price else: self._cash_flow(-(transaction.price * transaction.amount)) self.position_tracker.execute_transaction(transaction) # we only ever want the dict form from now on transaction_dict = transaction.to_dict() try: self._processed_transactions[transaction.dt].append( transaction_dict, ) except KeyError: self._processed_transactions[transaction.dt] = [transaction_dict]
Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list[(Asset, float)] A list of splits. Each split is a tuple of (asset, ratio). def process_splits(self, splits): """Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list[(Asset, float)] A list of splits. Each split is a tuple of (asset, ratio). """ leftover_cash = self.position_tracker.handle_splits(splits) if leftover_cash > 0: self._cash_flow(leftover_cash)
Keep track of an order that was placed. Parameters ---------- order : zp.Order The order to record. def process_order(self, order): """Keep track of an order that was placed. Parameters ---------- order : zp.Order The order to record. """ try: dt_orders = self._orders_by_modified[order.dt] except KeyError: self._orders_by_modified[order.dt] = OrderedDict([ (order.id, order), ]) self._orders_by_id[order.id] = order else: self._orders_by_id[order.id] = dt_orders[order.id] = order # to preserve the order of the orders by modified date move_to_end(dt_orders, order.id, last=True) move_to_end(self._orders_by_id, order.id, last=True)
Process the commission. Parameters ---------- commission : zp.Event The commission being paid. def process_commission(self, commission): """Process the commission. Parameters ---------- commission : zp.Event The commission being paid. """ asset = commission['asset'] cost = commission['cost'] self.position_tracker.handle_commission(asset, cost) self._cash_flow(-cost)
Process dividends for the next session. This will earn us any dividends whose ex-date is the next session as well as paying out any dividends whose pay-date is the next session def process_dividends(self, next_session, asset_finder, adjustment_reader): """Process dividends for the next session. This will earn us any dividends whose ex-date is the next session as well as paying out any dividends whose pay-date is the next session """ position_tracker = self.position_tracker # Earn dividends whose ex_date is the next trading day. We need to # check if we own any of these stocks so we know to pay them out when # the pay date comes. held_sids = set(position_tracker.positions) if held_sids: cash_dividends = adjustment_reader.get_dividends_with_ex_date( held_sids, next_session, asset_finder ) stock_dividends = ( adjustment_reader.get_stock_dividends_with_ex_date( held_sids, next_session, asset_finder ) ) # Earning a dividend just marks that we need to get paid out on # the dividend's pay-date. This does not affect our cash yet. position_tracker.earn_dividends( cash_dividends, stock_dividends, ) # Pay out the dividends whose pay-date is the next session. This does # affect out cash. self._cash_flow( position_tracker.pay_dividends( next_session, ), )
Retrieve the dict-form of all of the transactions in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up transactions for. If not passed, or None is explicitly passed, all of the transactions will be returned. Returns ------- transactions : list[dict] The transaction information. def transactions(self, dt=None): """Retrieve the dict-form of all of the transactions in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up transactions for. If not passed, or None is explicitly passed, all of the transactions will be returned. Returns ------- transactions : list[dict] The transaction information. """ if dt is None: # flatten the by-day transactions return [ txn for by_day in itervalues(self._processed_transactions) for txn in by_day ] return self._processed_transactions.get(dt, [])
Retrieve the dict-form of all of the orders in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up order for. If not passed, or None is explicitly passed, all of the orders will be returned. Returns ------- orders : list[dict] The order information. def orders(self, dt=None): """Retrieve the dict-form of all of the orders in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up order for. If not passed, or None is explicitly passed, all of the orders will be returned. Returns ------- orders : list[dict] The order information. """ if dt is None: # orders by id is already flattened return [o.to_dict() for o in itervalues(self._orders_by_id)] return [ o.to_dict() for o in itervalues(self._orders_by_modified.get(dt, {})) ]
Force a computation of the current portfolio state. def update_portfolio(self): """Force a computation of the current portfolio state. """ if not self._dirty_portfolio: return portfolio = self._portfolio pt = self.position_tracker portfolio.positions = pt.get_positions() position_stats = pt.stats portfolio.positions_value = position_value = ( position_stats.net_value ) portfolio.positions_exposure = position_stats.net_exposure self._cash_flow(self._get_payout_total(pt.positions)) start_value = portfolio.portfolio_value # update the new starting value portfolio.portfolio_value = end_value = portfolio.cash + position_value pnl = end_value - start_value if start_value != 0: returns = pnl / start_value else: returns = 0.0 portfolio.pnl += pnl portfolio.returns = ( (1 + portfolio.returns) * (1 + returns) - 1 ) # the portfolio has been fully synced self._dirty_portfolio = False
Override fields on ``self.account``. def override_account_fields(self, settled_cash=not_overridden, accrued_interest=not_overridden, buying_power=not_overridden, equity_with_loan=not_overridden, total_positions_value=not_overridden, total_positions_exposure=not_overridden, regt_equity=not_overridden, regt_margin=not_overridden, initial_margin_requirement=not_overridden, maintenance_margin_requirement=not_overridden, available_funds=not_overridden, excess_liquidity=not_overridden, cushion=not_overridden, day_trades_remaining=not_overridden, leverage=not_overridden, net_leverage=not_overridden, net_liquidation=not_overridden): """Override fields on ``self.account``. """ # mark that the portfolio is dirty to override the fields again self._dirty_account = True self._account_overrides = kwargs = { k: v for k, v in locals().items() if v is not not_overridden } del kwargs['self']
Given a datashape type, return the associated numpy type. Maps datashape's DateTime type to numpy's `datetime64[ns]` dtype, since the numpy datetime returned by datashape isn't supported by pipeline. Parameters ---------- type_: datashape.coretypes.Type The datashape type. Returns ------- type_ np.dtype The numpy dtype. def datashape_type_to_numpy(type_): """ Given a datashape type, return the associated numpy type. Maps datashape's DateTime type to numpy's `datetime64[ns]` dtype, since the numpy datetime returned by datashape isn't supported by pipeline. Parameters ---------- type_: datashape.coretypes.Type The datashape type. Returns ------- type_ np.dtype The numpy dtype. """ if isinstance(type_, Option): type_ = type_.ty if isinstance(type_, DateTime): return np.dtype('datetime64[ns]') if isinstance(type_, String): return np.dtype(object) if type_ in integral: return np.dtype('int64') else: return type_.to_numpy_dtype()
Creates or returns a dataset from a blaze expression. Parameters ---------- expr : Expr The blaze expression representing the values. missing_values : frozenset((name, value) pairs Association pairs column name and missing_value for that column. This needs to be a frozenset rather than a dict or tuple of tuples because we want a collection that's unordered but still hashable. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. Returns ------- ds : type A new dataset type. Notes ----- This function is memoized. repeated calls with the same inputs will return the same type. def new_dataset(expr, missing_values, domain): """ Creates or returns a dataset from a blaze expression. Parameters ---------- expr : Expr The blaze expression representing the values. missing_values : frozenset((name, value) pairs Association pairs column name and missing_value for that column. This needs to be a frozenset rather than a dict or tuple of tuples because we want a collection that's unordered but still hashable. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. Returns ------- ds : type A new dataset type. Notes ----- This function is memoized. repeated calls with the same inputs will return the same type. """ missing_values = dict(missing_values) class_dict = {'ndim': 2 if SID_FIELD_NAME in expr.fields else 1} for name, type_ in expr.dshape.measure.fields: # Don't generate a column for sid or timestamp, since they're # implicitly the labels if the arrays that will be passed to pipeline # Terms. if name in (SID_FIELD_NAME, TS_FIELD_NAME): continue type_ = datashape_type_to_numpy(type_) if can_represent_dtype(type_): col = Column( type_, missing_values.get(name, NotSpecified), ) else: col = NonPipelineField(name, type_) class_dict[name] = col if 'domain' in class_dict: raise ValueError("Got a column named 'domain' in new_dataset(). " "'domain' is reserved.") class_dict['domain'] = domain name = expr._name if name is None: name = next(_new_names) # unicode is a name error in py3 but the branch is only hit # when we are in python 2. if PY2 and isinstance(name, unicode): # pragma: no cover # noqa name = name.encode('utf-8') return type(name, (DataSet,), class_dict)
Validate that the expression and resources passed match up. Parameters ---------- name : str The name of the argument we are checking. expr : Expr The potentially bound expr. resources The explicitly passed resources to compute expr. Raises ------ ValueError If the resources do not match for an expression. def _check_resources(name, expr, resources): """Validate that the expression and resources passed match up. Parameters ---------- name : str The name of the argument we are checking. expr : Expr The potentially bound expr. resources The explicitly passed resources to compute expr. Raises ------ ValueError If the resources do not match for an expression. """ if expr is None: return bound = expr._resources() if not bound and resources is None: raise ValueError('no resources provided to compute %s' % name) if bound and resources: raise ValueError( 'explicit and implicit resources provided to compute %s' % name, )
Check that a field is a datetime inside some measure. Parameters ---------- name : str The name of the field to check. measure : Record The record to check the field of. Raises ------ TypeError If the field is not a datetime inside ``measure``. def _check_datetime_field(name, measure): """Check that a field is a datetime inside some measure. Parameters ---------- name : str The name of the field to check. measure : Record The record to check the field of. Raises ------ TypeError If the field is not a datetime inside ``measure``. """ if not isinstance(measure[name], (Date, DateTime)): raise TypeError( "'{name}' field must be a '{dt}', not: '{dshape}'".format( name=name, dt=DateTime(), dshape=measure[name], ), )
Find the correct metadata expression for the expression. Parameters ---------- field : {'deltas', 'checkpoints'} The kind of metadata expr to lookup. expr : Expr The baseline expression. metadata_expr : Expr, 'auto', or None The metadata argument. If this is 'auto', then the metadata table will be searched for by walking up the expression tree. If this cannot be reflected, then an action will be taken based on the ``no_metadata_rule``. no_metadata_rule : {'warn', 'raise', 'ignore'} How to handle the case where the metadata_expr='auto' but no expr could be found. Returns ------- metadata : Expr or None The deltas or metadata table to use. def _get_metadata(field, expr, metadata_expr, no_metadata_rule): """Find the correct metadata expression for the expression. Parameters ---------- field : {'deltas', 'checkpoints'} The kind of metadata expr to lookup. expr : Expr The baseline expression. metadata_expr : Expr, 'auto', or None The metadata argument. If this is 'auto', then the metadata table will be searched for by walking up the expression tree. If this cannot be reflected, then an action will be taken based on the ``no_metadata_rule``. no_metadata_rule : {'warn', 'raise', 'ignore'} How to handle the case where the metadata_expr='auto' but no expr could be found. Returns ------- metadata : Expr or None The deltas or metadata table to use. """ if isinstance(metadata_expr, bz.Expr) or metadata_expr is None: return metadata_expr try: return expr._child['_'.join(((expr._name or ''), field))] except (ValueError, AttributeError): if no_metadata_rule == 'raise': raise ValueError( "no %s table could be reflected for %s" % (field, expr) ) elif no_metadata_rule == 'warn': warnings.warn(NoMetaDataWarning(expr, field), stacklevel=4) return None
Verify that the baseline and deltas expressions have a timestamp field. If there is not a ``TS_FIELD_NAME`` on either of the expressions, it will be copied from the ``AD_FIELD_NAME``. If one is provided, then we will verify that it is the correct dshape. Parameters ---------- dataset_expr : Expr The baseline expression. deltas : Expr or None The deltas expression if any was provided. checkpoints : Expr or None The checkpoints expression if any was provided. Returns ------- dataset_expr, deltas : Expr The new baseline and deltas expressions to use. def _ensure_timestamp_field(dataset_expr, deltas, checkpoints): """Verify that the baseline and deltas expressions have a timestamp field. If there is not a ``TS_FIELD_NAME`` on either of the expressions, it will be copied from the ``AD_FIELD_NAME``. If one is provided, then we will verify that it is the correct dshape. Parameters ---------- dataset_expr : Expr The baseline expression. deltas : Expr or None The deltas expression if any was provided. checkpoints : Expr or None The checkpoints expression if any was provided. Returns ------- dataset_expr, deltas : Expr The new baseline and deltas expressions to use. """ measure = dataset_expr.dshape.measure if TS_FIELD_NAME not in measure.names: dataset_expr = bz.transform( dataset_expr, **{TS_FIELD_NAME: dataset_expr[AD_FIELD_NAME]} ) deltas = _ad_as_ts(deltas) checkpoints = _ad_as_ts(checkpoints) else: _check_datetime_field(TS_FIELD_NAME, measure) return dataset_expr, deltas, checkpoints
Create a Pipeline API object from a blaze expression. Parameters ---------- expr : Expr The blaze expression to use. deltas : Expr, 'auto' or None, optional The expression to use for the point in time adjustments. If the string 'auto' is passed, a deltas expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_deltas'. If None is passed, no deltas will be used. checkpoints : Expr, 'auto' or None, optional The expression to use for the forward fill checkpoints. If the string 'auto' is passed, a checkpoints expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_checkpoints'. If None is passed, no checkpoints will be used. loader : BlazeLoader, optional The blaze loader to attach this pipeline dataset to. If None is passed, the global blaze loader is used. resources : dict or any, optional The data to execute the blaze expressions against. This is used as the scope for ``bz.compute``. odo_kwargs : dict, optional The keyword arguments to pass to odo when evaluating the expressions. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. missing_values : dict[str -> any], optional A dict mapping column names to missing values for those columns. Missing values are required for integral columns. no_deltas_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``deltas='auto'`` but no deltas can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. no_checkpoints_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``checkpoints='auto'`` but no checkpoints can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. Returns ------- pipeline_api_obj : DataSet or BoundColumn Either a new dataset or bound column based on the shape of the expr passed in. If a table shaped expression is passed, this will return a ``DataSet`` that represents the whole table. If an array-like shape is passed, a ``BoundColumn`` on the dataset that would be constructed from passing the parent is returned. def from_blaze(expr, deltas='auto', checkpoints='auto', loader=None, resources=None, odo_kwargs=None, missing_values=None, domain=GENERIC, no_deltas_rule='warn', no_checkpoints_rule='warn'): """Create a Pipeline API object from a blaze expression. Parameters ---------- expr : Expr The blaze expression to use. deltas : Expr, 'auto' or None, optional The expression to use for the point in time adjustments. If the string 'auto' is passed, a deltas expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_deltas'. If None is passed, no deltas will be used. checkpoints : Expr, 'auto' or None, optional The expression to use for the forward fill checkpoints. If the string 'auto' is passed, a checkpoints expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_checkpoints'. If None is passed, no checkpoints will be used. loader : BlazeLoader, optional The blaze loader to attach this pipeline dataset to. If None is passed, the global blaze loader is used. resources : dict or any, optional The data to execute the blaze expressions against. This is used as the scope for ``bz.compute``. odo_kwargs : dict, optional The keyword arguments to pass to odo when evaluating the expressions. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. missing_values : dict[str -> any], optional A dict mapping column names to missing values for those columns. Missing values are required for integral columns. no_deltas_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``deltas='auto'`` but no deltas can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. no_checkpoints_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``checkpoints='auto'`` but no checkpoints can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. Returns ------- pipeline_api_obj : DataSet or BoundColumn Either a new dataset or bound column based on the shape of the expr passed in. If a table shaped expression is passed, this will return a ``DataSet`` that represents the whole table. If an array-like shape is passed, a ``BoundColumn`` on the dataset that would be constructed from passing the parent is returned. """ if 'auto' in {deltas, checkpoints}: invalid_nodes = tuple(filter(is_invalid_deltas_node, expr._subterms())) if invalid_nodes: raise TypeError( 'expression with auto %s may only contain (%s) nodes,' " found: %s" % ( ' or '.join( ['deltas'] if deltas is not None else [] + ['checkpoints'] if checkpoints is not None else [], ), ', '.join(map(get__name__, valid_deltas_node_types)), ', '.join( set(map(compose(get__name__, type), invalid_nodes)), ), ), ) deltas = _get_metadata( 'deltas', expr, deltas, no_deltas_rule, ) checkpoints = _get_metadata( 'checkpoints', expr, checkpoints, no_checkpoints_rule, ) # Check if this is a single column out of a dataset. if bz.ndim(expr) != 1: raise TypeError( 'expression was not tabular or array-like,' ' %s dimensions: %d' % ( 'too many' if bz.ndim(expr) > 1 else 'not enough', bz.ndim(expr), ), ) single_column = None if isscalar(expr.dshape.measure): # This is a single column. Record which column we are to return # but create the entire dataset. single_column = rename = expr._name field_hit = False if not isinstance(expr, traversable_nodes): raise TypeError( "expression '%s' was array-like but not a simple field of" " some larger table" % str(expr), ) while isinstance(expr, traversable_nodes): if isinstance(expr, bz.expr.Field): if not field_hit: field_hit = True else: break rename = expr._name expr = expr._child dataset_expr = expr.relabel({rename: single_column}) else: dataset_expr = expr measure = dataset_expr.dshape.measure if not isrecord(measure) or AD_FIELD_NAME not in measure.names: raise TypeError( "The dataset must be a collection of records with at least an" " '{ad}' field. Fields provided: '{fields}'\nhint: maybe you need" " to use `relabel` to change your field names".format( ad=AD_FIELD_NAME, fields=measure, ), ) _check_datetime_field(AD_FIELD_NAME, measure) dataset_expr, deltas, checkpoints = _ensure_timestamp_field( dataset_expr, deltas, checkpoints, ) if deltas is not None and (sorted(deltas.dshape.measure.fields) != sorted(measure.fields)): raise TypeError( 'baseline measure != deltas measure:\n%s != %s' % ( measure, deltas.dshape.measure, ), ) if (checkpoints is not None and (sorted(checkpoints.dshape.measure.fields) != sorted(measure.fields))): raise TypeError( 'baseline measure != checkpoints measure:\n%s != %s' % ( measure, checkpoints.dshape.measure, ), ) # Ensure that we have a data resource to execute the query against. _check_resources('expr', dataset_expr, resources) _check_resources('deltas', deltas, resources) _check_resources('checkpoints', checkpoints, resources) # Create or retrieve the Pipeline API dataset. if missing_values is None: missing_values = {} ds = new_dataset(dataset_expr, frozenset(missing_values.items()), domain) # Register our new dataset with the loader. (loader if loader is not None else global_loader).register_dataset( ds, bind_expression_to_resources(dataset_expr, resources), bind_expression_to_resources(deltas, resources) if deltas is not None else None, bind_expression_to_resources(checkpoints, resources) if checkpoints is not None else None, odo_kwargs=odo_kwargs, ) if single_column is not None: # We were passed a single column, extract and return it. return getattr(ds, single_column) return ds
Bind a Blaze expression to resources. Parameters ---------- expr : bz.Expr The expression to which we want to bind resources. resources : dict[bz.Symbol -> any] Mapping from the loadable terms of ``expr`` to actual data resources. Returns ------- bound_expr : bz.Expr ``expr`` with bound resources. def bind_expression_to_resources(expr, resources): """ Bind a Blaze expression to resources. Parameters ---------- expr : bz.Expr The expression to which we want to bind resources. resources : dict[bz.Symbol -> any] Mapping from the loadable terms of ``expr`` to actual data resources. Returns ------- bound_expr : bz.Expr ``expr`` with bound resources. """ # bind the resources into the expression if resources is None: resources = {} # _subs stands for substitute. It's not actually private, blaze just # prefixes symbol-manipulation methods with underscores to prevent # collisions with data column names. return expr._subs({ k: bz.data(v, dshape=k.dshape) for k, v in iteritems(resources) })
Computes a lower bound and a DataFrame checkpoints. Parameters ---------- checkpoints : Expr Bound blaze expression for a checkpoints table from which to get a computed lower bound. colnames : iterable of str The names of the columns for which checkpoints should be computed. lower_dt : pd.Timestamp The lower date being queried for that serves as an upper bound for checkpoints. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. def get_materialized_checkpoints(checkpoints, colnames, lower_dt, odo_kwargs): """ Computes a lower bound and a DataFrame checkpoints. Parameters ---------- checkpoints : Expr Bound blaze expression for a checkpoints table from which to get a computed lower bound. colnames : iterable of str The names of the columns for which checkpoints should be computed. lower_dt : pd.Timestamp The lower date being queried for that serves as an upper bound for checkpoints. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. """ if checkpoints is not None: ts = checkpoints[TS_FIELD_NAME] checkpoints_ts = odo( ts[ts < lower_dt].max(), pd.Timestamp, **odo_kwargs ) if pd.isnull(checkpoints_ts): # We don't have a checkpoint for before our start date so just # don't constrain the lower date. materialized_checkpoints = pd.DataFrame(columns=colnames) lower = None else: materialized_checkpoints = odo( checkpoints[ts == checkpoints_ts][colnames], pd.DataFrame, **odo_kwargs ) lower = checkpoints_ts else: materialized_checkpoints = pd.DataFrame(columns=colnames) lower = None # we don't have a good lower date constraint return lower, materialized_checkpoints
Query a blaze expression in a given time range properly forward filling from values that fall before the lower date. Parameters ---------- expr : Expr Bound blaze expression. lower : datetime The lower date to query for. upper : datetime The upper date to query for. checkpoints : Expr, optional Bound blaze expression for a checkpoints table from which to get a computed lower bound. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. ts_field : str, optional The name of the timestamp field in the given blaze expression. Returns ------- raw : pd.DataFrame A strict dataframe for the data in the given date range. This may start before the requested start date if a value is needed to ffill. def ffill_query_in_range(expr, lower, upper, checkpoints=None, odo_kwargs=None, ts_field=TS_FIELD_NAME): """Query a blaze expression in a given time range properly forward filling from values that fall before the lower date. Parameters ---------- expr : Expr Bound blaze expression. lower : datetime The lower date to query for. upper : datetime The upper date to query for. checkpoints : Expr, optional Bound blaze expression for a checkpoints table from which to get a computed lower bound. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. ts_field : str, optional The name of the timestamp field in the given blaze expression. Returns ------- raw : pd.DataFrame A strict dataframe for the data in the given date range. This may start before the requested start date if a value is needed to ffill. """ odo_kwargs = odo_kwargs or {} computed_lower, materialized_checkpoints = get_materialized_checkpoints( checkpoints, expr.fields, lower, odo_kwargs, ) pred = expr[ts_field] <= upper if computed_lower is not None: # only constrain the lower date if we computed a new lower date pred &= expr[ts_field] >= computed_lower raw = pd.concat( ( materialized_checkpoints, odo( expr[pred], pd.DataFrame, **odo_kwargs ), ), ignore_index=True, ) raw.loc[:, ts_field] = raw.loc[:, ts_field].astype('datetime64[ns]') return raw
Explicitly map a datset to a collection of blaze expressions. Parameters ---------- dataset : DataSet The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` def register_dataset(self, dataset, expr, deltas=None, checkpoints=None, odo_kwargs=None): """Explicitly map a datset to a collection of blaze expressions. Parameters ---------- dataset : DataSet The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` """ expr_data = ExprData( expr, deltas, checkpoints, odo_kwargs, ) for column in dataset.columns: self._table_expressions[column] = expr_data
Explicitly map a single bound column to a collection of blaze expressions. The expressions need to have ``timestamp`` and ``as_of`` columns. Parameters ---------- column : BoundColumn The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` def register_column(self, column, expr, deltas=None, checkpoints=None, odo_kwargs=None): """Explicitly map a single bound column to a collection of blaze expressions. The expressions need to have ``timestamp`` and ``as_of`` columns. Parameters ---------- column : BoundColumn The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` """ self._table_expressions[column] = ExprData( expr, deltas, checkpoints, odo_kwargs, )
Given a dict of mappings where the values are lists of OwnershipPeriod objects, returns a dict with the same structure with new OwnershipPeriod objects adjusted so that the periods have no gaps. Orders the periods chronologically, and pushes forward the end date of each period to match the start date of the following period. The end date of the last period pushed forward to the max Timestamp. def merge_ownership_periods(mappings): """ Given a dict of mappings where the values are lists of OwnershipPeriod objects, returns a dict with the same structure with new OwnershipPeriod objects adjusted so that the periods have no gaps. Orders the periods chronologically, and pushes forward the end date of each period to match the start date of the following period. The end date of the last period pushed forward to the max Timestamp. """ return valmap( lambda v: tuple( OwnershipPeriod( a.start, b.start, a.sid, a.value, ) for a, b in sliding_window( 2, concatv( sorted(v), # concat with a fake ownership object to make the last # end date be max timestamp [OwnershipPeriod( pd.Timestamp.max.tz_localize('utc'), None, None, None, )], ), ) ), mappings, )
Builds a dict mapping to lists of OwnershipPeriods, from a db table. def build_ownership_map(table, key_from_row, value_from_row): """ Builds a dict mapping to lists of OwnershipPeriods, from a db table. """ return _build_ownership_map_from_rows( sa.select(table.c).execute().fetchall(), key_from_row, value_from_row, )
Builds a dict mapping group keys to maps of keys to to lists of OwnershipPeriods, from a db table. def build_grouped_ownership_map(table, key_from_row, value_from_row, group_key): """ Builds a dict mapping group keys to maps of keys to to lists of OwnershipPeriods, from a db table. """ grouped_rows = groupby( group_key, sa.select(table.c).execute().fetchall(), ) return { key: _build_ownership_map_from_rows( rows, key_from_row, value_from_row, ) for key, rows in grouped_rows.items() }
Filter out kwargs from a dictionary. Parameters ---------- names : set[str] The names to select from ``dict_``. dict_ : dict[str, any] The dictionary to select from. Returns ------- kwargs : dict[str, any] ``dict_`` where the keys intersect with ``names`` and the values are not None. def _filter_kwargs(names, dict_): """Filter out kwargs from a dictionary. Parameters ---------- names : set[str] The names to select from ``dict_``. dict_ : dict[str, any] The dictionary to select from. Returns ------- kwargs : dict[str, any] ``dict_`` where the keys intersect with ``names`` and the values are not None. """ return {k: v for k, v in dict_.items() if k in names and v is not None}
Takes in a dict of Asset init args and converts dates to pd.Timestamps def _convert_asset_timestamp_fields(dict_): """ Takes in a dict of Asset init args and converts dates to pd.Timestamps """ for key in _asset_timestamp_fields & viewkeys(dict_): value = pd.Timestamp(dict_[key], tz='UTC') dict_[key] = None if isnull(value) else value return dict_
Whether or not `asset` was active at the time corresponding to `reference_date_value`. Parameters ---------- reference_date_value : int Date, represented as nanoseconds since EPOCH, for which we want to know if `asset` was alive. This is generally the result of accessing the `value` attribute of a pandas Timestamp. asset : Asset The asset object to check. Returns ------- was_active : bool Whether or not the `asset` existed at the specified time. def was_active(reference_date_value, asset): """ Whether or not `asset` was active at the time corresponding to `reference_date_value`. Parameters ---------- reference_date_value : int Date, represented as nanoseconds since EPOCH, for which we want to know if `asset` was alive. This is generally the result of accessing the `value` attribute of a pandas Timestamp. asset : Asset The asset object to check. Returns ------- was_active : bool Whether or not the `asset` existed at the specified time. """ return ( asset.start_date.value <= reference_date_value <= asset.end_date.value )
Retrieve asset types for a list of sids. Parameters ---------- sids : list[int] Returns ------- types : dict[sid -> str or None] Asset types for the provided sids. def lookup_asset_types(self, sids): """ Retrieve asset types for a list of sids. Parameters ---------- sids : list[int] Returns ------- types : dict[sid -> str or None] Asset types for the provided sids. """ found = {} missing = set() for sid in sids: try: found[sid] = self._asset_type_cache[sid] except KeyError: missing.add(sid) if not missing: return found router_cols = self.asset_router.c for assets in group_into_chunks(missing): query = sa.select((router_cols.sid, router_cols.asset_type)).where( self.asset_router.c.sid.in_(map(int, assets)) ) for sid, type_ in query.execute().fetchall(): missing.remove(sid) found[sid] = self._asset_type_cache[sid] = type_ for sid in missing: found[sid] = self._asset_type_cache[sid] = None return found
Retrieve all assets in `sids`. Parameters ---------- sids : iterable of int Assets to retrieve. default_none : bool If True, return None for failed lookups. If False, raise `SidsNotFound`. Returns ------- assets : list[Asset or None] A list of the same length as `sids` containing Assets (or Nones) corresponding to the requested sids. Raises ------ SidsNotFound When a requested sid is not found and default_none=False. def retrieve_all(self, sids, default_none=False): """ Retrieve all assets in `sids`. Parameters ---------- sids : iterable of int Assets to retrieve. default_none : bool If True, return None for failed lookups. If False, raise `SidsNotFound`. Returns ------- assets : list[Asset or None] A list of the same length as `sids` containing Assets (or Nones) corresponding to the requested sids. Raises ------ SidsNotFound When a requested sid is not found and default_none=False. """ sids = list(sids) hits, missing, failures = {}, set(), [] for sid in sids: try: asset = self._asset_cache[sid] if not default_none and asset is None: # Bail early if we've already cached that we don't know # about an asset. raise SidsNotFound(sids=[sid]) hits[sid] = asset except KeyError: missing.add(sid) # All requests were cache hits. Return requested sids in order. if not missing: return [hits[sid] for sid in sids] update_hits = hits.update # Look up cache misses by type. type_to_assets = self.group_by_type(missing) # Handle failures failures = {failure: None for failure in type_to_assets.pop(None, ())} update_hits(failures) self._asset_cache.update(failures) if failures and not default_none: raise SidsNotFound(sids=list(failures)) # We don't update the asset cache here because it should already be # updated by `self.retrieve_equities`. update_hits(self.retrieve_equities(type_to_assets.pop('equity', ()))) update_hits( self.retrieve_futures_contracts(type_to_assets.pop('future', ())) ) # We shouldn't know about any other asset types. if type_to_assets: raise AssertionError( "Found asset types: %s" % list(type_to_assets.keys()) ) return [hits[sid] for sid in sids]
Retrieve the most recent symbol for a set of sids. Parameters ---------- sid_group : iterable[int] The sids to lookup. The length of this sequence must be less than or equal to SQLITE_MAX_VARIABLE_NUMBER because the sids will be passed in as sql bind params. Returns ------- sel : Selectable The sqlalchemy selectable that will query for the most recent symbol for each sid. Notes ----- This is implemented as an inner select of the columns of interest ordered by the end date of the (sid, symbol) mapping. We then group that inner select on the sid with no aggregations to select the last row per group which gives us the most recently active symbol for all of the sids. def _select_most_recent_symbols_chunk(self, sid_group): """Retrieve the most recent symbol for a set of sids. Parameters ---------- sid_group : iterable[int] The sids to lookup. The length of this sequence must be less than or equal to SQLITE_MAX_VARIABLE_NUMBER because the sids will be passed in as sql bind params. Returns ------- sel : Selectable The sqlalchemy selectable that will query for the most recent symbol for each sid. Notes ----- This is implemented as an inner select of the columns of interest ordered by the end date of the (sid, symbol) mapping. We then group that inner select on the sid with no aggregations to select the last row per group which gives us the most recently active symbol for all of the sids. """ cols = self.equity_symbol_mappings.c # These are the columns we actually want. data_cols = (cols.sid,) + tuple(cols[name] for name in symbol_columns) # Also select the max of end_date so that all non-grouped fields take # on the value associated with the max end_date. The SQLite docs say # this: # # When the min() or max() aggregate functions are used in an aggregate # query, all bare columns in the result set take values from the input # row which also contains the minimum or maximum. Only the built-in # min() and max() functions work this way. # # See https://www.sqlite.org/lang_select.html#resultset, for more info. to_select = data_cols + (sa.func.max(cols.end_date),) return sa.select( to_select, ).where( cols.sid.in_(map(int, sid_group)) ).group_by( cols.sid, )
Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. Parameters --------- sids : iterable of int Asset ids to look up. asset_tbl : sqlalchemy.Table Table from which to query assets. asset_type : type Type of asset to be constructed. Returns ------- assets : dict[int -> Asset] Dict mapping requested sids to the retrieved assets. def _retrieve_assets(self, sids, asset_tbl, asset_type): """ Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. Parameters --------- sids : iterable of int Asset ids to look up. asset_tbl : sqlalchemy.Table Table from which to query assets. asset_type : type Type of asset to be constructed. Returns ------- assets : dict[int -> Asset] Dict mapping requested sids to the retrieved assets. """ # Fastpath for empty request. if not sids: return {} cache = self._asset_cache hits = {} querying_equities = issubclass(asset_type, Equity) filter_kwargs = ( _filter_equity_kwargs if querying_equities else _filter_future_kwargs ) rows = self._retrieve_asset_dicts(sids, asset_tbl, querying_equities) for row in rows: sid = row['sid'] asset = asset_type(**filter_kwargs(row)) hits[sid] = cache[sid] = asset # If we get here, it means something in our code thought that a # particular sid was an equity/future and called this function with a # concrete type, but we couldn't actually resolve the asset. This is # an error in our code, not a user-input error. misses = tuple(set(sids) - viewkeys(hits)) if misses: if querying_equities: raise EquitiesNotFound(sids=misses) else: raise FutureContractsNotFound(sids=misses) return hits
Resolve a symbol to an asset object without fuzzy matching. Parameters ---------- ownership_map : dict[(str, str), list[OwnershipPeriod]] The mapping from split symbols to ownership periods. multi_country : bool Does this mapping span multiple countries? symbol : str The symbol to look up. as_of_date : datetime or None If multiple assets have held this sid, which day should the resolution be checked against? If this value is None and multiple sids have held the ticker, then a MultipleSymbolsFound error will be raised. Returns ------- asset : Asset The asset that held the given symbol. Raises ------ SymbolNotFound Raised when the symbol or symbol as_of_date pair do not map to any assets. MultipleSymbolsFound Raised when multiple assets held the symbol. This happens if multiple assets held the symbol at disjoint times and ``as_of_date`` is None, or if multiple assets held the symbol at the same time and``multi_country`` is True. Notes ----- The resolution algorithm is as follows: - Split the symbol into the company and share class component. - Do a dictionary lookup of the ``(company_symbol, share_class_symbol)`` in the provided ownership map. - If there is no entry in the dictionary, we don't know about this symbol so raise a ``SymbolNotFound`` error. - If ``as_of_date`` is None: - If more there is more than one owner, raise ``MultipleSymbolsFound`` - Otherwise, because the list mapped to a symbol cannot be empty, return the single asset. - Iterate through all of the owners: - If the ``as_of_date`` is between the start and end of the ownership period: - If multi_country is False, return the found asset. - Otherwise, put the asset in a list. - At the end of the loop, if there are no candidate assets, raise a ``SymbolNotFound``. - If there is exactly one candidate, return it. - Othewise, raise ``MultipleSymbolsFound`` because the ticker is not unique across countries. def _lookup_symbol_strict(self, ownership_map, multi_country, symbol, as_of_date): """ Resolve a symbol to an asset object without fuzzy matching. Parameters ---------- ownership_map : dict[(str, str), list[OwnershipPeriod]] The mapping from split symbols to ownership periods. multi_country : bool Does this mapping span multiple countries? symbol : str The symbol to look up. as_of_date : datetime or None If multiple assets have held this sid, which day should the resolution be checked against? If this value is None and multiple sids have held the ticker, then a MultipleSymbolsFound error will be raised. Returns ------- asset : Asset The asset that held the given symbol. Raises ------ SymbolNotFound Raised when the symbol or symbol as_of_date pair do not map to any assets. MultipleSymbolsFound Raised when multiple assets held the symbol. This happens if multiple assets held the symbol at disjoint times and ``as_of_date`` is None, or if multiple assets held the symbol at the same time and``multi_country`` is True. Notes ----- The resolution algorithm is as follows: - Split the symbol into the company and share class component. - Do a dictionary lookup of the ``(company_symbol, share_class_symbol)`` in the provided ownership map. - If there is no entry in the dictionary, we don't know about this symbol so raise a ``SymbolNotFound`` error. - If ``as_of_date`` is None: - If more there is more than one owner, raise ``MultipleSymbolsFound`` - Otherwise, because the list mapped to a symbol cannot be empty, return the single asset. - Iterate through all of the owners: - If the ``as_of_date`` is between the start and end of the ownership period: - If multi_country is False, return the found asset. - Otherwise, put the asset in a list. - At the end of the loop, if there are no candidate assets, raise a ``SymbolNotFound``. - If there is exactly one candidate, return it. - Othewise, raise ``MultipleSymbolsFound`` because the ticker is not unique across countries. """ # split the symbol into the components, if there are no # company/share class parts then share_class_symbol will be empty company_symbol, share_class_symbol = split_delimited_symbol(symbol) try: owners = ownership_map[company_symbol, share_class_symbol] assert owners, 'empty owners list for %r' % symbol except KeyError: # no equity has ever held this symbol raise SymbolNotFound(symbol=symbol) if not as_of_date: # exactly one equity has ever held this symbol, we may resolve # without the date if len(owners) == 1: return self.retrieve_asset(owners[0].sid) options = {self.retrieve_asset(owner.sid) for owner in owners} if multi_country: country_codes = map(attrgetter('country_code'), options) if len(set(country_codes)) > 1: raise SameSymbolUsedAcrossCountries( symbol=symbol, options=dict(zip(country_codes, options)) ) # more than one equity has held this ticker, this # is ambiguous without the date raise MultipleSymbolsFound(symbol=symbol, options=options) options = [] country_codes = [] for start, end, sid, _ in owners: if start <= as_of_date < end: # find the equity that owned it on the given asof date asset = self.retrieve_asset(sid) # if this asset owned the symbol on this asof date and we are # only searching one country, return that asset if not multi_country: return asset else: options.append(asset) country_codes.append(asset.country_code) if not options: # no equity held the ticker on the given asof date raise SymbolNotFound(symbol=symbol) # if there is one valid option given the asof date, return that option if len(options) == 1: return options[0] # if there's more than one option given the asof date, a country code # must be passed to resolve the symbol to an asset raise SameSymbolUsedAcrossCountries( symbol=symbol, options=dict(zip(country_codes, options)) )
Lookup an equity by symbol. Parameters ---------- symbol : str The ticker symbol to resolve. as_of_date : datetime or None Look up the last owner of this symbol as of this datetime. If ``as_of_date`` is None, then this can only resolve the equity if exactly one equity has ever owned the ticker. fuzzy : bool, optional Should fuzzy symbol matching be used? Fuzzy symbol matching attempts to resolve differences in representations for shareclasses. For example, some people may represent the ``A`` shareclass of ``BRK`` as ``BRK.A``, where others could write ``BRK_A``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equity : Equity The equity that held ``symbol`` on the given ``as_of_date``, or the only equity to hold ``symbol`` if ``as_of_date`` is None. Raises ------ SymbolNotFound Raised when no equity has ever held the given symbol. MultipleSymbolsFound Raised when no ``as_of_date`` is given and more than one equity has held ``symbol``. This is also raised when ``fuzzy=True`` and there are multiple candidates for the given ``symbol`` on the ``as_of_date``. Also raised when no ``country_code`` is given and the symbol is ambiguous across multiple countries. def lookup_symbol(self, symbol, as_of_date, fuzzy=False, country_code=None): """Lookup an equity by symbol. Parameters ---------- symbol : str The ticker symbol to resolve. as_of_date : datetime or None Look up the last owner of this symbol as of this datetime. If ``as_of_date`` is None, then this can only resolve the equity if exactly one equity has ever owned the ticker. fuzzy : bool, optional Should fuzzy symbol matching be used? Fuzzy symbol matching attempts to resolve differences in representations for shareclasses. For example, some people may represent the ``A`` shareclass of ``BRK`` as ``BRK.A``, where others could write ``BRK_A``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equity : Equity The equity that held ``symbol`` on the given ``as_of_date``, or the only equity to hold ``symbol`` if ``as_of_date`` is None. Raises ------ SymbolNotFound Raised when no equity has ever held the given symbol. MultipleSymbolsFound Raised when no ``as_of_date`` is given and more than one equity has held ``symbol``. This is also raised when ``fuzzy=True`` and there are multiple candidates for the given ``symbol`` on the ``as_of_date``. Also raised when no ``country_code`` is given and the symbol is ambiguous across multiple countries. """ if symbol is None: raise TypeError("Cannot lookup asset for symbol of None for " "as of date %s." % as_of_date) if fuzzy: f = self._lookup_symbol_fuzzy mapping = self._choose_fuzzy_symbol_ownership_map(country_code) else: f = self._lookup_symbol_strict mapping = self._choose_symbol_ownership_map(country_code) if mapping is None: raise SymbolNotFound(symbol=symbol) return f( mapping, country_code is None, symbol, as_of_date, )
Lookup a list of equities by symbol. Equivalent to:: [finder.lookup_symbol(s, as_of, fuzzy) for s in symbols] but potentially faster because repeated lookups are memoized. Parameters ---------- symbols : sequence[str] Sequence of ticker symbols to resolve. as_of_date : pd.Timestamp Forwarded to ``lookup_symbol``. fuzzy : bool, optional Forwarded to ``lookup_symbol``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equities : list[Equity] def lookup_symbols(self, symbols, as_of_date, fuzzy=False, country_code=None): """ Lookup a list of equities by symbol. Equivalent to:: [finder.lookup_symbol(s, as_of, fuzzy) for s in symbols] but potentially faster because repeated lookups are memoized. Parameters ---------- symbols : sequence[str] Sequence of ticker symbols to resolve. as_of_date : pd.Timestamp Forwarded to ``lookup_symbol``. fuzzy : bool, optional Forwarded to ``lookup_symbol``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equities : list[Equity] """ if not symbols: return [] multi_country = country_code is None if fuzzy: f = self._lookup_symbol_fuzzy mapping = self._choose_fuzzy_symbol_ownership_map(country_code) else: f = self._lookup_symbol_strict mapping = self._choose_symbol_ownership_map(country_code) if mapping is None: raise SymbolNotFound(symbol=symbols[0]) memo = {} out = [] append_output = out.append for sym in symbols: if sym in memo: append_output(memo[sym]) else: equity = memo[sym] = f( mapping, multi_country, sym, as_of_date, ) append_output(equity) return out
Lookup a future contract by symbol. Parameters ---------- symbol : str The symbol of the desired contract. Returns ------- future : Future The future contract referenced by ``symbol``. Raises ------ SymbolNotFound Raised when no contract named 'symbol' is found. def lookup_future_symbol(self, symbol): """Lookup a future contract by symbol. Parameters ---------- symbol : str The symbol of the desired contract. Returns ------- future : Future The future contract referenced by ``symbol``. Raises ------ SymbolNotFound Raised when no contract named 'symbol' is found. """ data = self._select_asset_by_symbol(self.futures_contracts, symbol)\ .execute().fetchone() # If no data found, raise an exception if not data: raise SymbolNotFound(symbol=symbol) return self.retrieve_asset(data['sid'])
Get the value of a supplementary field for an asset. Parameters ---------- sid : int The sid of the asset to query. field_name : str Name of the supplementary field. as_of_date : pd.Timestamp, None The last known value on this date is returned. If None, a value is returned only if we've only ever had one value for this sid. If None and we've had multiple values, MultipleValuesFoundForSid is raised. Raises ------ NoValueForSid If we have no values for this asset, or no values was known on this as_of_date. MultipleValuesFoundForSid If we have had multiple values for this asset over time, and None was passed for as_of_date. def get_supplementary_field(self, sid, field_name, as_of_date): """Get the value of a supplementary field for an asset. Parameters ---------- sid : int The sid of the asset to query. field_name : str Name of the supplementary field. as_of_date : pd.Timestamp, None The last known value on this date is returned. If None, a value is returned only if we've only ever had one value for this sid. If None and we've had multiple values, MultipleValuesFoundForSid is raised. Raises ------ NoValueForSid If we have no values for this asset, or no values was known on this as_of_date. MultipleValuesFoundForSid If we have had multiple values for this asset over time, and None was passed for as_of_date. """ try: periods = self.equity_supplementary_map_by_sid[ field_name, sid, ] assert periods, 'empty periods list for %r' % (field_name, sid) except KeyError: raise NoValueForSid(field=field_name, sid=sid) if not as_of_date: if len(periods) > 1: # This equity has held more than one value, this is ambigious # without the date raise MultipleValuesFoundForSid( field=field_name, sid=sid, options={p.value for p in periods}, ) # this equity has only ever held this value, we may resolve # without the date return periods[0].value for start, end, _, value in periods: if start <= as_of_date < end: return value # Could not find a value for this sid on the as_of_date. raise NoValueForSid(field=field_name, sid=sid)
Convert asset_convertible to an asset. On success, append to matches. On failure, append to missing. def _lookup_generic_scalar(self, obj, as_of_date, country_code, matches, missing): """ Convert asset_convertible to an asset. On success, append to matches. On failure, append to missing. """ result = self._lookup_generic_scalar_helper( obj, as_of_date, country_code, ) if result is not None: matches.append(result) else: missing.append(obj)
Convert an object into an Asset or sequence of Assets. This method exists primarily as a convenience for implementing user-facing APIs that can handle multiple kinds of input. It should not be used for internal code where we already know the expected types of our inputs. Parameters ---------- obj : int, str, Asset, ContinuousFuture, or iterable The object to be converted into one or more Assets. Integers are interpreted as sids. Strings are interpreted as tickers. Assets and ContinuousFutures are returned unchanged. as_of_date : pd.Timestamp or None Timestamp to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. country_code : str or None ISO-3166 country code to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. Returns ------- matches, missing : tuple ``matches`` is the result of the conversion. ``missing`` is a list containing any values that couldn't be resolved. If ``obj`` is not an iterable, ``missing`` will be an empty list. def lookup_generic(self, obj, as_of_date, country_code): """ Convert an object into an Asset or sequence of Assets. This method exists primarily as a convenience for implementing user-facing APIs that can handle multiple kinds of input. It should not be used for internal code where we already know the expected types of our inputs. Parameters ---------- obj : int, str, Asset, ContinuousFuture, or iterable The object to be converted into one or more Assets. Integers are interpreted as sids. Strings are interpreted as tickers. Assets and ContinuousFutures are returned unchanged. as_of_date : pd.Timestamp or None Timestamp to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. country_code : str or None ISO-3166 country code to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. Returns ------- matches, missing : tuple ``matches`` is the result of the conversion. ``missing`` is a list containing any values that couldn't be resolved. If ``obj`` is not an iterable, ``missing`` will be an empty list. """ matches = [] missing = [] # Interpret input as scalar. if isinstance(obj, (AssetConvertible, ContinuousFuture)): self._lookup_generic_scalar( obj=obj, as_of_date=as_of_date, country_code=country_code, matches=matches, missing=missing, ) try: return matches[0], missing except IndexError: if hasattr(obj, '__int__'): raise SidsNotFound(sids=[obj]) else: raise SymbolNotFound(symbol=obj) # Interpret input as iterable. try: iterator = iter(obj) except TypeError: raise NotAssetConvertible( "Input was not a AssetConvertible " "or iterable of AssetConvertible." ) for obj in iterator: self._lookup_generic_scalar( obj=obj, as_of_date=as_of_date, country_code=country_code, matches=matches, missing=missing, ) return matches, missing
Compute and cache a recarray of asset lifetimes. def _compute_asset_lifetimes(self, country_codes): """ Compute and cache a recarray of asset lifetimes. """ equities_cols = self.equities.c if country_codes: buf = np.array( tuple( sa.select(( equities_cols.sid, equities_cols.start_date, equities_cols.end_date, )).where( (self.exchanges.c.exchange == equities_cols.exchange) & (self.exchanges.c.country_code.in_(country_codes)) ).execute(), ), dtype='f8', # use doubles so we get NaNs ) else: buf = np.array([], dtype='f8') lifetimes = np.recarray( buf=buf, shape=(len(buf),), dtype=[ ('sid', 'f8'), ('start', 'f8'), ('end', 'f8') ], ) start = lifetimes.start end = lifetimes.end start[np.isnan(start)] = 0 # convert missing starts to 0 end[np.isnan(end)] = np.iinfo(int).max # convert missing end to INTMAX # Cast the results back down to int. return lifetimes.astype([ ('sid', 'i8'), ('start', 'i8'), ('end', 'i8'), ])
Compute a DataFrame representing asset lifetimes for the specified date range. Parameters ---------- dates : pd.DatetimeIndex The dates for which to compute lifetimes. include_start_date : bool Whether or not to count the asset as alive on its start_date. This is useful in a backtesting context where `lifetimes` is being used to signify "do I have data for this asset as of the morning of this date?" For many financial metrics, (e.g. daily close), data isn't available for an asset until the end of the asset's first day. country_codes : iterable[str] The country codes to get lifetimes for. Returns ------- lifetimes : pd.DataFrame A frame of dtype bool with `dates` as index and an Int64Index of assets as columns. The value at `lifetimes.loc[date, asset]` will be True iff `asset` existed on `date`. If `include_start_date` is False, then lifetimes.loc[date, asset] will be false when date == asset.start_date. See Also -------- numpy.putmask zipline.pipeline.engine.SimplePipelineEngine._compute_root_mask def lifetimes(self, dates, include_start_date, country_codes): """ Compute a DataFrame representing asset lifetimes for the specified date range. Parameters ---------- dates : pd.DatetimeIndex The dates for which to compute lifetimes. include_start_date : bool Whether or not to count the asset as alive on its start_date. This is useful in a backtesting context where `lifetimes` is being used to signify "do I have data for this asset as of the morning of this date?" For many financial metrics, (e.g. daily close), data isn't available for an asset until the end of the asset's first day. country_codes : iterable[str] The country codes to get lifetimes for. Returns ------- lifetimes : pd.DataFrame A frame of dtype bool with `dates` as index and an Int64Index of assets as columns. The value at `lifetimes.loc[date, asset]` will be True iff `asset` existed on `date`. If `include_start_date` is False, then lifetimes.loc[date, asset] will be false when date == asset.start_date. See Also -------- numpy.putmask zipline.pipeline.engine.SimplePipelineEngine._compute_root_mask """ if isinstance(country_codes, string_types): raise TypeError( "Got string {!r} instead of an iterable of strings in " "AssetFinder.lifetimes.".format(country_codes), ) # normalize to a cache-key so that we can memoize results. country_codes = frozenset(country_codes) lifetimes = self._asset_lifetimes.get(country_codes) if lifetimes is None: self._asset_lifetimes[country_codes] = lifetimes = ( self._compute_asset_lifetimes(country_codes) ) raw_dates = as_column(dates.asi8) if include_start_date: mask = lifetimes.start <= raw_dates else: mask = lifetimes.start < raw_dates mask &= (raw_dates <= lifetimes.end) return pd.DataFrame(mask, index=dates, columns=lifetimes.sid)
Return all of the sids for a given country. Parameters ---------- country_code : str An ISO 3166 alpha-2 country code. Returns ------- tuple[int] The sids whose exchanges are in this country. def equities_sids_for_country_code(self, country_code): """Return all of the sids for a given country. Parameters ---------- country_code : str An ISO 3166 alpha-2 country code. Returns ------- tuple[int] The sids whose exchanges are in this country. """ sids = self._compute_asset_lifetimes([country_code]).sid return tuple(sids.tolist())
Parameters ---------- fields : list of str 'sid' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. def load_raw_arrays(self, columns, start_date, end_date, assets): """ Parameters ---------- fields : list of str 'sid' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. """ rolls_by_asset = {} for asset in assets: rf = self._roll_finders[asset.roll_style] rolls_by_asset[asset] = rf.get_rolls( asset.root_symbol, start_date, end_date, asset.offset ) num_sessions = len( self.trading_calendar.sessions_in_range(start_date, end_date) ) shape = num_sessions, len(assets) results = [] tc = self._bar_reader.trading_calendar sessions = tc.sessions_in_range(start_date, end_date) # Get partitions partitions_by_asset = {} for asset in assets: partitions = [] partitions_by_asset[asset] = partitions rolls = rolls_by_asset[asset] start = start_date for roll in rolls: sid, roll_date = roll start_loc = sessions.get_loc(start) if roll_date is not None: end = roll_date - sessions.freq end_loc = sessions.get_loc(end) else: end = end_date end_loc = len(sessions) - 1 partitions.append((sid, start, end, start_loc, end_loc)) if roll_date is not None: start = sessions[end_loc + 1] for column in columns: if column != 'volume' and column != 'sid': out = np.full(shape, np.nan) else: out = np.zeros(shape, dtype=np.int64) for i, asset in enumerate(assets): partitions = partitions_by_asset[asset] for sid, start, end, start_loc, end_loc in partitions: if column != 'sid': result = self._bar_reader.load_raw_arrays( [column], start, end, [sid])[0][:, 0] else: result = int(sid) out[start_loc:end_loc + 1, i] = result results.append(out) return results
Retrieve the value at the given coordinates. Parameters ---------- sid : int The asset identifier. dt : pd.Timestamp The timestamp for the desired data point. field : string The OHLVC name for the desired data point. Returns ------- value : float|int The value at the given coordinates, ``float`` for OHLC, ``int`` for 'volume'. Raises ------ NoDataOnDate If the given dt is not a valid market minute (in minute mode) or session (in daily mode) according to this reader's tradingcalendar. def get_value(self, continuous_future, dt, field): """ Retrieve the value at the given coordinates. Parameters ---------- sid : int The asset identifier. dt : pd.Timestamp The timestamp for the desired data point. field : string The OHLVC name for the desired data point. Returns ------- value : float|int The value at the given coordinates, ``float`` for OHLC, ``int`` for 'volume'. Raises ------ NoDataOnDate If the given dt is not a valid market minute (in minute mode) or session (in daily mode) according to this reader's tradingcalendar. """ rf = self._roll_finders[continuous_future.roll_style] sid = (rf.get_contract_center(continuous_future.root_symbol, dt, continuous_future.offset)) return self._bar_reader.get_value(sid, dt, field)
Get the latest minute on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. Parameters ---------- asset : zipline.asset.Asset The asset for which to get the last traded minute. dt : pd.Timestamp The minute at which to start searching for the last traded minute. Returns ------- last_traded : pd.Timestamp The dt of the last trade for the given asset, using the input dt as a vantage point. def get_last_traded_dt(self, asset, dt): """ Get the latest minute on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. Parameters ---------- asset : zipline.asset.Asset The asset for which to get the last traded minute. dt : pd.Timestamp The minute at which to start searching for the last traded minute. Returns ------- last_traded : pd.Timestamp The dt of the last trade for the given asset, using the input dt as a vantage point. """ rf = self._roll_finders[asset.roll_style] sid = (rf.get_contract_center(asset.root_symbol, dt, asset.offset)) if sid is None: return pd.NaT contract = rf.asset_finder.retrieve_asset(sid) return self._bar_reader.get_last_traded_dt(contract, dt)
Parameters ---------- fields : list of str 'open', 'high', 'low', 'close', or 'volume' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. def load_raw_arrays(self, columns, start_date, end_date, assets): """ Parameters ---------- fields : list of str 'open', 'high', 'low', 'close', or 'volume' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. """ rolls_by_asset = {} tc = self.trading_calendar start_session = tc.minute_to_session_label(start_date) end_session = tc.minute_to_session_label(end_date) for asset in assets: rf = self._roll_finders[asset.roll_style] rolls_by_asset[asset] = rf.get_rolls( asset.root_symbol, start_session, end_session, asset.offset) sessions = tc.sessions_in_range(start_date, end_date) minutes = tc.minutes_in_range(start_date, end_date) num_minutes = len(minutes) shape = num_minutes, len(assets) results = [] # Get partitions partitions_by_asset = {} for asset in assets: partitions = [] partitions_by_asset[asset] = partitions rolls = rolls_by_asset[asset] start = start_date for roll in rolls: sid, roll_date = roll start_loc = minutes.searchsorted(start) if roll_date is not None: _, end = tc.open_and_close_for_session( roll_date - sessions.freq) end_loc = minutes.searchsorted(end) else: end = end_date end_loc = len(minutes) - 1 partitions.append((sid, start, end, start_loc, end_loc)) if roll[-1] is not None: start, _ = tc.open_and_close_for_session( tc.minute_to_session_label(minutes[end_loc + 1])) for column in columns: if column != 'volume': out = np.full(shape, np.nan) else: out = np.zeros(shape, dtype=np.uint32) for i, asset in enumerate(assets): partitions = partitions_by_asset[asset] for sid, start, end, start_loc, end_loc in partitions: if column != 'sid': result = self._bar_reader.load_raw_arrays( [column], start, end, [sid])[0][:, 0] else: result = int(sid) out[start_loc:end_loc + 1, i] = result results.append(out) return results
Compute each asset's weight in the portfolio by calculating its held value divided by the total value of all positions. Each equity's value is its price times the number of shares held. Each futures contract's value is its unit price times number of shares held times the multiplier. def current_portfolio_weights(self): """ Compute each asset's weight in the portfolio by calculating its held value divided by the total value of all positions. Each equity's value is its price times the number of shares held. Each futures contract's value is its unit price times number of shares held times the multiplier. """ position_values = pd.Series({ asset: ( position.last_sale_price * position.amount * asset.price_multiplier ) for asset, position in self.positions.items() }) return position_values / self.portfolio_value
将微信明细中图片托管到云端,同时将html页面中的对应图片替换 Parameters ---------- content_info : dict 微信文章明细字典 { 'content_img_list': [], # 从微信文章解析出的原始图片列表 'content_html': '', # 从微信文章解析出文章的内容 } hosting_callback : callable 托管回调函数,传入单个图片链接,返回托管后的图片链接 Returns ------- dict { 'content_img_list': '', # 托管后的图片列表 'content_html': '', # 图片链接为托管后的图片链接内容 } def __hosting_wechat_img(self, content_info, hosting_callback): """将微信明细中图片托管到云端,同时将html页面中的对应图片替换 Parameters ---------- content_info : dict 微信文章明细字典 { 'content_img_list': [], # 从微信文章解析出的原始图片列表 'content_html': '', # 从微信文章解析出文章的内容 } hosting_callback : callable 托管回调函数,传入单个图片链接,返回托管后的图片链接 Returns ------- dict { 'content_img_list': '', # 托管后的图片列表 'content_html': '', # 图片链接为托管后的图片链接内容 } """ assert callable(hosting_callback) content_img_list = content_info.pop("content_img_list") content_html = content_info.pop("content_html") for idx, img_url in enumerate(content_img_list): hosting_img_url = hosting_callback(img_url) if not hosting_img_url: # todo 定义标准异常 raise Exception() content_img_list[idx] = hosting_img_url content_html = content_html.replace(img_url, hosting_img_url) return dict(content_img_list=content_img_list, content_html=content_html)
获取公众号微信号 wechatid 的信息 因为wechatid唯一确定,所以第一个就是要搜索的公众号 Parameters ---------- wecgat_id_or_name : str or unicode wechat_id or wechat_name unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict or None { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } def get_gzh_info(self, wecgat_id_or_name, unlock_callback=None, identify_image_callback=None, decode_url=True): """获取公众号微信号 wechatid 的信息 因为wechatid唯一确定,所以第一个就是要搜索的公众号 Parameters ---------- wecgat_id_or_name : str or unicode wechat_id or wechat_name unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict or None { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } """ info = self.search_gzh(wecgat_id_or_name, 1, unlock_callback, identify_image_callback, decode_url) try: return next(info) except StopIteration: return None
搜索 公众号 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } Raises ------ WechatSogouRequestsException requests error def search_gzh(self, keyword, page=1, unlock_callback=None, identify_image_callback=None, decode_url=True): """搜索 公众号 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } Raises ------ WechatSogouRequestsException requests error """ url = WechatSogouRequest.gen_search_gzh_url(keyword, page) session = requests.session() resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) gzh_list = WechatSogouStructuring.get_gzh_by_search(resp.text) for i in gzh_list: if decode_url: i['profile_url'] = self.__format_url(i['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) yield i
搜索 文章 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 the default is anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 ft, et : datetime.date or None 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } Raises ------ WechatSogouRequestsException requests error def search_article(self, keyword, page=1, timesn=WechatSogouConst.search_article_time.anytime, article_type=WechatSogouConst.search_article_type.all, ft=None, et=None, unlock_callback=None, identify_image_callback=None, decode_url=True): """搜索 文章 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 the default is anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 ft, et : datetime.date or None 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } Raises ------ WechatSogouRequestsException requests error """ url = WechatSogouRequest.gen_search_article_url(keyword, page, timesn, article_type, ft, et) session = requests.session() resp = self.__get_by_unlock(url, WechatSogouRequest.gen_search_article_url(keyword), unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) article_list = WechatSogouStructuring.get_article_by_search(resp.text) for i in article_list: if decode_url: i['article']['url'] = self.__format_url(i['article']['url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) i['gzh']['profile_url'] = self.__format_url(i['gzh']['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) yield i
从 公众号的最近10条群发页面 提取公众号信息 和 文章列表信息 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 公众号的id 或者name url : str or unicode 群发页url,如果不提供url,就先去搜索一遍拿到url unlock_callback_sogou : callable 处理出现 搜索 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_sogou : callable 处理 搜索 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example unlock_callback_weixin : callable 处理出现 历史页 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_weixin : callable 处理 历史页 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'gzh': { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 }, 'article': [ { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 }, ... ] } Raises ------ WechatSogouRequestsException requests error def get_gzh_article_by_history(self, keyword=None, url=None, unlock_callback_sogou=None, identify_image_callback_sogou=None, unlock_callback_weixin=None, identify_image_callback_weixin=None): """从 公众号的最近10条群发页面 提取公众号信息 和 文章列表信息 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 公众号的id 或者name url : str or unicode 群发页url,如果不提供url,就先去搜索一遍拿到url unlock_callback_sogou : callable 处理出现 搜索 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_sogou : callable 处理 搜索 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example unlock_callback_weixin : callable 处理出现 历史页 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_weixin : callable 处理 历史页 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'gzh': { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 }, 'article': [ { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 }, ... ] } Raises ------ WechatSogouRequestsException requests error """ if url is None: gzh_list = self.get_gzh_info(keyword, unlock_callback_sogou, identify_image_callback_sogou) if gzh_list is None: return {} if 'profile_url' not in gzh_list: raise Exception() # todo use ws exception url = gzh_list['profile_url'] resp = self.__get_by_unlock(url, WechatSogouRequest.gen_search_article_url(keyword), unlock_platform=self.__unlock_wechat, unlock_callback=unlock_callback_weixin, identify_image_callback=identify_image_callback_weixin) return WechatSogouStructuring.get_gzh_info_and_article_by_history(resp.text)
获取 首页热门文章 Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } def get_gzh_article_by_hot(self, hot_index, page=1, unlock_callback=None, identify_image_callback=None): """获取 首页热门文章 Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } """ assert hasattr(WechatSogouConst.hot_index, hot_index) assert isinstance(page, int) and page > 0 url = WechatSogouRequest.gen_hot_url(hot_index, page) resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback) resp.encoding = 'utf-8' return WechatSogouStructuring.get_gzh_article_by_hot(resp.text)
获取文章原文,避免临时链接失效 Parameters ---------- url : str or unicode 原文链接,临时链接 raw : bool True: 返回原始html False: 返回处理后的html del_qqmusic: bool True:微信原文中有插入的qq音乐,则删除 False:微信源文中有插入的qq音乐,则保留 del_mpvoice: bool True:微信原文中有插入的语音消息,则删除 False:微信源文中有插入的语音消息,则保留 unlock_callback : callable 处理 文章明细 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback : callable 处理 文章明细 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example hosting_callback: callable 将微信采集的文章托管到7牛或者阿里云回调函数,输入微信图片源地址,返回托管后地址 Returns ------- content_html 原文内容 content_img_list 文章中图片列表 Raises ------ WechatSogouRequestsException def get_article_content(self, url, del_qqmusic=True, del_mpvoice=True, unlock_callback=None, identify_image_callback=None, hosting_callback=None, raw=False): """获取文章原文,避免临时链接失效 Parameters ---------- url : str or unicode 原文链接,临时链接 raw : bool True: 返回原始html False: 返回处理后的html del_qqmusic: bool True:微信原文中有插入的qq音乐,则删除 False:微信源文中有插入的qq音乐,则保留 del_mpvoice: bool True:微信原文中有插入的语音消息,则删除 False:微信源文中有插入的语音消息,则保留 unlock_callback : callable 处理 文章明细 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback : callable 处理 文章明细 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example hosting_callback: callable 将微信采集的文章托管到7牛或者阿里云回调函数,输入微信图片源地址,返回托管后地址 Returns ------- content_html 原文内容 content_img_list 文章中图片列表 Raises ------ WechatSogouRequestsException """ resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_wechat, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback) resp.encoding = 'utf-8' if '链接已过期' in resp.text: raise WechatSogouException('get_article_content 链接 [{}] 已过期'.format(url)) if raw: return resp.text content_info = WechatSogouStructuring.get_article_detail(resp.text, del_qqmusic=del_qqmusic, del_voice=del_mpvoice) if hosting_callback: content_info = self.__hosting_wechat_img(content_info, hosting_callback) return content_info
获取微信搜狗搜索关键词联想 Parameters ---------- keyword : str or unicode 关键词 Returns ------- list[str] 联想关键词列表 Raises ------ WechatSogouRequestsException def get_sugg(self, keyword): """获取微信搜狗搜索关键词联想 Parameters ---------- keyword : str or unicode 关键词 Returns ------- list[str] 联想关键词列表 Raises ------ WechatSogouRequestsException """ url = 'http://w.sugg.sogou.com/sugg/ajaj_json.jsp?key={}&type=wxpub&pr=web'.format( quote(keyword.encode('utf-8'))) r = requests.get(url) if not r.ok: raise WechatSogouRequestsException('get_sugg', r) sugg = re.findall(u'\["' + keyword + '",(.*?),\["', r.text)[0] return json.loads(sugg)
手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'code': '', 'msg': '', } def unlock_sogou_callback_example(url, req, resp, img, identify_image_callback): """手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'code': '', 'msg': '', } """ # no use resp url_quote = url.split('weixin.sogou.com/')[-1] unlock_url = 'http://weixin.sogou.com/antispider/thank.php' data = { 'c': identify_image_callback(img), 'r': '%2F' + url_quote, 'v': 5 } headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': 'http://weixin.sogou.com/antispider/?from=%2f' + url_quote } r_unlock = req.post(unlock_url, data, headers=headers) r_unlock.encoding = 'utf-8' if not r_unlock.ok: raise WechatSogouVcodeOcrException( 'unlock[{}] failed: {}'.format(unlock_url, r_unlock.text, r_unlock.status_code)) return r_unlock.json()
手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'ret': '', 'errmsg': '', 'cookie_count': '', } def unlock_weixin_callback_example(url, req, resp, img, identify_image_callback): """手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'ret': '', 'errmsg': '', 'cookie_count': '', } """ # no use resp unlock_url = 'https://mp.weixin.qq.com/mp/verifycode' data = { 'cert': time.time() * 1000, 'input': identify_image_callback(img) } headers = { 'Host': 'mp.weixin.qq.com', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': url } r_unlock = req.post(unlock_url, data, headers=headers) if not r_unlock.ok: raise WechatSogouVcodeOcrException( 'unlock[{}] failed: {}[{}]'.format(unlock_url, r_unlock.text, r_unlock.status_code)) return r_unlock.json()
拼接搜索 文章 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 默认是 anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 默认是 all ft, et : datetime.date 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 Returns ------- str search_article_url def gen_search_article_url(keyword, page=1, timesn=WechatSogouConst.search_article_time.anytime, article_type=WechatSogouConst.search_article_type.all, ft=None, et=None): """拼接搜索 文章 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 默认是 anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 默认是 all ft, et : datetime.date 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 Returns ------- str search_article_url """ assert isinstance(page, int) and page > 0 assert timesn in [WechatSogouConst.search_article_time.anytime, WechatSogouConst.search_article_time.day, WechatSogouConst.search_article_time.week, WechatSogouConst.search_article_time.month, WechatSogouConst.search_article_time.year, WechatSogouConst.search_article_time.specific] if timesn == WechatSogouConst.search_article_time.specific: assert isinstance(ft, datetime.date) assert isinstance(et, datetime.date) assert ft <= et else: ft = '' et = '' interation_image = 458754 interation_video = 458756 if article_type == WechatSogouConst.search_article_type.rich: interation = '{},{}'.format(interation_image, interation_video) elif article_type == WechatSogouConst.search_article_type.image: interation = interation_image elif article_type == WechatSogouConst.search_article_type.video: interation = interation_video else: interation = '' qs_dict = OrderedDict() qs_dict['type'] = _search_type_article qs_dict['page'] = page qs_dict['ie'] = 'utf8' qs_dict['query'] = keyword qs_dict['interation'] = interation if timesn != 0: qs_dict['tsn'] = timesn qs_dict['ft'] = str(ft) qs_dict['et'] = str(et) # TODO 账号内搜索 # '账号内 http://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E9%AB%98%E8%80%83&tsn=3&ft=&et=&interation=458754 # &wxid=oIWsFt1tmWoG6vO6BcsS7St61bRE&usip=nanhangqinggong' # qs['wxid'] = wxid # qs['usip'] = usip return 'http://weixin.sogou.com/weixin?{}'.format(urlencode(qs_dict))
拼接搜索 公众号 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 Returns ------- str search_gzh_url def gen_search_gzh_url(keyword, page=1): """拼接搜索 公众号 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 Returns ------- str search_gzh_url """ assert isinstance(page, int) and page > 0 qs_dict = OrderedDict() qs_dict['type'] = _search_type_gzh qs_dict['page'] = page qs_dict['ie'] = 'utf8' qs_dict['query'] = keyword return 'http://weixin.sogou.com/weixin?{}'.format(urlencode(qs_dict))
拼接 首页热门文章 URL Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- str 热门文章分类的url def gen_hot_url(hot_index, page=1): """拼接 首页热门文章 URL Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- str 热门文章分类的url """ assert hasattr(WechatSogouConst.hot_index, hot_index) assert isinstance(page, int) and page > 0 index_urls = { WechatSogouConst.hot_index.hot: 0, # 热门 WechatSogouConst.hot_index.gaoxiao: 1, # 搞笑 WechatSogouConst.hot_index.health: 2, # 养生 WechatSogouConst.hot_index.sifanghua: 3, # 私房话 WechatSogouConst.hot_index.gossip: 4, # 八卦 WechatSogouConst.hot_index.technology: 5, # 科技 WechatSogouConst.hot_index.finance: 6, # 财经 WechatSogouConst.hot_index.car: 7, # 汽车 WechatSogouConst.hot_index.life: 8, # 生活 WechatSogouConst.hot_index.fashion: 9, # 时尚 WechatSogouConst.hot_index.mummy: 10, # 辣妈 / 育儿 WechatSogouConst.hot_index.travel: 11, # 旅行 WechatSogouConst.hot_index.job: 12, # 职场 WechatSogouConst.hot_index.food: 13, # 美食 WechatSogouConst.hot_index.history: 14, # 历史 WechatSogouConst.hot_index.study: 15, # 学霸 / 教育 WechatSogouConst.hot_index.constellation: 16, # 星座 WechatSogouConst.hot_index.sport: 17, # 体育 WechatSogouConst.hot_index.military: 18, # 军事 WechatSogouConst.hot_index.game: 19, # 游戏 WechatSogouConst.hot_index.pet: 20, # 萌宠 } return 'http://weixin.sogou.com/wapindex/wap/0612/wap_{}/{}.html'.format(index_urls[hot_index], page - 1)
抽取lxml.etree库中elem对象中文字 Args: element: lxml.etree.Element sub: str Returns: elem中文字 def get_first_of_element(element, sub, contype=None): """抽取lxml.etree库中elem对象中文字 Args: element: lxml.etree.Element sub: str Returns: elem中文字 """ content = element.xpath(sub) return list_or_empty(content, contype)
获取requests库get或post返回的对象编码 Args: r: requests库get或post返回的对象 Returns: 对象编码 def get_encoding_from_reponse(r): """获取requests库get或post返回的对象编码 Args: r: requests库get或post返回的对象 Returns: 对象编码 """ encoding = requests.utils.get_encodings_from_content(r.text) return encoding[0] if encoding else requests.utils.get_encoding_from_headers(r.headers)
替换html‘&quot;’等转义内容为正常内容 Args: s: 文字内容 Returns: s: 处理反转义后的文字 def _replace_str_html(s): """替换html‘&quot;’等转义内容为正常内容 Args: s: 文字内容 Returns: s: 处理反转义后的文字 """ html_str_list = [ ('&#39;', '\''), ('&quot;', '"'), ('&amp;', '&'), ('&yen;', '¥'), ('amp;', ''), ('&lt;', '<'), ('&gt;', '>'), ('&nbsp;', ' '), ('\\', '') ] for i in html_str_list: s = s.replace(i[0], i[1]) return s
从搜索公众号获得的文本 提取公众号信息 Parameters ---------- text : str or unicode 搜索公众号获得的文本 Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'view_perm': '', # 最近一月阅读量 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } def get_gzh_by_search(text): """从搜索公众号获得的文本 提取公众号信息 Parameters ---------- text : str or unicode 搜索公众号获得的文本 Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'view_perm': '', # 最近一月阅读量 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } """ post_view_perms = WechatSogouStructuring.__get_post_view_perm(text) page = etree.HTML(text) lis = page.xpath('//ul[@class="news-list2"]/li') relist = [] for li in lis: url = get_first_of_element(li, 'div/div[1]/a/@href') headimage = format_image_url(get_first_of_element(li, 'div/div[1]/a/img/@src')) wechat_name = get_elem_text(get_first_of_element(li, 'div/div[2]/p[1]')) info = get_elem_text(get_first_of_element(li, 'div/div[2]/p[2]')) qrcode = get_first_of_element(li, 'div/div[3]/span/img[1]/@src') introduction = get_elem_text(get_first_of_element(li, 'dl[1]/dd')) authentication = get_first_of_element(li, 'dl[2]/dd/text()') relist.append({ 'open_id': headimage.split('/')[-1], 'profile_url': url, 'headimage': headimage, 'wechat_name': wechat_name.replace('red_beg', '').replace('red_end', ''), 'wechat_id': info.replace('微信号:', ''), 'qrcode': qrcode, 'introduction': introduction.replace('red_beg', '').replace('red_end', ''), 'authentication': authentication, 'post_perm': -1, 'view_perm': -1, }) if post_view_perms: for i in relist: if i['open_id'] in post_view_perms: post_view_perm = post_view_perms[i['open_id']].split(',') if len(post_view_perm) == 2: i['post_perm'] = int(post_view_perm[0]) i['view_perm'] = int(post_view_perm[1]) return relist
从搜索文章获得的文本 提取章列表信息 Parameters ---------- text : str or unicode 搜索文章获得的文本 Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } def get_article_by_search(text): """从搜索文章获得的文本 提取章列表信息 Parameters ---------- text : str or unicode 搜索文章获得的文本 Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } """ page = etree.HTML(text) lis = page.xpath('//ul[@class="news-list"]/li') articles = [] for li in lis: url = get_first_of_element(li, 'div[1]/a/@href') if url: title = get_first_of_element(li, 'div[2]/h3/a') imgs = li.xpath('div[1]/a/img/@src') abstract = get_first_of_element(li, 'div[2]/p') time = get_first_of_element(li, 'div[2]/div/span/script/text()') gzh_info = li.xpath('div[2]/div/a')[0] else: url = get_first_of_element(li, 'div/h3/a/@href') title = get_first_of_element(li, 'div/h3/a') imgs = [] spans = li.xpath('div/div[1]/a') for span in spans: img = span.xpath('span/img/@src') if img: imgs.append(img) abstract = get_first_of_element(li, 'div/p') time = get_first_of_element(li, 'div/div[2]/span/script/text()') gzh_info = li.xpath('div/div[2]/a')[0] if title is not None: title = get_elem_text(title).replace("red_beg", "").replace("red_end", "") if abstract is not None: abstract = get_elem_text(abstract).replace("red_beg", "").replace("red_end", "") time = re.findall('timeConvert\(\'(.*?)\'\)', time) time = list_or_empty(time, int) profile_url = get_first_of_element(gzh_info, '@href') headimage = get_first_of_element(gzh_info, '@data-headimage') wechat_name = get_first_of_element(gzh_info, 'text()') gzh_isv = get_first_of_element(gzh_info, '@data-isv', int) articles.append({ 'article': { 'title': title, 'url': url, 'imgs': format_image_url(imgs), 'abstract': abstract, 'time': time }, 'gzh': { 'profile_url': profile_url, 'headimage': headimage, 'wechat_name': wechat_name, 'isv': gzh_isv, } }) return articles
从 历史消息页的文本 提取公众号信息 Parameters ---------- text : str or unicode 历史消息页的文本 Returns ------- dict { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 } def get_gzh_info_by_history(text): """从 历史消息页的文本 提取公众号信息 Parameters ---------- text : str or unicode 历史消息页的文本 Returns ------- dict { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 } """ page = etree.HTML(text) profile_area = get_first_of_element(page, '//div[@class="profile_info_area"]') profile_img = get_first_of_element(profile_area, 'div[1]/span/img/@src') profile_name = get_first_of_element(profile_area, 'div[1]/div/strong/text()') profile_wechat_id = get_first_of_element(profile_area, 'div[1]/div/p/text()') profile_desc = get_first_of_element(profile_area, 'ul/li[1]/div/text()') profile_principal = get_first_of_element(profile_area, 'ul/li[2]/div/text()') return { 'wechat_name': profile_name.strip(), 'wechat_id': profile_wechat_id.replace('微信号: ', '').strip('\n'), 'introduction': profile_desc, 'authentication': profile_principal, 'headimage': profile_img }
从 历史消息页的文本 提取文章列表信息 Parameters ---------- text : str or unicode 历史消息页的文本 article_json : dict 历史消息页的文本 提取出来的文章json dict Returns ------- list[dict] { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 } def get_article_by_history_json(text, article_json=None): """从 历史消息页的文本 提取文章列表信息 Parameters ---------- text : str or unicode 历史消息页的文本 article_json : dict 历史消息页的文本 提取出来的文章json dict Returns ------- list[dict] { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 } """ if article_json is None: article_json = find_article_json_re.findall(text) if not article_json: return [] article_json = article_json[0] + '}}]}' article_json = json.loads(article_json) items = list() for listdic in article_json['list']: if str(listdic['comm_msg_info'].get('type', '')) != '49': continue comm_msg_info = listdic['comm_msg_info'] app_msg_ext_info = listdic['app_msg_ext_info'] send_id = comm_msg_info.get('id', '') msg_datetime = comm_msg_info.get('datetime', '') msg_type = str(comm_msg_info.get('type', '')) items.append({ 'send_id': send_id, 'datetime': msg_datetime, 'type': msg_type, 'main': 1, 'title': app_msg_ext_info.get('title', ''), 'abstract': app_msg_ext_info.get('digest', ''), 'fileid': app_msg_ext_info.get('fileid', ''), 'content_url': WechatSogouStructuring.__handle_content_url(app_msg_ext_info.get('content_url')), 'source_url': app_msg_ext_info.get('source_url', ''), 'cover': app_msg_ext_info.get('cover', ''), 'author': app_msg_ext_info.get('author', ''), 'copyright_stat': app_msg_ext_info.get('copyright_stat', '') }) if app_msg_ext_info.get('is_multi', 0) == 1: for multi_dict in app_msg_ext_info['multi_app_msg_item_list']: items.append({ 'send_id': send_id, 'datetime': msg_datetime, 'type': msg_type, 'main': 0, 'title': multi_dict.get('title', ''), 'abstract': multi_dict.get('digest', ''), 'fileid': multi_dict.get('fileid', ''), 'content_url': WechatSogouStructuring.__handle_content_url(multi_dict.get('content_url')), 'source_url': multi_dict.get('source_url', ''), 'cover': multi_dict.get('cover', ''), 'author': multi_dict.get('author', ''), 'copyright_stat': multi_dict.get('copyright_stat', '') }) return list(filter(lambda x: x['content_url'], items))
从 首页热门搜索 提取公众号信息 和 文章列表信息 Parameters ---------- text : str or unicode 首页热门搜索 页 中 某一页 的文本 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } def get_gzh_article_by_hot(text): """从 首页热门搜索 提取公众号信息 和 文章列表信息 Parameters ---------- text : str or unicode 首页热门搜索 页 中 某一页 的文本 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } """ page = etree.HTML(text) lis = page.xpath('/html/body/li') gzh_article_list = [] for li in lis: url = get_first_of_element(li, 'div[1]/h4/a/@href') title = get_first_of_element(li, 'div[1]/h4/a/div/text()') abstract = get_first_of_element(li, 'div[1]/p[1]/text()') xpath_time = get_first_of_element(li, 'div[1]/p[2]') open_id = get_first_of_element(xpath_time, 'span/@data-openid') headimage = get_first_of_element(xpath_time, 'span/@data-headimage') gzh_name = get_first_of_element(xpath_time, 'span/text()') send_time = xpath_time.xpath('a/span/@data-lastmodified') main_img = get_first_of_element(li, 'div[2]/a/img/@src') try: send_time = int(send_time[0]) except ValueError: send_time = send_time[0] gzh_article_list.append({ 'gzh': { 'headimage': headimage, 'wechat_name': gzh_name, }, 'article': { 'url': url, 'title': title, 'abstract': abstract, 'time': send_time, 'open_id': open_id, 'main_img': main_img } }) return gzh_article_list
根据微信文章的临时链接获取明细 1. 获取文本中所有的图片链接列表 2. 获取微信文章的html内容页面(去除标题等信息) Parameters ---------- text : str or unicode 一篇微信文章的文本 del_qqmusic: bool 删除文章中的qq音乐 del_voice: bool 删除文章中的语音内容 Returns ------- dict { 'content_html': str # 微信文本内容 'content_img_list': list[img_url1, img_url2, ...] # 微信文本中图片列表 } def get_article_detail(text, del_qqmusic=True, del_voice=True): """根据微信文章的临时链接获取明细 1. 获取文本中所有的图片链接列表 2. 获取微信文章的html内容页面(去除标题等信息) Parameters ---------- text : str or unicode 一篇微信文章的文本 del_qqmusic: bool 删除文章中的qq音乐 del_voice: bool 删除文章中的语音内容 Returns ------- dict { 'content_html': str # 微信文本内容 'content_img_list': list[img_url1, img_url2, ...] # 微信文本中图片列表 } """ # 1. 获取微信文本content html_obj = BeautifulSoup(text, "lxml") content_text = html_obj.find('div', {'class': 'rich_media_content', 'id': 'js_content'}) # 2. 删除部分标签 if del_qqmusic: qqmusic = content_text.find_all('qqmusic') or [] for music in qqmusic: music.parent.decompose() if del_voice: # voice是一个p标签下的mpvoice标签以及class为'js_audio_frame db'的span构成,所以将父标签删除 voices = content_text.find_all('mpvoice') or [] for voice in voices: voice.parent.decompose() # 3. 获取所有的图片 [img标签,和style中的background-image] all_img_set = set() all_img_element = content_text.find_all('img') or [] for ele in all_img_element: # 删除部分属性 img_url = format_image_url(ele.attrs['data-src']) del ele.attrs['data-src'] ele.attrs['src'] = img_url if not img_url.startswith('http'): raise WechatSogouException('img_url [{}] 不合法'.format(img_url)) all_img_set.add(img_url) backgroud_image = content_text.find_all(style=re.compile("background-image")) or [] for ele in backgroud_image: # 删除部分属性 if ele.attrs.get('data-src'): del ele.attrs['data-src'] if ele.attrs.get('data-wxurl'): del ele.attrs['data-wxurl'] img_url = re.findall(backgroud_image_p, str(ele)) if not img_url: continue all_img_set.add(img_url[0]) # 4. 处理iframe all_img_element = content_text.find_all('iframe') or [] for ele in all_img_element: # 删除部分属性 img_url = ele.attrs['data-src'] del ele.attrs['data-src'] ele.attrs['src'] = img_url # 5. 返回数据 all_img_list = list(all_img_set) content_html = content_text.prettify() # 去除div[id=js_content] content_html = re.findall(js_content, content_html)[0][0] return { 'content_html': content_html, 'content_img_list': all_img_list }