Calculating loss or profit from multiple trade signals

Elvis

New Member
Joined
Apr 23, 2006
Messages
45
Hi,

I have pricing data on a company's stock price in column C ordered with oldest pricing history at the top and newest at bottom.

In column F, I have trading signals based on the prices in Column C, 1 denotes a buy and -1 denote a sell.

I need to work out the profit/loss I would make buying and selling 200 shares/units based on the corresponding buy and sell prices in column C.

I would buy every time a cell in column F generates a 1 and continue to hold the stock until a cell column F generates the next -1. And then I would buy agian on on the next occurrence of 1 in coloum F and sell on the next occurrence of -1.

So for example,

The first 1 occurs in F8, so I would to buy at the corresponding price in c8, all subsequent cells continue to have 1 until F13 (i.e. F8 to F12 all have 1 (buy signals in them so I hold the stock), a -1 appears in F13, I want to sell at the corresponding price in C13. Then I want to buy at the nest buy signal F15 and sell at the next occurring -1 (F16).

My pricing data runs from D8: D2948, I want to calculate the returns from all these separate trades and sum the results in say G2 so I can work out what that trading strategy over the entire period would yield in terms of loss or profit.

I know how I could do this for any given single trade but know all trades. I'm assuming it's likely I need some kind of continous formula in say column G to work out profit/loss for each trade and then I could simply sum that range in G2 but I'm juist guessing.

Hope someone can help!

Thanks,

Elvis
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I guess your data is something like the sheet given below.
a helper column in G or any other blank column . see formula in G2 and copy this formula down

and see the formula in F23
check whether this is ok.

Book1
ABCDEFG
1datepricebuy/sell
21/1/200970170
31/2/20098510
41/3/20099110
51/4/20097510
61/5/200984-184
71/6/200984-10
81/7/200991-10
91/8/200994-10
101/9/200972172
111/10/20097310
121/11/20099310
131/12/20097810
141/13/200983-183
151/14/200989-10
161/15/200979-10
171/16/200970-10
181/17/200973173
191/18/20098910
201/19/20097810
211/20/200994-194
22
23profit/loss46
Sheet1
 
Upvote 0
Hello Venkat,

Thanks for your solution.

I've worked out the profit/loss for few trades manually and compared against your sheet and the numbers look fine.

Using the existing framework, would it be easy to also work out the number of trades that return a profit and those that generate a loss?

Thanks,

Elvis
 
Upvote 0
At present I am not able to think of any formula solutions. I have written two macro 1."blank" and 2. "test:. copy paste the two macro in vb editor of my file sent earlier.(IN A SEPARATE MODULE IF YOU HAVE ALREADY SOME MODULES)
then run ONLY THE MACRO 'TEST"
the no. of profits and losses are in H2 and I2. of your sheet
posts feedback .

if this is ok with m y file try this on your file.

Code:
Dim rng As Range, cel As Range
Dim profit As Integer, loss As Integer
Sub blank()
'Set rng = Range("H1:H2828")
Set rng = Range(Range("G2"), Range("G2").End(xlDown))
For Each cel In rng
If cel = 0 Then cel.Clear
Next

End Sub
Code:
Sub test()
blank
profit = 0
loss = 0
Set rng = Range(Range("G3"), Cells(Rows.Count, "G").End(xlUp))
'MsgBox rng.Address

For Each cel In rng

If cel <> "" Then
If cel.Offset(0, -1) = -1 Then
If cel > cel.End(xlUp) Then
profit = profit + 1
ElseIf cel < cel.End(xlUp) Then
'MsgBox cel.Address
loss = loss + 1
End If
End If
End If
line1:
Next
Range("H1") = "no. of profits"
Range("H2") = profit
Range("I1") = "no. of losses"
Range("I2") = loss
End Sub
 
Upvote 0
Hello again venkat1926,

Thanks for the code. I tried running it but it doesn't appear to work.

The no. of profits and losses H2 and I2 appear as 0.

Also I notice a couple of other things after running the macro, some of the the values in column H get overwritten by the h2 and I2 entries, which changes the total profit and loss number in G23. I have changed the macro for the macro output to appear in non empty cells as per below which fixes the overwriting isuue but the number of profit and loss trades still appear as 0 which is incorrect!

Thanks

Elvis

Range("j1") = "no. of profits"
Range("j2") = profit
Range("k1") = "no. of losses"
Range("k2") = loss
 
Upvote 0
see my sheet below.
F24 is manual formula
H2 and I2 are results of macro. of course my columns H and I are empty.

will this help you to modify the macro.

Elvis.xls
ABCDEFGHI
1DATEpricebuy/sellno.ofprofitsno.oflosses
21/1/20097017021
31/2/2009851
41/3/2009911
51/4/2009751
61/5/200984-184
71/6/200984-1
81/7/200991-1
91/8/200994-1
101/9/200972172
111/10/2009731
121/11/2009931
131/12/2009781
141/13/200983-183
151/14/200989-1
161/15/200979-1
171/16/200970-1
181/17/200973173
191/18/2009891
201/19/2009781
211/20/200994-154
22
23
246
Sheet1
 
Upvote 0
Hello venkat1926,

Sorry my mistake, because I didn't see the full worksheet range, I was looking at the wrong columns.

On the sumproduct function, I noticed if the first value of the sumproduct range as a -1 in column G then it includes this in the calculation (via populating the equivalent cell in column G with value from column C) when it should be excluded. Similary if the last cell in the sumproduct range (Column F) is a postive number it also included this in the calculation by (via populating equivalent number in column F based on value in column C).

In your example, the calculation is correct because the first value starts with a 1 and the last value ends with a -1, and so column G has zeros for these cells. But where the first value is a -1 and or last value is 1, then calculation is incorrect because the cells in column G do not include zeros but actula values from column C. These should be excluded from the calcuation.

Is there anyway to get the sumproduct to autoselect the range based the first occcuring 1 value and the last occuring 1- value? Or is that any other way round it?

Thanks,

Elvis
 
Upvote 0
I presume you are doing paper trading. In that case you should not start with the date of sell sign but start with a date of buy sign. similalry end with a date of sell sign.
 
Upvote 0

Forum statistics

Threads
1,223,676
Messages
6,173,761
Members
452,534
Latest member
autodiscreet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top