-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtickers.py
More file actions
106 lines (82 loc) · 3.64 KB
/
tickers.py
File metadata and controls
106 lines (82 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pandas as pd
from collections import deque
class Matrix:
def __init__(self, df):
# Keep the original DataFrame for reference
self.headers = df.columns.tolist()
self.df = df.reset_index(drop=True)
self.matrix = self._convert_to_matrix(self.df.copy())
def _convert_to_matrix(self, df):
df = df.fillna(0) # Replace NaN with -1
matrix = df.map(lambda x: 1 if isinstance(x, str) else x).values # Replace all strings with 1
return matrix
def print_matrix(self):
print(self.matrix)
def get_matrix(self):
return self.matrix
def get_df(self):
return self.df
def print_df(self):
print(self.df)
def get_currency(self, paths, row, col):
# Get the base currency from the row index
base_currency = self.df.iloc[row, col] # Adjust as necessary if row index is not the currency
# Get the quote currency from the column header
quote_currency = self.headers[col] # Adjust as necessary if column headers are not the currency
return f"{base_currency}/{quote_currency}"
def check_if_equal_to_base(self, start_col, row, col) -> bool:
quote_currency = self.headers[start_col]
base_currency = self.df.iloc[row, col]
if quote_currency == base_currency:
return True
else:
return False
def get_arbitrage_paths(self, start_row, start_col, max_length, conversion=True):
all_paths = []
# Queue stores (current_position, path, move count, last_move_was_horizontal, path_visited)
queue = deque([((start_row, start_col), [(start_row, start_col)], 0, False, set([(start_row, start_col)]))])
while queue:
(row, col), path, moves_count, last_move_was_horizontal, path_visited = queue.popleft()
# Debugging print
if moves_count == max_length:
if col == start_col:
all_paths.append(path)
elif self.check_if_equal_to_base(start_col, row, col):
# row.currency in current column equal to currency of starting header
all_paths.append(path)
continue # Stop exploring further for this path
if last_move_was_horizontal:
moves = self.get_vertical_moves(row, col)
else:
moves = self.get_horizontal_moves(row, col)
for move in moves:
new_row, new_col = move
if (new_row, new_col) not in path_visited:
new_path = path + [move]
new_path_visited = path_visited.copy()
new_path_visited.add((new_row, new_col))
queue.append(
((new_row, new_col), new_path, moves_count + 1, not last_move_was_horizontal, new_path_visited))
if conversion:
currency_paths = [
[self.get_currency(all_paths, row, col) for row, col in path]
for path in all_paths]
else:
currency_paths = all_paths
return currency_paths
def get_horizontal_moves(self, row, col):
cols = self.matrix.shape[1]
current = (row, col)
moves = []
for i in range(cols):
if self.matrix[row][i] == 1 and (row, i) != current:
moves.append((row, i))
return moves
def get_vertical_moves(self, row, col):
rows = self.matrix.shape[0]
current = (row, col)
moves = []
for i in range(rows):
if self.matrix[i][col] == 1 and (i, col) != current:
moves.append((i, col))
return moves