I need a robot operation to close me, when 2 intersect MA

Created at 15 Dec 2013, 18:58
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!
RE

realesau

Joined 15.12.2013

I need a robot operation to close me, when 2 intersect MA
15 Dec 2013, 18:58


MA single crossing MA linear Weighted closing the open operation.


@realesau
Replies

kalex718
16 Dec 2013, 15:21

This robot looks for a position by label and closes it if MA (type defined by input) crosses Lwma (/algos/indicators/show/81)

 

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class CrossingRobot : Robot
    {
        private MovingAverage movingAverage;
        private Lwma lwma;

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

        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

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


        [Parameter(DefaultValue = "Crossing Robot")]
        public string Label { get; set; }
        
        protected override void OnStart()
        {
            movingAverage = Indicators.MovingAverage(SourceSeries, MaPeriods, MAType);
            lwma = Indicators.GetIndicator<Lwma>(SourceSeries, LwmaPeriods);

        }

        protected override void OnTick()
        {
            var position = Positions.Find(Label);
            if (position == null)
                return;

            if (movingAverage.Result.HasCrossedAbove(lwma.Result, 0))
                ClosePosition(position);
        }

    }
}

 


@kalex718