<commit_before>from datetime import datetime, timedelta
from decimal import Decimal
from functools import partial
import logging

from mexbtcapi import concepts
from mexbtcapi.concepts.currencies import BTC
from mexbtcapi.concepts.currency import Amount, Currency, ExchangeRate
from mexbtcapi.concepts.market import ActiveParticipant, Market as BaseMarket, Order, Trade
import mtgox as low_level


logger = logging.getLogger(__name__)


class MtgoxTicker(concepts.market.Ticker):
    TIME_PERIOD = timedelta(days=1)

    def __repr__(self):
        return \
            "<MtgoxTicker({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})" \
            .format(self.market, self.time, self.high, self.high, self.last,
            self.volume, self.average, self.buy, self.sell)


class MtGoxOrder(Order):

    def __init__(self, market, oid, timestamp, buy_or_sell, from_amount,
                 exchange_rate, properties="", entity=None):
        super(MtGoxOrder, self).__init__(market, timestamp, buy_or_sell, from_amount, exchange_rate, properties, entity)
        self.oid = oid

    def __repr__(self):
        return \
            "<MtGoxOrder({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}>" \
            .format(self.market, self.timestamp, self.oid, self.buy_or_sell,
            self.from_amount, self.exchange_rate, self.properties, self.entity)


class MtGoxMarket(BaseMarket):
    MARKET_NAME = "MtGox"

    def __init__(self, currency):
        super(MtGoxMarket, self).__init__(self.MARKET_NAME, currency, BTC)

        # to convert low level data
        self.multiplier = low_level.multiplier
        self.xchg_factory = partial(concepts.currency.ExchangeRate,
                                    currency, BTC)

    def _multiplier(self, currency):
        return self.multiplier[currency.name]

    def getTicker(self):
        logger.debug("getting ticker")

        time = datetime.now()
        data = low_level.ticker(self.currency1.name)

        data2 = [Decimal(data[name]['value_int']) /
                 self._multiplier(self.currency1)
                 for name in ('high', 'low', 'avg', 'last', 'sell', 'buy')]
        high, low, avg, last, sell, buy = map(self.xchg_factory, data2)

        volume = Decimal(data['vol']['value_int']) / self._multiplier(BTC)
        ticker = MtgoxTicker(market=self, time=time, high=high, low=low,
                             average=avg, last=last, sell=sell, buy=buy,
                             volume=volume)
        return ticker

    def getDepth(self):
        logger.debug("getting depth")

        low_level_depth = low_level.depth()

        return {
            'asks': self._depthToOrders(low_level_depth['asks'], Order.ASK),
            'bids': self._depthToOrders(low_level_depth['bids'], Order.BID),
        }

    def _depthToOrders(self, depth, order_type):
        orders = []

        for d in depth:
            timestamp = datetime.fromtimestamp(d['stamp'] / 1000 / 1000)
            amount = Amount(
                Decimal(d['amount_int']) / self._multiplier(BTC), BTC)
            price = ExchangeRate(
                self.currency1, BTC,
                Decimal(d['price_int']) / self._multiplier(self.currency1))
            order = Order(self, timestamp, order_type, amount, price)
            orders.append(order)

        return orders

    def getTrades(self):
        logger.debug("getting trades")

        low_level_trades = low_level.trades()

        # convert tradres to array of Trades
        trades = []
        for trade in low_level_trades:
            price = Decimal(trade['price_int']) / \
                self._multiplier(self.currency1)
            amount = Decimal(trade['amount_int']) / \
                self._multiplier(BTC)
            timestamp = datetime.fromtimestamp(trade['date'])

            btc_amount = Amount(amount, BTC)
            exchange_rate = ExchangeRate(self.currency1, BTC, price)

            t = Trade(self, timestamp, btc_amount, exchange_rate)
            t.tid = ['tid']

            trades.append(t)

        return trades


