Esempio n. 1
0
def test_update_s_wrongCurrencyFormat():
    stack = Stack()

    stack.update_s("ETH:,USDT:12.1,BTC:11")

    assert stack._USDT == 12.1
    assert stack._ETH == 0
    assert stack._BTC == 11
Esempio n. 2
0
def test_update_s_not_a_float():
    stack = Stack()

    stack.update_s("ETH:a,USDT:42,BTC:12.1")

    assert stack._USDT == 42
    assert stack._ETH == 0
    assert stack._BTC == 12.1
Esempio n. 3
0
def test_update_s_wrongFormat():
    stack = Stack()

    stack.update_s("ETH;42,USDT;12.1,BTC;a")

    assert stack._USDT == 0
    assert stack._ETH == 0
    assert stack._BTC == 0
Esempio n. 4
0
def test_update_s_normalCase_other_order():
    stack = Stack()

    stack.update_s("ETH:21,USDT:42,BTC:12.1")

    assert stack._USDT == 42
    assert stack._ETH == 21
    assert stack._BTC == 12.1
Esempio n. 5
0
def test_update_s_normalCase():
    stack = Stack()

    stack.update_s("USDT:42,ETH:21,BTC:12.1")

    assert stack._USDT == 42
    assert stack._ETH == 21
    assert stack._BTC == 12.1
Esempio n. 6
0
    def __init__(self):
        self._settings = {}
        self._candles = []
        self._stack = Stack()

        # Attributes to other classes
        self._utils = Utilities()
        self._t = Transaction()

        # Run program's main loop
        self.run()
Esempio n. 7
0
def test_update_s_notAllCurrencies(caplog):
    stack = Stack()

    stack.update_s("ETH:42")

    assert stack._USDT == 0
    assert stack._ETH == 0
    assert stack._BTC == 0

    with caplog.at_level(logging.ERROR):
        assert caplog.records[
            0].message == "Might update the three currencies."
Esempio n. 8
0
def test_update_s_wrongCurrency(caplog):
    stack = Stack()

    stack.update_s("None:42,USDT:12.1,BTC:1")

    assert stack._USDT == 12.1
    assert stack._ETH == 0
    assert stack._BTC == 1

    with caplog.at_level(logging.ERROR):
        assert caplog.records[0].message == f"None is not a valid currency."\
                                            f"Currencies are {globals.currencies}"
Esempio n. 9
0
def test_constructor():
    stack = Stack()

    assert stack._USDT == 0
    assert stack._ETH == 0
    assert stack._BTC == 0
Esempio n. 10
0
class Trade():
    """
    Main class of the Trade project.
    """
    def __init__(self):
        self._settings = {}
        self._candles = []
        self._stack = Stack()

        # Attributes to other classes
        self._utils = Utilities()
        self._t = Transaction()

        # Run program's main loop
        self.run()

    def getInput(self) -> str:
        """
        Get input and catch potential errors.

        Returns:
            str: Input received from stdin.
        """

        try:
            inputs = input()
        except EOFError:
            exit(84)
        except KeyboardInterrupt:
            Logger("\nexit.", logType="INFO")
            exit(0)
        else:
            return inputs

    def initSettings(self, inputs: list) -> None:
        """
        Parse the input passed as argument
        and initialise corresponding class's attribute.

        Args:
            inputs (list): Input to be parsed.
        """

        if inputs[globals.VARIABLE] == "candle_format":
            # Candle format is set by default, don't need to stock it.
            return

        if self._utils.isInt(inputs[globals.VALUE]):
            value = int(inputs[globals.VALUE])
        elif self._utils.isFloat(inputs[globals.VALUE]):
            value = float(inputs[globals.VALUE])
        else:
            value = inputs[globals.VALUE]

        if inputs[globals.VARIABLE] == "initial_stack":
            self._stack._USDT = value
        if inputs[globals.VARIABLE] == "candles_given":
            self._t._period = int(value / 3)
        self._settings[inputs[globals.VARIABLE]] = value

    def fetchCommand(self, command: list) -> None:
        """
        Fetch the right command depending of the input and execute it.

        Args:
            command (list): the command to be checked and executed.
        """

        if command[0] == "settings" and len(command) is 3:
            self.initSettings(command)
        elif command[0] == "update" and len(command) == 4:
            if f"{command[1]} {command[2]}" == "game next_candles":
                newCandle = Candle(command[3])
                if newCandle._state == globals.VALID:
                    self._candles.append(newCandle)
                    self._t.update_lastClosePrices(newCandle)
            elif f"{command[1]} {command[2]}" == "game stacks":
                self._stack.update_s(command[3])
        elif f"{command[0]} {command[1]}" == "action order":
            # Server is waiting for a move. (`sell`|`buy`|`pass`)
            self._t.strategy(candles=self._candles, stack=self._stack)
        else:
            Logger("Unrecognized command.")

    def run(self) -> None:
        """
        Main loop of the Trade project.
        """

        while (True):
            inputCommand = self.getInput()
            # If no input: restart loop
            if len(inputCommand) is 0:
                continue
            inputParsed = inputCommand.split()
            # Fetch to the right command
            self.fetchCommand(inputParsed)