Ejemplo n.º 1
0
def test_retrieving_intermittently_cached_daily(tmpdir, provider):
    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir,)

    start_date = date(2020, 3, 3)
    end_date = date(2020, 3, 3)

    retriever.retrieve_bar_data(
        symbol="SPY",
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(days=1),
    )

    start_date = date(2020, 3, 5)
    end_date = date(2020, 3, 5)

    retriever.retrieve_bar_data(
        symbol="SPY",
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(days=1),
    )

    start_date = date(2020, 3, 2)
    end_date = date(2020, 3, 6)

    data = retriever.retrieve_bar_data(
        symbol="SPY",
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(days=1),
    )

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
Ejemplo n.º 2
0
def test_retrieving_intermittently_cached_intraday(tmpdir, provider):
    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir,)

    data = pd.DataFrame()
    dates = generate_trading_days(
        start_date=date.today() - timedelta(days=10),
        end_date=date.today() - timedelta(days=1),
    )
    date_ranges = [
        dates[1:2],
        dates[-3:-2],
        dates,
    ]
    for date_range in date_ranges:
        start_date = date_range[0]
        end_date = date_range[-1]

        data = retriever.retrieve_bar_data(
            symbol="SPY",
            start_date=start_date,
            end_date=end_date,
            bar_size=timedelta(days=1),
        )

    validate_data_range(data=data, start_date=dates[0], end_date=dates[-1])
Ejemplo n.º 3
0
def test_retrieving_intermittently_cached_intraday(tmpdir, provider):
    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir,)

    data = pd.DataFrame()
    dates = generate_trading_days(
        start_date=date.today() - timedelta(days=10),
        end_date=date.today() - timedelta(days=1),
    )
    date_ranges = [
        dates[1:2],
        dates[-3:-2],
        dates,
    ]
    for date_range in date_ranges:
        start_date = date_range[0]
        end_date = date_range[-1]

        contract = StockContract(symbol="SPY")

        try:
            data = retriever.retrieve_bar_data(
                contract=contract,
                start_date=start_date,
                end_date=end_date,
                bar_size=timedelta(days=1),
            )
        except NotImplementedError:  # todo: fix
            return

    validate_data_range(data=data, start_date=dates[0], end_date=dates[-1])
Ejemplo n.º 4
0
 def __init__(self, start_date: date, end_date: date, bar_size: timedelta):
     hist_retriever = HistoricalRetriever(hist_data_dir=TEST_DATA_DIR)
     self._sim_data = hist_retriever.retrieve_bar_data(
         symbol="SPY",
         bar_size=bar_size,
         start_date=start_date,
         end_date=end_date,
         cache_only=True,
     )
     self._sim_idx = 0
Ejemplo n.º 5
0
def test_retrieve_non_cached_daily(tmpdir, provider):
    start_date = date(2020, 4, 1)
    end_date = date(2020, 4, 2)

    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir,)
    data = retriever.retrieve_bar_data(
        symbol="SPY",
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(days=1),
    )

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
Ejemplo n.º 6
0
def test_retrieving_intermittently_cached_daily(tmpdir, provider):
    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir)

    start_date = date(2020, 3, 3)
    end_date = date(2020, 3, 3)

    contract = StockContract(symbol="SPY")

    try:
        retriever.retrieve_bar_data(
            contract=contract,
            start_date=start_date,
            end_date=end_date,
            bar_size=timedelta(days=1),
        )
    except NotImplementedError:  # todo: fix
        return

    start_date = date(2020, 3, 5)
    end_date = date(2020, 3, 5)

    retriever.retrieve_bar_data(
        contract=contract,
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(days=1),
    )

    start_date = date(2020, 3, 2)
    end_date = date(2020, 3, 6)

    data = retriever.retrieve_bar_data(
        contract=contract,
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(days=1),
    )

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
Ejemplo n.º 7
0
def test_retrieve_non_cached_intraday(tmpdir, provider):
    start_date = date.today() - timedelta(days=7)
    end_date = date.today() - timedelta(days=1)

    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir,)
    data = retriever.retrieve_bar_data(
        symbol="SPY",
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(minutes=1),
    )

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
    assert np.isclose(len(data), 5 * 6.5 * 60, atol=7 * 60)