class MtGoxParticipant(ActiveParticipant):

    def __init__(self, market, key, secret):
        super(MtGoxParticipant, self).__init__(market)
        self.private = low_level.Private(key, secret)

    def placeOrder(self, order):
        """places an Order in the market for price/amount"""
        now = datetime.now()
        if order.is_buy_order():
            logger.debug("placing buy order")
            oid = self.private.bid(order.from_amount.value, order.exchange_rate)
            return MtGoxOrder(self, oid, now, Order.BID, amount, price, entity=self)
        else:
            logger.debug("placing ask order")
            oid = self.private.ask(amount, price)
            return MtGoxOrder(self, oid, now, Order.ASK, amount, price, entity=self)

    def cancelOrder(self, order):
        """Cancel an existing order"""
        assert(isinstance(order, MtGoxOrder))

        logger.debug("cancelling order {0}".format(order.oid))

        oid = order.oid
        if order.is_buy_order():
            result = self.private.cancel_bid(oid)
        else:
            result = self.private.cancel_ask(oid)

        if not result:
            raise ActiveParticipant.ActiveParticipantError()

    def getOpenOrders(self):
        """Gets all the open orders"""

        logger.debug("getting open orders")

        low_level_orders = self.private.orders()
        orders = []

        for o in low_level_orders:
            currency = Currency(o['currency'])
            oid = o['oid']
            timestamp = datetime.fromtimestamp(o['date'])
            order_type = Order.BID if o['type'] else Order.ASK
            amount = Amount(Decimal(o['amount']['value_int']) / self._multiplier(BTC), BTC)
            price = ExchangeRate(currency, BTC, Decimal(o['price']['value_int']) / self._multiplier(currency))
            order = MtGoxOrder(self, oid, timestamp, order_type, amount, price, entity=self)

            # add additional status from MtGox
            order.status = o['status']

            orders.append(order)

        return orders

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return "<MtGoxParticipant({0})>".format(self.currency1)
<commit_msg>api.mtgox.http_v1.high_level: forgot to change this in commit 68cfe2ca454d25f5cd37f8c3579868f68320cd28<commit_after>from datetime import datetime, timedelta
from decimal import Decimal
from functools import partial
import logging

from mexbtcapi import concepts
from mexbtcapi.concepts.currencies import BTC
from mexbtcapi.concepts.currency import Amount, Currency, ExchangeRate
from mexbtcapi.concepts.market import ActiveParticipant, Market as BaseMarket, Order, Trade
import mtgox as low_level


logger = logging.getLogger(__name__)


class MtgoxTicker(concepts.market.Ticker):
    TIME_PERIOD = timedelta(days=1)

    def __repr__(self):
        return \
            "<MtgoxTicker({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8})" \
            .format(self.market, self.time, self.high, self.high, self.last,
            self.volume, self.average, self.buy, self.sell)


class MtGoxOrder(Order):

    def __init__(self, market, oid, timestamp, buy_or_sell, from_amount,
                 exchange_rate, properties="", entity=None):
        super(MtGoxOrder, self).__init__(market, timestamp, buy_or_sell, from_amount, exchange_rate, properties, entity)
        self.oid = oid

    def __repr__(self):
        return \
            "<MtGoxOrder({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}>" \
            .format(self.market, self.timestamp, self.oid, self.buy_or_sell,
            self.from_amount, self.exchange_rate, self.properties, self.entity)


