Module backtrader.commissions

Expand source code
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

from ..comminfo import CommInfoBase


class CommInfo(CommInfoBase):
    pass  # clone of CommissionInfo but with xx% instead of 0.xx


class CommInfo_Futures(CommInfoBase):
    params = (
        ('stocklike', False),
    )


class CommInfo_Futures_Perc(CommInfo_Futures):
    params = (
        ('commtype', CommInfoBase.COMM_PERC),
    )


class CommInfo_Futures_Fixed(CommInfo_Futures):
    params = (
        ('commtype', CommInfoBase.COMM_FIXED),
    )


class CommInfo_Stocks(CommInfoBase):
    params = (
        ('stocklike', True),
    )


class CommInfo_Stocks_Perc(CommInfo_Stocks):
    params = (
        ('commtype', CommInfoBase.COMM_PERC),
    )


class CommInfo_Stocks_Fixed(CommInfo_Stocks):
    params = (
        ('commtype', CommInfoBase.COMM_FIXED),
    )

Classes

class CommInfo

Base Class for the Commission Schemes.

Params

  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation. It only applies if the final _stocklike attribute in the class is set to False

  • automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy

    • Use param margin if param automargin evaluates to False

    • Use param mult * price if automargin < 0

    • Use param automargin * price if automargin > 0

  • commtype (def: None): Supported values are CommInfoBase.COMM_PERC (commission to be understood as %) and CommInfoBase.COMM_FIXED (commission to be understood as monetary units)

The default value of None is a supported value to retain compatibility with the legacy CommissionInfo object. If commtype is set to None, then the following applies:

- <code>margin</code> is <code>None</code>: Internal <code>\_commtype</code> is set to
  <code>COMM\_PERC</code> and <code>\_stocklike</code> is set to <code>True</code> (Operating
  %-wise with Stocks)

- <code>margin</code> is not <code>None</code>: <code>\_commtype</code> set to <code>COMM\_FIXED</code> and
  <code>\_stocklike</code> set to <code>False</code> (Operating with fixed rount-trip
  commission with Futures)

If this param is set to something else than None, then it will be passed to the internal _commtype attribute and the same will be done with the param stocklike and the internal attribute _stocklike

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like (see the commtype discussion above)

  • percabs (def: False): when commtype is set to COMM_PERC, whether the parameter commission has to be understood as XX% or 0.XX

If this param is True: 0.XX If this param is False: XX%

  • interest (def: 0.0)

If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling

The formula: days * price * abs(size) * (interest / 365)

It must be specified in absolute terms: 0.05 -> 5%

!!! note "Note: the behavior can be changed by overriding the method:" _get_credit_interest

  • interest_long (def: False)

Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions

  • leverage (def: 1.0)

Amount of leverage for the asset with regards to the needed cash

Attributes

  • _stocklike: Final value to use for Stock-like/Futures-like behavior
  • _commtype: Final value to use for PERC vs FIXED commissions

This two are used internally instead of the declared params to enable the compatibility check described above for the legacy CommissionInfo object

Expand source code
class CommInfo(CommInfoBase):
    pass  # clone of CommissionInfo but with xx% instead of 0.xx

Ancestors

Class variables

var frompackages
var packages
var params

Inherited members

class CommInfo_Futures

Base Class for the Commission Schemes.

Params

  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation. It only applies if the final _stocklike attribute in the class is set to False

  • automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy

    • Use param margin if param automargin evaluates to False

    • Use param mult * price if automargin < 0

    • Use param automargin * price if automargin > 0

  • commtype (def: None): Supported values are CommInfoBase.COMM_PERC (commission to be understood as %) and CommInfoBase.COMM_FIXED (commission to be understood as monetary units)

The default value of None is a supported value to retain compatibility with the legacy CommissionInfo object. If commtype is set to None, then the following applies:

- <code>margin</code> is <code>None</code>: Internal <code>\_commtype</code> is set to
  <code>COMM\_PERC</code> and <code>\_stocklike</code> is set to <code>True</code> (Operating
  %-wise with Stocks)

- <code>margin</code> is not <code>None</code>: <code>\_commtype</code> set to <code>COMM\_FIXED</code> and
  <code>\_stocklike</code> set to <code>False</code> (Operating with fixed rount-trip
  commission with Futures)

