Instructions
Objective
Write a Python assignment program to calculate drawdown in the Python language. This program aims to determine the drawdown, which is a measure of the decline from a peak value to a trough value in a time series. By writing a Python assignment that calculates drawdown, you'll be able to analyze the magnitude of losses within the given data. This assignment provides an opportunity to showcase your understanding of Python programming concepts and your ability to manipulate financial data for analytical purposes.
Requirements and Specifications
Source Code
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'drawdowncalc' function below.
#
# The function is expected to return a FLOAT.
# The function accepts following parameters:
# 1. FLOAT_ARRAY rets
# 2. INTEGER N
#
def findmax(all_amounts):
maxdrawdowncalc = None
maxdrawdowncalc_int = None
maxdrawdowncalc_index = -1
for k in range(len(all_amounts)):
amounts = all_amounts[k]
l = len(amounts)
for i in range(l-1):
for j in range(i+1,l):
drawdown = (amounts[i]-amounts[j])/amounts[i]
if drawdown > 0:
if (maxdrawdowncalc is None) or (drawdown > maxdrawdowncalc):
maxdrawdowncalc = drawdown
maxdrawdowncalc_int = [i,j]
maxdrawdowncalc_index = k
new_amounts = []
for k in range(len(all_amounts)):
if k != maxdrawdowncalc_index:
new_amounts.append(all_amounts[k])
else:
new_amounts.append(all_amounts[k][:maxdrawdowncalc_int[0]+1])
new_amounts.append(all_amounts[k][maxdrawdowncalc_int[1]:])
return maxdrawdowncalc, new_amounts
def drawdowncalc(rets, N):
curr = 1
amounts = [curr]
for i in range(len(rets)):
curr *= (1 + rets[i])
amounts.append(curr)
result = None
new_amounts = [amounts]
for i in range(N):
result, new_amounts = findmax(new_amounts)
if result is None:
return i
return -round(result, 4)
if __name__ == '__main__':
l = int(input())
rets = []
for _ in range(l):
rets.append(float(input()))
n = int(input())
print(drawdowncalc(rets, n))
# test cases
# print(drawdowncalc([0.01, -0.01, 0.004, -0.02, 0.01], 1))
# print(drawdowncalc([0.01, -0.01, -0.01, 0.05, -0.04, 0.01], 2))
# print(drawdowncalc([0.01, -0.04, 0.05, -0.01, -0.01, 0.01], 3))
# print(drawdowncalc([0.01, 0.01, 0.01, -0.01, 0.01, 0.01], 1))
# print(drawdowncalc([0.01, 0.02, 0.03, -0.01, -0.02, 0.04, 0.05, -0.02, -0.02, 0.03], 2))
Similar Samples
Explore our range of programming assignment samples at ProgrammingHomeworkHelp.com. From Java to Python, C++, and beyond, our samples showcase effective solutions to enhance your programming knowledge. Each example is tailored to assist students in mastering essential coding concepts. Find inspiration and improve your skills with our diverse collection of practical coding samples.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python