My question is how to use vba to transform a csv file of trades into a form where I can use Excel to
analyze profit and loss and to evaluate alternative trading strategies.
My trading platform generates a record of transactions that looks similar to this:
<tbody>
</tbody>
The trade shows taking six long positions, then selling on the 7th trade. Then the account goes short twice and closes out on the tenth trade.
In this example, trades 1 to 6 are offset by trade 7, while trades 8 and 9 are offset by trade 10. Results in the real world can get even more complicated -- sometimes a trade has multiple closes as well as entries.
I have figured out how to use vba to move the values of the rows with closing trades to new columns.
Table2
<tbody>
</tbody>
The next step in this line of thinking would be to copy the values of the closing trades into the rows of the corresponding open positions. That would look like this:
Table3
<tbody>
</tbody>
Then, it would be a simple matter for me to write vba that would figure profit and loss on each leg of the trade since the values used are in the same row.
Where I am stuck is how to get from "Table2" to "Table3". I have tried different sorts of loops but the complexity of the conditions (and their variety) is beyond my beginner's skill. Manually copying the values is not feasible because of the large number of trades analyzed (plus the chance of operator error.) My apologies for writing such a long post.
I have given this the old college try, but I think I need help now. Thanks in advance.
analyze profit and loss and to evaluate alternative trading strategies.
My trading platform generates a record of transactions that looks similar to this:
a | b | c | d |
Id | Side | Price | Date/Time |
1 | Buy to Open | 2,165.25 | 8/1/16 9:00 |
2 | Buy to Open | 2,167.00 | 8/1/16 9:10 |
3 | Buy to Open | 2,168.75 | 8/1/16 9:20 |
4 | Buy to Open | 2,168.25 | 8/1/16 9:30 |
5 | Buy to Open | 2,168.25 | 8/1/16 9:45 |
6 | Buy to Open | 2,171.00 | 8/1/16 10:10 |
7 | Sell to Close | 2,171.75 | 8/1/16 10:25 |
8 | Sell to Open | 2,171.75 | 8/1/16 10:25 |
9 | Sell to Open | 2,167.25 | 8/1/16 10:55 |
10 | Buy to Close | 2,161.25 | 8/1/16 11:30 |
<tbody>
</tbody>
The trade shows taking six long positions, then selling on the 7th trade. Then the account goes short twice and closes out on the tenth trade.
In this example, trades 1 to 6 are offset by trade 7, while trades 8 and 9 are offset by trade 10. Results in the real world can get even more complicated -- sometimes a trade has multiple closes as well as entries.
I have figured out how to use vba to move the values of the rows with closing trades to new columns.
Table2
Id | Side | Price | Date/Time | ClosingSide | Close_Price | ProfitOrLoss |
1 | Buy to Open | 2,165.25 | 8/1/16 9:00 | 0 | ||
2 | Buy to Open | 2,167.00 | 8/1/16 9:10 | 0 | ||
3 | Buy to Open | 2,168.75 | 8/1/16 9:20 | 0 | ||
4 | Buy to Open | 2,168.25 | 8/1/16 9:30 | 0 | ||
5 | Buy to Open | 2,168.25 | 8/1/16 9:45 | 0 | ||
6 | Buy to Open | 2,171.00 | 8/1/16 10:10 | 0 | ||
7 | Sell to Close | 2171.75 | ||||
8 | Sell to Open | 2,171.75 | 8/1/16 10:25 | 0 | ||
9 | Sell to Open | 2,167.25 | 8/1/16 10:55 | 0 | ||
10 | Buy to Close | 2161.25 |
<tbody>
</tbody>
The next step in this line of thinking would be to copy the values of the closing trades into the rows of the corresponding open positions. That would look like this:
Table3
Id | Sell to Close) | Price | Date/Time | ClosingSide | Close_Price | ProfitOrLoss |
1 | Buy to Open | 2,165.25 | 8/1/16 9:00 | Sell to Close | 2171.75 | |
2 | Buy to Open | 2,167.00 | 8/1/16 9:10 | Sell to Close | 2171.75 | |
3 | Buy to Open | 2,168.75 | 8/1/16 9:20 | Sell to Close | 2171.75 | |
4 | Buy to Open | 2,168.25 | 8/1/16 9:30 | Sell to Close | 2171.75 | |
5 | Buy to Open | 2,168.25 | 8/1/16 9:45 | Sell to Close | 2171.75 | |
6 | Buy to Open | 2,171.00 | 8/1/16 10:10 | Sell to Close | 2171.75 | |
7 | Sell to Close | 2,171.75 | 8/1/16 10:25 | Sell to Close | 2171.75 | |
8 | Sell to Open | 2,171.75 | 8/1/16 10:25 | Buy to Close | 2161.25 | |
9 | Sell to Open | 2,167.25 | 8/1/16 10:55 | Buy to Close | 2161.25 | |
10 | Buy to Close | 2,161.25 | 8/1/16 11:30 | Buy to Close | 2161.25 |
<tbody>
</tbody>
Then, it would be a simple matter for me to write vba that would figure profit and loss on each leg of the trade since the values used are in the same row.
Where I am stuck is how to get from "Table2" to "Table3". I have tried different sorts of loops but the complexity of the conditions (and their variety) is beyond my beginner's skill. Manually copying the values is not feasible because of the large number of trades analyzed (plus the chance of operator error.) My apologies for writing such a long post.
I have given this the old college try, but I think I need help now. Thanks in advance.