我一直在寻求为英国CGT税收实施算法(或伪代码)(如HRMC's website所述),但我无法理解我需要做的那种算法这有效地没有循环太多次。我试图尽可能多地搜索,甚至尝试了一些我自己的实现,但似乎无法让它正常工作。
TimeToTrade's website也有一个恰当的例子。我尽力在这里解释。
假设我有以下买/卖表(日期为yyyy-mm-dd):
id action amount per share total date
1. buy 2000 £1 £2000 2017-05-05 10:00
2. buy 1000 £1 £1000 2017-05-10 10:00
3. buy 1000 £1.5 £1500 2017-06-01 10:00
4. sell 2000 £2.5 £5000 2017-06-01 10:00
5. sell 1000 £2 £2000 2017-06-01 10:00
6. buy 1500 £0.5 £750 2017-06-20 10:00
根据HRMC,必须遵守以下规则:
在手动操作时,我给出了以下计算:
1 + 2 合并= 3000股@ 3000英镑(每股1英镑)
3 在同一天有卖出交易,因此暂时不会合并。
5 在同一天无法再与 3 相匹配,因为它已经耗尽了匹配的股票
总收费增加£3000 +£1250 =£4250 - 剩余可持续2500股@ 2500英镑
我认为需要的伪代码:
- loop through all trades chronologically
- if it's a sell trade
- check if there any trades on the same day to match
- check for future buy trades (where sell trade < buy trade < sell trade + 30 days)
- use existing holdings if there are none and/or the matches aren't sufficient to fill the sell
- if it's a buy trade
- add it to a 'same_day' array
- loop through 'same_day' array on the next day to sum it up into a 'section 104 holding'
- keep reference of each trade in an indexed by trade id dictionary/object/hashmap to subtract and such when matching sell trades
非常感谢任何正确方向的指针。