如何使用先进先出法计算股票交易的已实现盈亏? [英] How to calculate realized P&L of stock trades using the FIFO method?

查看:57
本文介绍了如何使用先进先出法计算股票交易的已实现盈亏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个 Python 插件,它可以使用 FIFO 方法计算许多股票交易的已实现盈亏.

I'm looking for a Python plugin that would calculate the realized P&L for a number of stock transactions using the FIFO method.

例如,假设我们有以下三个 MSFT 交易:

For example, assume we have the following three MSFT trades:

+75 MSFT 25.10
+50 毫秒 25.12
-100 毫秒 25.22

+75 MSFT 25.10
+50 MSFT 25.12
-100 MSFT 25.22

在 25.22 卖出 100 股将完全抵消在 25.10 买入 75 股的净额,以及在 25.12 买入 50 股的部分净额,即

The sell of 100 shares at 25.22 would fully net against the buy of 75 at 25.10 and partially net against the buy of 50 at 25.12 i.e.

已实现盈亏 = 75 * (25.22 - 25.10) + 25 * (25.22 - 25.12) = 11.50 美元

Realized P&L = 75 * (25.22 - 25.10) + 25 * (25.22 - 25.12) = $ 11.50

突出位置是:

+25 MSFT 25.12

+25 MSFT 25.12

推荐答案

这应该很容易用 Python 编写.FIFO"是先进先出队列"的缩写.购买被添加到队列的后面.排在队伍前面,出售大量购买的商品(或其中的一部分).

This should be easy to write yourself in Python. "FIFO" is short for "first in, first out queue". Buys are added to the back of the queue. Sells munch buys (or parts of them) off the front of the queue.

Python 的 collection.deque(双端队列)是机械师需要什么.

Python's collection.deque (double-ended queue) is what you need for the mechanics.

这篇关于如何使用先进先出法计算股票交易的已实现盈亏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