def test_market_provider_market_from_epic(config):
    """
    Test the MarketProvider get_market_from_epic() function
    """
    # Define configuration for this test
    config["alpha_vantage"]["enable"] = True
    config["general"]["market_source"]["value"] = "list"
    config["general"]["epic_ids_filepath"] = "test/test_data/epics_list.txt"

    # Mock services and other components
    services = {
        "ig_index":
        MockIG(
            "test/test_data/mock_ig_market_info.json",
            "test/test_data/mock_ig_historic_price.json",
        ),
        "alpha_vantage":
        MockAV("test/test_data/mock_macdext_buy.json"),
    }
    broker = MockBroker(config, services)

    # Create class to test
    mp = MarketProvider(config, broker)
    market = mp.get_market_from_epic("MOCK")
    assert market is not None
    assert market.epic == "MOCK"
def test_market_provider_watchlist(config):
    """
    Test the MarketProvider configured to fetch markets from an IG watchlist
    """
    # Define configuration for this test
    config["alpha_vantage"]["enable"] = True
    config["general"]["market_source"]["value"] = "watchlist"
    config["general"]["watchlist_name"] = "mock"

    # Mock services and other components
    services = {
        "ig_index":
        MockIG(
            "test/test_data/mock_ig_market_info.json",
            "test/test_data/mock_ig_historic_price.json",
        ),
        "alpha_vantage":
        MockAV("test/test_data/mock_macdext_buy.json"),
    }
    broker = MockBroker(config, services)

    # Create class to test
    mp = MarketProvider(config, broker)

    # The MockBroker is configured to return a mock watchlist
    # Run the test several times resetting the market provider
    for _ in range(4):
        assert mp.next().epic == "EPIC1"
        assert mp.next().epic == "EPIC2"
        assert mp.next().epic == "EPIC3"
        assert mp.next().epic == "EPIC4"

        with pytest.raises(StopIteration) as e:
            mp.next()
        mp.reset()
Example #3
0
def test_find_trade_signal_buy(config):
    services = {
        "ig_index":
        MockIG(
            "test/test_data/mock_ig_market_info.json",
            "test/test_data/mock_ig_historic_price.json",
        ),
        "alpha_vantage":
        MockAV("test/test_data/mock_macdext_buy.json"),  # BUY json
    }
    broker = MockBroker(config, services)
    strategy = SimpleMACD(config, broker)
    prices = broker.get_prices("", "", "", "")

    # Create a mock market data from the json file
    market = create_mock_market(broker)

    # Call function to test
    tradeDir, limit, stop = strategy.find_trade_signal(market, prices)

    assert tradeDir is not None
    assert limit is not None
    assert stop is not None

    assert tradeDir == TradeDirection.BUY
def test_market_provider_epics_list(config):
    """
    Test the MarketProvider configured to fetch markets from an epics list
    """
    # Define configuration for this test
    config["alpha_vantage"]["enable"] = True
    config["general"]["market_source"]["value"] = "list"
    config["general"]["epic_ids_filepath"] = "test/test_data/epics_list.txt"

    # Mock services and other components
    services = {
        "ig_index":
        MockIG(
            "test/test_data/mock_ig_market_info.json",
            "test/test_data/mock_ig_historic_price.json",
        ),
        "alpha_vantage":
        MockAV("test/test_data/mock_macdext_buy.json"),
    }
    broker = MockBroker(config, services)

    # Create class to test
    mp = MarketProvider(config, broker)

    # Request all markets and verify the MarketProvider return the correct ones
    # Run the test several times resetting the market provider
    for _ in range(4):
        expected_list = []
        with open("test/test_data/epics_list.txt", "r") as epics_list:
            for cnt, line in enumerate(epics_list):
                expected_list += [line.rstrip()]

        n = mp.next().epic
        assert n in expected_list
        expected_list.remove(n)
        try:
            while n:
                n = mp.next().epic
                assert n in expected_list
                expected_list.remove(n)
        except StopIteration:
            # Verify we read all epics in the list
            assert len(expected_list) == 0
            # Verify reading the next raise another exception
            with pytest.raises(StopIteration) as e:
                mp.next()
            mp.reset()
            continue
        # If we get here it means that next did not raise an exception at the end of the list
        assert False
Example #5
0
def strategy(config):
    """
    Initialise the strategy with mock services
    """
    services = {
        "ig_index":
        MockIG(
            "test/test_data/mock_ig_market_info.json",
            "test/test_data/mock_ig_historic_price.json",
        ),
        "alpha_vantage":
        MockAV("test/test_data/mock_macdext_buy.json"),
    }
    broker = MockBroker(config, services)
    return SimpleMACD(config, broker)
def test_find_trade_signal(config):
    services = {
        "ig_index":
        MockIG(
            "test/test_data/mock_ig_market_info.json",
            "test/test_data/mock_ig_historic_price.json",
        ),
        "alpha_vantage":
        MockAV("test/test_data/mock_av_weekly.json"),
    }
    broker = MockBroker(config, services)
    strategy = WeightedAvgPeak(config, broker)
    prices = broker.get_prices("", "", "", "")

    market = create_mock_market(broker)

    tradeDir, limit, stop = strategy.find_trade_signal(market, prices)

    assert tradeDir is not None
    assert limit is None
    assert stop is None

    assert tradeDir == TradeDirection.NONE
def test_market_provider_api(config):
    """
    Test the MarketProvider configured to fetch markets from IG nodes
    """
    # Define configuration for this test
    config["alpha_vantage"]["enable"] = True
    config["general"]["market_source"]["value"] = "api"

    # Mock services and other components
    services = {
        "ig_index":
        MockIG(
            "test/test_data/mock_ig_market_info.json",
            "test/test_data/mock_ig_historic_price.json",
        ),
        "alpha_vantage":
        MockAV("test/test_data/mock_macdext_buy.json"),
    }
    broker = MockBroker(config, services)

    # TODO
    # Create class to test
    #mp = MarketProvider(config, broker)
    assert True