Skip to content

Commit 29b08a7

Browse files
committed
[Server] Add route
1 parent a3012d3 commit 29b08a7

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

dev_requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ pytest
22

33
redis
44
Flask
5-
Flask-Caching

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515

1616
# start arbitrage detection
1717
print("Scanning...")
18-
best_opportunities, best_profit = asyncio.run(triangular_arbitrage.detector.run_detection())
18+
best_opportunities, best_profit, exchange_name = asyncio.run(triangular_arbitrage.detector.run_detection())
1919
def opportunity_symbol(opportunity):
2020
return symbols.parse_symbol(str(opportunity.symbol))
2121

2222
# Display arbitrage detection result
2323
print("-------------------------------------------")
24-
print(f"Start by selling {str(opportunity_symbol(best_opportunities[0]).base)} to {str(opportunity_symbol(best_opportunities[0]).quote)} then sell {str(opportunity_symbol(best_opportunities[1]).base)} to {str(opportunity_symbol(best_opportunities[1]).quote)} and finally sell {str(opportunity_symbol(best_opportunities[2]).base)} to {str(opportunity_symbol(best_opportunities[2]).quote)} to make a profit of {(best_profit - 1) * 100}%")
24+
print(f"[{exchange_name}] Start by selling {str(opportunity_symbol(best_opportunities[0]).base)} to {str(opportunity_symbol(best_opportunities[0]).quote)} then sell {str(opportunity_symbol(best_opportunities[1]).base)} to {str(opportunity_symbol(best_opportunities[1]).quote)} and finally sell {str(opportunity_symbol(best_opportunities[2]).base)} to {str(opportunity_symbol(best_opportunities[2]).quote)} to make a profit of {(best_profit - 1) * 100}%")
2525
print("-------------------------------------------")
2626

2727
if benchmark:

server.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22
import os
33
from dotenv import load_dotenv
44
from flask import Flask, jsonify
5-
from flask_caching import Cache
65

76
import triangular_arbitrage.detector
87

9-
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
108
app = Flask(__name__)
11-
cache.init_app(app)
129

13-
@app.route("/")
14-
@cache.cached(timeout=os.getenv('CACHE', 60*60)) # cache it 1h by default
15-
def get_data():
10+
@app.route("/", defaults={'exchange': 'binance'})
11+
@app.route("/<exchange>")
12+
def get_data(exchange):
1613
# start arbitrage detection
1714
print("Scanning...")
18-
best_opportunities, best_profit = asyncio.run(triangular_arbitrage.detector.run_detection())
15+
best_opportunities, best_profit, exchange_name = asyncio.run(triangular_arbitrage.detector.run_detection(exchange))
1916
return jsonify({
2017
'best_opportunity': [str(best_opportunity.symbol) for best_opportunity in best_opportunities],
21-
'best_profit': best_profit
18+
'best_profit': best_profit,
19+
'exchange_name': exchange_name
2220
})
2321

2422

triangular_arbitrage/detector.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,21 @@ def get_opportunity_symbol(a, b):
9292

9393
return best_triplet, best_profit
9494

95-
async def run_detection():
96-
exchange = ccxt.binance()
95+
async def run_detection(exchange_name = "binance"):
96+
exchange_class = getattr(ccxt, exchange_name)
97+
exchange = exchange_class()
9798
try:
9899
tickers = await fetch_tickers(exchange)
99100
exchange_time = exchange.milliseconds()
100101
last_prices = get_last_prices(exchange_time, tickers)
101102
best_opportunity, best_profit = get_best_opportunity(last_prices)
102103
if os.getenv(REDIS_HOST_ENV, None) is not None:
103-
upload_result(best_opportunity, best_profit)
104+
upload_result(best_opportunity, best_profit, exchange.id)
104105
finally:
105106
await exchange.close()
106-
return best_opportunity, best_profit
107+
return best_opportunity, best_profit, exchange.name
107108

108-
def upload_result(best_opportunities, best_profit):
109+
def upload_result(best_opportunities, best_profit, exchange_id):
109110
import redis
110111
redis_client = redis.Redis(
111112
host=os.getenv(REDIS_HOST_ENV, None),
@@ -117,6 +118,7 @@ def upload_result(best_opportunities, best_profit):
117118
data = {
118119
'best_opportunity': [str(best_opportunity.symbol) for best_opportunity in best_opportunities],
119120
'best_profit': best_profit,
121+
'exchange_id': exchange_id,
120122
'timestamp': time.time()
121123
}
122-
redis_client.json().set(f"{os.getenv(REDIS_KEY_ENV, None)}", '$', data)
124+
redis_client.json().set(f"{os.getenv(REDIS_KEY_ENV, None)}:{exchange_id}", '$', data)

0 commit comments

Comments
 (0)