Description
Solutions
Stock Buy Sell Position Indicator

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:

buyIndicator = [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 from 51->56->56->58. 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.

Function Description

Write a function in python:

def solution(prices, buyIndicator, sellIndicator):

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.

Example 1:

Input:  prices = [51, 56, 56, 58, 60, 59, 54, 57, 52, 48], buyIndicator = [1, 1], sellIndicator = [-1, -1, 1]
Output: [0, 0, 0, 1, 2, 2, 2, 1, 1, 1]
Explanation:

For the test case:

buyIndicator = [1, 1] // buy after 2 price increases
sellIndicator = [-1, -1, 1] // sell after 2 price decreases then an increase
prices = [51, 56, 56, 58, 60, 59, 54, 57, 52, 48]

The result should be:

position = [0, 0, 0, 1, 2, 2, 2, 1, 1, 1]

Constraints:
    na
Thumbnail 0
Testcase

Result
Case 1

input:

output: