Replies

ojey39
27 Jun 2023, 12:35

RE:

firemyst said:

This is a multistep process:

 

1) create a global variable that counts the number of crossovers.

private int _maCrossOverCount;

2) In the OnStart method, set the count to zero:

_maCrossOverCount = 0;

3) Every time a cross over happens, increase the count by 1. However, you have to decide if you want the cross over to count if it happens when a "tick" comes in or a new bar. Reason being is obviously if the MA's are close, they could cross over hundreds of time within a bar on every tick depending on how much price fluctuates

if (_sma1.Result.LastValue > _sma2.Result.LastValue)
            {
              _maCrossOverCount += 1;



//and


if (_sma1.Result.LastValue < _sma2.Result.LastValue)
            {
              _maCrossOverCount += 1;

 

 

4) Check the crossover count before you open a position:

if (_maCrossOverCount > 1)

{

   /// code what you need to in order to open your position

}

Thanks a lot.


@ojey39

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