I came up with this algorithm to find triplets of
pairs (I call them
trairs), the criteria is that all 3 elements (coins), and only the 3, must be present in all 3
However, it is probably possible to solve the same problem in a more elegant way. For instance, I am indexing all the loops, which makes it look extra complicated. Also, there is a
break there that makes me uncomfortable!
The input data is
pairs, it is a
pairs = ['BCH/BTC','BCH/ETH','DASH/USD','BTC/USDT','ETH/BTC']
The wanted output is a
list of strings:
trair = [['BCH/BTC','BCH/ETH','ETH/BTC]]
def find_trairs(exchange): ''' Find possible triplets of pairs (trairs) that can be traded for unbalance. Example of trairs: 'ETC/BTC' 'ETC/ETH' 'ETH/BTC' 'BCH/BTC' 'BCH/EUR' 'BTC/EUR' ''' exchange_pairs = exchange.symbols #loads all pairs from the exchange. pairs = list(filter(lambda x: not '.d' in x, exchange_pairs)) #filters off #the darkpool pairs. pair = ['', '', ''] coin = ['', '', ''] trair =  #Semi-optimized loop to look for triplece of pairs. #Example:['BCH/BTC', 'BCH/EUR', 'BTC/EUR'] for i in range(len(pairs)-3): #not all coins are 3 digits long, we must find the slash that separetes #each coin in order to have a robust algorithm. slash_position = pairs[i].find('/') coin = pairs[i][0:slash_position] coin = pairs[i][slash_position+1:] for j in range(i+1, len(pairs)-2): if (coin in pairs[j]): slash_position = pairs[j].find('/') coin = pairs[j][slash_position+1:] for k in range(j+1, len(pairs)-1): if coin in pairs[k] and coin in pairs[k]: trair.append([pairs[i], pairs[j], pairs[k]]) break return trair
Any hints or comments?
Use itertools permutations, with filtering the results, and elimination of duplicates:
import itertools currency_pairs = ['BCH/BTC', 'BCH/ETH', 'DASH/USD', 'BTC/USDT', 'ETH/BTC'] set_triplets = set() for triplet in itertools.permutations(currency_pairs, 3): c1, c2 = triplet.split('/') if (c1 in triplet or c1 in triplet) and (c2 in triplet or c2 in triplet): set_triplets.add(tuple(sorted(triplet))) for triplet in set_triplets: print(triplet)
('BCH/ETH', 'BTC/USDT', 'ETH/BTC') ('BCH/BTC', 'BCH/ETH', 'BTC/USDT') ('BCH/BTC', 'BCH/ETH', 'ETH/BTC')
Please note that the ordering of the currency pairs in a triplet is lexicographically ascending, do not expect the first pair to always be the link between the two others.