Ejemplo n.º 1
0
def test_bad_logout():
    """logout without logging in"""
    if not LOGIN_OK:
        pytest.xfail("cannot test without valid user/passwd")
    rh_obj = Robinhood()
    with pytest.warns(UserWarning):
        req = rh_obj.logout()

    assert req.status_code != 200
Ejemplo n.º 2
0
def test_logout(config=CONFIG):
    """make sure logout works"""
    if not LOGIN_OK:
        pytest.xfail("cannot test without valid user/passwd")
    rh_obj = Robinhood()
    assert rh_obj.login(
        username=config.get("LOGIN", "username"),
        password=config.get("LOGIN", "password"),
    )
    assert rh_obj.auth_token is not None
    req = rh_obj.logout()

    assert req.status_code == 200
Ejemplo n.º 3
0
            source = cls.yfinance

        data_dict = {}
        for key, value in stocks_dict.items():
            if len(value) < 2:
                continue
            if isinstance(value, list):
                value = [
                    s for s in value if isinstance(s, int)
                    or isinstance(s, float) or s.isdigit()
                ]
            if isinstance(value, str):
                value = [
                    v.strip() for v in value.split(',') if v.strip().isdigit()
                ]
            try:
                if result := source(stock_ticker=key,
                                    stock_max=float(max(value)),
                                    stock_min=float(min(value))):
                    data_dict[key] = result
            except InvalidTickerSymbol:
                print(
                    f"\033[31m{prefix(level='ERROR')}"
                    f"Faced an InvalidTickerSymbol with the Ticker::{key}\033[00m"
                )

        if rh.auth_token:
            rh.logout()

        return data_dict
class PyrhAdapter(QSM):
    def __init__(self, name: str = 'pyrh_adapter'):
        super().__init__(name, ['pyrh_request', 'trade'])
        self.rbn = Robinhood()
        self.logged_in = False
        self.client_req = Queue()
        self.requests = Queue()
        self.request_lock = Lock()

    def setup_states(self):
        super().setup_states()
        self.mappings['login'] = self.login
        self.mappings['quote'] = self.quote
        self.mappings['buy'] = self.buy
        self.mappings['sell'] = self.sell

    def idle_state(self):
        try:
            req = self.client_req.get_nowait()
            self.append_state('pyrh_request', req)
        except Empty as _:
            pass

        super().idle_state()

    def trade_msg(self, msg: Message):
        transaction = msg.payload
        self.handler.send(
            Message('pyrh_request', 'buy' if transaction.buy else 'sell',
                    transaction))

    def pyrh_request_msg(self, msg: Message):
        if msg.msg == 'logout':
            if self.logged_in:
                self.rbn.logout()
                print("Logged out")
                self.logged_in = False
        if msg.msg == 'quote':
            if not self.logged_in:
                self.append_state('login')
            self.append_state('quote', msg.payload)
        elif msg.msg == 'buy':
            if not self.logged_in:
                self.append_state('login')
            self.append_state('buy', msg.payload)
        elif msg.msg == 'sell':
            if not self.logged_in:
                self.append_state('login')
            self.append_state('sell', msg.payload)

    def login(self):
        while True:
            user = input('Username(email): ')
            pwd = input('Password: '******'Logged in successfully')
                self.logged_in = True
                break
            print('Something went wrong, try again?')
            continue

    def quote(self, acronym: str):
        while True:
            try:
                self.requests.put(self.rbn.get_quote(acronym))
                break
            except Full as _:
                print('requests queue is full, skipping...')
                break
            except Exception as _:
                print('Something went wrong, trying again')
                time.sleep(5)

    def buy(self, descriptor: ManagedStock):
        # self.rbn.place_buy_order(self.rbn.get_quote(descriptor.acronym)['instrument'], descriptor.shares)
        print("Buying {}".format(descriptor))

    def sell(self, descriptor: ManagedStock):
        # self.rbn.place_sell_order(self.rbn.get_quote(descriptor.acronym)['instrument'], descriptor.shares)
        print("Selling {}".format(descriptor))

    def get_quote(self, acronym: str) -> dict:
        self.request_lock.acquire()
        self.client_req.put(Message('pyrh_request', 'quote', acronym))
        d = self.requests.get()
        self.request_lock.release()
        return d

    def place_buy(self, s: ManagedStock):
        self.client_req.put(Message('pyrh_request', 'buy', s))

    def place_sell(self, s: ManagedStock):
        self.client_req.put(Message('pyrh_request', 'sell', s))