Problem comparing price and EMA

Created at 10 Jul 2019, 21:04
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!
MA

martin100181@gmail.com

Joined 10.06.2019

Problem comparing price and EMA
10 Jul 2019, 21:04


Hello, 

I need a part of my bot to set a var Status with a 1 value when price closes between two EMAs. 

Example: 

Close value is between Fast MA and Slow MA, so Status should be 1. 

It is not working. When running, executes case 0 (there´s a print) but there is some probem with the second if, because status remains in 0. Part of the Code: 

              if (slowMa.Result.Last(1) > fastMa.Result.Last(1))
            {
                Print("Configuración de Venta");


                switch (status)
                {
                    case 0:
                        Print("Entro en case 0 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) <= slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) >= fastMa.Result.Last(-1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 0;
                        break;
                    case 1:
                        Print("Entro en case 1 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) > fastMa.Result.Last(-1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 2;
                        break;
                    case 2:
                        Print("Entro en case 2 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) > fastMa.Result.Last(-1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 0;
                        break;
                    case -1:
                        Print("Entro en case -1 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) > fastMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 0;
                        break;
                }
                Print("Status: " + status);

 


@martin100181@gmail.com
Replies

PanagiotisCharalampous
11 Jul 2019, 09:46

Hi martin100181@gmail.com,

Can you post the complete cBot code?

Best Regards,

Panagiotis


@PanagiotisCharalampous

martin100181@gmail.com
11 Jul 2019, 15:43

RE:
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 FrankiRangetraderv1 : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }


        [Parameter("Slow Periods", DefaultValue = 50)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 14)]
        public int FastPeriods { get; set; }
        [Parameter("Volumen", DefaultValue = 10000)]
        public double volumen { get; set; }
        [Parameter("TP", DefaultValue = 6)]
        public double SL { get; set; }
        [Parameter("SL", DefaultValue = 6)]
        public double TP { get; set; }


        public int status;


        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage fastMa;

        protected override void OnStart()
        {
            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);
            status = 0;



        }

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count;
            Print("------------------ New Bar ------------------");
            Print("Close: " + MarketSeries.Close.Last(1));
            Print("Fast MA: " + fastMa.Result.Last(1));
            Print("Slow MA: " + slowMa.Result.Last(1));

            if (slowMa.Result.Last(1) < fastMa.Result.Last(1))
            {
                Print("Configuracion de Compra");

                switch (status)
                {


                    case 0:
                        Print("Entro en case 0 de compra");
                        if (MarketSeries.Close.Last(1) > fastMa.Result.Last(1))
                            status = 0;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(1) && MarketSeries.Close.Last(1) > slowMa.Result.Last(1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(1))
                            status = -1;
                        break;
                    case 1:
                        Print("Entro en case 1 de compra");
                        if (MarketSeries.Close.Last(1) > fastMa.Result.Last(1))
                            status = 2;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(1) && MarketSeries.Close.Last(1) > slowMa.Result.Last(1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(1))
                            status = -1;
                        break;
                    case 2:
                        Print("Entro en case 2 de compra");
                        if (MarketSeries.Close.Last(1) > fastMa.Result.Last(1))
                            status = 0;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(1) && MarketSeries.Close.Last(1) > slowMa.Result.Last(1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(1))
                            status = -1;
                        break;
                    case -1:
                        Print("Entro en case -1 de compra");
                        if (MarketSeries.Close.Last(1) > fastMa.Result.Last(1))
                            status = 0;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(1) && MarketSeries.Close.Last(1) > slowMa.Result.Last(1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(1))
                            status = -1;
                        break;
                }

                Print("Status: " + status);
                int Open_trades = Positions.Count(p => p.SymbolName == Symbol.Name);
                int Open_orders = PendingOrders.Count(p => p.SymbolCode == Symbol.Name);



                if (Open_trades == 0 && Open_orders == 0 && status == 2)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volumen, "Compra", SL, TP);
                    Print("Abre Compra");
                }
                {


                }

            }

            if (slowMa.Result.Last(1) > fastMa.Result.Last(1))
            {
                Print("Configuración de Venta");


                switch (status)
                {
                    case 0:
                        Print("Entro en case 0 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) <= slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) >= fastMa.Result.Last(-1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 0;
                        break;
                    case 1:
                        Print("Entro en case 1 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) > fastMa.Result.Last(-1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 2;
                        break;
                    case 2:
                        Print("Entro en case 2 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) > fastMa.Result.Last(-1))
                            status = 1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 0;
                        break;
                    case -1:
                        Print("Entro en case -1 de venta");
                        if (MarketSeries.Close.Last(1) > slowMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < slowMa.Result.Last(-1) && MarketSeries.Close.Last(1) > fastMa.Result.Last(-1))
                            status = -1;
                        if (MarketSeries.Close.Last(1) < fastMa.Result.Last(-1))
                            status = 0;
                        break;
                }
                Print("Status: " + status);
                int Open_trades = Positions.Count(p => p.SymbolName == Symbol.Name);
                int Open_orders = PendingOrders.Count(p => p.SymbolCode == Symbol.Name);



                if (Open_trades == 0 && Open_orders == 0 && status == 2)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, volumen, "Venta", SL, TP);
                    Print("Status: " + status);
                    Print("Abre Venta");
                }
            }

            Print("---------------------------------------------");


        }


    }
}

Panagiotis Charalampous said:

Hi martin100181@gmail.com,

Can you post the complete cBot code?

Best Regards,

Panagiotis

 


@martin100181@gmail.com

martin100181@gmail.com
11 Jul 2019, 16:56

Maybe my code is not so good because I´m not a coder. The Idea is to make the bot trade when price make a retracement inside both EMAs and get out of them. Long or Short will depend on EMAs direction. 

The way I find to make this was assigning a status code for the different stages of the cicle. Maybe there´s a much better way, of course.


@martin100181@gmail.com

PanagiotisCharalampous
11 Jul 2019, 17:02

Hi martin100181@gmail.com,

Why do you set -1 in Last() function? e.g. below

slowMa.Result.Last(-1)

Best regards,

Panagiotis


@PanagiotisCharalampous

martin100181@gmail.com
11 Jul 2019, 17:19 ( Updated at: 21 Dec 2023, 09:21 )

Also I discovered I´m getting wrong EMAs values. They don´t match the ones on chart. help please!! 

 


@martin100181@gmail.com

PanagiotisCharalampous
11 Jul 2019, 17:30

Hi martin100181@gmail.com,

Can you post the cBot parameters as well?

Best regards,

Panagiotis


@PanagiotisCharalampous

martin100181@gmail.com
11 Jul 2019, 18:10

RE:

Panagiotis Charalampous said:

Hi martin100181@gmail.com,

Why do you set -1 in Last() function? e.g. below

slowMa.Result.Last(-1)

Best regards,

Panagiotis

That was a Mistake! thank you. 


@martin100181@gmail.com

martin100181@gmail.com
11 Jul 2019, 18:12

RE:

Panagiotis Charalampous said:

Hi martin100181@gmail.com,

Can you post the cBot parameters as well?

Best regards,

Panagiotis

I use default parameters. 

GER30, Range bars 3 pips.

Slow EMA 50

Fast EMA 10

6 pips SL

6 pips TP


@martin100181@gmail.com