Ejemplo n.º 1
0
    def test_floor_with_ten_ones_inputs(self):
        # Arrange
        floor = 0.00005
        floored_atr = AverageTrueRange(10, value_floor=floor)

        for _i in range(20):
            floored_atr.update_raw(1.00000, 1.00000, 1.00000)

        # Act, Assert
        assert floored_atr.value == 5e-05
Ejemplo n.º 2
0
    def test_floor_with_ten_ones_inputs(self):
        # Arrange
        floor = 0.00005
        floored_atr = AverageTrueRange(10, value_floor=floor)

        for i in range(20):
            floored_atr.update(1.00000, 1.00000, 1.00000)

        # Act
        # Assert
        self.assertEqual(5e-05, floored_atr.value)
    def __init__(
            self,
            symbol: Symbol,
            bar_spec: BarSpecification,
            trade_size: Decimal,
            fast_ema_period: int,
            slow_ema_period: int,
            atr_period: int,
            trail_atr_multiple: float,
            order_id_tag: str,  # Must be unique at 'trader level'
    ):
        """
        Initialize a new instance of the `EMACrossStopEntryWithTrailingStop` class.

        Parameters
        ----------
        symbol : Symbol
            The symbol for the strategy.
        bar_spec : BarSpecification
            The bar specification for the strategy.
        trade_size : Decimal
            The position size per trade.
        fast_ema_period : int
            The period for the fast EMA indicator.
        slow_ema_period : int
            The period for the slow EMA indicator.
        atr_period : int
            The period for the ATR indicator.
        trail_atr_multiple : float
            The ATR multiple for the trailing stop.
        order_id_tag : str
            The unique order identifier tag for the strategy. Must be unique
            amongst all running strategies for a particular trader identifier.

        """
        super().__init__(order_id_tag=order_id_tag)

        # Custom strategy variables
        self.symbol = symbol
        self.bar_type = BarType(symbol, bar_spec)
        self.trade_size = trade_size
        self.trail_atr_multiple = trail_atr_multiple
        self.instrument = None  # Initialize in on_start
        self.tick_size = None  # Initialize in on_start
        self.price_precision = None  # Initialize in on_start

        # Create the indicators for the strategy
        self.fast_ema = ExponentialMovingAverage(fast_ema_period)
        self.slow_ema = ExponentialMovingAverage(slow_ema_period)
        self.atr = AverageTrueRange(atr_period)

        # Users order management variables
        self.entry = None
        self.trailing_stop = None
Ejemplo n.º 4
0
    def test_handle_bar_updates_indicator(self):
        # Arrange
        indicator = AverageTrueRange(10)

        bar = TestStubs.bar_5decimal()

        # Act
        indicator.handle_bar(bar)

        # Assert
        self.assertTrue(indicator.has_inputs)
        self.assertEqual(2.999999999997449e-05, indicator.value)
Ejemplo n.º 5
0
    def test_handle_bar_updates_indicator(self):
        # Arrange
        indicator = AverageTrueRange(10)

        bar = TestDataStubs.bar_5decimal()

        # Act
        indicator.handle_bar(bar)

        # Assert
        assert indicator.has_inputs
        assert indicator.value == 2.999999999997449e-05
Ejemplo n.º 6
0
    def __init__(
        self,
        symbol: Symbol,
        bar_spec: BarSpecification,
        trade_size: Decimal,
        fast_ema_period: int=10,
        slow_ema_period: int=20,
        atr_period: int=20,
        trail_atr_multiple: float=2.0,
    ):
        """
        Initialize a new instance of the `EMACrossWithTrailingStop` class.

        Parameters
        ----------
        symbol : Symbol
            The symbol for the strategy.
        bar_spec : BarSpecification
            The bar specification for the strategy.
        trade_size : Decimal
            The position size per trade.
        fast_ema_period : int
            The period for the fast EMA indicator.
        slow_ema_period : int
            The period for the slow EMA indicator.
        atr_period : int
            The period for the ATR indicator.
        trail_atr_multiple : float
            The ATR multiple for the trailing stop.

        """
        # The order_id_tag should be unique at the 'trader level', here we are
        # just using the traded instruments symbol as the strategy order id tag.
        super().__init__(order_id_tag=symbol.code.replace('/', ""))

        # Custom strategy variables
        self.symbol = symbol
        self.bar_type = BarType(symbol, bar_spec)
        self.trade_size = trade_size
        self.trail_atr_multiple = trail_atr_multiple
        self.instrument = None  # Initialize in on_start
        self.tick_size = None   # Initialize in on_start

        # Create the indicators for the strategy
        self.fast_ema = ExponentialMovingAverage(fast_ema_period)
        self.slow_ema = ExponentialMovingAverage(slow_ema_period)
        self.atr = AverageTrueRange(atr_period)

        # Users order management variables
        self.entry = None
        self.trailing_stop = None
