Sample code – Hammer and EMA cross

by Chris White on February 3, 2010

An EdgeRater user recently asked about using the candlestick pattern library and combining one of the candlestick patterns with another condition such as the close price crossing over the EMA.

This sample code shows one way to achieve that using the Managed Code API.

To add this code to your EdgeRater insallation just create a file in the {EdgeRater}\Selectors\Series directory called Hammer.cs and copy and paste the code into that file. The next time you refresh the ‘Managed Code’ library, Hammer.cs will be discovered and you can drag and drop it onto the selector design surface. It will need to be compiled before it can be run.

using System;
using System.ComponentModel;
using EdgeRater.Edge;
using EdgeRater.Edge.TechnicalAnalysis;

namespace EdgeRater.UserCode
{
    [Serializable]
    [Description("Hammer and Close cross over EMA")]
    [DisplayName("Hammer EMA cross")]
    public class Hammer : SeriesSelector
    {

        //
        // This method is called for each bar of each series in the
        // data snapshot. If you return true, an event will be created
        // for that bar and will be shown in the event grid
        //
        public override bool Execute(BarSeries series, SelectorContext context)
        {
            DataSeries hammer = CandleFunctions.Hammer(series.Open, series.High, series.Low, series.Close);

            DataSeries ema = Functions.Ema(series.Close, 22);

            bool isCross = SelectorHelper.CheckCrossCondition(series.Close, CrossCondition.CrossesFromBelow, ema);

            bool isHammer = !double.IsNaN(hammer[0]) && hammer[0] != 0;

            return isCross && isHammer;
        }

        //
        // The value of this property should be the number of prior
        // bars needed to perform the selection. For instance, if in
        // the Execute() method you have used a function that needs
        // 5 bars before it can calculate a result, and another that
        // needs 10 bars, you should return a value of 10 for this
        // property.
        //
        public override int Lookback
        {
            get
            {
                return (Math.Max(CandleFunctions.HammerLookback(), 22));
            }
        }
    }
}

{ 1 comment… read it below or add one }

David February 15, 2010 at 1:55 pm

Thanks Chris. This works well and has given me other ideas on how to consolidate scans to narrow down even further.

Leave a Comment

Previous post:

Next post: