Skip to content

Simple Moving Average

SimpleMovingAverage

Simple Moving Average representation.

Attributes:

Name Type Description
max_periods

max closes kept in memory (max periods to calculate SMA)

value_class Type[estrade.graph.base_indicator.BaseIndicatorValue]

Class of value to generate on new frame.

frame_set Optional[estrade.graph.frame_set.FrameSet]

The FrameSet instance that send ticks to this instance.

market_open_only bool

apply this indicator only when market is open.

ref str

reference of this instance

epic: Optional[Epic] inherited property readonly

Return the Epic associated to this instance.

Returns:

Type Description
Optional[Epic]

Epic associated to this instance.

ref: str inherited property writable

Return ref of current instance.

Returns:

Type Description
str

reference of current instance.

__init__(self, max_periods, **kwargs) special

Create a new instance of a Simple Moving Average.

Parameters:

Name Type Description Default
max_periods int

max periods

required
kwargs

see BaseIndicator

{}
Source code in estrade/graph/indicators/simple_moving_average.py
def __init__(self, max_periods: int, **kwargs):
    """
    Create a new instance of a Simple Moving Average.

    Arguments:
        max_periods: max periods
        kwargs: see [`BaseIndicator`][estrade.graph.base_indicator.BaseIndicator]
    """
    BaseIndicator.__init__(
        self,
        value_class=SimpleMovingAverageValue,
        **kwargs,
    )

    self.max_periods = max_periods

build_value_from_frame(self, frame, epic_market_open) inherited

Create a new value for this indicator.

This method is triggered when its frame_set create a new Frame.

Parameters:

Name Type Description Default
frame Frame

The newly created frame.

required
epic_market_open bool

is the epic open?

required

Returns:

Type Description
Optional[BaseIndicatorValue]

An instance of this instance value_class (None Market is not open).

Source code in estrade/graph/indicators/simple_moving_average.py
def build_value_from_frame(
    self, frame: "Frame", epic_market_open: bool
) -> Optional["BaseIndicatorValue"]:
    """
    Create a new value for this indicator.

    This method is triggered when its `frame_set` create a new
        [`Frame`][estrade.graph.frame_set.Frame].

    Arguments:
        frame: The newly created frame.
        epic_market_open: is the epic open?

    Returns:
        An instance of this instance `value_class` (`None` Market is not open).
    """
    if not self.market_open_only or epic_market_open:
        new_value = self.value_class(
            indicator=self,
            frame=frame,
        )
        return new_value
    return None

SimpleMovingAverageValue

Simple Moving Average Value representation.

Attributes:

Name Type Description
indicator estrade.graph.base_indicator.BaseIndicator

Parent indicator.

frame estrade.graph.frame_set.Frame

Parent frame.

last_closes List[float]

List of closes values of previous frames

extended_closes List[float]

List of closes values of previous frames + last value of the current frame.

closed: bool inherited property readonly

Check if the parent frame is closed.

Returns:

Type Description
bool

Is the parent frame closed?

nb_ticks: int inherited property readonly

Count of Ticks received by the parent frame.

Returns:

Type Description
int

Number of Ticks received by the parent frame.

next: Optional[BaseIndicatorValue] inherited property readonly

Return the value of this indicator on the next frame.

Returns:

Type Description
Optional[BaseIndicatorValue]

This indicator value on the next frame (None if the parent have no next frame)

previous: Optional[BaseIndicatorValue] inherited property readonly

Return the value of this indicator on the previous frame.

Returns:

Type Description
Optional[BaseIndicatorValue]

This indicator value on the previous frame (None if the parent have no previous frame)

__init__(self, indicator, frame) special

Create a new Moving Average Value for the input Frame.

Parameters:

Name Type Description Default
indicator SimpleMovingAverage

parent indicator

required
frame Frame

parent frame

required
Source code in estrade/graph/indicators/simple_moving_average.py
def __init__(
    self,
    indicator: "SimpleMovingAverage",
    frame: "Frame",
) -> None:
    """
    Create a new Moving Average Value for the input Frame.

    Arguments:
        indicator: parent indicator
        frame: parent frame
    """
    BaseIndicatorValue.__init__(self, indicator=indicator, frame=frame)

    self.last_closes: List[float] = []
    if self.previous:
        self.last_closes = self.previous.extended_closes[:]  # type: ignore

    max_last_closes_size = self.indicator.max_periods - 1  # type: ignore
    self.last_closes = self.last_closes[(max_last_closes_size * -1) :]

get_value(self, periods)

Get a SMA value.

Parameters:

Name Type Description Default
periods int

Number of periods to calculate the SMA on.

required

Returns:

Type Description
Optional[float]

Simple Moving Average Value

Source code in estrade/graph/indicators/simple_moving_average.py
def get_value(self, periods: int) -> Optional[float]:
    """
    Get a SMA value.

    Arguments:
        periods: Number of periods to calculate the SMA on.

    Returns:
        Simple Moving Average Value
    """
    if len(self.extended_closes) < periods:
        return None
    raw_value = sum(self.extended_closes[(periods * -1) :]) / periods
    return round(raw_value, 2)