Example #1
0
    def __init__(
        self,
        unit: Unit,
        unit_quantity: int,
        ref: Optional[str] = None,
        max_frames_in_memory: int = 100,
    ) -> None:
        """
        Instanciate a new [`FrameSet`][estrade.graph.frame_set.FrameSet].

        Arguments:
            unit: unit used to group ticks.
            unit_quantity: quantity of unit
            max_frames_in_memory: number of frames to keep in memory
            ref: reference of this instance
        """
        TimeframeMixin.__init__(self, unit, unit_quantity)
        RefMixin.__init__(self, ref)

        self._is_timeframe_over_func = self.is_frame_over_timed
        self._create_new_frame_func = self._create_new_frame_timed
        if self.unit == Unit.TICK:
            self._is_timeframe_over_func = self.is_frame_over_tick
            self._create_new_frame_func = self._create_new_frame_tick

        self.epic: Optional["Epic"] = None
        self.frames: List[Frame] = []
        self.indicators: Dict[str, "BaseIndicator"] = {}
        self.max_frames_in_memory = max_frames_in_memory
Example #2
0
    def __init__(
        self,
        trade: Trade,
        close_value: float,
        quantity: int,
        datetime: Union[pydatetime, arrow.Arrow],
        status: Optional[TransactionStatus] = TransactionStatus.CREATED,
        ref: Optional[str] = None,
        meta: Optional[Dict[Any, Any]] = None,
    ) -> None:
        """
        Create a new partial close of a trade.

        Arguments:
            trade: [`Trade`][estrade.trade.Trade] instance.
            close_value: current market value at close time.
            quantity: closed quantities
            datetime: close datetime
            ref: an optional name for this close
            meta: trade close metadata
        """
        self.trade = trade
        self.close_value = close_value
        self.quantity = quantity

        TransactionMixin.__init__(self, status)
        TimedMixin.__init__(self, datetime)
        MetaMixin.__init__(self, meta)
        RefMixin.__init__(self, ref)
Example #3
0
    def __init__(self, ref: str = None) -> None:
        """
        Create a new Strategy instance.

        Arguments:
            ref: reference of this Strategy
        """
        RefMixin.__init__(self, ref)
        self.epics: Dict[str, "Epic"] = {}
        self.paused_until: Optional["Arrow"] = None
        self.stopped = False
        self.trades: List["Trade"] = []
Example #4
0
    def __init__(self, ref: Optional[str] = None) -> None:
        """
        Create a new TradeProvider.

        Arguments:
            ref: trade provider identifier.
        """
        self.trades: List["Trade"] = []
        self.threads: List[threading.Thread] = []
        self.default_transaction_status = TransactionStatus.REQUIRED
        RefMixin.__init__(self, ref)

        logger.info("New trade provider created.")
Example #5
0
    def __init__(
        self,
        timezone: str = "UTC",
        open_time: time = time(9, 30),
        close_time: time = time(17, 30),
        trade_days: Optional[List[int]] = None,
        holidays: Optional[List[date]] = None,
        trade_provider: Optional["BaseTradeProvider"] = None,
        ref: Optional[str] = None,
    ) -> None:
        """
        Create a new Epic instance.

        Arguments:
            trade_provider: instance inheriting from
                [`TradeProvider`][estrade.trade_provider.BaseTradeProvider]. Defaults to
                [`TradeProviderBacktests`][estrade.trade_provider.TradeProviderBacktests]
                if not provided
            timezone: timezone string from `pytz.all_timezones`, it is applied on tick
                on association with Epic.
            open_time: Instance open_time
            close_time: Instance close_time
            trade_days: List of weekdays when the market is open (Monday=0).
            holidays: List of dates where the Epic is open.
            ref: epic code name

        Raises:
            estrade.EpicException: if timezone is invalid

        """
        # call parent mixins
        RefMixin.__init__(self, ref)

        # handle arguments to set attributes
        self.timezone: str = timezone
        self.open_time = open_time
        self.close_time = close_time
        self.trade_days = trade_days or [0, 1, 2, 3, 4]
        self.holidays = holidays or []
        self.trade_provider = trade_provider or TradeProviderBacktests()

        # create a fake tick to init last_tick attribute
        self.last_tick: Tick = Tick(
            datetime=arrow.get("1900-01-01 00:00:00"),
            bid=0,
            ask=0,
        )
        self.frame_sets: Dict[str, "FrameSet"] = {}
        self.strategies: Dict[str, "BaseStrategy"] = {}
        self.market_open: bool = False
        logger.info(f"New Epic created: {self}")
Example #6
0
    def __init__(
        self,
        direction: TradeDirection,
        quantity: int,
        open_value: float,
        open_datetime: arrow.Arrow,
        epic: "Epic",
        current_close_value: Optional[float] = None,
        status: Optional[TransactionStatus] = TransactionStatus.CREATED,
        strategy: Optional["BaseStrategy"] = None,
        ref: Optional[str] = None,
        meta: Optional[Dict[str, Any]] = None,
    ) -> None:
        """
        Create a new trade instance.

        Arguments:
            direction: trade direction (buy or sell)
            quantity: quantity of the trade.
            open_value: market value to open the Trade with
            open_datetime: datetime of the trade open
            status: operation status
            epic: [`Epic`][estrade.epic.Epic] instance having at least one tick.
            strategy: [`Strategy`][estrade.strategy.BaseStrategy] instance
            ref: trade reference
            meta: trade metadata
        """
        self.epic = epic
        self.strategy = strategy  # TODO: test Strategy type
        self.direction = direction  # TODO: check invalid direction
        self.open_quantity = quantity  # TODO: check positive
        self.closes: List[TradeClose] = []

        self.open_value = open_value
        self.current_close_value = current_close_value or open_value

        self.max_result: float = self.result
        self.min_result: float = self.result

        RefMixin.__init__(self, ref)
        TimedMixin.__init__(self, open_datetime)
        MetaMixin.__init__(self, meta)
        TransactionMixin.__init__(self, status)

        # self.epic.trade_provider.open_trade(self)
        logger.info("New %s trade created: %s @ %s", self.direction, self.ref,
                    self.open_value)
Example #7
0
    def __init__(
        self,
        value_class: Type["BaseIndicatorValue"],
        ref: Optional[str] = None,
        market_open_only: bool = False,
    ) -> None:
        """
        Create a new indicator.

        Arguments:
            value_class: Class of value to generate on new frame.
            market_open_only: apply this indicator only when market is open.
            ref: reference of this instance
        """
        RefMixin.__init__(self, ref=ref)

        self.value_class = value_class
        self.market_open_only = market_open_only
        self.frame_set: Optional["FrameSet"] = None