1

Title may be misleading, not sure what this is called what I am aiming to do.

I want to calculate the difference between the first time I buy, at 79, until I sell, at 38, which I then calculate 38 until 34, where a buy happens again.

Please see the desired output for clarification.

Data for reproducibility:

df = pd.DataFrame([[1, 0], [1, 0], [1, 0],[1, 0],[1, 0],[1, 0],[0, 1],[0, 1],[0, 1],[0, 1],[0, 1],[0, 1],[1, 0],[1, 0],[1, 0],[1, 0],[1, 0],[1, 0],[1, 0],[0, 1],[0, 1],[0, 1],[0, 1],[0, 1],[1, 0],[1, 0],[1, 0],[1, 0],[1, 0]], columns=['BUY', 'SELL'])
df['Price'] = np.random.randint(30, 100, df.shape[0])

Current output.

  Price BUY SELL
0   79  1   0
1   47  1   0
2   70  1   0
3   58  1   0
4   53  1   0
5   57  1   0
6   38  0   1
7   86  0   1
8   64  0   1
9   53  0   1
10  47  0   1
11  30  0   1
12  34  1   0
13  96  1   0
14  79  1   0
15  81  1   0
16  31  1   0
17  76  1   0
18  38  1   0
19  38  0   1
20  61  0   1
21  50  0   1
22  85  0   1
23  55  0   1
24  80  1   0
25  31  1   0
26  82  1   0
27  44  1   0
28  53  1   0

Desired output.

Price  BUY SELL Buy_price Difference    
0   79  1   0   79   0
1   47  1   0   79  -0.4050632911
2   70  1   0   79  -0.1139240506
3   58  1   0   79  -0.2658227848
4   53  1   0   79  -0.3291139241
5   57  1   0   79  -0.2784810127
6   38  0   1   38  0
7   86  0   1   38  1.263157895
8   64  0   1   38  0.6842105263
9   53  0   1   38  0.3947368421
10  47  0   1   38  0.2368421053
11  30  0   1   38  -0.2105263158
12  34  1   0   34  0
13  96  1   0   34  1.823529412
14  79  1   0   34  1.323529412
15  81  1   0   34  1.382352941
16  31  1   0   34  -0.08823529412
17  76  1   0   34  1.235294118
18  38  1   0   34  0.1176470588
19  38  0   1   38  0
20  61  0   1   38  0.6052631579
21  50  0   1   38  0.3157894737
22  85  0   1   38  1.236842105
23  55  0   1   38  0.4473684211
24  80  1   0   80  0
25  31  1   0   80  -0.6125
26  82  1   0   80  0.025
27  44  1   0   80  -0.45
28  53  1   0   80  -0.3375
3
  • In row 1, difference is -0.4. How is it calculated? or do you just want the buy_price column? Commented Apr 30, 2021 at 8:09
  • Just buy price column, thank you Commented Apr 30, 2021 at 8:24
  • Difference is (Buy_price-price) /buy_price Commented Apr 30, 2021 at 8:28

1 Answer 1

1

There may be more efficient ways to do this. but the below snippet will give the desired result as well:

price = df.Price.values
buy = df.BUY.values
sell = df.SELL.values
difference = [0]
buy_price = [price[0]]
for i,v in enumerate(price[1:]):
    if sell[i+1] == buy[i]:
        buy_price.append(v)
    else:
        buy_price.append(buy_price[-1])
    difference.append((buy_price[-1] - v)/buy_price[-1])
        
    
df['buy_price'] =  buy_price
df['difference'] =  difference
df

Output:

    BUY SELL    Price   buy_price   difference
0   1   0   75  75  0.000000
1   1   0   30  75  0.600000
2   1   0   98  75  -0.306667
3   1   0   34  75  0.546667
4   1   0   62  75  0.173333
5   1   0   95  75  -0.266667
6   0   1   88  88  0.000000
7   0   1   61  88  0.306818
8   0   1   98  88  -0.113636
9   0   1   69  88  0.215909
10  0   1   53  88  0.397727
11  0   1   66  88  0.250000
12  1   0   79  79  0.000000
13  1   0   72  79  0.088608
14  1   0   67  79  0.151899
15  1   0   66  79  0.164557
16  1   0   56  79  0.291139
17  1   0   34  79  0.569620
18  1   0   97  79  -0.227848
19  0   1   72  72  0.000000
20  0   1   96  72  -0.333333
21  0   1   65  72  0.097222
22  0   1   39  72  0.458333
23  0   1   78  72  -0.083333
24  1   0   86  86  0.000000
25  1   0   70  86  0.186047
26  1   0   72  86  0.162791
27  1   0   85  86  0.011628
28  1   0   92  86  -0.069767
Sign up to request clarification or add additional context in comments.

1 Comment

I will give it a try when I'm back in the pc!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.