def query_recently_bought(ticker, accapi, options):
    """Looking back 30 days for purchases that would cause wash sales"""
    wash_pattern = options.get('wash_pattern', '')
    account_field = options.get('account_field', 'LEAF(account)')
    wash_pattern_sql = 'AND account ~ "{}"'.format(wash_pattern
        ) if wash_pattern else ''
    sql = (
        """
    SELECT
        {account_field} as account,
        date as acquisition_date,
        DATE_ADD(date, 30) as until,
        units(sum(position)) as units,
        cost(sum(position)) as basis
      WHERE
        number > 0 AND
        date >= DATE_ADD(TODAY(), -30) AND
        currency = "{ticker}"
        {wash_pattern_sql}
      GROUP BY {account_field},date,until
      ORDER BY date DESC
      """
        .format(**locals()))
    rtypes, rrows = accapi.query_func(sql)
    return rtypes, rrows
