예제 #1
0
def test_broker_e2e():
    """Run through the base functionality of broker"""
    broker_inst = broker.Broker(nick="test_nick")
    broker_inst.checkout()
    assert len(broker_inst._hosts) == 1
    broker_host = broker_inst._hosts[0]
    assert broker_host.hostname == "test.host.example.com"
    broker_host_dict = broker_host.to_dict()
    assert broker_host_dict["_broker_provider"] == "TestProvider"
    broker_inst.checkin()
    assert len(broker_inst._hosts) == 0
예제 #2
0
def test_mp_checkout_twice():
    broker_inst = broker.Broker(nick="test_nick", _count=2)

    def cycle():
        assert len(broker_inst.checkout()) == 2
        assert len(broker_inst._hosts) == 2

        broker_inst.checkin()
        assert len(broker_inst._hosts) == 0

    cycle()
    cycle()
예제 #3
0
def test_mp_checkout():
    """Test that broker can checkout multiple hosts using multiprocessing"""
    VM_COUNT = 50  # This is intentionaly made high to catch run condition that
    # was discovered when reviewing
    # https://github.com/SatelliteQE/broker/pull/53
    # With count like this, I've got reproducibility probability
    # arround 0.5
    broker_inst = broker.Broker(nick="test_nick", _count=VM_COUNT)
    broker_inst.checkout()
    assert len(broker_inst._hosts) == VM_COUNT
    broker_inst.checkin()
    assert len(broker_inst._hosts) == 0
예제 #4
0
def test_full_init():
    """Make sure all init checks and assignments work"""
    broker_hosts = [
        "test1.example.com", "test2.example.com", "test3.example.com"
    ]
    broker_inst = broker.Broker(hosts=broker_hosts,
                                test_action="blank",
                                nick="test_nick")
    assert broker_inst._hosts == broker_hosts
    assert not broker_inst._kwargs.get("hosts")
    assert broker_inst._provider_actions == {
        "test_action": (test_provider.TestProvider, "test_action")
    }
    assert not broker_inst._kwargs.get("nick")
    assert broker_inst._kwargs["test_action"] == "blank"
예제 #5
0
    def __init__(self,
                 symbols: list,
                 startDate: str,
                 endDate: str,
                 dividendAdjustment: str,
                 strategy: stg.Strategy,
                 portfolio: pf.Portfolio,
                 freq: str = 'D',
                 commission: float = 7e-4,
                 slippage: float = 0.0015):
        self._strategy = strategy
        print("Strategy initialized.")
        self._portfolio = portfolio
        print("Portfolio initialized.")
        self.__initialValue = self._portfolio.check_value()

        self._dividendAdjustment = dividendAdjustment
        self._commission = commission
        self._slippage = slippage
        self._freq = freq
        self._symbols = symbols if isinstance(symbols, list) else [symbols]

        def date_test(value):
            patternDate = re.compile(r"\d{4}-?\d{2}-?\d{2}")
            if patternDate.match(value) is not None:
                if len(value) == 8:
                    value = '-'.join((value[0:4], value[4:6], value[6:8]))
                date = value
                return date
            else:
                raise ValueError(
                    "Input date should be in format 'yyyymmdd' or 'yyyy-mm-dd'"
                )

        self._startDate = date_test(startDate)
        self._endDate = date_test(endDate)

        if self._freq[0:1].upper() == 'D':
            self._quotation = qt.QuotationDailyBar(self._symbols,
                                                   self._startDate,
                                                   self._endDate,
                                                   self._dividendAdjustment)
            pushData = self._quotation.push_data()
        elif self._freq.startswith(("5", "15", "30", "60")):
            self._quotation = qt.QuotationMinuteBar(self._symbols,
                                                    self._startDate,
                                                    self._endDate,
                                                    self._dividendAdjustment,
                                                    self._freq)
            pushData = self._quotation.push_data()
        else:
            raise RuntimeError(
                "Now only support tick or 5/15/30/60 minutes or daily data.")
        print("Quotation is online.")

        self._broker = bk.Broker(symbols=self._symbols,
                                 startDate=self._startDate,
                                 endDate=self._endDate,
                                 freq=self._freq,
                                 data=pushData,
                                 commission=self._commission,
                                 slippage=self._slippage)
        print("Broker is online.")
        self._strategy.set_broker(self._broker)
        self._strategy.set_portfolio(self._portfolio)
        self.__pnl = []
예제 #6
0
def test_empty_init():
    """Broker should be able to init without any arguments"""
    broker_inst = broker.Broker()
    assert isinstance(broker_inst, broker.Broker)
예제 #7
0
def test_broker_empty_checkin():
    """Try to checkin with no hosts on the instance"""
    broker_inst = broker.Broker(nick="test_nick")
    assert not broker_inst._hosts
    broker_inst.checkin()
예제 #8
0
def test_kwarg_assignment():
    """Broker should copy all kwargs into its _kwargs attribute"""
    broker_kwargs = {"test": "value", "another": 17}
    broker_inst = broker.Broker(**broker_kwargs)
    assert broker_inst._kwargs == broker_kwargs