Get the price value at the moment of intersection with MA

Created at 02 Dec 2021, 14:41
V.

v.fiodorov83

Joined 24.07.2019

Get the price value at the moment of intersection with MA
02 Dec 2021, 14:41


Hello good people!

For example, a bar closes below the MA. How to get and store in a variable the price value that MA generates at this moment of intersection


@v.fiodorov83
Replies

amusleh
03 Dec 2021, 08:37

Hi,

Try this:

using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ExponentialMovingAverageSample : Robot
    {
        private ExponentialMovingAverage _ema;

        protected override void OnStart()
        {
            _ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
        }
        protected override void OnBar()
        {
            // This variable will have the cross price if there was any
            double maCrossPoint = double.NaN;
            
            // Bar close price crosses MA upward
            if (Bars.ClosePrices.Last(1) > _ema.Result.Last(1) && Bars.ClosePrices.Last(2) < _ema.Result.Last(2))
            {
                maCrossPoint = _ema.Result.Last(1);
            }
            // Bar close price crosses MA downward
            else if (Bars.ClosePrices.Last(1) < _ema.Result.Last(1) && Bars.ClosePrices.Last(2) > _ema.Result.Last(2))
            {
                maCrossPoint = _ema.Result.Last(1);
            }
        }
    }
}

 


@amusleh

v.fiodorov83
19 Dec 2021, 23:05

RE: Sorry for taking so long to answer. But everything works, thanks for the help !

amusleh said:

Hi,

Try this:

using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ExponentialMovingAverageSample : Robot
    {
        private ExponentialMovingAverage _ema;

        protected override void OnStart()
        {
            _ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 20);
        }
        protected override void OnBar()
        {
            // This variable will have the cross price if there was any
            double maCrossPoint = double.NaN;
            
            // Bar close price crosses MA upward
            if (Bars.ClosePrices.Last(1) > _ema.Result.Last(1) && Bars.ClosePrices.Last(2) < _ema.Result.Last(2))
            {
                maCrossPoint = _ema.Result.Last(1);
            }
            // Bar close price crosses MA downward
            else if (Bars.ClosePrices.Last(1) < _ema.Result.Last(1) && Bars.ClosePrices.Last(2) > _ema.Result.Last(2))
            {
                maCrossPoint = _ema.Result.Last(1);
            }
        }
    }
}

 

 


@v.fiodorov83