Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given a sequence of integer prices, we can determine when to buy and sell by identifying special sequences of prices movements called indicators. For example,
Given a sequence of integer prices, we can determine when to buy and sell by identifying special sequences
of prices movements called indicators.
For example, we can decide to buy every time we see consecutive price increases, and we can decide to sell
every time we see consecutive price decreases followed by a price increase. If we represent a price
increase as and a price decrease as we could write the above rules as:
buylndicator buy after price increases
sellindicator sell after price decreases then increase
Specifically, consider the price sequence:
indexes
prices
According to the buysell rules described in the indicators above, we would decide to buy at index
price because that price is a second consecutive price increase. Note that the price changes need not
occur on consecutive indices. For example the sequence counts as a buy signal since after
ignoring indices with no change, the price increased twice from We would also buy at index
price for the same reason. We would sell at indexprice because that price is preceded by
consecutive price decreases then an increase. Buysell signal will never trigger if the current price is equal to
the previous price ie if the price sequence is we will only buy once at index Note that
this is just an example of buyIndicator and sellIndicator they may differ between tests.
Finally, we define a position as the sum of buys and sells we have done up to that point, where a buy and a
sell When we have made no buys or sells, our position is In the above example, our positions would be:
indexes
prices
position
Note that when we buy at index and index our position increases by each time, and when we sell at
index our position decreases by
Write a function:
solutionprices buyIndicator, selllndicator
that, given a sequence of prices and sequences defining buy and sell indicators, returns a list of positions.
Your position list must be the same length as the input price list. The buyIndicator and sellindicator lists will
each have at least one element and each element can only be or It is possible for both the buy and sell
indicators to match at the same time. In that case the change in position should be
Your solution will be evaluated both on correctness and coding style. We will read your code.
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