Ejemplo n.º 7
0
    def __init__(self, config: EMACrossBracketConfig):
        super().__init__(config)

        # Configuration
        self.instrument_id = InstrumentId.from_str(config.instrument_id)
        self.bar_type = BarType.from_str(config.bar_type)
        self.bracket_distance_atr = config.bracket_distance_atr
        self.trade_size = Decimal(config.trade_size)

        # Create the indicators for the strategy
        self.atr = AverageTrueRange(config.atr_period)
        self.fast_ema = ExponentialMovingAverage(config.fast_ema_period)
        self.slow_ema = ExponentialMovingAverage(config.slow_ema_period)

        self.instrument: Optional[Instrument] = None  # Initialized in on_start
Ejemplo n.º 8
0
    def test_floor_with_exponentially_decreasing_high_inputs(self):
        # Arrange
        floor = 0.00005
        floored_atr = AverageTrueRange(10, value_floor=floor)

        high = 1.00020
        low = 1.00000
        close = 1.00000

        for _i in range(20):
            high -= (high - low) / 2
            floored_atr.update_raw(high, low, close)

        # Act, Assert
        assert floored_atr.value == 5e-05
    def __init__(self, config: VolatilityMarketMakerConfig):
        super().__init__(config)

        # Configuration
        self.instrument_id = InstrumentId.from_str(config.instrument_id)
        self.bar_type = BarType.from_str(config.bar_type)
        self.trade_size = Decimal(config.trade_size)
        self.atr_multiple = config.atr_multiple

        # Create the indicators for the strategy
        self.atr = AverageTrueRange(config.atr_period)

        self.instrument: Optional[Instrument] = None  # Initialized in on_start

        # Users order management variables
        self.buy_order: Union[LimitOrder, None] = None
        self.sell_order: Union[LimitOrder, None] = None
    def __init__(
            self,
            instrument_id: InstrumentId,
            bar_spec: BarSpecification,
            trade_size: Decimal,
            atr_period: int,
            atr_multiple: float,
            order_id_tag: str,  # Must be unique at 'trader level'
    ):
        """
        Initialize a new instance of the `VolatilityMarketMaker` class.

        Parameters
        ----------
        instrument_id : InstrumentId
            The instrument identifier for the strategy.
        bar_spec : BarSpecification
            The bar specification for the strategy.
        trade_size : Decimal
            The position size per trade.
        atr_period : int
            The period for the ATR indicator.
        atr_multiple : float
            The ATR multiple for bracketing limit orders.
        order_id_tag : str
            The unique order identifier tag for the strategy. Must be unique
            amongst all running strategies for a particular trader identifier.

        """
        super().__init__(order_id_tag=order_id_tag)

        # Custom strategy variables
        self.instrument_id = instrument_id
        self.bar_type = BarType(instrument_id, bar_spec)
        self.trade_size = trade_size
        self.atr_multiple = atr_multiple
        self.instrument = None  # Request on start instead
        self.price_precision = None  # Initialized on start

        # Create the indicators for the strategy
        self.atr = AverageTrueRange(atr_period)

        # Users order management variables
        self.buy_order: Union[LimitOrder, None] = None
        self.sell_order: Union[LimitOrder, None] = None
    def __init__(
        self,
        symbol: Symbol,
        bar_spec: BarSpecification,
        trade_size: Decimal,
        atr_period: int = 20,
        atr_multiple: float = 2.0,
    ):
        """
        Initialize a new instance of the `VolatilityMarketMaker` class.

        Parameters
        ----------
        symbol : Symbol
            The symbol for the strategy.
        bar_spec : BarSpecification
            The bar specification for the strategy.
        trade_size : Decimal
            The position size per trade.
        atr_period : int
            The period for the ATR indicator.
        atr_multiple : float
            The ATR multiple for bracketing limit orders.

        """
        # The order_id_tag should be unique at the 'trader level', here we are
        # just using the traded instruments symbol as the strategy order id tag.
        super().__init__(order_id_tag=symbol.code.replace('/', ""))

        # Custom strategy variables
        self.symbol = symbol
        self.bar_type = BarType(symbol, bar_spec)
        self.trade_size = trade_size
        self.atr_multiple = atr_multiple
        self.instrument = None  # Request on start instead
        self.price_precision = None  # Initialized on start

        # Create the indicators for the strategy
        self.atr = AverageTrueRange(atr_period)

        # Users order management variables
        self.buy_order: Union[LimitOrder, None] = None
        self.sell_order: Union[LimitOrder, None] = None