class MtGoxMarket(BaseMarket):
    MARKET_NAME = "MtGox"

    def __init__(self, currency):
        super(MtGoxMarket, self).__init__(self.MARKET_NAME, currency, BTC)

        # to convert low level data
        self.multiplier = low_level.multiplier
        self.xchg_factory = partial(concepts.currency.ExchangeRate,
                                    currency, BTC)

    def _multiplier(self, currency):
        return self.multiplier[currency.name]

    def getTicker(self):
        logger.debug("getting ticker")

        time = datetime.now()
        data = low_level.ticker(self.currency1.name)

        data2 = [Decimal(data[name]['value_int']) /
                 self._multiplier(self.currency1)
                 for name in ('high', 'low', 'avg', 'last', 'sell', 'buy')]
        high, low, avg, last, sell, buy = map(self.xchg_factory, data2)

        volume = Decimal(data['vol']['value_int']) / self._multiplier(BTC)
        ticker = MtgoxTicker(market=self, time=time, high=high, low=low,
                             average=avg, last=last, sell=sell, buy=buy,
                             volume=volume)
        return ticker

    def getDepth(self):
        logger.debug("getting depth")

        low_level_depth = low_level.depth()

        return {
            'asks': self._depthToOrders(low_level_depth['asks'], Order.ASK),
            'bids': self._depthToOrders(low_level_depth['bids'], Order.BID),
        }

    def _depthToOrders(self, depth, order_type):
        orders = []

        for d in depth:
            timestamp = datetime.fromtimestamp(d['stamp'] / 1000 / 1000)
            amount = Amount(
                Decimal(d['amount_int']) / self._multiplier(BTC), BTC)
            price = ExchangeRate(
                self.currency1, BTC,
                Decimal(d['price_int']) / self._multiplier(self.currency1))
            order = Order(self, timestamp, order_type, amount, price)
            orders.append(order)

        return orders

    def getTrades(self):
        logger.debug("getting trades")

        low_level_trades = low_level.trades()

        # convert tradres to array of Trades
        trades = []
        for trade in low_level_trades:
            price = Decimal(trade['price_int']) / \
                self._multiplier(self.currency1)
            amount = Decimal(trade['amount_int']) / \
                self._multiplier(BTC)
            timestamp = datetime.fromtimestamp(trade['date'])

            btc_amount = Amount(amount, BTC)
            exchange_rate = ExchangeRate(self.currency1, BTC, price)

            t = Trade(self, timestamp, btc_amount, exchange_rate)
            t.tid = ['tid']

            trades.append(t)

        return trades


class MtGoxParticipant(ActiveParticipant):

    def __init__(self, market, key, secret):
        super(MtGoxParticipant, self).__init__(market)
        self.private = low_level.Private(key, secret)

    def placeOrder(self, order):
        """places an Order in the market for price/amount"""
        now = datetime.now()
        if order.is_buy_order():
            logger.debug("placing buy order")
            oid = self.private.bid(order.from_amount.value, order.exchange_rate)
            return MtGoxOrder(self.market, oid, now, Order.BID, amount, price, entity=self)
        else:
            logger.debug("placing ask order")
            oid = self.private.ask(amount, price)
            return MtGoxOrder(self.market, oid, now, Order.ASK, amount, price, entity=self)

    def cancelOrder(self, order):
        """Cancel an existing order"""
        assert(isinstance(order, MtGoxOrder))

        logger.debug("cancelling order {0}".format(order.oid))

        oid = order.oid
        if order.is_buy_order():
            result = self.private.cancel_bid(oid)
        else:
            result = self.private.cancel_ask(oid)

        if not result:
            raise ActiveParticipant.ActiveParticipantError()

    def getOpenOrders(self):
        """Gets all the open orders"""

        logger.debug("getting open orders")

        low_level_orders = self.private.orders()
        orders = []

        for o in low_level_orders:
            currency = Currency(o['currency'])
            oid = o['oid']
            timestamp = datetime.fromtimestamp(o['date'])
            order_type = Order.BID if o['type'] else Order.ASK
            amount = Amount(Decimal(o['amount']['value_int']) / self.market._multiplier(BTC), BTC)
            price = ExchangeRate(currency, BTC, Decimal(o['price']['value_int']) / self.market._multiplier(currency))
            order = MtGoxOrder(self.market, oid, timestamp, order_type, amount, price, entity=self)

            # add additional status from MtGox
            order.status = o['status']

            orders.append(order)

        return orders

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return "<MtGoxParticipant({0})>".format(self.currency1)

{'fraction_non_alphanumeric': 0.11761453396524486, 'fraction_numerical': 0.007977883096366508, 'mean_word_length': 2.4311653116531167, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5204713', 'n_tokens_mistral': 3972, 'n_tokens_neox': 3731, 'n_words': 835}