예제 #1
0
def test_span_interval():
    assert Duration('1h').span_interval(Interval.open(3600, 2 * 3600),
                                        start_open=False) == span(
                                            3600, 2 * 3600, start_open=False)
    assert Duration('1h').span_interval(Interval.open(3600, 2 * 3600),
                                        start_open=True) == span(
                                            3600, 2 * 3600, start_open=True)
    assert Duration('1h').span_interval(Interval.closed(3600, 2 * 3600),
                                        start_open=False) == span(
                                            3600, 3 * 3600, start_open=False)
    assert Duration('1h').span_interval(Interval.closed(3600, 2 * 3600),
                                        start_open=True) == span(
                                            0, 2 * 3600, start_open=True)
예제 #2
0
def test_max():
    f = Curve.max([Points([(0, -1), (1, 0), (2, 1)]), 0])
    assert f.domain == Interval.closed(0, 2)
    assert f.y(-1) is None
    assert f.y(0) == 0
    assert f.y(1) == 0
    assert f.y(2) == 1
    assert f.y(3) is None

    f = Curve.max([0, Points([(0, -1), (1, 0), (2, 1)])])
    assert f.domain == Interval.closed(0, 2)
    assert f.y(-1) is None
    assert f.y(0) == 0
    assert f.y(1) == 0
    assert f.y(2) == 1
    assert f.y(3) is None
예제 #3
0
파일: points.py 프로젝트: diatche/curvepy
    def append_list(self, points):
        """
        `points` are assumed to be strictly ordered in ascending order w.r.t. to `x`.
        """
        points_len = len(self._points)
        if points_len == 0:
            return self.set(points)
        new_points_len = len(points)
        if new_points_len == 0:
            return

        if points[0][0] <= self._points[-1][0]:
            raise Exception(
                'Attempting to append points in non-ascending order')

        if self.domain.is_empty:
            domain = Interval.closed(points[0][0], points[-1][0])
        else:
            domain = Interval(self.domain.end,
                              points[-1][0],
                              start_open=True,
                              end_open=False)
        self.begin_update(domain)
        self._is_equally_spaced = self._points_equally_spaced(
            self._points, points)
        if self._force_equally_spaced and not self._is_equally_spaced:
            raise Exception(
                'Attempting to append points at non-regular intervals: {}{} + {}{}'
                .format('...' if len(self._points) > 2 else '',
                        self._points[len(self._points) - 2:], points[:2],
                        '...' if len(points) > 2 else ''))
        self._points += points
        self._did_change_points()
        self.end_update(domain)
예제 #4
0
def test_iterate_uniform():
    spans = list(
        Duration('1h').iterate(Interval.closed(2 * 3600, 4 * 3600),
                               start_open=False))
    assert len(spans) == 3
    assert spans[0] == span(2 * 3600, 3 * 3600, start_open=False)
    assert spans[1] == span(3 * 3600, 4 * 3600, start_open=False)
    assert spans[2] == span(4 * 3600, 5 * 3600, start_open=False)

    spans = list(
        Duration('1h').iterate(Interval.closed(2 * 3600, 4 * 3600),
                               backward=True,
                               start_open=False))
    assert len(spans) == 3
    assert spans[0] == span(4 * 3600, 5 * 3600, start_open=False)
    assert spans[1] == span(3 * 3600, 4 * 3600, start_open=False)
    assert spans[2] == span(2 * 3600, 3 * 3600, start_open=False)
예제 #5
0
def test_offset_close():
    ps = Quotes(1,
                quote_points=Quote.mock_ohlcv_points(
                    [(1, 2.2, 0.9, 2),
                     (2, 3.1, 1.9, 3)], t_step=1)).close.offset(10)
    assert ps.domain == Interval.closed(10, 11)
    assert ps(9) is None
    assert ps(10) == 2
    assert ps(11) == 3
    assert ps(12) is None
