Building Trading Strategies Indicators Simple Moving Average

Simple Moving Average

The SimpleMovingAverage (SMA) is a widely utilized indicator in technical analysis, offering a smoothed-out price average over a specific time period. It's calculated by adding up the prices of the asset for a set number of time periods, then dividing this total by the number of time periods.

Key Characteristics of SMA:

  • Simplicity and Clarity: The SMA provides a clear view of the price trend by dampening the noise of daily price fluctuations.
  • Trend Identification: It helps in identifying the direction of the trend. A rising SMA indicates an uptrend, while a declining SMA suggests a downtrend.
  • Support and Resistance: Often, the SMA can act as a support or resistance level for price movements.
  • Crossovers: Traders pay close attention to SMA crossovers. When a short-term SMA crosses above a long-term SMA, it may signal a buying opportunity, known as a "golden cross." Conversely, when the short-term SMA crosses below
    import { Bar, InstrumentProcessorBase } from '@botmain/common';
    import { SimpleMovingAverage } from '@botmain/indicators';

    export class InstrumentProcessor extends InstrumentProcessorBase {
        sma = new SimpleMovingAverage(14);

        ...

        onBarClose(bar: Bar) {
            //Add bar close to calculate new simple moving average
            this.sma.add(bar.close);

            ...

            if(bar.open < this.sma.last && bar.close > this.sma.last) {
                //price crossed simple moving average. Place trade..
            }
        }
    }