Back

EMA 10-50 Cross Trading Strategy

Trading signal is trigger by ema 10 day cross 50 day.
Update Backtest








Very cool indicator class @xiaoxiao! We've been meaning to make a bunch of those an build them in. Would you mind if add that class to the GIT repo?

Seems like solid proof short term EMA crosses don't work well :) Perhaps you could experiment with the purchase size?
0

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.


Hi Xiaoxiao,

I have the feeling your moving average does not move forward:
ema10.Push(Securities["IBM"].Close);
ema50.Push(Securities["IBM"].Close);
if (ema10.EMA > ema50.EMA)
{


The Push does only enque, not calculate the average. It is quite hard to debug online in the cloud to set a breakpoint to what your code is actually doing. So this is just a visual interpretation of your code. It would make sense since ema10.EMA is effective after 10 ticks and the ema50 needs 5 times more data. At start both are zero, so ema10.EMA is larger after 10 ticks. I assume data comes in every 1 minute, so N=10 and N=50 means you look on 10-50 minutes scale?

J.
0

Hang in there @Quant :) We'll have custom charting soon to make debugging easier.. @Gustavo is working on the charts and the backend already supports it. I'm also preparing to release a desktop version to run backtests locally on QC.. its slow going with live trading though.

Re: EMA -- Pushing into a Queue[10] type will dump old data after 10 items are in the queue if I understand it correctly. EMA should be calculated using online formulas anyway and you don't need a queue.
0

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.


I believe the queue is used to store data items before the average can be calculated on the average of last tick. If you know a library which can process tick data on the fly instead of doing numerous calculations, that would be great.

Sample code (not tested yet!):

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;
}
}
}
0

I had something similar prepped for next release -- to kick off the community indicator library :) Would you be interested in contributing to the OS project?


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

0

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.


Depends on the effort required :-).
0

it only shows one trade though.
1

Update Backtest





0

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.


Loading...

This discussion is closed