If this param is set to something else than None, then it will be passed to the internal _commtype attribute and the same will be done with the param stocklike and the internal attribute _stocklike

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like (see the commtype discussion above)

  • percabs (def: False): when commtype is set to COMM_PERC, whether the parameter commission has to be understood as XX% or 0.XX

If this param is True: 0.XX If this param is False: XX%

  • interest (def: 0.0)

If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling

The formula: days * price * abs(size) * (interest / 365)

It must be specified in absolute terms: 0.05 -> 5%

!!! note "Note: the behavior can be changed by overriding the method:" _get_credit_interest

  • interest_long (def: False)

Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions

  • leverage (def: 1.0)

Amount of leverage for the asset with regards to the needed cash

Attributes

  • _stocklike: Final value to use for Stock-like/Futures-like behavior
  • _commtype: Final value to use for PERC vs FIXED commissions

This two are used internally instead of the declared params to enable the compatibility check described above for the legacy CommissionInfo object

Expand source code
class CommInfo_Futures(CommInfoBase):
    params = (
        ('stocklike', False),
    )

Ancestors

Subclasses

Class variables

var frompackages
var packages
var params

Inherited members

class CommInfo_Futures_Fixed

Base Class for the Commission Schemes.

Params

  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation. It only applies if the final _stocklike attribute in the class is set to False

  • automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy

    • Use param margin if param automargin evaluates to False

    • Use param mult * price if automargin < 0

    • Use param automargin * price if automargin > 0

  • commtype (def: None): Supported values are CommInfoBase.COMM_PERC (commission to be understood as %) and CommInfoBase.COMM_FIXED (commission to be understood as monetary units)

The default value of None is a supported value to retain compatibility with the legacy CommissionInfo object. If commtype is set to None, then the following applies:

- <code>margin</code> is <code>None</code>: Internal <code>\_commtype</code> is set to
  <code>COMM\_PERC</code> and <code>\_stocklike</code> is set to <code>True</code> (Operating
  %-wise with Stocks)

- <code>margin</code> is not <code>None</code>: <code>\_commtype</code> set to <code>COMM\_FIXED</code> and
  <code>\_stocklike</code> set to <code>False</code> (Operating with fixed rount-trip
  commission with Futures)

If this param is set to something else than None, then it will be passed to the internal _commtype attribute and the same will be done with the param stocklike and the internal attribute _stocklike

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like (see the commtype discussion above)

  • percabs (def: False): when commtype is set to COMM_PERC, whether the parameter commission has to be understood as XX% or 0.XX

If this param is True: 0.XX If this param is False: XX%

  • interest (def: 0.0)

If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling

The formula: days * price * abs(size) * (interest / 365)

It must be specified in absolute terms: 0.05 -> 5%

!!! note "Note: the behavior can be changed by overriding the method:" _get_credit_interest

  • interest_long (def: False)

Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions

  • leverage (def: 1.0)

Amount of leverage for the asset with regards to the needed cash

Attributes

  • _stocklike: Final value to use for Stock-like/Futures-like behavior
  • _commtype: Final value to use for PERC vs FIXED commissions

This two are used internally instead of the declared params to enable the compatibility check described above for the legacy CommissionInfo object

Expand source code
class CommInfo_Futures_Fixed(CommInfo_Futures):
    params = (
        ('commtype', CommInfoBase.COMM_FIXED),
    )

Ancestors

Class variables

var frompackages
var packages
var params

Inherited members

class CommInfo_Futures_Perc

Base Class for the Commission Schemes.

Params

  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation. It only applies if the final _stocklike attribute in the class is set to False

  • automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy

    • Use param margin if param automargin evaluates to False

    • Use param mult * price if automargin < 0

    • Use param automargin * price if automargin > 0

  • commtype (def: None): Supported values are CommInfoBase.COMM_PERC (commission to be understood as %) and CommInfoBase.COMM_FIXED (commission to be understood as monetary units)

The default value of None is a supported value to retain compatibility with the legacy CommissionInfo object. If commtype is set to None, then the following applies:

- <code>margin</code> is <code>None</code>: Internal <code>\_commtype</code> is set to
  <code>COMM\_PERC</code> and <code>\_stocklike</code> is set to <code>True</code> (Operating
  %-wise with Stocks)

