Transaction Netting: Multiple Buys/Sells - Looking to minimize number of transactions

dgarland

New Member
Joined
Dec 29, 2015
Messages
15
My goal would be to do this systematically through either functions (I doubt this is possible), or through VBA:

I receive instructions for buying or selling in accounts. Once I receive them, I submit official trade tickets for those transactions. When the trades are all one direction, there is no issue - each trade has a separate ticket.

However, when there are buys AND sells I need to incorporate exchanges. Ultimately my goal is to minimize the number of transactions in total.

As an example:
Buy 100 A
Buy 200 B
Sell 300 C

would best be output as two exchanges:
exchange 200 C for B
exchange 100 C for A

I am struggling right now to come up with the logic for this type of process, but have the following so far:

1. determine if trades are one-sided in which case do not generate any exchanges
if they are NOT one sided:
2. determine if any are perfect offsets (if a buy = 100, AND a sell = 100), if so, generate exchange(s) reflecting said match(es) and populate a table
if there are no perfect offsets:
3. start with the largest absolute value, take the smallest opposite value(s) and generate exchanges to the table until the largest value is used up, then proceed to the second largest value until there are no more offsetting trades.

Finally, after all three of the above steps, paste the residual single-direction trades in the table below the exchanges.

I imagine this can be accomplished through VBA, but so far I do not how to approach step #2. Any help in approaching this problem would be much appreciated!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Here is my partial code so far (#1 answered only) - I can likely take care of #2, but #3 is beyond my current level of VBA expertise. Any input even general tips at approaching the problem would be much appreciated!
Code:
Sub Populate_Blotter()

Dim Buys
Dim Sells


Buys = Application.SumIf(Range("D4:D20"), ">0")
Sells = Application.SumIf(Range("D4:D20"), "<0")


    If Buys > 0 And Sells < 0 Then
'Code for calculating exchanges:
'2. determine if any are perfect offsets (if a buy = 100, AND a sell = 100), _
 if so, generate exchange(s) reflecting said match(es) and populate a table


'3. start with the largest absolute value, take the smallest opposite value(s) _
 and generate exchanges to the table until the largest value is used up, then _
 proceed to the second largest value until there are no more offsetting trades.




    Else
'Paste Buys & Sells directly into blotter
Range("A4:C20").Copy
Range("A25").PasteSpecial xlPasteValues


    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,014
Messages
6,122,697
Members
449,092
Latest member
snoom82

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