예제 #6
0
def test_sample_points():
    original_quotes_points = Quote.mock_ohlcv_points([(1, 2.2, 0.9, 2),
                                                      (2, 3.1, 1.9, 3),
                                                      (3, 5.1, 2.9, 5)],
                                                     t_start=0,
                                                     t_step=1)
    f = Quotes(1, quote_points=original_quotes_points)

    ps = f.sample_points()
    assert ps == original_quotes_points

    ps = f.sample_points(domain=Interval.open_closed(1, 2))
    assert ps == original_quotes_points[1:3]

    ps = f.sample_points(domain=Interval.closed(1, 2))
    assert ps == original_quotes_points[1:3]

    ps = f.sample_points(domain=Interval.closed(3, 4))
    assert ps == []

    ps = f.sample_points(domain=Interval.closed(-1, 0))
    assert ps == original_quotes_points[0:1]
예제 #7
0
파일: trend.py 프로젝트: diatche/curvepy
    def update_interval(self):
        len_trend_points = len(self.trend_points)
        if self._line is None:
            if len_trend_points == 1:
                self.search_interval = Interval.point(self.trend_points[0][0])
            else:
                self.search_interval = Interval.empty()
            return

        p0, p1 = self._line
        if p0[0] == p1[0]:
            # vertical line
            self.search_interval = Interval.point(p0[0])
            return

        if self.search_length_rel <= 0:
            self.search_interval = Interval.closed(p0[0], p1[0])
            return

        x_min = p0[0]
        x_max = p1[0]

        intersection = self.intersections[0] if len(
            self.intersections) != 0 else None
        if intersection is not None and intersection[0] <= x_min:
            intersection = None

        if intersection is None or len_trend_points >= self.min_points:
            # search ahead
            x_length = (x_max - x_min) * self.search_length_rel
        else:
            # not enought points to go through intersection
            x_length = intersection[0] - x_min

        # if intersection is None:
        #     # search ahead
        #     x_length = (x_max - x_min) * self.search_length_rel
        # elif len_trend_points >= self.min_points:
        #     # search ahead of intersection
        #     x_length = (intersection[0] - x_min) * self.search_length_int_rel
        # else:
        #     # not enought points to go through intersection
        #     x_length = intersection[0] - x_min

        x_max = x_min + x_length

        self.search_interval = Interval.closed_open(x_min, x_max)
예제 #8
0
def test_multiple_nested_quotes_update():
    duration = 1
    quotes = Quotes(duration)
    jaw = quotes.close
    teeth = quotes.close
    ave = (jaw * 13 + teeth * 8) / 21

    mock_quotes = Quote.mock_ohlcv_points(list(np.arange(1, 25)), t_step=1)
    for q in mock_quotes:
        quotes.append(q)
        assert jaw.is_updating is False
        assert teeth.is_updating is False
        assert ave.is_updating is False
        _ = ave.domain

    assert ave.domain == Interval.closed(0, 23)
    assert ave(0) == 1
예제 #9
0
def test_offset_close_update_from_empty():
    quotes = Quotes(1)
    ps = quotes.close.offset(10)
    assert ps.domain.is_empty

    mock_quotes = Quote.mock_ohlcv_points([(1, 2.2, 0.9, 2), (2, 3.1, 1.9, 3)],
                                          t_step=1)

    for q in mock_quotes:
        quotes.append(q)
        _ = ps.domain

    assert ps.domain == Interval.closed(10, 11)
    assert ps(9) is None
    assert ps(10) == 2
    assert ps(11) == 3
    assert ps(12) is None
예제 #10
0
def test_interval_indexes():
    points = Points([(0, 0), (1, 1), (2, 2), (3, 3)])

    i0, i1 = points._domain_indexes(Interval.closed(-2, -1))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(-2, 0))
    assert i0 == 0
    assert i1 == 1

    i0, i1 = points._domain_indexes(Interval.open(-2, 0))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(0, 0))
    assert i0 == 0
    assert i1 == 1

    i0, i1 = points._domain_indexes(Interval.open(0, 1))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(0, 1))
    assert i0 == 0
    assert i1 == 2

    i0, i1 = points._domain_indexes(Interval.closed(2, 3))
    assert i0 == 2
    assert i1 == 4

    i0, i1 = points._domain_indexes(Interval.open(2, 3))
    assert i0 == 3
    assert i1 == 3

    i0, i1 = points._domain_indexes(Interval.closed(3, 3))
    assert i0 == 3
    assert i1 == 4

    i0, i1 = points._domain_indexes(Interval.open(3, 3))
    assert i0 == i1

    i0, i1 = points._domain_indexes(Interval.closed(3, 4))
    assert i0 == 3
    assert i1 == 4

    i0, i1 = points._domain_indexes(Interval.open(3, 4))
    assert i0 == i1
