量化交易策略

已實現和未實現損益

  • February 3, 2020

https://www.tradingtechnologies.com/xtrader-help/fix-adapter-reference/pl-calculation-algorithm/understanding-pl-calculations/

我想知道的是,在本文的場景 4 之後,已實現盈虧、開盤價、未實現盈虧如何表現,現在方向已經改變!

編輯:我已經想出了當開倉頭寸> = 0時如何處理這種情況,但是,我想知道在本文的場景4之後會發生什麼,在下一次填充期間,開倉頭寸將<0,如何那麼不同的因素是否會表現出來。

我想我終於得到了我的問題所需的答案。我正在共享 Python 3 中的程式碼,以防它幫助任何遇到類似問題的人。

在 Python 3 中執行盈虧

import pandas as pd
import numpy as np
dataset=pd.read_csv('trades.txt',delimiter= ' ');

#Initialising Variables to be used throughout the program
position=0;
averageopenprice=0;
pnlunrealized=0;
pnlrealized=0;
pnltotal=0;

#Inserting Columns into the dataset to visualise the working in a better way
dataset.insert(2,'Position',np.zeros(len(dataset.index)));
dataset.insert(3,'Average Open Price',np.zeros(len(dataset.index)));
dataset.insert(4,'PnL Unrealized',np.zeros(len(dataset.index)));
dataset.insert(5,'PnL Realized',np.zeros(len(dataset.index)));
dataset.insert(6,'PnL Total',np.zeros(len(dataset.index)));

#Algorithm Begins
for i in range(0,len(dataset.index)):
   qty=dataset.iat[i,0]; #This will help in writing more legible codes
   price=dataset.iat[i,1];
   if position==0:
       position=qty;
       averageopenprice=price;
       pnlrealized=pnlrealized;
       pnlunrealized=0;
       pnltotal=pnlrealized+pnlunrealized;
   elif position&gt;0:
       if qty&gt;0:
           averageopenprice=(position*averageopenprice+qty*price)/(position+qty);
           position+=qty;
           pnlrealized=pnlrealized;
           pnlunrealized=(price-averageopenprice)*position;
           pnltotal=pnlrealized+pnlunrealized;
       elif qty&lt;0:
           if abs(qty)&lt;position:
               averageopenprice=averageopenprice;
               pnlrealized+=(price-averageopenprice)*abs(qty);
               position+=qty;
               pnlunrealized=(price-averageopenprice)*position;
               pnltotal=pnlrealized+pnlunrealized;
           elif abs(qty)==position:
               pnlrealized+=(price-averageopenprice)*abs(qty);
               position=0;
               averageopenprice=0;
               pnlunrealized=0;
               pnltotal=pnlrealized+pnlunrealized;
           elif abs(qty)&gt;position:
               pnlrealized+=(price-averageopenprice)*position;
               position+=qty;
               averageopenprice=price;
               pnlunrealized=(price-averageopenprice)*position;
               pnltotal=pnlrealized+pnlunrealized;
   elif position&lt;0:
       if qty&lt;0:
           averageopenprice=(abs(position)*averageopenprice+abs(qty)*price)/(abs(position+qty));
           position+=qty;
           pnlrealized=pnlrealized;
           pnlunrealized=(averageopenprice-price)*abs(position);
           pnltotal=pnlrealized+pnlunrealized;
       elif qty&gt;0:
           if qty&lt;abs(position):
               averageopenprice=averageopenprice;
               pnlrealized=(averageopenprice-price)*qty;
               position+=qty;
               pnlunrealized=(averageopenprice-price)*abs(position);
               pnltotal=pnlrealized+pnlunrealized;
           elif qty==abs(position):
               pnlrealized+=(averageopenprice-price)*qty;
               position=0;
               averageopenprice=0;
               pnlunrealized=0;
               pnltotal=pnlrealized+pnlunrealized;
           elif qty&gt;abs(position):
               pnlrealized+=(averageopenprice-price)*abs(position);
               position+=qty;
               averageopenprice=price;
               pnlunrealized=0;
               pnltotal=pnlrealized+pnlunrealized;
   dataset.iat[i,2]=position;
   dataset.iat[i,3]=averageopenprice;
   dataset.iat[i,4]=pnlunrealized;
   dataset.iat[i,5]=pnlrealized;
   dataset.iat[i,6]=pnltotal;


output=dataset[['qty','price','PnL Realized','PnL Unrealized','PnL Total']];   

引用自:https://quant.stackexchange.com/questions/50979