2015-07-03 40 views
0

鑑於一系列的交易考慮到交易歷史,您如何計算交易者的損益?

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 

的你如何在編程方面%計算隨着時間的推移該交易員的表現?有沒有圖書館可以爲你做到這一點?

這個想法是使用這些數據來創建一個長期的圖表,以瞭解這個交易者的表現並能夠將其與其他交易者進行比較。

+0

balance = [];如果side ==「buy」;餘額<< Price * Quantity *(1-comission);其他餘額<< -Quantity *價格*(1-comission); end_balance = balance.inject {| sum,x | sum + x}; pnl =(end_balance + initial_balance)/ initial_balance -1 –

回答

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