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!
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!