コード例 #1
0
def test_trailing_price_hit():
    actions = []
    market = 'ETH-EUR'
    init_price = Decimal('1000')
    price = Decimal('950')
    ticker_price = Decimal('800')
    trailing_percentage = Decimal('0.9')
    trailing_price = init_price * trailing_percentage

    alert = Alert(actions=actions,
                  dt=datetime.datetime.now(),
                  init_price=init_price,
                  market=market,
                  price=price,
                  status=Alert.STATUS_ACTIVE,
                  trailing_percentage=trailing_percentage,
                  trailing_price=trailing_price,
                  _client=BitvavoClient(market=market,
                                        _response_ticker_price={
                                            "market": market,
                                            "price": ticker_price
                                        }))

    alert.update_by_client()

    assert alert.actions == actions
    assert alert.init_price == init_price
    assert alert.market == market
    assert alert.price == ticker_price
    assert alert.status == Alert.STATUS_HIT
    assert alert.trailing_percentage == trailing_percentage
    assert alert.trailing_price == trailing_price
コード例 #2
0
def test_populate_new_alert_init_price_not_set():
    actions = []
    market = 'ETH-EUR'
    init_price = None
    price = Decimal('1000')
    ticker_price = Decimal('1511.7')
    trailing_percentage = Decimal('0.9')
    trailing_price = ticker_price * trailing_percentage

    alert = Alert(actions=actions,
                  dt=datetime.datetime.now(),
                  init_price=init_price,
                  market=market,
                  price=price,
                  status=Alert.STATUS_NOT_INIT,
                  trailing_percentage=trailing_percentage,
                  trailing_price=trailing_price,
                  _client=BitvavoClient(market=market,
                                        _response_ticker_price={
                                            "market": market,
                                            "price": ticker_price
                                        }))

    assert alert.update_by_client() is False

    assert alert.actions == actions
    assert alert.init_price is None
    assert alert.market == market
    assert alert.price == price
    assert alert.status == Alert.STATUS_NOT_INIT
    assert alert.trailing_percentage == trailing_percentage
    assert alert.trailing_price == trailing_percentage * ticker_price
コード例 #3
0
def get_alert(**kwargs):
    status = kwargs.get("status")
    client_response_ticker_price_scenario = kwargs.get(
        "client_response_ticker_price_scenario")
    client_response_ticker_price = None
    market = kwargs.get('market', 'ETH-EUR')

    fake = Faker()

    trailing_price = fake.pydecimal(min_value=100)
    trailing_percentage = Decimal('0.' +
                                  str(fake.pyint(min_value=70, max_value=97)))
    init_price = trailing_price / trailing_percentage + fake.pydecimal(
        min_value=100, max_value=200)

    if status == Alert.STATUS_ACTIVE:
        price = trailing_price / trailing_percentage + fake.pydecimal(
            min_value=100, max_value=200)
    elif status == Alert.STATUS_HIT:
        price = trailing_price / trailing_percentage
    else:
        return None

    if client_response_ticker_price_scenario is not None:
        if client_response_ticker_price_scenario == 'hit':
            client_response_ticker_price = trailing_price - fake.pydecimal(
                min_value=30, max_value=80)
        elif client_response_ticker_price_scenario == 'increased':
            client_response_ticker_price = price + fake.pydecimal(min_value=30,
                                                                  max_value=80)
        elif client_response_ticker_price_scenario == 'decreased':
            client_response_ticker_price = fake.pydecimal(
                min_value=int(trailing_price + 1), max_value=int(price - 1))

    dt = fake.date_time() + datetime.timedelta(microseconds=1)

    return Alert(amount=None,
                 actions=[],
                 dt=dt,
                 init_dt=dt,
                 init_price=init_price,
                 market=market,
                 price=price,
                 status=status,
                 trailing_percentage=trailing_percentage,
                 trailing_price=trailing_price,
                 _client=BitvavoClient(market=market,
                                       _response_ticker_price={
                                           "market": market,
                                           "price":
                                           client_response_ticker_price
                                       }))
