贸易#
贸易的定义:
当仪器的头寸从0变为大小为X的位置时(对于多头/空头仓位可能是正/负值),该交易是开放的。
当仓位从X变为0时,该交易被关闭。
以下两种情况:
正向变为负向
负向变为正向
实际上被视为:
一个交易已关闭(仓位从X变为0)
一个新的交易已开放(仓位从0变为Y)交易只是信息性的,没有用户可调用的方法。
参考:交易#
- class backtrader.trade.Trade(data=None, tradeid=0, historyon=False, size=0, price=0.0, value=0.0, commission=0.0)#
Keeps track of the life of an trade: size, price, commission (and value?)
An trade starts at 0 can be increased and reduced and can be considered closed if it goes back to 0.
The trade can be long (positive size) or short (negative size)
An trade is not meant to be reversed (no support in the logic for it)
Member Attributes:
ref
: unique trade identifierstatus
(int
): one of Created, Open, Closedtradeid
: grouping tradeid passed to orders during creation The default in orders is 0size
(int
): current size of the tradeprice
(float
): current price of the tradevalue
(float
): current value of the tradecommission
(float
): current accumulated commissionpnl
(float
): current profit and loss of the trade (gross pnl)pnlcomm
(float
): current profit and loss of the trade minus commission (net pnl)isclosed
(bool
): records if the last update closed (set size to null the tradeisopen
(bool
): records if any update has opened the tradejustopened
(bool
): if the trade was just openedbaropen
(int
): bar in which this trade was openeddtopen
(float
): float coded datetime in which the trade was openedUse method
open_datetime
to get a Python datetime.datetime or use the platform providednum2date
method
barclose
(int
): bar in which this trade was closeddtclose
(float
): float coded datetime in which the trade was closedUse method
close_datetime
to get a Python datetime.datetime or use the platform providednum2date
method
barlen
(int
): number of bars this trade was openhistoryon
(bool
): whether history has to be recordedhistory
(list
): holds a list updated with each “update” event containing the resulting status and parameters used in the updateThe first entry in the history is the Opening Event The last entry in the history is the Closing Event