High & Low of selected bars

Created at 07 Jul 2022, 19:40
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!
PA

PattyTradeTeam

Joined 07.07.2022

High & Low of selected bars
07 Jul 2022, 19:40


Hello,

I want to calculate and draw horizontal lines on chart for High and Low of selected Bars excluding the last 3 Bars.

If I use:

High = Functions.Maximum(Bars.ClosePrices, 15);

Low = Functions.Minimum(Bars.ClosePrices, 15);

Chart.DrawHorizontalLine("high", High, Color.Blue);

Chart.DrawHorizontalLine("low", Low, Color.Yellow);

I always get the values and lines updated to the latest bar.

Is there a workaround to get the values and lines excluding a specific number of the latest bars (in my case 3 bars for example) ?

Thanks for advices,

Paul

 


@PattyTradeTeam
Replies

PanagiotisCharalampous
08 Jul 2022, 11:45

Hi Paul,

Can you share the complete code that reproduces this behavior?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

PattyTradeTeam
11 Jul 2022, 14:49

RE: Example of Robot

Hi Panagiotis,

this is an example robot as you requested.

 

 

 

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.EEuropeStandardTime, AccessRights = AccessRights.None)]

    public class Example_Robot : Robot

    {

        [Parameter("Volume Buy", DefaultValue = 2, MinValue = 0, MaxValue = 5)]

        public double VolumeBuy { get; set; }

        [Parameter("Volume Sell", DefaultValue = 2, MinValue = 0, MaxValue = 5)]

        public double VolumeSell { get; set; }

 

        //MAX BUY

        [Parameter("MAX BUY", DefaultValue = 1, MinValue = 1, MaxValue = 5)]

        public int MaxBuy { get; set; }

 

        //MAX SELL

        [Parameter("MAX SELL", DefaultValue = 1, MinValue = 1, MaxValue = 5)]

        public int MaxSell { get; set; }

 

        // MA FAST

        [Parameter("MA Fast Source", Group = "MA Fast")]

        public DataSeries MAfast { get; set; }

        [Parameter("MA Fast Period", DefaultValue = 5, Group = "MA Fast")]

        public int MAfastperiod { get; set; }

        [Parameter("MA Fast Type", DefaultValue = "Simple", Group = "MA Fast")]

        public MovingAverageType MAfasttype { get; set; }

        private MovingAverage MA_fast;

 

        // MA SLOW

        [Parameter("MA Slow Source", Group = "MA Slow")]

        public DataSeries MAslow { get; set; }

        [Parameter("MA Slow Period", DefaultValue = 13, Group = "MA Slow")]

        public int MAslowperiod { get; set; }

        [Parameter("MA Slow Type", DefaultValue = "Simple", Group = "MA Slow")]

        public MovingAverageType MAslowtype { get; set; }

        private MovingAverage MA_slow;

 

        // CALC

        protected override void OnStart()

        {

            MA_fast = Indicators.MovingAverage(MAfast, MAfastperiod, MAfasttype);

            MA_slow = Indicators.MovingAverage(MAslow, MAslowperiod, MAslowtype);

        }

 

        protected override void OnTick()

        {

            bool MA_fast_rise = Functions.IsRising(MA_fast.Result);

            bool MA_fast_fall = Functions.IsFalling(MA_fast.Result);

            bool MA_slow_rise = Functions.IsRising(MA_slow.Result);

            bool MA_slow_fall = Functions.IsFalling(MA_slow.Result);

 

            double max = Functions.Maximum(Bars.ClosePrices, 12);

            double min = Functions.Minimum(Bars.ClosePrices, 12);

            Chart.DrawHorizontalLine("max", max, Color.Blue);

            Chart.DrawHorizontalLine("min", min, Color.Yellow);

 

            // CLOSE BUY

            if (MA_fast_fall == true)

            {

                var pos_USNDAQ_B = Positions.FindAll("USNDAQ_B");

                foreach (var position in pos_USNDAQ_B)

                {

                    if (position.NetProfit > 0.1)

                    {

                        position.Close();

                        Print("CLOSE BUY");

                    }

                }

            }

            // CLOSE SELL

            if (MA_fast_rise == true)

            {

                var pos_USNDAQ_S = Positions.FindAll("USNDAQ_S");

                foreach (var position in pos_USNDAQ_S)

                {

                    if (position.NetProfit > 0.1)

                    {

                        position.Close();

                        Print("CLOSE SELL");

                    }

                }

            }

 

            // TRADE

            var pos_buy = Positions.FindAll("USNDAQ_B");

            var pos_sell = Positions.FindAll("USNDAQ_S");

            if (pos_buy.Length < MaxBuy && pos_sell.Length < MaxSell)

            {

 

                // BUY

                if (MA_slow_rise == true)

                {

                    if (MA_fast_rise == true)

                    {

                        if (Bars.LastBar.Close > MA_fast.Result.LastValue)

                        {

                            var pos_b_USNDAQ = Positions.FindAll("USNDAQ_B");

                            if (pos_b_USNDAQ.Length < MaxBuy && VolumeBuy > 0)

                            {

                                Print("BUY");

                                ExecuteMarketOrder(TradeType.Buy, Symbol.Name, VolumeBuy, "USNDAQ_B", 0, 0);

                            }

                        }

                    }

                }

 

                // SELL

                if (MA_slow_fall == true)

                {

                    if (MA_fast_fall == true)

                    {

                        if (Bars.LastBar.Close < MA_fast.Result.LastValue)

                        {

                            var pos_s_USNDAQ = Positions.FindAll("USNDAQ_S");

                            if (pos_s_USNDAQ.Length < MaxSell && VolumeSell > 0)

                            {

                                Print("SELL");

                                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, VolumeSell, "USNDAQ_S", 0, 0);

                            }

                        }

                    }

                }

 

            }

        }

    }

}

 

 

 

 


@PattyTradeTeam