Ejemplo n.º 12
0
    def __init__(self, config: EMACrossWithTrailingStopConfig):
        super().__init__(config)

        # Configuration
        self.instrument_id = InstrumentId.from_str(config.instrument_id)
        self.bar_type = BarType.from_str(config.bar_type)
        self.trade_size = Decimal(config.trade_size)
        self.trail_atr_multiple = config.trail_atr_multiple

        # Create the indicators for the strategy
        self.fast_ema = ExponentialMovingAverage(config.fast_ema_period)
        self.slow_ema = ExponentialMovingAverage(config.slow_ema_period)
        self.atr = AverageTrueRange(config.atr_period)

        self.instrument: Optional[Instrument] = None  # Initialized in on_start
        self.tick_size = None  # Initialized in on_start

        # Users order management variables
        self.entry = None
        self.trailing_stop = None
Ejemplo n.º 13
0
    def __init__(self,
                 symbol: Symbol,
                 bar_spec: BarSpecification,
                 risk_bp: float = 10.0,
                 fast_ema: int = 10,
                 slow_ema: int = 20,
                 atr_period: int = 20,
                 sl_atr_multiple: float = 2.0,
                 extra_id_tag: str = ''):
        """
        Initializes a new instance of the EMACrossPy class.

        :param symbol: The symbol for the strategy.
        :param bar_spec: The bar specification for the strategy.
        :param risk_bp: The risk per trade (basis points).
        :param fast_ema: The fast EMA period.
        :param slow_ema: The slow EMA period.
        :param atr_period: The ATR period.
        :param sl_atr_multiple: The ATR multiple for stop-loss prices.
        :param extra_id_tag: An optional extra tag to append to order ids.
        """
        super().__init__(order_id_tag=symbol.code + extra_id_tag,
                         bar_capacity=40)

        # Custom strategy variables
        self.symbol = symbol
        self.bar_type = BarType(symbol, bar_spec)
        self.precision = 5  # dummy initial value for FX
        self.risk_bp = risk_bp
        self.entry_buffer = 0.0  # instrument.tick_size
        self.SL_buffer = 0.0  # instrument.tick_size * 10
        self.SL_atr_multiple = sl_atr_multiple

        self.position_sizer = None  # initialized in on_start()
        self.quote_currency = None  # initialized in on_start()

        # Create the indicators for the strategy
        self.fast_ema = ExponentialMovingAverage(fast_ema)
        self.slow_ema = ExponentialMovingAverage(slow_ema)
        self.atr = AverageTrueRange(atr_period)
Ejemplo n.º 14
0
 def setUp(self):
     # Arrange
     self.atr = AverageTrueRange(10)
Ejemplo n.º 15
0
 def setUp(self):
     # Fixture Setup
     self.atr = AverageTrueRange(10)