Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
montecarlo_cw_files
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Someshwar, Rudra (PG/T - Comp Sci & Elec Eng)
montecarlo_cw_files
Commits
69c81676
Commit
69c81676
authored
2 years ago
by
Someshwar, Rudra (PG/T - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
Update file ec2_function.py
parent
726b1862
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ec2_function.py
+78
-0
78 additions, 0 deletions
ec2_function.py
with
78 additions
and
0 deletions
ec2_function.py
0 → 100644
+
78
−
0
View file @
69c81676
from
flask
import
Flask
,
request
,
jsonify
import
json
import
random
import
math
app
=
Flask
(
__name__
)
@app.route
(
'
/
'
,
methods
=
[
'
POST
'
])
def
run_simulation
():
# Parse the JSON input data
input_json
=
request
.
json
# Extract the required parameters from the input data
minhistory
=
int
(
input_json
[
'
history
'
])
shots
=
int
(
input_json
[
'
shots
'
])
signaltype
=
str
(
input_json
[
'
signal_type
'
])
P
=
int
(
input_json
[
'
time_horizon
'
])
closing_prices
=
input_json
[
'
closing_prices
'
]
buy_signals
=
input_json
[
'
buy_signals
'
]
sell_signals
=
input_json
[
'
sell_signals
'
]
# print(minhistory, shots, signaltype, P)
# print(buy_signals)
# print(sell_signals)
# create empty lists to store the results
risk95_values
=
[]
risk99_values
=
[]
for
i
in
range
(
minhistory
,
len
(
closing_prices
)):
if
(
i
+
P
)
<
len
(
closing_prices
):
# this ignores signals where we don't have price_p_days_forward
if
signaltype
==
"
Buy
"
and
buy_signals
[
i
]
==
1
:
# for buy signals
# calculate the mean and standard deviation of the price changes over the past minhistory days
pct_changes
=
[
closing_prices
[
j
]
/
closing_prices
[
j
-
1
]
-
1
for
j
in
range
(
i
-
minhistory
,
i
)]
# percent changes with previous closing price
mean
=
sum
(
pct_changes
)
/
len
(
pct_changes
)
std
=
math
.
sqrt
(
sum
([(
x
-
mean
)
**
2
for
x
in
pct_changes
])
/
len
(
pct_changes
))
# generate much larger random number series with same broad characteristics
simulated
=
[
random
.
gauss
(
mean
,
std
)
for
x
in
range
(
shots
)]
# sort and pick 95% and 99% - not distinguishing long/short risks here
simulated
.
sort
(
reverse
=
True
)
var95
=
simulated
[
int
(
len
(
simulated
)
*
0.95
)]
var99
=
simulated
[
int
(
len
(
simulated
)
*
0.99
)]
# record the risk values
risk95_values
.
append
(
var95
)
risk99_values
.
append
(
var99
)
elif
signaltype
==
"
Sell
"
and
sell_signals
[
i
]
==
1
:
# calculate the mean and standard deviation of the price changes over the past minhistory days
pct_changes
=
[
closing_prices
[
j
]
/
closing_prices
[
j
-
1
]
-
1
for
j
in
range
(
i
-
minhistory
,
i
)]
# percent changes with previous closing price
mean
=
sum
(
pct_changes
)
/
len
(
pct_changes
)
std
=
math
.
sqrt
(
sum
([(
x
-
mean
)
**
2
for
x
in
pct_changes
])
/
len
(
pct_changes
))
# generate much larger random number series with same broad characteristics
simulated
=
[
random
.
gauss
(
mean
,
std
)
for
x
in
range
(
shots
)]
# sort and pick 95% and 99% - not distinguishing long/short risks here
simulated
.
sort
(
reverse
=
True
)
var95
=
simulated
[
int
(
len
(
simulated
)
*
0.95
)]
var99
=
simulated
[
int
(
len
(
simulated
)
*
0.99
)]
# record the risk values
risk95_values
.
append
(
var95
)
risk99_values
.
append
(
var99
)
return
(
risk95_values
,
risk99_values
)
# # Return the results as a JSON response
# response_json = {
# 'result': result
# }
# return jsonify(response_json)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment