示例#1
0
def test_trade_from_bitmex():
    margin_positions = [
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536580800,
            profit_loss=FVal('0.00000003'),
            pl_currency=A_BTC,
            notes='XBTZ18',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536580800,
            profit_loss=FVal('0.0000004'),
            pl_currency=A_BTC,
            notes='XBTUSD',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536580800,
            profit_loss=FVal('0.00000183'),
            pl_currency=A_BTC,
            notes='XBTJPY',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536580800,
            profit_loss=FVal('0.00000683'),
            pl_currency=A_BTC,
            notes='ETHUSD',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536494400,
            profit_loss=FVal('0.00000002'),
            pl_currency=A_BTC,
            notes='XRPU18',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536494400,
            profit_loss=FVal('0.00000003'),
            pl_currency=A_BTC,
            notes='XBTUSD',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536494400,
            profit_loss=FVal('0.00000003'),
            pl_currency=A_BTC,
            notes='XBTJPY',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536494400,
            profit_loss=FVal('0.00000005'),
            pl_currency=A_BTC,
            notes='ETHUSD',
        ),
        MarginPosition(
            exchange='bitmex',
            open_time=None,
            close_time=1536494400,
            profit_loss=FVal('-0.00007992'),
            pl_currency=A_BTC,
            notes='ETHU18',
        ),
    ]

    assert len(BITMEX_FIRST_9_TRADES) == len(margin_positions)
    for idx, trade in enumerate(BITMEX_FIRST_9_TRADES):
        position = trade_from_bitmex(trade)
        assert position == margin_positions[idx]
        assert isinstance(position.pl_currency, Asset)
示例#2
0
    def create_history(self, start_ts, end_ts, end_at_least_ts):
        """Creates trades and loans history from start_ts to end_ts or if
        `end_at_least` is given and we have a cache history for that particular source
        which satisfies it we return the cache
        """
        log.info(
            'Starting trade history creation',
            start_ts=start_ts,
            end_ts=end_ts,
            end_at_least_ts=end_at_least_ts,
        )

        # start creating the all trades history list
        history = list()
        asset_movements = list()
        empty_or_error = ''

        if self.kraken is not None:
            try:
                kraken_history = self.kraken.query_trade_history(
                    start_ts=start_ts,
                    end_ts=end_ts,
                    end_at_least_ts=end_at_least_ts,
                )
                history.extend(kraken_history)

                kraken_asset_movements = self.kraken.query_deposits_withdrawals(
                    start_ts=start_ts,
                    end_ts=end_ts,
                    end_at_least_ts=end_at_least_ts,
                )
                asset_movements.extend(kraken_asset_movements)

            except RemoteError as e:
                empty_or_error += '\n' + str(e)

        poloniex_query_error = False
        poloniex_margin_trades = []
        polo_loans = []
        try:
            (
                history,
                asset_movements,
                poloniex_margin_trades,
                polo_loans,
            ) = self.query_poloniex_history(
                history,
                asset_movements,
                start_ts,
                end_ts,
                end_at_least_ts,
            )
        except RemoteError as e:
            empty_or_error += '\n' + str(e)
            poloniex_query_error = True

        if self.bittrex is not None:
            try:
                bittrex_history = self.bittrex.query_trade_history(
                    start_ts=start_ts,
                    end_ts=end_ts,
                    end_at_least_ts=end_at_least_ts,
                )
                history.extend(bittrex_history)

            except RemoteError as e:
                empty_or_error += '\n' + str(e)

        if self.bitmex is not None:
            try:
                bitmex_history = self.bitmex.query_trade_history(
                    start_ts=start_ts,
                    end_ts=end_ts,
                    end_at_least_ts=end_at_least_ts,
                )
                for trade in bitmex_history:
                    history.append(trade_from_bitmex(trade))

                bitmex_asset_movements = self.bitmex.query_deposits_withdrawals(
                    start_ts=start_ts,
                    end_ts=end_ts,
                    end_at_least_ts=end_at_least_ts,
                )
                asset_movements.extend(bitmex_asset_movements)

            except RemoteError as e:
                empty_or_error += '\n' + str(e)

        if self.binance is not None:
            try:
                binance_history = self.binance.query_trade_history(
                    start_ts=start_ts,
                    end_ts=end_ts,
                    end_at_least_ts=end_at_least_ts,
                )
                history.extend(binance_history)
            except RemoteError as e:
                empty_or_error += '\n' + str(e)

        try:
            eth_transactions = query_etherscan_for_transactions(
                self.eth_accounts)
        except RemoteError as e:
            empty_or_error += '\n' + str(e)

        # We sort it here ... but when accounting runs through the entire actions list,
        # it resorts, so unless the fact that we sort is used somewhere else too, perhaps
        # we can skip it?
        history.sort(key=lambda trade: trade.timestamp)
        history = limit_trade_list_to_period(history, start_ts, end_ts)

        # Write to files
        historyfile_path = os.path.join(self.user_directory,
                                        TRADES_HISTORYFILE)
        write_tupledata_history_in_file(history, historyfile_path, start_ts,
                                        end_ts)
        if self.poloniex is not None:
            if not self.read_manual_margin_positions and not poloniex_query_error:
                marginfile_path = os.path.join(self.user_directory,
                                               MARGIN_HISTORYFILE)
                write_tupledata_history_in_file(
                    poloniex_margin_trades,
                    marginfile_path,
                    start_ts,
                    end_ts,
                )

        if not poloniex_query_error:
            loansfile_path = os.path.join(self.user_directory,
                                          LOANS_HISTORYFILE)
            write_history_data_in_file(polo_loans, loansfile_path, start_ts,
                                       end_ts)
        assetmovementsfile_path = os.path.join(self.user_directory,
                                               ASSETMOVEMENTS_HISTORYFILE)
        write_tupledata_history_in_file(asset_movements,
                                        assetmovementsfile_path, start_ts,
                                        end_ts)
        eth_tx_log_path = os.path.join(self.user_directory,
                                       ETHEREUM_TX_LOGFILE)
        write_tupledata_history_in_file(eth_transactions, eth_tx_log_path,
                                        start_ts, end_ts)

        # After writting everything to files include the external trades in the history
        history = maybe_add_external_trades_to_history(
            db=self.db,
            start_ts=start_ts,
            end_ts=end_ts,
            history=history,
            msg_aggregator=self.msg_aggregator,
        )

        return (
            empty_or_error,
            history,
            poloniex_margin_trades,
            polo_loans,
            asset_movements,
            eth_transactions,
        )