hzjken / crypto-arbitrage-framework

A cryptocurrency arbitrage framework implemented with ccxt and cplex. It can be used to monitor multiple exchanges, find a multi-lateral arbitrage path which maximizes rate of return, calculate the optimal trading amount for each pair in the path given flexible constraints, and execute trades with multi-threading implemenation.
579 stars 174 forks source link

I am getting the following RuntimeWarning: RuntimeWarning: divide by zero encountered in log final_transit_matrix #14

Open Freiburg-AI-Research opened 3 years ago

Freiburg-AI-Research commented 3 years ago

Hello ,

i am getting the following runtime warning:

RuntimeWarning: divide by zero encountered in log final_transit_matrix = np.log(self.transit_price_matrix (1 - self.commission_matrix) (

in the following part of the code:

self.update_balance() self.update_transit_price() self.update_vol_matrix() self.update_changeable_constraint()

final_transit_matrix = np.log(self.transit_price_matrix (1 - self.commission_matrix) ( (self.vol_matrix >= self.min_trading_limit).astype(int))) final_transit = final_transit_matrix[self.var_location] x = self.x[self.var_location] self.maximize(self.sum(x * final_transit))

It calculates the results but I wonder how I can revise the code to avoid this warning?

I think the self.transit_price_matrix part is the problem. When I use dir(str(self.transit_price_matrix) he show me only zeros. And maybe because he makes log from zero then it results in this warning?

So I had to find out why transit_price_matrix is zero so I looked into update_transit_price

this was the following code:

def update_transit_price(self): '''to update data of the transit_price_matrix''' self.price = {} self.transit_price_matrix = np.zeros([self.length, self.length])

    exc_name_list = list(self.exchanges.keys())
    thread_num = len(exc_name_list)
    exc_price_list = multiThread(self.parallel_fetch_tickers, exc_name_list, thread_num)
    for exc_price in exc_price_list:
        self.price.update(exc_price)

    for pair, items in self.price.items():
        from_cur, to_cur = pair.split('/')
        if from_cur in self.currency_set and to_cur in self.currency_set:
            from_index = self.currency2index[from_cur]
            to_index = self.currency2index[to_cur]
            if items['ask'] != 0 and items['bid'] != 0:
                self.transit_price_matrix[from_index, to_index] = items['bid']
                self.transit_price_matrix[to_index, from_index] = 1 / items['ask']

    for from_cur, to_cur in self.inter_convert_list:
        from_index = self.currency2index[from_cur]
        to_index = self.currency2index[to_cur]

        if from_cur in self.withdrawal_fee:
            self.transit_price_matrix[from_index, to_index] = 1
        else:
            self.transit_price_matrix[from_index, to_index] = 0

        if to_cur in self.withdrawal_fee:
            self.transit_price_matrix[to_index, from_index] = 1
        else:
            self.transit_price_matrix[to_index, from_index] = 0

So my guess is that it doesn't update the transit_price properly. Is my way of thinking right? How would a expert proceed to find and fix the problem?

Thank you very much for your help.