Ejemplo n.º 8
0
def test_retriever_cached_daily():
    start_date = date(2020, 4, 1)
    end_date = date(2020, 4, 2)

    retriever = HistoricalRetriever(hist_data_dir=TEST_DATA_DIR)
    data = retriever.retrieve_bar_data(
        symbol="SPY",
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(days=1),
        cache_only=True,
    )

    assert len(data) != 0

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
Ejemplo n.º 9
0
def test_retriever_cached_tick():
    start_date = date(2020, 6, 17)
    end_date = date(2020, 6, 19)

    retriever = HistoricalRetriever(hist_data_dir=TEST_DATA_DIR)
    contract = StockContract(symbol="SCHW")
    data = retriever.retrieve_bar_data(
        contract=contract,
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(0),
        cache_only=True,
    )

    assert len(data) != 0

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
Ejemplo n.º 10
0
def test_retrieve_non_cached_daily(tmpdir, provider):
    start_date = date(2020, 4, 1)
    end_date = date(2020, 4, 2)

    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir,)
    contract = StockContract(symbol="SPY")

    try:
        data = retriever.retrieve_bar_data(
            contract=contract,
            start_date=start_date,
            end_date=end_date,
            bar_size=timedelta(days=1),
        )
    except NotImplementedError:
        return

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
Ejemplo n.º 11
0
def test_historical_bar_aggregator():
    start_date = date(2020, 4, 6)
    end_date = date(2020, 4, 7)

    retriever = HistoricalRetriever(hist_data_dir=TEST_DATA_DIR)
    contract = StockContract(symbol="SPY")
    base_data = retriever.retrieve_bar_data(
        contract=contract,
        start_date=start_date,
        end_date=end_date,
        bar_size=timedelta(minutes=1),
        cache_only=True,
    )

    aggregator = HistoricalAggregator(hist_data_dir=TEST_DATA_DIR)
    agg_data = aggregator.aggregate_data(
        contract=contract,
        start_date=start_date,
        end_date=end_date,
        base_bar_size=timedelta(minutes=1),
        target_bar_size=timedelta(minutes=5),
    )

    assert len(agg_data) == 156
    assert agg_data.iloc[0]["open"] == base_data.iloc[0]["open"]
    assert agg_data.iloc[0]["close"] == base_data.iloc[4]["close"]
    assert agg_data.iloc[0]["high"] == base_data.iloc[:5]["high"].max()
    assert agg_data.iloc[0]["low"] == base_data.iloc[:5]["low"].min()
    assert agg_data.iloc[0]["volume"] == base_data.iloc[:5]["volume"].sum()

    agg_data = aggregator.aggregate_data(
        contract=contract,
        start_date=start_date,
        end_date=end_date,
        base_bar_size=timedelta(minutes=1),
        target_bar_size=timedelta(minutes=10),
    )

    assert len(agg_data) == 78
Ejemplo n.º 12
0
def test_retrieve_non_cached_intraday(tmpdir, provider):
    start_date = date.today() - timedelta(days=7)
    end_date = date.today() - timedelta(days=1)

    retriever = HistoricalRetriever(provider=provider, hist_data_dir=tmpdir)
    contract = StockContract(symbol="SPY")

    try:
        data = retriever.retrieve_bar_data(
            contract=contract,
            start_date=start_date,
            end_date=end_date,
            bar_size=timedelta(minutes=1),
        )
    except NotImplementedError:
        return

    validate_data_range(data=data, start_date=start_date, end_date=end_date)
    assert (
        np.isclose(len(data), 5 * 6.5 * 60, atol=7 * 60)
        or len(data) == 4800  # for outside RTHs IB
    )
Ejemplo n.º 13
0
from datetime import timedelta, date

from algotradepy.historical.loaders import HistoricalRetriever
from algotradepy.historical.providers import YahooProvider

if __name__ == "__main__":
    provider = YahooProvider()
    hr = HistoricalRetriever(provider=provider)
    end_date = date.today()
    hr.retrieve_bar_data(
        symbol="SPY",
        bar_size=timedelta(days=1),
        start_date=date(2000, 1, 1),
        end_date=end_date,
    )