-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
169 lines (142 loc) · 5.47 KB
/
api.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
from flask import Flask, make_response
from flask_restful import Api
from flask import jsonify
from flask_restful import Resource
from flask_restful import reqparse
from stocks_analiz import Stock
import os
import datetime as dt
from bonds import Bond
app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('date', required=True)
parser.add_argument('length_invest_horizon', required=True)
parser.add_argument('shorts', required=True)
parser.add_argument('budget', required=True, type=int)
parser.add_argument('tg_id', required=True, type=int)
class Briefcase(Resource):
def get(self):
args = parser.parse_args()
news = {
'date': args['date'],
'length_invest_horizon': args['length_invest_horizon'],
'budget': args['budget'],
'tg_id': args['tg_id'],
'shorts': args['shorts']
}
if int(news['length_invest_horizon']) == 1:
mon_stoks = int(news['budget']) * 0.2
mon_bonds = int(news['budget']) * 0.8
elif int(news['length_invest_horizon']) == 3:
mon_stoks = int(news['budget']) * 0.3
mon_bonds = int(news['budget']) * 0.7
elif int(news['length_invest_horizon']) == 5:
mon_stoks = int(news['budget']) * 0.5
mon_bonds = int(news['budget']) * 0.5
stock = Stock(money=mon_stoks, shorts=bool(news['shorts']))
stock.sharp()
stock.profit()
stock.volatility()
name_file = stock.plot()
bond = Bond()
bond.get_bonds(mon_bonds, int(news['length_invest_horizon']))
with open(name_file, 'rb') as file:
image_binary = file.read(-1)
response = make_response(image_binary)
response.headers.set('Content-Type', 'image/png')
response.headers.set(
'Content-Disposition', 'attachment', filename=name_file)
return response
def post(self):
args = parser.parse_args()
news = {
'date': args['date'],
'length_invest_horizon': args['length_invest_horizon'],
'budget': args['budget'],
'tg_id': args['tg_id'],
'shorts': args['shorts']
}
if int(news['length_invest_horizon']) == 1:
mon_stoks = int(news['budget']) * 0.2
mon_bonds = int(news['budget']) * 0.8
elif int(news['length_invest_horizon']) == 3:
mon_stoks = int(news['budget']) * 0.3
mon_bonds = int(news['budget']) * 0.7
elif int(news['length_invest_horizon']) == 5:
mon_stoks = int(news['budget']) * 0.5
mon_bonds = int(news['budget']) * 0.5
stock = Stock(money=mon_stoks, shorts=bool(news['shorts']))
sharp = stock.sharp()
profit = stock.profit()
volatility = stock.volatility()
# name_file = stock.plot()
bond = Bond()
gbonds = bond.get_bonds(mon_bonds, int(news['length_invest_horizon']))
# with open(name_file, 'rb') as file:
# image_binary = file.read(-1)
# response = make_response(image_binary)
# response.headers.set('Content-Type', 'image/png')
# response.headers.set(
# 'Content-Disposition', 'attachment', filename=name_file)
return jsonify({
'success': 'OK',
'time': dt.datetime.now().strftime("%d-%m-%Y"),
'stoks': {
'volatility': {
'stoks_and_count': volatility[0],
'balance': volatility[1]
},
'sharp': {
'stoks_and_count': sharp[0],
'balance': sharp[1]
}
},
'bonds': gbonds,
'yield': profit
})
class Test(Resource):
def post(self, id_img):
args = parser.parse_args()
news = {
'date': args['date'],
'length_invest_horizon': args['length_invest_horizon'],
'budget': args['budget'],
'tg_id': args['tg_id'],
'shorts': args['shorts']
}
if int(news['length_invest_horizon']) == 1:
mon_stoks = int(news['budget']) * 0.2
mon_bonds = int(news['budget']) * 0.8
elif int(news['length_invest_horizon']) == 3:
mon_stoks = int(news['budget']) * 0.3
mon_bonds = int(news['budget']) * 0.7
elif int(news['length_invest_horizon']) == 5:
mon_stoks = int(news['budget']) * 0.5
mon_bonds = int(news['budget']) * 0.5
stock = Stock(money=mon_stoks, shorts=bool(news['shorts']))
stock.sharp()
stock.volatility()
if id_img == 1:
name_file = stock.plot()
elif id_img == 2:
name_file = stock.plot_equal_sharp()
elif id_img == 3:
name_file = stock.plot_equal_volatility()
# bond = Bond()
# bond.get_bonds(mon_bonds, int(news['length_invest_horizon']))
with open(name_file, 'rb') as file:
image_binary = file.read(-1)
response = make_response(image_binary)
response.headers.set('Content-Type', 'image/png')
response.headers.set(
'Content-Disposition', 'attachment', filename=name_file)
return response
def main():
api.add_resource(Briefcase, '/api/v2/briefcase/')
api.add_resource(Test, '/api/v2/test/<int:id_img>')
# db_session.global_init("db/db_users.db")
app.run()
if __name__ == '__main__':
main()