Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_impacted_price_worse_than_limit(self):
model = VolatilityVolumeShare(volume_limit=0.05)
# Use all the same numbers from the 'calculate_impact' tests. Since the
# impacted price is 59805.5, which is worse than the limit price of
# 59800, the model should return None.
minute = pd.Timestamp('2006-03-01 11:35AM', tz='UTC')
data = self.create_bardata(simulation_dt_func=lambda: minute)
order = Order(
dt=data.current_dt, asset=self.ASSET, amount=10, limit=59800,
)
price, amount = model.process_order(data, order)
self.assertIsNone(price)
self.assertIsNone(amount)
def test_orders_limit(self):
slippage_model = VolumeShareSlippage()
slippage_model.data_portal = self.data_portal
# long, does not trade
open_orders = [
Order(**{
'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc),
'amount': 100,
'filled': 0,
'asset': self.ASSET133,
'limit': 3.5})
]
bar_data = self.create_bardata(
simulation_dt_func=lambda: self.minutes[3],
)
orders_txns = list(slippage_model.simulate(
bar_data,
self.ASSET133,
open_orders,
))
def _calculate_impact(self, test_order, answer_key):
model = VolatilityVolumeShare(volume_limit=0.05)
first_minute = pd.Timestamp('2006-03-31 11:35AM', tz='UTC')
next_3_minutes = self.trading_calendar.minutes_window(first_minute, 3)
remaining_shares = test_order.open_amount
for i, minute in enumerate(next_3_minutes):
data = self.create_bardata(simulation_dt_func=lambda: minute)
new_order = Order(
dt=data.current_dt, asset=self.ASSET, amount=remaining_shares,
)
price, amount = model.process_order(data, new_order)
self.assertEqual(price, answer_key[i][0])
self.assertEqual(amount, answer_key[i][1])
amount = amount or 0
if remaining_shares < 0:
remaining_shares = min(0, remaining_shares - amount)
else:
remaining_shares = max(0, remaining_shares - amount)
def create_orders_dict(self, asset, last_order):
"""
create an orders dict which mocks the .orders object
:param asset: TradingPair
:param last_order: bool, adds another order to the dict.
mocks the functionality of the fetchOrder methods
:return: dict(Order)
"""
orders = dict()
orders['208612980769'] = Order(
dt=pd.to_datetime('2018-05-01 17:34', utc=True),
asset=asset,
amount=2,
stop=None,
limit=0.0025,
id='208612980769'
)
orders['656797594'] = Order(
dt=pd.to_datetime('2018-05-01 18:34', utc=True),
asset=asset,
amount=1,
stop=None,
limit=0.0027,
id='656797594'
)
orders['656797494'] = Order(
def test_orders_stop(self, name, order_data, event_data, expected):
data = order_data
data['asset'] = self.ASSET133
order = Order(**data)
if expected['transaction']:
expected['transaction']['asset'] = self.ASSET133
event_data['asset'] = self.ASSET133
assets = (
(133, pd.DataFrame(
{
'open': [event_data['open']],
'high': [event_data['high']],
'low': [event_data['low']],
'close': [event_data['close']],
'volume': [event_data['volume']],
},
index=[pd.Timestamp('2006-01-05 14:31', tz='UTC')],
)),
status = ORDER_STATUS.OPEN
if order_status['side'] == 'sell':
amount = -amount
filled = -filled
price = order_status['price']
order_type = order_status['type']
limit_price = price if order_type == 'limit' else None
executed_price = order_status['cost'] / order_status['amount']
commission = order_status['fee']
date = from_ms_timestamp(order_status['timestamp'])
order = Order(
dt=date,
asset=asset,
amount=amount,
stop=None,
limit=limit_price,
filled=filled,
id=order_id,
commission=commission
)
order.status = status
return order, executed_price
# if order_type.endswith('limit'):
# limit_price = price
# elif order_type.endswith('stop'):
# stop_price = price
# executed_price = float(order_status['avg_execution_price'])
executed_price = price
# TODO: Set Poloniex comission
commission = None
# date=pd.Timestamp.utcfromtimestamp(float(order_status['timestamp']))
# date=pytz.utc.localize(date)
date = None
order = Order(
dt=date,
asset=self.assets[order_status['symbol']],
# No such field in Poloniex
amount=amount,
stop=stop_price,
limit=limit_price,
filled=filled,
id=str(order_status['orderNumber']),
commission=commission
)
order.status = status
return order, executed_price
# TODO: is this comprehensive enough?
if order_type.endswith('limit'):
limit_price = price
elif order_type.endswith('stop'):
stop_price = price
executed_price = float(order_status['avg_execution_price'])
# TODO: bitfinex does not specify comission.
# I could calculate it but not sure if it's worth it.
commission = None
date = pd.Timestamp.utcfromtimestamp(float(order_status['timestamp']))
date = pytz.utc.localize(date)
order = Order(
dt=date,
asset=self.assets[order_status['symbol']],
amount=amount,
stop=stop_price,
limit=limit_price,
filled=filled,
id=str(order_status['id']),
commission=commission
)
order.status = status
return order, executed_price
def _create_order(self, order_status):
log.info(
'creating catalyst order from Bittrex {}'.format(order_status))
if order_status['CancelInitiated']:
status = ORDER_STATUS.CANCELLED
elif order_status['Closed'] is not None:
status = ORDER_STATUS.FILLED
else:
status = ORDER_STATUS.OPEN
date = pd.to_datetime(order_status['Opened'], utc=True)
amount = order_status['Quantity']
filled = amount - order_status['QuantityRemaining']
order = Order(
dt=date,
asset=self.assets[order_status['Exchange']],
amount=amount,
stop=None, # Not yet supported by Bittrex
limit=order_status['Limit'],
filled=filled,
id=order_status['OrderUuid'],
commission=order_status['CommissionPaid']
)
order.status = status
executed_price = order_status['PricePerUnit']
return order, executed_price