Ejemplo n.º 1
0
 def test_get_last_LTPs(self):
     m = MongoManager(use_archive=True)
     m.reconnect(use_archive=False)
     now = datetime.datetime.strptime('2018-02-28 16:54:15',
                                      "%Y-%m-%d %H:%M:%S")
     LTPs = m.get_last_LTPs(selection_id=16377754,
                            amount_of_prices=3,
                            now=now)
     self.assertEqual(len(LTPs), 3)
Ejemplo n.º 2
0
def lay_saftey_check(selection_id, last_price, current_lay_price, best_back):
    """ takes election ID and last scraped LTP as input, returns maximum lay """
    m = MongoManager()
    ltps = m.get_last_LTPs(selection_id, amount_of_prices=5)
    ltps.append(last_price)
    log.info("LTPs from db (not used): {}".format(ltps))

    try:

        # cap the value of the list to 500 and clean ltps from any erroneous/non-numerical value
        ltps = [
            min(x, 500) for x in ltps
            if isinstance(x, numbers.Number) and not np.isnan(x)
        ]

        # take the median
        median = np.median(ltps)

    except:
        log.warning(
            "Unable to calculate median from ltps: {}. Assigning a safety threshold"
            .format(ltps))
        median = 500

    last_price_multiplier = config.getfloat("Restrictions",
                                            "last_price_multiplier")
    multiple_to_best_competitor = config.getfloat(
        "Restrictions", "multiple_to_best_compatitor")
    absolute_maximum = config.getfloat("Restrictions", "absolute_maximum")

    max_lay = min(current_lay_price, absolute_maximum,
                  last_price * last_price_multiplier,
                  best_back * multiple_to_best_competitor)

    if max_lay < current_lay_price:
        log.warning(
            "Restrict lay from {0} to {1}. | LTPs: {2} | best back: {3}".
            format(current_lay_price, max_lay, ltps, best_back))

    log.info(
        "current_lay_price (back level n for replacing_unfilled): {}".format(
            current_lay_price))
    log.info("LTP median (not used): {}".format(median))
    log.info("Last price (ltp or back): {}".format(last_price))
    log.info("best back (=best competitor): {}".format(best_back))

    return max_lay