Exemple #1
0
def trade_from_kraken(kraken_trade):
    """Turn a kraken trade returned from kraken trade history to our common trade
    history format"""
    currency_pair = kraken_to_world_pair(kraken_trade['pair'])
    quote_currency = get_pair_position(currency_pair, 'second')
    # Kraken timestamps have floating point
    timestamp = convert_to_int(kraken_trade['time'], accept_only_exact=False)
    amount = FVal(kraken_trade['vol'])
    cost = FVal(kraken_trade['cost'])
    fee = FVal(kraken_trade['fee'])
    order_type = kraken_trade['type']
    rate = FVal(kraken_trade['price'])

    log.debug(
        'Processing kraken Trade',
        sensitive_log=True,
        timestamp=timestamp,
        order_type=order_type,
        kraken_pair=kraken_trade['pair'],
        pair=currency_pair,
        quote_currency=quote_currency,
        amount=amount,
        cost=cost,
        fee=fee,
        rate=rate,
    )

    return Trade(
        timestamp=timestamp,
        pair=currency_pair,
        type=order_type,
        rate=rate,
        cost=cost,
        cost_currency=quote_currency,
        fee=fee,
        fee_currency=quote_currency,
        amount=amount,
        location='kraken',
    )
def trades_from_dictlist(given_trades, start_ts, end_ts):
    """ Gets a list of dict trades, most probably read from the json files and
    a time period. Returns it as a list of the Trade tuples that are inside the time period
    """
    returned_trades = list()
    for given_trade in given_trades:
        if given_trade['timestamp'] < start_ts:
            continue
        if given_trade['timestamp'] > end_ts:
            break

        pair = given_trade['pair']
        rate = FVal(given_trade['rate'])
        amount = FVal(given_trade['amount'])
        # Slowly trying to make cost go away as it can be calculated anyway
        if 'cost' not in given_trade:
            cost = rate * amount
            # Cost is always on the quote currency
            cost_currency = get_pair_position(pair, 'second')
        else:
            cost = FVal(given_trade['cost'])
            cost_currency = given_trade['cost_currency']

        returned_trades.append(
            Trade(
                timestamp=given_trade['timestamp'],
                pair=pair,
                type=given_trade['type'],
                rate=rate,
                cost=cost,
                cost_currency=cost_currency,
                fee=FVal(given_trade['fee']),
                fee_currency=given_trade['fee_currency'],
                amount=amount,
                location=given_trade['location'],
            ))
    return returned_trades