- <code>margin</code> is not <code>None</code>: <code>\_commtype</code> set to <code>COMM\_FIXED</code> and
  <code>\_stocklike</code> set to <code>False</code> (Operating with fixed rount-trip
  commission with Futures)

If this param is set to something else than None, then it will be passed to the internal _commtype attribute and the same will be done with the param stocklike and the internal attribute _stocklike

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like (see the commtype discussion above)

  • percabs (def: False): when commtype is set to COMM_PERC, whether the parameter commission has to be understood as XX% or 0.XX

If this param is True: 0.XX If this param is False: XX%

  • interest (def: 0.0)

If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling

The formula: days * price * abs(size) * (interest / 365)

It must be specified in absolute terms: 0.05 -> 5%

!!! note "Note: the behavior can be changed by overriding the method:" _get_credit_interest

  • interest_long (def: False)

Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions

  • leverage (def: 1.0)

Amount of leverage for the asset with regards to the needed cash

Attributes

  • _stocklike: Final value to use for Stock-like/Futures-like behavior
  • _commtype: Final value to use for PERC vs FIXED commissions

This two are used internally instead of the declared params to enable the compatibility check described above for the legacy CommissionInfo object

Expand source code
class CommInfo_Futures_Perc(CommInfo_Futures):
    params = (
        ('commtype', CommInfoBase.COMM_PERC),
    )

Ancestors

Class variables

var frompackages
var packages
var params

Inherited members

class CommInfo_Stocks

Base Class for the Commission Schemes.

Params

  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation. It only applies if the final _stocklike attribute in the class is set to False

  • automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy

    • Use param margin if param automargin evaluates to False

    • Use param mult * price if automargin < 0

    • Use param automargin * price if automargin > 0

  • commtype (def: None): Supported values are CommInfoBase.COMM_PERC (commission to be understood as %) and CommInfoBase.COMM_FIXED (commission to be understood as monetary units)

The default value of None is a supported value to retain compatibility with the legacy CommissionInfo object. If commtype is set to None, then the following applies:

- <code>margin</code> is <code>None</code>: Internal <code>\_commtype</code> is set to
  <code>COMM\_PERC</code> and <code>\_stocklike</code> is set to <code>True</code> (Operating
  %-wise with Stocks)

- <code>margin</code> is not <code>None</code>: <code>\_commtype</code> set to <code>COMM\_FIXED</code> and
  <code>\_stocklike</code> set to <code>False</code> (Operating with fixed rount-trip
  commission with Futures)

If this param is set to something else than None, then it will be passed to the internal _commtype attribute and the same will be done with the param stocklike and the internal attribute _stocklike

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like (see the commtype discussion above)

  • percabs (def: False): when commtype is set to COMM_PERC, whether the parameter commission has to be understood as XX% or 0.XX

If this param is True: 0.XX If this param is False: XX%

  • interest (def: 0.0)

If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling

The formula: days * price * abs(size) * (interest / 365)

It must be specified in absolute terms: 0.05 -> 5%

!!! note "Note: the behavior can be changed by overriding the method:" _get_credit_interest

  • interest_long (def: False)

Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions

  • leverage (def: 1.0)

Amount of leverage for the asset with regards to the needed cash

Attributes

  • _stocklike: Final value to use for Stock-like/Futures-like behavior
  • _commtype: Final value to use for PERC vs FIXED commissions

This two are used internally instead of the declared params to enable the compatibility check described above for the legacy CommissionInfo object

Expand source code
class CommInfo_Stocks(CommInfoBase):
    params = (
        ('stocklike', True),
    )

Ancestors

Subclasses

Class variables

var frompackages
var packages
var params

Inherited members

class CommInfo_Stocks_Fixed

Base Class for the Commission Schemes.

Params

  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation. It only applies if the final _stocklike attribute in the class is set to False

  • automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy

    • Use param margin if param automargin evaluates to False

    • Use param mult * price if automargin < 0

    • Use param automargin * price if automargin > 0

  • commtype (def: None): Supported values are CommInfoBase.COMM_PERC (commission to be understood as %) and CommInfoBase.COMM_FIXED (commission to be understood as monetary units)

The default value of None is a supported value to retain compatibility with the legacy CommissionInfo object. If commtype is set to None, then the following applies:

- <code>margin</code> is <code>None</code>: Internal <code>\_commtype</code> is set to
  <code>COMM\_PERC</code> and <code>\_stocklike</code> is set to <code>True</code> (Operating
  %-wise with Stocks)

