Question: 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 2 consecutive price increases, and we can decide to sell
every time we see 2 consecutive price decreases followed by a price increase. If we represent a price
increase as 1 and a price decrease as -1, we could write the above rules as:
buylndicator =[1,1],?? buy after 2 price increases
sellindicator =-1,-1,1?? sell after 2 price decreases then an increase
Specifically, consider the price sequence:
indexes =[0,1,2,3,4,5,6,7,8,9]
prices =[51,56,56,58,60,59,54,57,52,48]
According to the buy/sell rules described in the indicators above, we would decide to buy at index=3
(price=58) because that price is a second consecutive price increase. Note that the price changes need not
occur on consecutive indices. For example the sequence "51565658" counts as a buy signal since after
ignoring indices with no change, the price increased twice from 515658. We would also buy at index =4
(price=60) for the same reason. We would sell at index=7(price=57) because that price is preceded by 2
consecutive price decreases then an increase. Buy/sell signal will never trigger if the current price is equal to
the previous price (i.e. if the price sequence is 51,56,56,58,58 we will only buy once at index=3). 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=1 and a
sell=-1. When we have made no buys or sells, our position is 0. In the above example, our positions would be:
indexes =[0,1,2,3,4,5,6,7,8,9]
prices =[51,56,56,58,60,59,54,57,52,48]
position =[0,0,0,1,2,2,2,1,1,1]
Note that when we buy at index =3 and index=4 our position increases by 1 each time, and when we sell at
index=7 our position decreases by 1.
Write a function:
solution(prices, 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 1 or -1. 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 1+(-1)=0.
Your solution will be evaluated both on correctness and coding style. We will read your code.
 Given a sequence of integer prices, we can determine when to

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!