def optimiz(currencies, debug):
currencies = sorted(currencies)
if len(currencies) < 2 or len(currencies) > 10:
return {"error": "2 to 10 currencies"}
max_workers = 4 if sys.version_info[1] < 5 else None
executor = ThreadPoolExecutor(max_workers)
data = dict(future.result() for future in wait([executor.submit(get_ochl, cur) for cur in currencies]).done)
data = [data[cur] for cur in currencies]
errors = [x['error'] for x in data if 'error' in x]
if errors:
return {"error": "Currencies not found : " + str(errors)}
weights, m, s, a, b = markowitz_optimization(data, debug)
if debug:
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
plt.plot(s, m, 'o', markersize=1)
plt.plot(b, a, 'or')
fig.savefig("chalu.png")
result = dict()
for i, cur in enumerate(currencies):
result[cur] = weights[i]
return {"result": result}
评论列表
文章目录