Question

Write a python function called "get_sma", that takes two input arguments, similar to the example provided....

Write a python function called "get_sma", that takes two input arguments, similar to the example provided. First argument is the original prices of type list, and the 2nd input argument being the number of days we want to calculate SMA, like a 8 day sma or 35 day sma. This function should return a new list of sma price, based on the input day range, and should be able to handle any SMA calculation, like: sma200 for 200 days moving average.

Example code:

def sma(prices, nday):
sma_data = [0] * len(prices)
for i in range(nday, len(prices)):
sma_data[i] = sum(prices[i-nday:i]) / nday
return sma_data

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Program in Python

import sys # for sys.exit()
# required function which takes a list of prices
# and number of days in SMA and returns a SMA list

def get_sma(prices,nday):
# list to store the calculated SMA
list = []
# iterate over the price list
for i in range(0,n-nday+1):
# local temporary variable to store the intermediate results
temp = 0;
# iterate to find the SMA
for j in range(i,i + nday):
temp += prices[j]
# append the SMA value to the list
list.append(float(temp/nday))
# return the list
return list

# price_list list
price_list = []

# get the number of elements in the input list
n = int(input("Enter number of elements : "))

print ("\nEnter elements in the price list: ")
# store the input as list by iterating within range
for i in range(0, n):
ele = float(input())
# add the element to the list
price_list.append(ele)

# get the number of days in SMA
NoOfDay = int(input("Enter number of SMA Days : "))

# check if days of SMA is greater than size of input list
if n < NoOfDay:
print ("Days of SMA is greater than size of input list!")
# exit from the program
sys.exit(0)
# check if days of SMA is equal to 0
elif NoOfDay == 0:
print ("Invalid Days of SMA!")
# exit from the program
sys.exit(0)
  
# call the get_sma function to get the SMA list
a = get_sma(price_list,NoOfDay)
# print the SMA list
print("Required SMA list is " , a)

# Output

Enter number of elements : 5
Enter elements in the price list: 43.5
20.5
32.25
54
12

Enter number of SMA Days : 4

Required SMA list is [37.5625, 29.6875]

# Screenshot of Code, Input & Output

(Thank You!)

Add a comment
Know the answer?
Add Answer to:
Write a python function called "get_sma", that takes two input arguments, similar to the example provided....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON Write a function called "def top_prices(prices, n):", which will take two arguments. The first...

    IN PYTHON Write a function called "def top_prices(prices, n):", which will take two arguments. The first argument is the list of prices, each price should be a number type (int, float). The second argument 'n' indicates top number of prices. For example, if n-3, this function should return a new list of the top 3 prices, sorted from lowest to highest. If n-10, this function should return a new list of the top 10 prices, sorted from lowest to highest....

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • In Python. #1. please write a function called "stocksell", #2. the input parameters are the price...

    In Python. #1. please write a function called "stocksell", #2. the input parameters are the price list (l) and the ith day,i from [0, 7], #3. the return value is the jth day you should sell to make the most profit, #4. print the profit #for example a = stocksell(l, 2) then a = 6 #(2nd day price is 32, selling day is 6th day, price is 35) l = [34,47,32, 28, 29, 31, 35,34]

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

    IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList). For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3,...

  • Write a function that takes two lists that are in ascending order as arguments. The function...

    Write a function that takes two lists that are in ascending order as arguments. The function should return a new list that has all the values from the two argument lists and is also in ascending order (e.g., [1,3,5] and [2,4,6] become [1,2,3,4,5,6]). Please use python

  • Write a Python Code for a Function: you need to come up with code for shift_char....

    Write a Python Code for a Function: you need to come up with code for shift_char. Write the code in a way in which you can shift the character by writing the number of shifts. Use the ASCII code for this. For example in lie 11, the input for char_to shift is r. U shift it by 1 so it becomes s. below is the function def shift_char(char_to_shift, amount_to_shift): ''' shifts any character by moving it a certain amount on...

  • In Python beginner code: Write a function named even_div, that takes two arguments (you can safely...

    In Python beginner code: Write a function named even_div, that takes two arguments (you can safely assume that both arguments will always be non-zero integers). When this function is called, it should return how many times the first argument can be divided by the second argument, if it is evenly divisible (no remainder). If it is not evenly divisible, the function should return 0. Examples: even_div(5, 2) will return 0 (5 divided by 2 is 2.5, not evenly divisible) even_div(10,...

  • Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount,...

    Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount, an integer specifying the number of “buckets” to return and numberLs, a list of numbers ) buckets should split the range of values in numberLs into bucketCount sub-ranges. Specifically buckets should return a list-of-lists of length bucketCount. Each list in the returned list-of-lists represents a “bucket”. Each bucket should contain the numbers from numberLs whose values are greater than or equal to min(numberLs) +...

  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT