Answered step by step
Verified Expert Solution
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 week of data.
Price Stock Pressure
Day
Day
Day
Day
Day
Day
Day
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 computepressure
def computepressurestockhistory: list:
n lenstockhistory
pressures n
stack
for i in rangen:
while stack and stockhistorystack stockhistoryi:
stack.pop
if not stack:
pressuresi i
else:
pressuresi i stack
stack.appendi
return pressures
class TestGeneratePressure:
def rununittestsself:
self.testexample
self.test
self.test
self.testnodaysprovided
self.testlargelist
self.testrepeatingprices
def printtestresultself testname, result:
color m if result else m
reset m
printfcolorresulttestnamereset
def testanswerself testname, result, expected:
if result expected:
self.printtestresulttestname, True
else:
self.printtestresulttestname, False
printfExpected: expected
Got: result
def testexampleself:
stockhistory
result computepressurestockhistory
expectedanswer
self.testanswertestexample", result, expectedanswer
def testself:
stockhistory
result computepressurestockhistory
expectedanswer
self.testanswertest result, expectedanswer
def testself:
stockhistory
result computepressurestockhistory
expectedanswer
self.testanswertest result, expectedanswer
def testnodaysprovidedself:
stockhistory
result computepressurestockhistory
expectedanswer
self.testanswertestnodaysprovided", result, expectedanswer
def testlargelistself:
stockhistory
result computepressurestockhistory
expectedanswer
self.testanswertestlargelist", result, expectedanswer
def testrepeatingpricesself:
stockhistory
result computepressurestockhistory
expectedanswer
self.testanswertestrepeatingprices", result, expectedanswer
if namemain:
testrunner TestGeneratePressure
testrunner.rununittests
The formula for calculating positive stock pressure is: day last day wirh smaller or equal price
Day :
Day :
Day :
Day :
Day :
Day :
Day :
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started