Exemple #1
0
    def add_external_trade(self, data):
        timestamp, message = check_otctrade_data_valid(data)
        if not timestamp:
            return False, message

        rate = float(data['otc_rate'])
        amount = float(data['otc_amount'])
        cost = rate * amount
        pair = data['otc_pair']
        external_trades = get_external_trades(self.data_directory)
        external_trades.append({
            'timestamp': timestamp,
            'pair': pair,
            'type': data['otc_type'],
            'rate': rate,
            'cost': cost,
            # for now cost/fee currency is always second.
            # TODO: Make it configurable
            'cost_currency': get_pair_position(pair, 'second'),
            'fee_currency': get_pair_position(pair, 'second'),
            'fee': data['otc_fee'],
            'amount': amount,
            'location': 'external',
            'link': data['otc_link'],
            'notes': data['otc_notes'],
        })
        with open(os.path.join(self.data_directory, EXTERNAL_TRADES_FILE), 'w') as f:
            f.write(rlk_jsondumps(external_trades))

        return True, ''
Exemple #2
0
def trade_from_bittrex(bittrex_trade):
    """Turn a bittrex trade returned from bittrex trade history to our common trade
    history format"""
    amount = FVal(bittrex_trade['Quantity']) - FVal(
        bittrex_trade['QuantityRemaining'])
    rate = FVal(bittrex_trade['PricePerUnit'])
    order_type = bittrex_trade['OrderType']
    bittrex_price = FVal(bittrex_trade['Price'])
    bittrex_commission = FVal(bittrex_trade['Commission'])
    pair = bittrex_pair_to_world(bittrex_trade['Exchange'])
    base_currency = get_pair_position(pair, 'first')
    if order_type == 'LIMIT_BUY':
        order_type = 'buy'
        cost = bittrex_price + bittrex_commission
        fee = bittrex_commission
    elif order_type == 'LIMIT_SEL':
        order_type = 'sell'
        cost = bittrex_price - bittrex_commission
        fee = bittrex_commission
    else:
        raise ValueError(
            'Got unexpected order type "{}" for bittrex trade'.format(
                order_type))

    return Trade(timestamp=bittrex_trade['TimeStamp'],
                 pair=pair,
                 type=order_type,
                 rate=rate,
                 cost=cost,
                 cost_currency=base_currency,
                 fee=fee,
                 fee_currency=base_currency,
                 amount=amount,
                 location='bittrex')
Exemple #3
0
    def edit_external_trade(self, data):
        timestamp, message = check_otctrade_data_valid(data)
        if not timestamp:
            return False, message

        rate = float(data['otc_rate'])
        amount = float(data['otc_amount'])
        cost = rate * amount
        pair = data['otc_pair']
        external_trades = get_external_trades(self.data_directory)

        # TODO: When we switch to sql, editing should be done with the primary key
        found = False
        for idx, trade in enumerate(external_trades):
            if timestamp == trade['timestamp']:
                external_trades[idx] = {
                    'timestamp': timestamp,
                    'pair': pair,
                    'type': data['otc_type'],
                    'rate': rate,
                    'cost': cost,
                    # for now cost/fee currency is always second.
                    # TODO: Make it configurable
                    'cost_currency': get_pair_position(pair, 'second'),
                    'fee_currency': get_pair_position(pair, 'second'),
                    'fee': data['otc_fee'],
                    'amount': amount,
                    'location': 'external',
                    'link': data['otc_link'],
                    'notes': data['otc_notes'],
                }
                found = True
                break

        if not found:
            return False, 'Could not find the requested trade for editing'

        with open(os.path.join(self.data_directory, EXTERNAL_TRADES_FILE), 'w') as f:
            f.write(rlk_jsondumps(external_trades))

        return True, ''
Exemple #4
0
def trade_from_poloniex(poloniex_trade, pair):
    """Turn a poloniex trade returned from poloniex trade history to our common trade
    history format"""

    trade_type = poloniex_trade['type']
    amount = FVal(poloniex_trade['amount'])
    rate = FVal(poloniex_trade['rate'])
    perc_fee = FVal(poloniex_trade['fee'])
    base_currency = get_pair_position(pair, 'first')
    quote_currency = get_pair_position(pair, 'second')
    if trade_type == 'buy':
        cost = rate * amount
        cost_currency = base_currency
        fee = amount * perc_fee
        fee_currency = quote_currency
    elif trade_type == 'sell':
        cost = amount * rate
        cost_currency = base_currency
        fee = cost * perc_fee
        fee_currency = base_currency
    else:
        raise ValueError(
            'Got unexpected trade type "{}" for poloniex trade'.format(
                trade_type))

    if poloniex_trade['category'] == 'settlement':
        trade_type = "settlement_%s" % trade_type

    return Trade(timestamp=createTimeStamp(poloniex_trade['date'],
                                           formatstr="%Y-%m-%d %H:%M:%S"),
                 pair=pair,
                 type=trade_type,
                 rate=rate,
                 cost=cost,
                 cost_currency=cost_currency,
                 fee=fee,
                 fee_currency=fee_currency,
                 amount=amount,
                 location='poloniex')
Exemple #5
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')
    return Trade(
        # Kraken timestamps have floating point ...
        timestamp=convert_to_int(kraken_trade['time'],
                                 accept_only_exact=False),
        pair=currency_pair,
        type=kraken_trade['type'],
        rate=FVal(kraken_trade['price']),
        cost=FVal(kraken_trade['cost']),
        cost_currency=quote_currency,
        fee=FVal(kraken_trade['fee']),
        fee_currency=quote_currency,
        amount=FVal(kraken_trade['vol']),
        location='kraken')