Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@expect_types(asset=Asset)
def __init__(self, asset):
self.asset = asset
self.amount = 0
self.cost_basis = 0.0 # per share
self.last_sale_price = 0.0
self.last_sale_date = None
@expect_types(halflife=Number)
def from_halflife(cls, inputs, window_length, halflife, **kwargs):
"""
Convenience constructor for passing ``decay_rate`` in terms of half
life.
Forwards ``decay_rate`` as ``exp(log(.5) / halflife)``. This provides
the behavior equivalent to passing `halflife` to pandas.ewma.
Examples
--------
.. code-block:: python
# Equivalent to:
# my_ewma = EWMA(
# inputs=[USEquityPricing.close],
# window_length=30,
@expect_types(asset=Asset)
def create_txn_history(asset, priceList, amtList, interval, sim_params,
trading_calendar):
txns = []
current = sim_params.first_open
for price, amount in zip(priceList, amtList):
dt = get_next_trading_dt(current, interval, trading_calendar)
txns.append(create_txn(asset, price, amount, dt, None))
current = current + interval
return txns
@expect_types(
values=np.ndarray,
missing_value=SUPPORTED_SCALAR_TYPES,
categories=optional(list),
)
@expect_kinds(values=("O", "S", "U"))
def __new__(cls,
values,
missing_value,
categories=None,
sort=True):
# Numpy's fixed-width string types aren't very efficient. Working with
# object arrays is faster than bytes or unicode arrays in almost all
# cases.
if not is_object(values):
values = values.astype(object)
@expect_types(asset=Asset)
def adjust_commission_cost_basis(self, asset, cost):
"""
A note about cost-basis in catalyst: all positions are considered
to share a cost basis, even if they were executed in different
transactions with different commission costs, different prices, etc.
Due to limitations about how catalyst handles positions, catalyst will
currently spread an externally-delivered commission charge across
all shares in a position.
"""
if asset != self.asset:
raise Exception('Updating a commission for a different asset?')
if cost == 0.0:
return
@expect_types(asset=Asset)
def order(self, asset, amount, style, order_id=None):
"""Place an order.
Parameters
----------
asset : catalyst.assets.Asset
The asset that this order is for.
amount : int
The amount of assets to order. If ``amount`` is positive, this is
the number of assets to buy or cover. If ``amount`` is negative,
this is the number of assets to sell.
style : catalyst.finance.execution.ExecutionStyle
The execution style for the order.
order_id : str, optional
The unique identifier for this order.
@expect_types(asset=Asset)
def create_txn(asset, price, amount, datetime, order_id):
return Transaction(
asset=asset,
price=price,
amount=amount,
dt=datetime,
order_id=order_id,
)
@expect_types(asset=Asset)
def handle_commission(self, asset, cost):
# Adjust the cost basis of the stock if we own it
if asset in self.positions:
self.positions[asset].adjust_commission_cost_basis(asset, cost)
@expect_types(prefix=(bytes, unicode))
def startswith(self, prefix):
"""
Construct a Filter matching values starting with ``prefix``.
Parameters
----------
prefix : str
String prefix against which to compare values produced by ``self``.
Returns
-------
matches : Filter
Filter returning True for all sid/date pairs for which ``self``
produces a string starting with ``prefix``.
"""
return ArrayPredicate(
@expect_types(tz=optional(tzinfo))
def get_datetime(self, tz=None):
"""
Returns the current simulation datetime.
Parameters
----------
tz : tzinfo or str, optional
The timezone to return the datetime in. This defaults to utc.
Returns
-------
dt : datetime
The current simulation datetime converted to ``tz``.
"""
dt = self.datetime
assert dt.tzinfo == pytz.utc, "Algorithm should have a utc datetime"