Return High Or Low Value of the candle when the Condition is True

Created at 21 Nov 2018, 16:32
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!
VI

vishalsbharati22

Joined 19.11.2018

Return High Or Low Value of the candle when the Condition is True
21 Nov 2018, 16:32


Consider the following code: Simple MA crossover code

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendFollow10100Exponenetial : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

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

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

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

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

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Exponential10and100";

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnBar()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);
            //&&  previousSlowMa > previousFastMa

            if (currentSlowMa <= currentFastMa && longPosition == null)
            {
                if (shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            }
            //&& previousSlowMa < previousFastMa
            else if (currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if (longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }
    }
}

 

Here I Need help to identify high or low of the candle of crossover and trade when one of succeding candles is closed above or below this high or low respectively.

i.e 1. When fastMA crosses above slowMA, the high of that candle should be stored in variable, and buy trade should be executed when this high is taken out by the close one of succeding candles. Trade will be executed on opening of new candle when the close of the previous candle is above this high.

2. Vice Versa for Sell trade, when fastMA crosses below SlowMA, the low of that candle should be stored in variable, and Sell trade should executed when this Low is taken out by the close of one of the succeding candles. Trade will be executed on opening of new candle when the close of the previous candle is below this low.

Please Help me out for the same, i m new to cTrader cAlgo, If anyone familiar to Amibroker, there is ValueWhen Function to do so, e.g.   BuyConditionHigh = ValueWhen(Cross(fastMA,slowMA),H);//stores the value of High of that candle when condition is true

Thanks In Advance.

 


@vishalsbharati22