Exponential Moving Average

Created at 04 Feb 2019, 14: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!
EL

eliezer_barros

Joined 21.12.2018

Exponential Moving Average
04 Feb 2019, 14:50


Please, I need help to describe a little routine to identify, for example, in this curve, the last 8 hours until now, if the direction is up, so open the buy position.

I don't know pick up a part of curve (in this case last 8 hours) and your direction, if up or down.

tks for help


@eliezer_barros
Replies

PanagiotisCharalampous
04 Feb 2019, 15:01

Hi Eliezer,

You can use a moving average and check of it is rising or falling. See below

            var sma = Indicators.SimpleMovingAverage(MarketSeries.Close, 8);
            if (sma.Result.IsRising())
            {
                // Open buy position
            }

Best Regards,

Panagiotis


@PanagiotisCharalampous

eliezer_barros
04 Feb 2019, 15:38

This system is working, but sometimes it's does not open the position in correct direction.

Please, can you see this problem?

tks

____________________________________________

 

//moeda system robot
// robot para iniciar uma operacao em determinada hora usando a media exponencial
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TesteExponentialMovingAverage : Robot
    {
        [Parameter(DefaultValue = 1)]
        public double VolumeToOpenOrders { get; set; }

        [Parameter(DefaultValue = 1000)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Stop Loss", DefaultValue = 2000)]
        public int StopLoss { get; set; }

        [Parameter("MAType", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType MaType { get; set; }

        protected override void OnBar()
        {
            if (MarketSeries.OpenTime.LastValue.Hour == 3)
            {
                var sma = Indicators.ExponentialMovingAverage(MarketSeries.Close, 4);
                {
                    if (sma.Result.IsRising())
                        ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeToOpenOrders, "", StopLoss, TakeProfitInPips);
                }
                {
                    if (sma.Result.IsFalling())
                        ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeToOpenOrders, "", StopLoss, TakeProfitInPips);
                    {
                    }

                }

            }
        }
    }
}


@eliezer_barros

PanagiotisCharalampous
04 Feb 2019, 15:41

Hi Eliezer,

Can you give us an example?

Best Regards,

Panagiotis


@PanagiotisCharalampous

eliezer_barros
04 Feb 2019, 15:58

I sent by email because here the imagen is not good.


@eliezer_barros

eliezer_barros
04 Feb 2019, 18:38

I understood that is happening.

The system is picking information of last hour and it's does not considering the tentence of curve.

Please, can you check?

Tks


@eliezer_barros

PanagiotisCharalampous
05 Feb 2019, 09:58

Hi Eliezer,

This is how it should work. The function checks if the curve is rising or failing at that moment.

Best Regards,

Panagiotis


@PanagiotisCharalampous

eliezer_barros
05 Feb 2019, 14:23

RE:

Panagiotis Charalampous said:

Hi Eliezer,

This is how it should work. The function checks if the curve is rising or failing at that moment.

Best Regards,

Panagiotis

The program should get the informations of the curve the last 4 hours and to open the position according this direction. Please see again, because sometimes this is correct, but have a several errors and the position is open in wrong direction and I can't find this problem. Observe that I created a print to log to try identify this error.

tks

 

//moeda system robot
// robot para iniciar uma operacao em determinada hora usando a media exponencial
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TesteExponentialMovingAverage : Robot
    {
        [Parameter(DefaultValue = 1)]
        public double VolumeToOpenOrders { get; set; }

        [Parameter(DefaultValue = 1000)]
        public int TakeProfitInPips { get; set; }

        [Parameter("Stop Loss", DefaultValue = 2000)]
        public int StopLoss { get; set; }

        [Parameter("MAType", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType MaType { get; set; }

        protected override void OnBar()
        {
            if (MarketSeries.OpenTime.LastValue.Hour == 3)
            {
                var sma1 = Indicators.ExponentialMovingAverage(MarketSeries.Close, 4);
                Print("Close", sma1.Result);
                var sma2 = Indicators.ExponentialMovingAverage(MarketSeries.Open, 4);
                Print("Open", sma2.Result);
                {
                    if (sma1.Result.IsRising())
                        //                   Print("UP", sma.Result);

                        ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeToOpenOrders, "", StopLoss, TakeProfitInPips);
                }
                {
                    if (sma2.Result.IsFalling())
                        //                       Print("Down", sma.Result);
                        ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeToOpenOrders, "", StopLoss, TakeProfitInPips);
                    {
                    }

                }

            }
        }
    }
}


@eliezer_barros

eliezer_barros
05 Feb 2019, 14:55

I identified the problem.
I wish that the program analyse the last 4 hours and open the position, but the system is pickup a short time.


@eliezer_barros