otp.Source.pnl_realized#

Source.pnl_realized(computation_method='fifo', output_field_name='PNL_REALIZED', inplace=False)#

Computes the realized Profit and Loss (PNL) on each tick and is applicable to both scenarios, whether selling after buying or buying after selling.

Parameters
  • computation_method (str) –

    This parameter determines the approach used to calculate the realized Profit and Loss (PnL).

    Possible options are:

    • fifo - Stands for ‘First-In-First-Out,’ is used to calculate Profit and Loss (PnL) based on the principle that the first trading positions bought are the first ones to be sold, or conversely, the first trading positions sold are the first ones to be bought.

  • output_field_name (str) –

    This parameter defines the name of the output field.

    Default: PNL_REALIZED

  • self (Source) –

Return type

Optional[Source]

Examples

Simple usage

>>> data = otp.DataSource('SOME_DATABASE', tick_type='TT', symbols='AA')  
>>> data = data.pnl_realized()  
>>> otp.run(data)  
                     Time  BUY_SELL_FLAG  PRICE  SIZE  PNL_REALIZED
0 2003-12-01 00:00:00.000              0      5    20           0.0
1 2003-12-01 00:00:00.001              0      4    30           0.0
2 2003-12-01 00:00:00.002              1      3    15         -30.0
3 2003-12-01 00:00:00.003              0     10    50           0.0
4 2003-12-01 00:00:00.004              1      7    18          49.0
5 2003-12-01 00:00:00.005              1      9     6          30.0
6 2003-12-01 00:00:00.006              0      8    40           0.0

See also

PNL_REALIZED OneTick event processor