def test_intervals_low(self): value = 2 delta_type = "low" shortest = find_shortest_intervals(history_data=self.history, delta_type=delta_type, threshold=value) self.assertEqual(shortest, [{'delta': 4.46, 'start': datetime.datetime(2018, 5, 8, 0, 0), 'end': datetime.datetime(2018, 5, 9, 0, 0), 'length': 1}])
def test_negative_value_open(self): """ Предположительно, отрицательный value не имеет ввиду то, что ищем разницу "в обратную сторону", т.е. результат не подразумевает "меньше отрицательного", а всегда больше. """ value = -1 delta_type = "open" shortest = find_shortest_intervals(history_data=self.history, delta_type=delta_type, threshold=value) self.assertEqual(shortest, [ {'delta': 0, 'start': datetime.datetime(2018, 5, 4, 0, 0), 'end': datetime.datetime(2018, 5, 4, 0, 0), 'length': 0}, {'delta': 0, 'start': datetime.datetime(2018, 5, 7, 0, 0), 'end': datetime.datetime(2018, 5, 7, 0, 0), 'length': 0}, {'delta': 0, 'start': datetime.datetime(2018, 5, 8, 0, 0), 'end': datetime.datetime(2018, 5, 8, 0, 0), 'length': 0}, {'delta': 0, 'start': datetime.datetime(2018, 5, 9, 0, 0), 'end': datetime.datetime(2018, 5, 9, 0, 0), 'length': 0} ])
def delta(ticker_name): value = request.args.get("value") try: value = int(value) except ValueError: return bad_request(400, message="Недопустимый тип атрибута value") delta_type = request.args.get("type") ticker = Ticker.query.filter_by(name=ticker_name).first() history_data = History.query.filter_by(ticker_id=ticker.id).order_by( History.date.asc()).all() if not hasattr(history_data[0], delta_type): return bad_request(400, message=f"Недопустимый атрибут {delta_type}") shortest_intervals = find_shortest_intervals(history_data=history_data, delta_type=delta_type, threshold=value) return jsonify(intervals=shortest_intervals)
def delta(ticker_name): value = request.args.get("value") try: value = int(value) except ValueError: flash("Недопустимый тип атрибута value") return redirect(url_for("ticker", ticker_name=ticker_name)) delta_type = request.args.get("type") ticker = get_or_404(Ticker, name=ticker_name) history_data = History.query.filter_by(ticker_id=ticker.id).order_by( History.date.asc()).all() if not hasattr(history_data[0], delta_type): flash(f"Недопустимый атрибут {delta_type}") return redirect(url_for("ticker", ticker_name=ticker_name)) shortest_intervals = find_shortest_intervals(history_data=history_data, delta_type=delta_type, threshold=value) delta_form = DeltaForm() if delta_form.validate_on_submit(): delta_val = delta_form.value.data delta_type = delta_form.type.data return redirect( url_for("delta", ticker_name=ticker_name, value=delta_val, type=delta_type)) return render_template("intervals.html", ticker=ticker, value=value, type=delta_type, intervals=shortest_intervals, delta_form=delta_form)
def test_zero_intervals_open(self): value = 10 delta_type = "open" shortest = find_shortest_intervals(history_data=self.history, delta_type=delta_type, threshold=value) self.assertEqual(shortest, [])