The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
ema10.Push(Securities["IBM"].Close);
ema50.Push(Securities["IBM"].Close);
if (ema10.EMA > ema50.EMA)
{
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using QuantConnect;
using QuantConnect.Models;
namespace QuantConnect.Algorithm.Library
{
///
/// An indicator shows the average value of secuitry's price
/// over a set period
///
public class ExponentialMovingAverage
{
private int _period;
private decimal _ema;
private bool _initialized;
private Queue _data = new Queue();
public void Clear()
{
_data.Clear();
}
///
/// EMA value
///
public decimal EMA
{
get { return _ema; }
}
private decimal ExponentialComponent
{
get { return (decimal) 2 / (_period + 1); }
}
///
/// Initialise the Algorithm
///
public ExponentialMovingAverage(int period)
{
_period = period;
_ema = 0;
_initialized = false;
}
public bool IsInitialized
{
get { return _initialized;}
}
///
/// Calculate the exponential moving average
///
public bool Push(decimal priceToday)
{
if (_initialized)
{
_ema = (1 - ExponentialComponent) * _ema + ExponentialComponent * priceToday;
//http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
}
else
{
_data.Enqueue(priceToday);
if (_data.Count >= _period)
{
_ema = _data.Average();
_initialized = true;
}
}
return _initialized;
}
}
}
namespace QuantConnect
{
using QuantConnect.Models;
///
/// Online exponential moving average indicator class:
///
public class EMA : Indicator
{
///
/// Initialize a new instance of the online EMA.
///
/// number of samples in the EMA.
public EMA(int period) {
_period = period;
}
private decimal _ema = 0;
private decimal _period = 0;
///
/// Period of the EMA.
///
public decimal Period
{
get {
return _period;
}
}
///
/// Last calculated EMA
///
public decimal Result
{
get {
return _ema;
}
}
///
/// Push in the latest price and get back an updated EMA.
///
/// latest price of asset
///Period EMA
public decimal Push(decimal latestPrice) {
try
{
//Set the first value of EMA.
if (_ema == 0) _ema = latestPrice;
//Online EMA
_ema = (1m / _period) * latestPrice + ((_period - 1m) / _period) * _ema;
}
catch (Exception err)
{
throw new Exception("Indicator: EMA(): Sorry there was an error your EMA: " + err.Message);
}
return _ema;
}
///
/// Push in the last market data to get the period based EMA.
///
/// Tick Market Data
///Decimal EMA over the requested period.
public decimal Push(Tick tick)
{
return Push(tick.Price);
}
///
/// Push in the last market data to get the period based EMA.
///
/// TradeBar Market Data
///Decimal EMA over the requested period.
public decimal Push(TradeBar bar)
{
return Push(bar.Price);
}
} // End Indicator Class
} // End QC Namespace
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can
continue your Boot Camp training progress from the terminal. We
hope to see you in the community soon!