- <code>margin</code> is not <code>None</code>: <code>\_commtype</code> set to <code>COMM\_FIXED</code> and
  <code>\_stocklike</code> set to <code>False</code> (Operating with fixed rount-trip
  commission with Futures)

If this param is set to something else than None, then it will be passed to the internal _commtype attribute and the same will be done with the param stocklike and the internal attribute _stocklike

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like (see the commtype discussion above)

  • percabs (def: False): when commtype is set to COMM_PERC, whether the parameter commission has to be understood as XX% or 0.XX

If this param is True: 0.XX If this param is False: XX%

  • interest (def: 0.0)

If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling

The formula: days * price * abs(size) * (interest / 365)

It must be specified in absolute terms: 0.05 -> 5%

!!! note "Note: the behavior can be changed by overriding the method:" _get_credit_interest

  • interest_long (def: False)

Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions

  • leverage (def: 1.0)

Amount of leverage for the asset with regards to the needed cash

Attributes

  • _stocklike: Final value to use for Stock-like/Futures-like behavior
  • _commtype: Final value to use for PERC vs FIXED commissions

This two are used internally instead of the declared params to enable the compatibility check described above for the legacy CommissionInfo object

Expand source code
class CommInfo_Stocks_Fixed(CommInfo_Stocks):
    params = (
        ('commtype', CommInfoBase.COMM_FIXED),
    )

Ancestors

Class variables

var frompackages
var packages
var params

Inherited members

class CommInfo_Stocks_Perc

Base Class for the Commission Schemes.

Params

  • commission (def: 0.0): base commission value in percentage or monetary units

  • mult (def 1.0): multiplier applied to the asset for value/profit

  • margin (def: None): amount of monetary units needed to open/hold an operation. It only applies if the final _stocklike attribute in the class is set to False

  • automargin (def: False): Used by the method get_margin to automatically calculate the margin/guarantees needed with the following policy

    • Use param margin if param automargin evaluates to False

    • Use param mult * price if automargin < 0

    • Use param automargin * price if automargin > 0

  • commtype (def: None): Supported values are CommInfoBase.COMM_PERC (commission to be understood as %) and CommInfoBase.COMM_FIXED (commission to be understood as monetary units)

The default value of None is a supported value to retain compatibility with the legacy CommissionInfo object. If commtype is set to None, then the following applies:

- <code>margin</code> is <code>None</code>: Internal <code>\_commtype</code> is set to
  <code>COMM\_PERC</code> and <code>\_stocklike</code> is set to <code>True</code> (Operating
  %-wise with Stocks)

- <code>margin</code> is not <code>None</code>: <code>\_commtype</code> set to <code>COMM\_FIXED</code> and
  <code>\_stocklike</code> set to <code>False</code> (Operating with fixed rount-trip
  commission with Futures)

If this param is set to something else than None, then it will be passed to the internal _commtype attribute and the same will be done with the param stocklike and the internal attribute _stocklike

  • stocklike (def: False): Indicates if the instrument is Stock-like or Futures-like (see the commtype discussion above)

  • percabs (def: False): when commtype is set to COMM_PERC, whether the parameter commission has to be understood as XX% or 0.XX

If this param is True: 0.XX If this param is False: XX%

  • interest (def: 0.0)

If this is non-zero, this is the yearly interest charged for holding a short selling position. This is mostly meant for stock short-selling

The formula: days * price * abs(size) * (interest / 365)

It must be specified in absolute terms: 0.05 -> 5%

!!! note "Note: the behavior can be changed by overriding the method:" _get_credit_interest

  • interest_long (def: False)

Some products like ETFs get charged on interest for short and long positions. If ths is True and interest is non-zero the interest will be charged on both directions

  • leverage (def: 1.0)

Amount of leverage for the asset with regards to the needed cash

Attributes

  • _stocklike: Final value to use for Stock-like/Futures-like behavior
  • _commtype: Final value to use for PERC vs FIXED commissions

This two are used internally instead of the declared params to enable the compatibility check described above for the legacy CommissionInfo object

Expand source code
class CommInfo_Stocks_Perc(CommInfo_Stocks):
    params = (
        ('commtype', CommInfoBase.COMM_PERC),
    )

Ancestors

Class variables

var frompackages
var packages
var params

Inherited members