How to Change Moving Average Source from close to Low or High.

Created at 14 Feb 2022, 15:02
OJ

ojey39

Joined 14.02.2022

How to Change Moving Average Source from close to Low or High.
14 Feb 2022, 15:02


Hello,

I want to change the moving average source from Close to Low for SMA 1 and from Close to High for SMA2? Currently both are at default close

 

[Parameter("Instance Name", DefaultValue = "001")]
        public string InstanceName { get; set; }

        [Parameter("Lot Size", DefaultValue = 0.1)]
        public double LotSize { get; set; }

        [Parameter("Source SMA #1")]
        public DataSeries SourceSma1 { get; set; }

        [Parameter("Source SMA #2")]
        public DataSeries SourceSma2 { get; set; }

        [Parameter("Period SMA #1", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma1 { get; set; }

        [Parameter("Period SMA #2", DefaultValue = 20, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma2 { get; set; }

        [Parameter("Calculate OnBar", DefaultValue = false)]
        public bool CalculateOnBar { get; set; }

        #endregion

        #region Indicator declarations

        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }

        #endregion

        #region cTrader events

        /// <summary>
        /// This is called when the robot first starts, it is only called once.
        /// </summary>
        protected override void OnStart()
        {
            // construct the indicators
            _sma1 = Indicators.SimpleMovingAverage(SourceSma1, PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(SourceSma2, PeriodsSma2);
        }

        /// <summary>
        /// This method is called every time the price changes for the symbol
        /// </summary>

 


@ojey39
Replies

amusleh
15 Feb 2022, 09:01

Hi,

You can use an enum, ex:

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Instance Name", DefaultValue = "001")]
        public string InstanceName { get; set; }

        [Parameter("Lot Size", DefaultValue = 0.1)]
        public double LotSize { get; set; }

        [Parameter("Source SMA #1", DefaultValue = DataSource.Low)]
        public DataSource SourceSma1 { get; set; }

        [Parameter("Source SMA #2", DefaultValue = DataSource.High)]
        public DataSource SourceSma2 { get; set; }

        [Parameter("Period SMA #1", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma1 { get; set; }

        [Parameter("Period SMA #2", DefaultValue = 20, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma2 { get; set; }

        [Parameter("Calculate OnBar", DefaultValue = false)]
        public bool CalculateOnBar { get; set; }

        #region Indicator declarations

        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }

        #endregion Indicator declarations

        /// <summary>
        /// This is called when the robot first starts, it is only called once.
        /// </summary>
        protected override void OnStart()
        {
            // construct the indicators
            _sma1 = Indicators.SimpleMovingAverage(GetSeries(SourceSma1), PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(GetSeries(SourceSma2), PeriodsSma2);
        }

        private DataSeries GetSeries(DataSource source)
        {
            switch (source)
            {
                case DataSource.High:
                    return Bars.HighPrices;

                case DataSource.Low:
                    return Bars.LowPrices;

                case DataSource.Close:
                    return Bars.ClosePrices;

                case DataSource.Open:
                    return Bars.OpenPrices;

                default:
                    throw new ArgumentOutOfRangeException("source");
            }
        }
    }

    public enum DataSource
    {
        Open,
        High,
        Low,
        Close
    }
}

 


@amusleh

ojey39
16 Feb 2022, 11:12

RE:

amusleh said:

Hi,

You can use an enum, ex:

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Instance Name", DefaultValue = "001")]
        public string InstanceName { get; set; }

        [Parameter("Lot Size", DefaultValue = 0.1)]
        public double LotSize { get; set; }

        [Parameter("Source SMA #1", DefaultValue = DataSource.Low)]
        public DataSource SourceSma1 { get; set; }

        [Parameter("Source SMA #2", DefaultValue = DataSource.High)]
        public DataSource SourceSma2 { get; set; }

        [Parameter("Period SMA #1", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma1 { get; set; }

        [Parameter("Period SMA #2", DefaultValue = 20, MinValue = 1, MaxValue = 100)]
        public int PeriodsSma2 { get; set; }

        [Parameter("Calculate OnBar", DefaultValue = false)]
        public bool CalculateOnBar { get; set; }

        #region Indicator declarations

        private SimpleMovingAverage _sma1 { get; set; }
        private SimpleMovingAverage _sma2 { get; set; }

        #endregion Indicator declarations

        /// <summary>
        /// This is called when the robot first starts, it is only called once.
        /// </summary>
        protected override void OnStart()
        {
            // construct the indicators
            _sma1 = Indicators.SimpleMovingAverage(GetSeries(SourceSma1), PeriodsSma1);
            _sma2 = Indicators.SimpleMovingAverage(GetSeries(SourceSma2), PeriodsSma2);
        }

        private DataSeries GetSeries(DataSource source)
        {
            switch (source)
            {
                case DataSource.High:
                    return Bars.HighPrices;

                case DataSource.Low:
                    return Bars.LowPrices;

                case DataSource.Close:
                    return Bars.ClosePrices;

                case DataSource.Open:
                    return Bars.OpenPrices;

                default:
                    throw new ArgumentOutOfRangeException("source");
            }
        }
    }

    public enum DataSource
    {
        Open,
        High,
        Low,
        Close
    }
}

 

Thanks a lot. It works. 


@ojey39