A useful question for everyone

Created at 08 Aug 2020, 10:56
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!
SA

samuel.jus.cornelio

Joined 19.03.2020

A useful question for everyone
08 Aug 2020, 10:56


 

Hello everyone, I'm on my next project now and I need a little help. I'm wondering how exactly I would encode an action that recognizes whether the bar has touched an indicator or not. I'm trying to code an EMA and I need to know if the current price is touching the EMA Thanks for an answer,


@samuel.jus.cornelio
Replies

PanagiotisCharalampous
10 Aug 2020, 07:56

Hi samuel.jus.cornelio,

Can you define what "touch" means? Do you mean the close price to be the same with the EMA or the EMA price to be between the open and close prices? In any case, both comparisons seem pretty straight forward. Let us know what is the difficulty for you.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

samuel.jus.cornelio
10 Aug 2020, 15:04

RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

Can you define what "touch" means? Do you mean the close price to be the same with the EMA or the EMA price to be between the open and close prices? In any case, both comparisons seem pretty straight forward. Let us know what is the difficulty for you.

Best Regards,

Panagiotis 

Join us on Telegram

 

I need to create an entry trigger.
This is when the current price is equal to the MME price.
But I don't know how to code this.

@samuel.jus.cornelio

PanagiotisCharalampous
10 Aug 2020, 15:07

RE:

Hi samuel.jus.cornelio,

See an example below

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 NewcBot : Robot
    {
        ExponentialMovingAverage _ema;
        protected override void OnStart()
        {
            _ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 14);
        }


        protected override void OnTick()
        {

            if (_ema.Result.LastValue == Symbol.Bid)
            {
                // Execute trade
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuel.jus.cornelio
10 Aug 2020, 16:54 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

See an example below

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 NewcBot : Robot
    {
        ExponentialMovingAverage _ema;
        protected override void OnStart()
        {
            _ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 14);
        }


        protected override void OnTick()
        {

            if (_ema.Result.LastValue == Symbol.Bid)
            {
                // Execute trade
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram

I tried to apply the example, but when backtesting nothing happened. Below is the code and a photo of my screen

 

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 bot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }


        [Parameter("Take_Profit", DefaultValue = 45, MinValue = 10)]
        public int TakeProfit { get; set; }

        [Parameter("Begin Trading Hour", DefaultValue = 5.0)]
        public double Begin { get; set; }

        [Parameter("Ending Trading Hour", DefaultValue = 19.0)]
        public double Ending { get; set; }

        private DateTime startTime;
        private DateTime endTime;






        public RelativeStrengthIndex _rsi;
        private ExponentialMovingAverage _Ema1;




        protected override void OnStart()
        {


        }


        protected override void OnTick()
        {

            {
                startTime = Server.Time.Date.AddHours(Begin);
                endTime = Server.Time.Date.AddHours(Ending);

                if (Trade.IsExecuting)
                    return;

                bool tradeTime = false;
                if (Begin < Ending)
                    tradeTime = Server.Time.Hour >= Begin && Server.Time.Hour < Ending;
                if (Ending < Begin)
                    tradeTime = Server.Time.Hour >= Begin || Server.Time.Hour <= Ending;

                if (!tradeTime)
                    return;
            }








            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 48);
            _Ema1 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 6);



            {
                if (_rsi.Result.LastValue < 45)
                    if (_Ema1.Result.LastValue == Symbol.Bid)



                        ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "bot", StopLoss, TakeProfit);


            }


            {
                if (_rsi.Result.LastValue > 55)
                    if (_Ema1.Result.LastValue == Symbol.Bid)





                        ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit);






            }
        }


        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

 

 

 


@samuel.jus.cornelio

PanagiotisCharalampous
10 Aug 2020, 17:06

Hi samuel.jus.cornelio,

It is very rare the EMA to become equal to the price. You need to rethink your logic. Maybe your intentions are different to what you have written.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuel.jus.cornelio
10 Aug 2020, 17:15 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

It is very rare the EMA to become equal to the price. You need to rethink your logic. Maybe your intentions are different to what you have written.

Best Regards,

Panagiotis 

Join us on Telegram

here is a picture of what i have in mind.
as I put in the code, the deal would start when the price touched or broke the EMA

 

 


@samuel.jus.cornelio

PanagiotisCharalampous
10 Aug 2020, 17:21

Hi samuel.jus.cornelio,

So you need to check if the price is above or below the EMA accordingly and not if it is equal.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuel.jus.cornelio
10 Aug 2020, 17:29

RE:

PanagiotisCharalampous said:

Hi samuel.jus.cornelio,

So you need to check if the price is above or below the EMA accordingly and not if it is equal.

Best Regards,

Panagiotis 

Join us on Telegram

 

Exactly my friend. thanks for the correction. Please, how can I code this?


@samuel.jus.cornelio

PanagiotisCharalampous
11 Aug 2020, 07:42

Hi samuel.jus.cornelio,

You need to change your conditions accordingly e.g.

if (_Ema1.Result.LastValue > Symbol.Bid)

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous