top of page

Price Moving Average Cross


Price Moving Average Cross strategy is a commonly used algorithm by traders. It simply says that: If the price of an asset crosses its moving average downwards, sell all the assets. If the price of the the asset crosses its moving average upwards, then buy the asset with all of the available money. Here selecting a convenient period for moving average and time period for the bars on the chart are important.

Good selections will make the algorithm produce good results. If we select short periods for moving average, we will trade more frequently because price will cross moving average up and down more. If we select long periods for moving average, algorithm will trade less because price will hardly cross the moving average. After we implement the algorithm we will backtest it for different moving average periods and bar periods. Before we go to live, we will select the best moving average time period and bar time period.


Here I am going to use exponential moving average. Simple moving average can also be used the same way. It is just a matter of taste, if you think simple moving averge works better you can use simple moving average as well.


100 period 15 minutes resolution chart:

  • Consider hourly chart of TSLA

  • At the end of every hour do this:

    • Calculate 200 period exponential moving average value and name it as EMA200.

    • Make a record of closing value of TSLA as CLS

    • If there exists TSLA stocks in the account and if CLS is smaller than EMA200 then

      • Sell all the stocks

    • If there is no TSLA stock in the account and if CLS is bigger than EMA200 then

      • Buy TSLA with all the available money



At the end of each period, exponential moving average should be calculated with this formula:

EMA = ((1/period)* currentPriceOfSecurity) + ((1-(1/period))*previousValueOfEMA)

Implementation with C#

There are 2 files to implement this agorithm:


  • MovingAverage.cs

  • PriceEMACross.cs

In MovingAverage.cs the exponential moving average formula is implemented. At the end of each period exponential moving average value is caluculated by using the MovingAverage class defined in this file.

PriceEMACross.cs includes the actual implementation of algorithm. OnData function in this file triggers at the end of each period and it checks if the price crosses moving average. If yes, it buys or sells.

Backtest Results

01.01.2017 is selected the execution start date of the algorithm and below table shows the results of different executions with different parameters. Selecting 50 as moving average period and working on day resolution graph produces best results among the trials.

We can state from the results that as the bar resolution gets bigger, buy-sell signals are more trustable because they have less volatility noise. We can also say that as the EMA period gets smaller buy-sell signals are produced more and algorithm trades more. However, on the other hand, EMA period should be kept as large as possible because small EMA periods produce more noisy signals.

bottom of page