def handle_position(message): """Receive position data from the IB/Risk websocket connection, store the new positions, check for alerts, and send to the right user on the frontend""" logger.info(message) token, data, user = parse_message(message) if user: alerts = Alert.objects(user=user) original_number_alerts = alerts.count() positions = Position.create_positions(user, data) position_data_formatted = marshal(list(positions), position_schema) #logger.info([user, 'EMIT to', user.websocket_session_id]) emit('frontend_position', position_data_formatted, room=user.websocket_session_id) for alert in alerts: alert_event = alert.check() if alert_event: alerts = Alert.objects(user=user) alert_events = AlertEvent.objects(user=user) alert_data_formatted = marshal(list(alerts), alert_schema) alert_events_data_formatted = marshal(list(alert_events), alert_event_schema) alert_event_data_formatted = marshal(alert_event, alert_event_schema) emit('frontend_alerts', alert_data_formatted, room=user.websocket_session_id) emit('frontend_alert_events', alert_events_data_formatted, room=user.websocket_session_id) emit('frontend_alert_event', alert_event_data_formatted, room=user.websocket_session_id) alerts = Alert.objects(user=user) alert_data_formatted = marshal(list(alerts), alert_schema) if alerts.count() != original_number_alerts: emit('frontend_alerts', alert_data_formatted, room=user.websocket_session_id) return { "status": "success", "position": data, "token": token, }
def test_alert_closed_from_position_close(user): example_portfolio = ExamplePortfolio(user.trading_account_username) position_data = example_portfolio.data().get('data') Position.create_positions(user, position_data) open_positions = Position.objects(user=user, is_open=True) position = open_positions[0] alert = Alert.create(user, condition='greater than', value=100, field='price', expiration_date=None, alert_types=['sms'], message=None, position=position) assert Alert.objects(user=user, is_open=True).count() == 1 position.remove() alert.check_position_still_open() assert Alert.objects(user=user, is_open=True).count() == 0
def put(self, id): '''Update an alert''' alert = Alert.objects(user=g.user, id=id).first() position_id = request.json.get('position').get('id') position = Position.objects(id=position_id).first() alert.condition = request.json.get('condition') alert.value = float(request.json.get('value')) alert.field = request.json.get('field') alert.expiration_date = request.json.get('expiration_date') alert.alert_types = request.json.get('alert_types') alert.message = request.json.get('message') alert.position = position alert.save() return alert
def check_alerts(self): alerts = Alert.objects(user=self, is_open=True) for alert in alerts: alert.check()
def delete(self, id): '''Delete an alert''' alert = Alert.objects(user=g.user, id=id).first() alert.remove() return {'message': 'Deleted'}
def get(self, id): '''Retrieve an alert''' alert = Alert.objects(user=g.user, id=id).first() return alert
def get(self): '''Retrieve all alerts''' alerts = Alert.objects(user=g.user) return list(alerts)