-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlambda_function.py
80 lines (75 loc) · 1.98 KB
/
lambda_function.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
import json
from lib import efficient_frontier
from lib import dataframe as dataframe
from lib import discrete_states
def main(amount : float,stocks : list) -> list:
try:
df = dataframe.getStockData(stocks)
except:
return []
n=len(stocks)
values = efficient_frontier.ef(n=n,df=df)
out = discrete_states.discreteStates(stocks,amount,df,values)
#print(out)
return out
def lambda_handler(event : dict, context=None):
try:
amount = event['queryStringParameters']['amount']
stocks = event['queryStringParameters']['stocks']
except:
return {
'statusCode': 400,
'body': {
'msg':json.dumps('Parameters error in request')
}
}
stocks = stocks.split(',')
n=len(stocks)
try :
amount = float(amount)
except:
return {
'statusCode': 400,
'body': {
'msg':json.dumps('Enter Valid Amount')
}
}
for i in range(n):
stocks[i]=stocks[i].strip()
stocks[i]=stocks[i].upper()
out = main(float(amount),stocks)
if out ==[]:
return {
'statusCode': 400,
'body': {
'msg':json.dumps('Invalid stock name')
}
}
if out == -1:
return {
'statusCode': 400,
'body': {
'msg':json.dumps('Insufficient funds')
}
}
d={}
c=1
for i in out:
price,output_stocks,remaining,exr,risk=i
price = [round(i,2) for i in price]
remaining = round(remaining,2)
d1 = {"price":price,"output_stocks":output_stocks,"remaining":remaining,"expected return":exr,"risk":risk}
d[c]=d1
c+=1
return {
'statusCode': 200,
"body": {"symbols":stocks,"list":d}
}
#test
# js = {
# "queryStringParameters": {
# "amount": "5000",
# "stocks": "AAPL,GOOG,AMZN,MSFT"
# }
# }
print(lambda_handler(js,None))