python - 英国税收的算法/伪代码(资本收益)
问题描述
我一直在寻找实现英国 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,必须遵守以下规则:
- 在同一天首先将销售与购买 (FIFO) 匹配
- 然后在接下来的 30 天内将销售与购买 (FIFO) 匹配
- 销售最终与过去的平均购买匹配(合并为“第 104 节持有”)
手动进行时,这给了我以下计算:
1+2合并 = 总计 3000 股 @ 3000 英镑(每股 1 英镑)
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
- 2000股中的1000股当日匹配(先进先出)
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
- 1000 股中有 500 股与买入6匹配,因为它还剩 500 股
总应课税收益£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
任何指向正确方向的指针都将不胜感激。
解决方案
推荐阅读
- kubernetes - 如何计算每个节点上特定 pod CPU 使用率的百分比?
- python - 如何在 scipy 中实现 ILU 预处理器?
- google-sheets - 谷歌表格功能分组和连接行
- python - 如何写函数的导数
- c - 如何修复 swapBack 函数以获得我想要的结果?
- python - 将多个嵌套的 JSON 文件读入 Pandas DataFrame
- c++ - 指向类内部成员函数的C++函数指针
- javascript - 将多个对象值显示为表单 Reactjs 中的输入值
- jquery - Knockout JS - 您不能多次将绑定应用于同一个元素 - MVC
- ruby-on-rails - 对使用或不使用嵌套属性感到困惑