コード例 #4
0
    def load_alerts(self):
        alerts = list()

        try:
            with open(self.alerts_file_path + self.alerts_file_name, 'r') as fp:
                alerts = json.load(fp, parse_float=self.get_decimal, parse_int=self.get_decimal)
        except FileNotFoundError:
            logging.info('No alert file.')
            Path(self.alerts_file_path + self.alerts_file_name).touch()
            logging.info('Created alert file.')

            return
        except simplejson.errors.JSONDecodeError as e:
            logging.warning(e)

        # load new alert from dedicated file
        if self.new_alert_file_name is not None:
            try:
                with open(self.alerts_file_path + self.new_alert_file_name, 'r') as fp:
                    alerts.append(json.load(fp, parse_float=self.get_decimal, parse_int=self.get_decimal))

                os.remove(self.alerts_file_path + self.new_alert_file_name)
            except FileNotFoundError:
                logging.debug('No new alert file.')
            except simplejson.errors.JSONDecodeError as e:
                logging.warning(e)

        if not alerts:
            logging.warning("No alerts set.")

        for idx, alert in enumerate(alerts):
            alert = Alert(
                actions=alert['actions'],
                amount=alert['amount'],
                dt=datetime.datetime.strptime(alert['dt'], "%Y-%m-%d %H:%M:%S.%f"),
                init_dt=datetime.datetime.strptime(alert['init_dt'], "%Y-%m-%d %H:%M:%S.%f"),
                init_price=alert['init_price'],
                market=alert['market'],
                price=alert['price'],
                status=alert['status'],
                trailing_percentage=alert['trailing_percentage'],
                trailing_price=alert['trailing_price'],
                _client=BitvavoClient(
                    market=alert['market']
                )
            )

            self.alerts.append(alert)

        logging.debug('ALERT:load_alerts:loaded_alerts:' + str(self.alerts))
コード例 #5
0
def test_is_ticker_price_diverted_false():
    actions = []
    market = 'ETH-EUR'
    init_price = Decimal('1000')
    price = Decimal('1500.0')
    ticker_price = '1499.0'
    trailing_percentage = Decimal('0.9')

    alert = Alert(
        actions=actions,
        dt=datetime.datetime.now(),
        init_price=init_price,
        market=market,
        price=price,
        status=Alert.STATUS_NOT_INIT,
        trailing_percentage=trailing_percentage,
        trailing_price=None,
        _client_backup=CryptowatchClient(
            _response_ticker_price={'result': {
                'price': ticker_price
            }}),
        _price_diversion_threshold=Decimal('0.01'))

    assert alert.is_ticker_price_diverted() is False
コード例 #6
0
def test_attributes():
    amount = Decimal('1.20')
    actions = ['send_email']
    dt = datetime.datetime.now()
    init_price = Decimal('1000')
    market = 'ADA-EUR'
    price = Decimal('1300.3')
    status = Alert.STATUS_ACTIVE
    trailing_percentage = Decimal('0.9')
    trailing_price = Decimal('23.9')

    alert = Alert(amount=amount,
                  actions=actions,
                  dt=dt,
                  init_dt=dt,
                  init_price=init_price,
                  market=market,
                  price=price,
                  status=status,
                  trailing_percentage=trailing_percentage,
                  trailing_price=trailing_price)

    alert_dict = {
        'amount': amount,
        'actions': actions,
        'dt': dt,
        'init_dt': dt,
        'init_price': init_price,
        'market': market,
        'price': price,
        'status': status,
        'trailing_percentage': trailing_percentage,
        'trailing_price': trailing_price
    }

    assert alert.attributes() == alert_dict
コード例 #7
0
    def add_by_console(self):
        if os.path.isfile(self.file_name):
            print(
                "Pending new alert, try again please (Updating process probably didn't yet pick up new alert)."
            )

            exit(1)

        print('Input new alert data:')
        print('---------------------')

        if self.market is None:
            self.choose_market()

        print('Add new init price:')
        print(' [1] Use manual value')
        print(' [2] Set by market')

        if self.init_price_type is None:
            self.init_price_type = input()

        if self.init_price_type == '1' and self.alert_init_price is None:
            print('Type in your value for init price as string like "2345.43"')
            self.alert_init_price = Decimal(input())

        if self.alert_trailing_percentage is None:
            print('Insert trail in percentage like ["0.9", "0.45"]')
            self.alert_trailing_percentage = Decimal(input())

        if self.actions_selection is None:
            print('Which actions should be activated?')
            print('[1] Send e-mail')
            print('[2] Sell')
            print('[3] Send e-mail and sell')

            self.actions_selection = input()

        if self.actions_selection == '1':
            self.actions.append(Alert.ACTION_SEND_EMAIL)
        if self.actions_selection == '2':
            self.actions.append(Alert.ACTION_SELL_ASSET)
        elif self.actions_selection == '3':
            self.actions.extend(
                [Alert.ACTION_SEND_EMAIL, Alert.ACTION_SELL_ASSET])

        if self._client is None:
            self._client = BitvavoClient(market=self.market)

        self.alert = Alert(actions=self.actions,
                           init_price=self.alert_init_price,
                           trailing_percentage=self.alert_trailing_percentage,
                           market=self.market,
                           _client=self._client)

        if not self.alert.init_attributes():
            print('Error occurred.')

        self.save_alert()

        print('New alert created.')
        print(self.alert.attributes())
コード例 #8
0
def test_get_symbol():
    market = 'ETH-EUR'

    alert = Alert(market=market, )

    assert alert.get_symbol() == 'ETH'