-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
136 lines (96 loc) · 3.29 KB
/
app.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from flask import Flask, request, jsonify
from flask_jsonpify import jsonpify
from flask_socketio import SocketIO, send, emit
import requests
import time
import stockstats
from threading import Lock
from .utils import fetch, get_macd_by_id, is_macd_object_exists, parse_data
from .macd import MACD
macd_objects = []
data = dict()
app = Flask(__name__)
app.debug = True
@app.route('/macd', methods=['GET'])
def get_all_macd_objects():
"""
Return all MACD-objects
:return: list of dict
"""
global macd_objects
return jsonpify([m.__dict__ for m in macd_objects])
@app.route('/calc', methods=['GET'])
def calcAll():
"""
Calculate the new macd-coefficients for all MACD-objects
:return: List of serialized MACD-objects
"""
global macd_objects
global data
for macd in macd_objects:
try:
if macd.pair not in data:
data[macd.pair] = fetch(macd.pair) # get data
data[macd.pair] = parse_data(data[macd.pair]) # in each pair is stored sdf-data itself
except Exception as err:
return jsonpify(err)
sdf = macd.calculate_coefficient(data[macd.pair][macd.time_period])
data = dict() # empty data
return jsonpify([m.__dict__ for m in macd_objects])
@app.route('/addplato', methods=['PUT'])
def addplato():
"""
Create the new MACD-object and store it in 'macd_objects' list globally
:query_param pair
:query_param fast_period
:query_param slow_period
:query_param signal_period
:query_param time_period
:query_param plato_ids
:return: dict
"""
params = request.args
if MACD.paramsIsNotValid(params):
return 'Error'
global macd_objects
global data
if get_macd_by_id(params['plato_ids'], macd_objects) != None:
return 'Object already exists'
# request to Plato-microservice
macd = MACD(params['pair'], params['fast_period'], params['slow_period'], params['signal_period'], params['time_period'], params['plato_ids'])
macd_objects.append(macd)
return jsonpify(macd.__dict__)
@app.route('/calc/<string:plato_ids>', methods=['PUT'])
def calc(plato_ids):
"""
Calculate the new macd-coefficients for existing MACD-object by 'plato_ids'
:param plato_id: Id of MACD-object
:return: dict
"""
global macd_objects
macd = get_macd_by_id(plato_ids, macd_objects)
if macd == None:
return jsonpify({ 'message': 'Object is not exists', 'status': 1 })
try:
sdf = fetch(macd.pair, macd.time_period)
except Exception as err:
return jsonpify({ 'message': err, 'status': 1 })
sdf = macd.calculate_coefficient(sdf)
print(macd_objects)
return jsonpify(macd.__dict__)
@app.route('/delete/macd/<string:plato_ids>', methods=['DELETE'])
def delete_macd_object(plato_ids):
"""
Delete the MACD-object by 'plato_ids'
:param plato_id: Id of MACD-object
:return: None or dict
"""
global macd_objects
macd = get_macd_by_id(plato_ids, macd_objects)
if macd != None:
macd_objects.remove(macd)
return jsonpify({ 'message': 'Object has been deleted', 'status': 0 })
else:
return jsonpify({ 'message': 'Object is not exists', 'status': 1 })
if __name__ == '__main__':
app.run() # Run app