首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >考虑到交易员的交易历史,你如何计算他们的损益表?

考虑到交易员的交易历史,你如何计算他们的损益表?
EN

Stack Overflow用户
提问于 2015-07-03 16:58:15
回答 1查看 926关注 0票数 2

考虑到一系列的交易

代码语言:javascript
复制
Symbol,Quantity,Price,Side
SPY,100,127,Buy
SPY,87,125,Sell
SPY,109,115,Sell
SPY,122,95,Sell
SPY,66,89,Buy
SPY,101,175,Sell

您如何以%的方式编程计算这个交易者的表现?有图书馆可以为你做到这一点吗?

这个想法是利用这些数据来创建一个% P&L图表,随着时间的推移,得到这个交易者的表现的想法,并能够比较它与其他交易者。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-03 20:15:44

代码语言:javascript
复制
variables used:
  - pos_base = 0      // base price of the position, at any given time
  - pos_amount = 0    // position amount (negative for shorts), at any given time
  - pl = 0            // total P/L, at any given time, fees already removed
  - tot_fees = 0      // total fees paid
  - volume = 0        // total volume exchanged

RECORD_EXECUTION(side, amount, price, fee_rate)
{
  // normalize amount
  n_amount = amount * (side == buy ? 1.0 : -1.0)

  // remember base price and amount before changing them
  prev_base = pos_base
  prev_amount = pos_amount

  // update 'virtual' position
  pos_amount += n_amount
  pos_base = pos_amount == 0.0 ? 0.0 : (prev_amount * prev_base + n_amount * price) / pos_amount
  if ((prev_amount < 0.0 && pos_amount > 0.0) || (prev_amount > 0.0 && pos_amount < 0.0)) pos_base = price

  // calculate fees paid
  fees_paid = amount * price * fee_rate
  tot_fees += fees_paid

  // update P/L
  if (prev_amount < 0.0 && pos_amount >= 0.0) pl += (prev_base - price) * -prev_amount // short closed
  if (prev_amount > 0.0 && pos_amount <= 0.0) pl += (price - prev_base) *  prev_amount // long closed
  pl -= fees_paid

  // keep track of total volume
  volume += amount
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31211438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档