如何根据交易历史记录计算交易员的盈亏?

3

给定一系列交易

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

如何通过编程计算这位交易员的绩效百分比?是否有库可以为您完成此操作?

目的是使用这些数据创建一个随时间变化的%盈亏图表,以便了解该交易员的表现并将其与其他交易员进行比较。


balance = []; 如果 side == "buy",则 balance << Price * Quantity*(1-comission); 否则 balance << -Quantity * Price*(1-comission); end_balance = balance.inject{|sum,x| sum + x }; pnl = (end_balance+initial_balance)/initial_balance -1 - Ilya Cherevkov
1个回答

2
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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接