首页 > 解决方案 > 英国税收的算法/伪代码(资本收益)

问题描述

我一直在寻找实现英国 CGT 税收的算法(或伪代码)(如HRMC 网站上所述),但我无法理解我需要在不循环太多的情况下有效执行此操作的算法次。我尝试尽可能多地搜索,甚至尝试了一些我自己的实现,但似乎无法让它正常工作。

TimeToTrade 的网站上也有一个恰当的例子。我已经尽我所能在这里解释了。

假设我有以下买入/卖出表(日期为 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. 1+2合并 = 总计 3000 股 @ 3000 英镑(每股 1 英镑)

  2. 3在同一天有卖出交易,所以暂时不合并。

    • 2000股中的1000股当日匹配(先进先出)
      • 1000 股 @ 1.5 英镑 = 1500 英镑 - 收益 1000 * 2.5 英镑 - 1500 英镑 = 1000 英镑
    • 卖出4剩余的 1000 股中有 1000 股与未来 30 天内的买入匹配 ( 6 )
      • 1000 股 @ 0.5 英镑 = 500 英镑 - 收益 1000 * 2.5 英镑 - 500 英镑 = 2000 英镑
    • 卖出交易 4 的总应课税收益为 £1000+£2000=£3000
  3. 5不能再在同一天与买3匹配,因为它已经耗尽了匹配的股票

    • 1000 股中有 500 股与买入6匹配,因为它还剩 500 股
      • 500 股 @ 0.5 = 250 英镑 - 收益为 (500*2 英镑)- 250 英镑 = 750 英镑
    • 剩余500股中的500股最终与(1+2)的集合持股匹配
      • 500 股 @ 1 = 500 英镑 - 收益 (500*2 英镑)-500 英镑 = 500 英镑
    • 卖出交易 5 的总应课税收益为 £750+500=£1250

总应课税收益£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   

任何指向正确方向的指针都将不胜感激。

标签: pythonalgorithmpseudocode

解决方案


推荐阅读