3 EMA Crossover

Created at 03 Jun 2020, 15:50
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
IU

iucpxleps

Joined 01.11.2017

3 EMA Crossover
03 Jun 2020, 15:50


Hi all,

 

I think this has been discussed before and I've been trying to debug the error but my problem is when the crossover happens, it requires both fast and medium EMA to crossover the slowema in the same bar. But sometimes medium EMA lags 1 or 2 bars to crossover the slow ema while Fast ema had already crossed. So I don't need them to crossover the slowema in the same bar but trigger whenever both has crossed over the slowema ie fast crosses at 1PM while medium can cross 1 or 2 hrs later at 3PM etc. I've tried using the below template from an older post.

 

Any help appreciated.

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class EMACross_RSI : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }
        [Parameter("Label", DefaultValue = "EMA")]
        public string label { get; set; }

        [Parameter("Slow Periods", DefaultValue = 30)]
        public int SlowPeriods { get; set; }
        [Parameter("Medium Periods", DefaultValue = 12)]
        public int MediumPeriods { get; set; }
        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage mediumMa;
        private ExponentialMovingAverage fastMa;


        protected override void OnStart()
        {

            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            mediumMa = Indicators.ExponentialMovingAverage(SourceSeries, MediumPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);

        }


        protected override void OnBar()
        {
            int index = MarketSeries.OpenTime.Count - 2;
            var longPosition = Positions.Find(label, SymbolName, TradeType.Buy);
            var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

            if ((fastMa.Result[index] > slowMa.Result[index]) && (mediumMa.Result[index] > slowMa.Result[index]) && (fastMa.Result[index - 1] < slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] < slowMa.Result[index - 1]) && shortPosition == null)
            {
                                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label);

            }
            else if ((fastMa.Result[index] < slowMa.Result[index]) && (mediumMa.Result[index] < slowMa.Result[index]) && (fastMa.Result[index - 1] > slowMa.Result[index - 1]) && (mediumMa.Result[index - 1] > slowMa.Result[index - 1]) && longPosition == null)
            {

                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label);

            }

        }
        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

 


@iucpxleps
Replies

PanagiotisCharalampous
03 Jun 2020, 16:04

Hi iucpxleps,

You can use the built-in HasCrossedAbove() and HasCrossedBelow()  methods that allow you to check for a crossover for a defined number of bars in the past.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

iucpxleps
03 Jun 2020, 16:08

RE:

PanagiotisCharalampous said:

Hi iucpxleps,

You can use the built-in HasCrossedAbove() and HasCrossedBelow()  methods that allow you to check for a crossover for a defined number of bars in the past.

Best Regards,

Panagiotis 

Join us on Telegram

 

Ah thanks Panagiotis, I actually looked into that but somehow it didnt occur to me that I could use it in a lookback . It should work and probably can be easier to implement. I'll drop a line if I ran into any problems :) Thanks for the help.

 

just an edit: It  can be used with onBar() right or is it completely unrelated?


@iucpxleps