Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
add algorithm optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
CelestialCrafter committed Dec 5, 2023
1 parent 3125d92 commit 8b811d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
from price import update_cached_prices
from views import internal_checker, plot, worth, interval, update_interval
from views import internal_checker, plot, worth, interval, update_interval, optimize

load_dotenv()

Expand All @@ -16,6 +16,7 @@
app.add_url_rule('/interval', view_func=interval.interval)
app.add_url_rule('/internal_checker', view_func=internal_checker.internal_checker)
app.add_url_rule('/update_interval', view_func=update_interval.update_interval, methods=['POST'])
app.add_url_rule('/optimize', view_func=optimize.optimize, methods=['POST'])

def job_loop():
while True:
Expand Down
31 changes: 31 additions & 0 deletions views/optimize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import jwt, os, utils
from flask import json, request
from bson.objectid import ObjectId
from backtesting import optimize_algorithm

algorithms = utils.client['database']['algorithms']

def optimize():
jwt_encoded = request.headers.get('Authorization')
if not jwt_encoded:
return 'Bad Request', 400

if 'algorithms' not in request.json or type(request.json['algorithms']) is not list:
return 'Bad Request', 400

try:
jwt_decoded = jwt.decode(jwt_encoded, os.environ['JWT_SECRET'], algorithms=['HS256'])
except Exception:
return 'Unauthorized', 401

return json(
optimize_algorithm({
algorithm['name']: algorithm['params'] for algorithm in
algorithms.find({'$or': [{
'owner': ObjectId(jwt_decoded['_id'])
}, {
'owner': {
'$exists': False
}
}]})
}))

0 comments on commit 8b811d2

Please sign in to comment.