예제 #11
0
파일: points.py 프로젝트: diatche/curvepy
 def set(self, points, update_domain=None):
     """
     `points` are assumed to be strictly ordered in ascending order w.r.t. to `x`.
     """
     if update_domain is None:
         points_len = len(points)
         if points_len == 0:
             update_domain = self.domain
         else:
             update_domain = Interval.union([
                 self.domain,
                 Interval.closed(points[0][0], points[-1][0])
             ])
     self.begin_update(update_domain)
     self._is_equally_spaced = self._points_equally_spaced([], points)
     if self._force_equally_spaced and not self._is_equally_spaced:
         raise Exception(
             'Attempting to set points at non-regular intervals')
     self._points = list(points)
     self._did_change_points()
     self.end_update(update_domain)
예제 #12
0
파일: sma.py 프로젝트: diatche/curvepy
 def get_domain(self):
     d = self.curve.domain
     if d.is_empty:
         return Interval.empty()
     if self.period is not None:
         x = self.curve.x_previous(d.start + self.period,
                                   min_step=self.min_step)
         if x is None:
             p = self.period
         else:
             p = x - d.start
     elif self.degree is not None:
         x = d.start
         for _ in range(self.degree - 1):
             x = self.curve.x_next(x, min_step=self.min_step)
             if x is None:
                 return Interval.empty()
         p = x - d.start
     else:
         raise Exception('Bad SMA configuration')
     d_start = d.start + p
     if math.isnan(d_start) or d_start > d.end:
         return Interval.empty()
     return Interval.closed(d_start, d.end)
예제 #13
0
파일: quote.py 프로젝트: diatche/curvepy
 def price_domain(self):
     if self._price_domain is None:
         self._price_domain = Interval.closed(self.low, self.high)
     return self._price_domain
예제 #14
0
def test_intersects():
    assert Interval.closed(1, 3).intersects(Interval.closed(2, 3))
    assert Interval.closed(1, 3).intersects((2, 3))
    assert Interval.closed(1, 3).intersects((1, 3))
    assert Interval.closed(1, 3).intersects(Interval.open(1, 3))

    assert Interval.closed(1, 3).intersects(Interval.closed(3, 4))
    assert not Interval.closed(1, 3).intersects(Interval.open(3, 4))
    assert not Interval.open(1, 3).intersects(Interval.closed(3, 4))

    assert Interval.point(3).intersects(Interval.closed(3, 4))
    assert Interval.point(3).intersects(Interval.closed(1, 3))
    assert not Interval.point(3).intersects(Interval.open(3, 4))
    assert not Interval.point(3).intersects(Interval.open(1, 3))

    assert Interval.closed(1, 3).intersects(Interval.closed(0, 1))
    assert not Interval.closed(1, 3).intersects(Interval.open(0, 1))
    assert not Interval.open(1, 3).intersects(Interval.closed(0, 1))

    assert not Interval.closed(1, 3).intersects(Interval.closed(4, 5))
    assert not Interval.closed(1, 3).intersects(Interval.closed(-2, 0))

    assert not Interval.closed(1, 3).intersects(Interval.empty())
    assert Interval.closed(1, 3).intersects(Interval.infinite())

    assert not Interval.point(1).intersects(Interval.open_closed(1, 2))
예제 #15
0
def test_reset_closed_interval():
    points = [(0, 1), (1, 5), (2, 3), (3, 4)]
    pf = Points(points, uniform=False)
    assert pf(1) == 5
    pf.reset(domain=Interval.closed(1, 2))
    assert pf(1) == 2