Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are working for a promising new stock trading startup called AlgoStreet. You have been tasked with developing a new trading signal

"""
You are working for a promising new stock trading startup called "AlgoStreet."
You have been tasked with developing a new trading signal that will be incorporated into the
automatic trading strategy. A new metric has been introduced called
positive stock pressure, which measures how many consecutive days before today (not includ-
ing today) have a higher price.
You will be given daily stock prices for the last N days and must return the list of daily positive
stock pressures for each day.
Below is an example for 1 week of data.
| Price | Stock Pressure
------------------------------
| Day 1|100|1
------------------------------
| Day 2|90|2
------------------------------
| Day 3|95|1
------------------------------
| Day 4|100|1
-------------------------------
| Day 5|105|1
-------------------------------
| Day 6|110|1
-------------------------------
| Day 7|80|7
-------------------------------
Implement the function to compute stock pressure.
To compute positive stock pressure for each stock price, you should find the last day with the
lower (or equal) price. In other words, the positive stock pressure is the number of days since the
last day with a lower or equal price.
You must solve this problem by using a stack as the primary data structure.
Code Author:
Running Time Analysis of compute_pressure
--------------------
"""
def compute_pressure(stock_history: list):
n = len(stock_history)
pressures =[0]* n
stack =[]
for i in range(n):
while stack and stock_history[stack[-1]]<= stock_history[i]:
stack.pop()
if not stack:
pressures[i]= i +1
else:
pressures[i]= i - stack[-1]
stack.append(i)
return pressures
class TestGeneratePressure:
def run_unit_tests(self):
self.test_example()
self.test_2()
self.test_3()
self.test_no_days_provided()
self.test_large_list()
self.test_repeating_prices()
def print_test_result(self, test_name, result):
color ="\033[92m" if result else "\033[91m"
reset ="\033[0m"
print(f"{color}[{result}]{test_name}{reset}")
def test_answer(self, test_name, result, expected):
if result == expected:
self.print_test_result(test_name, True)
else:
self.print_test_result(test_name, False)
print(f"Expected: {expected}
Got: {result}")
def test_example(self):
stock_history =[100,90,95,100,105,110,80]
result = compute_pressure(stock_history)
expected_answer =[1,2,1,1,1,1,7]
self.test_answer("test_example", result, expected_answer)
def test_2(self):
stock_history =[80,74,75,90,120,81]
result = compute_pressure(stock_history)
expected_answer =[1,2,1,1,1,3]
self.test_answer("test_2", result, expected_answer)
def test_3(self):
stock_history =[1,2,5,10,12,20]
result = compute_pressure(stock_history)
expected_answer =[1,1,1,1,1,1]
self.test_answer("test_3", result, expected_answer)
def test_no_days_provided(self):
stock_history =[]
result = compute_pressure(stock_history)
expected_answer =[]
self.test_answer("test_no_days_provided", result, expected_answer)
def test_large_list(self):
stock_history =[100,90,80,85,90,95,100,105,110,120,140,120,100,80]
result = compute_pressure(stock_history)
expected_answer =[1,2,3,1,1,1,1,1,1,1,1,2,6,11]
self.test_answer("test_large_list", result, expected_answer)
def test_repeating_prices(self):
stock_history =[10,10,10]
result = compute_pressure(stock_history)
expected_answer =[1,1,1]
self.test_answer("test_repeating_prices", result, expected_answer)
if __name__=='__main__':
test_runner = TestGeneratePressure()
test_runner.run_unit_tests()
The formula for calculating positive stock pressure is: day - last day wirh smaller or equal price
Day 1: 1-0=1
Day 2: 2-0=2
Day 3: 3-2=1
Day 4: 4-3=1
Day 5: 5-4=1
Day 6: 6-5=1
Day 7: 7-0=7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

=+j What rules will apply to the process of negotiations?

Answered: 1 week ago