/// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Calculation
            double[] adOpenPrice = new double[Bars];

            for (int iBar = 1; iBar < Bars; iBar++)
            {
                if (Time[iBar - 1].Day != Time[iBar].Day)
                {
                    adOpenPrice[iBar] = Open[iBar];
                }
            }

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0]           = new IndicatorComp();
            Component[0].CompName  = "Opening price of the day";
            Component[0].DataType  = IndComponentType.OpenPrice;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 2;
            Component[0].Value     = adOpenPrice;

            return;
        }
Exemplo n.º 2
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Calculation
            const int firstBar  = 2;
            var       insideBAr = new double[Bars];

            for (int bar = 2; bar < Bars; bar++)
            {
                insideBAr[bar] = ((High[bar - 1] < High[bar - 2]) && (Low[bar - 1] > Low[bar - 2])) ? 1 : 0;
            }

            // Saving the components
            Component = new IndicatorComp[2];

            Component[0] = new IndicatorComp
            {
                CompName  = "Allow long entry",
                DataType  = IndComponentType.AllowOpenLong,
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = insideBAr
            };

            Component[1] = new IndicatorComp
            {
                CompName  = "Allow short entry",
                DataType  = IndComponentType.AllowOpenShort,
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = insideBAr
            };
        }
        /// <summary>
        /// Calculates the indicator's components.
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Reading the parameters
            int iPercent = (int)IndParam.NumParam[0].Value;

            // Calculation
            int iFirstBar = 1;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0]               = new IndicatorComp();
            Component[0].CompName      = "Stop to a transferred position";
            Component[0].DataType      = IndComponentType.Other;
            Component[0].ShowInDynInfo = false;
            Component[0].FirstBar      = iFirstBar;
            Component[0].Value         = new double[Bars];

            // The program calculates the Account Percent Stop during the backtest.
            // It depends on the account value at the moment of the transaction.
            //
            // double dDeltaMoney = iPercent * MoneyBalance(iBar) / (100 * iLots);
            // double dDeltaStop  = Math.Max(MoneyToPips(dDeltaMoney, iBar), 5) * Point;
            // double dStop = FormOrdPrice + dDeltaStop;

            return;
        }
        private void CalculateForBacktester()
        {
            // Calculation
            var adClosePrice = new double[Bars];

            for (int bar = 1; bar < Bars; bar++)
            {
                if (Time[bar - 1].Day != Time[bar].Day)
                {
                    adClosePrice[bar - 1] = Close[bar - 1];
                }
            }

            // Check the last bar
            TimeSpan tsBarClosing = Time[Bars - 1].TimeOfDay.Add(new TimeSpan(0, (int)Period, 0));
            var      tsDayClosing = new TimeSpan(24, 0, 0);

            if (tsBarClosing == tsDayClosing)
            {
                adClosePrice[Bars - 1] = Close[Bars - 1];
            }

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName  = "Closing price of the day",
                DataType  = IndComponentType.ClosePrice,
                ChartType = IndChartType.NoChart,
                FirstBar  = 2,
                Value     = adClosePrice
            };
        }
Exemplo n.º 5
0
        public Indicator()
        {
            IndParam = new IndicatorParam();

            IndicatorName = string.Empty;
            PossibleSlots = SlotTypes.NotDefined;
            SlotType = SlotTypes.NotDefined;

            SeparatedChart = false;
            SeparatedChartMinValue = double.MaxValue;
            SeparatedChartMaxValue = double.MinValue;
            SpecialValues = new double[0];

            IsDiscreteValues = false;
            CustomIndicator = false;
            IsBacktester = true;
            IsGeneratable = true;
            WarningMessage = string.Empty;
            AllowClosingFilters = false;

            IndicatorAuthor = "Forex Software Ltd";
            IndicatorVersion = "1.0";
            IndicatorDescription = "Bundled in FSB distribution.";

            ExitFilterShortDescription = "Not defined";
            EntryFilterShortDescription = "Not defined";
            ExitFilterLongDescription = "Not defined";
            EntryFilterLongDescription = "Not defined";
            ExitPointShortDescription = "Not defined";
            ExitPointLongDescription = "Not defined";
            EntryPointShortDescription = "Not defined";
            EntryPointLongDescription = "Not defined";

            Component = new IndicatorComp[] {};
        }
Exemplo n.º 6
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var entryHour   = (int)IndParam.NumParam[0].Value;
            var entryMinute = (int)IndParam.NumParam[1].Value;
            var entryTime   = new TimeSpan(entryHour, entryMinute, 0);

            // Calculation
            const int firstBar = 1;
            var       signal   = new double[Bars];

            // Calculation of the logic
            for (int bar = firstBar; bar < Bars; bar++)
            {
                signal[bar] = Time[bar].TimeOfDay == entryTime ? Open[bar] : 0;
            }

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName      = "Entry hour",
                DataType      = IndComponentType.OpenPrice,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = signal
            };
        }
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            if (!IsBacktester)
            {
                // FST sends the N bars for exit to the expert. Expert watches the position and closes it.
                return;
            }

            var nExit = (int)IndParam.NumParam[0].Value;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName      = "N Bars Exit (" + nExit.ToString(CultureInfo.InvariantCulture) + ")",
                DataType      = IndComponentType.ForceClose,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = true,
                FirstBar      = 1,
                Value         = new double[Bars]
            };
        }
Exemplo n.º 8
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Calculation
            var adOpenPrice = new double[Bars];

            for (int iBar = 1; iBar < Bars; iBar++)
            {
                if (Time[iBar - 1].Day != Time[iBar].Day)
                {
                    adOpenPrice[iBar] = Open[iBar];
                }
            }

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName  = "Opening price of the day",
                DataType  = IndComponentType.OpenPrice,
                ChartType = IndChartType.NoChart,
                FirstBar  = 2,
                Value     = adOpenPrice
            };
        }
        /// <summary>
        /// Calculates the indicator's components.
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Reading the parameters
            int iPercent = (int)IndParam.NumParam[0].Value;

            // Calculation
            int iFirstBar = 1;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp();
            Component[0].CompName      = "Stop to a transferred position";
            Component[0].DataType      = IndComponentType.Other;
            Component[0].ShowInDynInfo = false;
            Component[0].FirstBar      = iFirstBar;
            Component[0].Value         = new double[Bars];

            // The program calculates the Account Percent Stop during the backtest.
            // It depends on the account value at the moment of the transaction.
            //
            // double dDeltaMoney = iPercent * MoneyBalance(iBar) / (100 * iLots);
            // double dDeltaStop  = Math.Max(MoneyToPips(dDeltaMoney, iBar), 5) * Point;
            // double dStop = FormOrdPrice + dDeltaStop;

            return;
        }
Exemplo n.º 10
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var dowFromDay  = (DayOfWeek)IndParam.ListParam[1].Index;
            var dowUntilDay = (DayOfWeek)IndParam.ListParam[2].Index;

            // Calculation
            const int firstBar = 1;
            var       adBars   = new double[Bars];

            // Calculation of the logic
            for (int iBar = firstBar; iBar < Bars; iBar++)
            {
                if (dowFromDay < dowUntilDay)
                {
                    adBars[iBar] = Time[iBar].DayOfWeek >= dowFromDay &&
                                   Time[iBar].DayOfWeek < dowUntilDay
                                       ? 1
                                       : 0;
                }
                else if (dowFromDay > dowUntilDay)
                {
                    adBars[iBar] = Time[iBar].DayOfWeek >= dowFromDay ||
                                   Time[iBar].DayOfWeek < dowUntilDay
                                       ? 1
                                       : 0;
                }
                else
                {
                    adBars[iBar] = 1;
                }
            }

            // Saving the components
            Component = new IndicatorComp[2];

            Component[0] = new IndicatorComp
            {
                CompName      = "Allow long entry",
                DataType      = IndComponentType.AllowOpenLong,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = adBars
            };

            Component[1] = new IndicatorComp
            {
                CompName      = "Allow short entry",
                DataType      = IndComponentType.AllowOpenShort,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = adBars
            };
        }
Exemplo n.º 11
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var fromHour  = (int)IndParam.NumParam[0].Value;
            var fromMin   = (int)IndParam.NumParam[1].Value;
            var untilHour = (int)IndParam.NumParam[2].Value;
            var untilMin  = (int)IndParam.NumParam[3].Value;
            var fromTime  = new TimeSpan(fromHour, fromMin, 0);
            var untilTime = new TimeSpan(untilHour, untilMin, 0);

            // Calculation
            int firstBar = 2;
            var signal   = new double[Bars];

            // Calculation of the logic
            for (int bar = firstBar; bar < Bars; bar++)
            {
                if (fromTime < untilTime)
                {
                    signal[bar] = Time[bar].TimeOfDay >= fromTime && Time[bar].TimeOfDay < untilTime ? 1 : 0;
                }
                else if (fromTime > untilTime)
                {
                    signal[bar] = Time[bar].TimeOfDay >= fromTime || Time[bar].TimeOfDay < untilTime ? 1 : 0;
                }
                else
                {
                    signal[bar] = 1;
                }
            }

            // Saving the components
            Component = new IndicatorComp[2];

            Component[0] = new IndicatorComp
            {
                CompName      = "Is long entry allowed",
                DataType      = IndComponentType.AllowOpenLong,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = signal
            };

            Component[1] = new IndicatorComp
            {
                CompName      = "Is short entry allowed",
                DataType      = IndComponentType.AllowOpenShort,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = signal
            };
        }
Exemplo n.º 12
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var maMethod = (MAMethod)IndParam.ListParam[1].Index;
            var period   = (int)IndParam.NumParam[0].Value;
            var multipl  = (int)IndParam.NumParam[1].Value;
            int prev     = IndParam.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int firstBar = period + 2;

            var atr = new double[Bars];

            for (int bar = 1; bar < Bars; bar++)
            {
                atr[bar] = Math.Max(High[bar], Close[bar - 1]) - Math.Min(Low[bar], Close[bar - 1]);
            }

            atr = MovingAverage(period, 0, maMethod, atr);

            var    atrStop = new double[Bars];
            double pip     = (Digits == 5 || Digits == 3) ? 10 * Point : Point;
            double minStop = 5 * pip;

            for (int bar = firstBar; bar < Bars - prev; bar++)
            {
                atrStop[bar + prev] = Math.Max(atr[bar] * multipl, minStop);
            }

            // Saving the components
            Component = new IndicatorComp[2];

            Component[0] = new IndicatorComp
            {
                CompName      = "ATR Stop margin",
                DataType      = IndComponentType.IndicatorValue,
                FirstBar      = firstBar,
                ShowInDynInfo = false,
                Value         = atrStop
            };

            Component[1] = new IndicatorComp
            {
                CompName      = "ATR Stop for the transferred position",
                DataType      = IndComponentType.Other,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = new double[Bars]
            };
        }
Exemplo n.º 13
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var exitHour   = (int)IndParam.NumParam[0].Value;
            var tsExitHour = new TimeSpan(exitHour, 0, 0);

            // Calculation
            const int firstBar = 1;
            var       adBars   = new double[Bars];

            // Calculation of the logic
            for (int bar = firstBar; bar < Bars; bar++)
            {
                if (Time[bar - 1].DayOfYear == Time[bar].DayOfYear &&
                    Time[bar - 1].TimeOfDay < tsExitHour &&
                    Time[bar].TimeOfDay >= tsExitHour)
                {
                    adBars[bar - 1] = Close[bar - 1];
                }
                else if (Time[bar - 1].DayOfYear != Time[bar].DayOfYear &&
                         Time[bar - 1].TimeOfDay < tsExitHour)
                {
                    adBars[bar - 1] = Close[bar - 1];
                }
                else
                {
                    adBars[bar] = 0;
                }
            }

            // Check the last bar
            if (Time[Bars - 1].TimeOfDay.Add(new TimeSpan(0, (int)Period, 0)) == tsExitHour)
            {
                adBars[Bars - 1] = Close[Bars - 1];
            }

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName      = "Exit hour",
                DataType      = IndComponentType.ClosePrice,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = adBars
            };
        }
Exemplo n.º 14
0
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[1];

            Component[0]               = new IndicatorComp();
            Component[0].CompName      = "Trailing Stop for a transferred position";
            Component[0].DataType      = IndComponentType.Other;
            Component[0].ShowInDynInfo = false;
            Component[0].FirstBar      = 1;
            Component[0].Value         = new double[Bars];

            return;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[1];

            Component[0]           = new IndicatorComp();
            Component[0].CompName  = "Closing price of the bar";
            Component[0].DataType  = (IndParam.SlotType == SlotTypes.Open) ? IndComponentType.OpenPrice : IndComponentType.ClosePrice;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 2;
            Component[0].Value     = Close;

            return;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[1];

            Component[0]           = new IndicatorComp();
            Component[0].CompName  = "Opening price of the bar";
            Component[0].DataType  = IndComponentType.OpenPrice;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 2;
            Component[0].Value     = Open;

            return;
        }
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp();
            Component[0].CompName  = "Closing price of the bar";
            Component[0].DataType  = (IndParam.SlotType == SlotTypes.Open) ? IndComponentType.OpenPrice : IndComponentType.ClosePrice;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 2;
            Component[0].Value     = Close;

            return;
        }
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp();
            Component[0].CompName      = "Trailing Stop for the transferred position";
            Component[0].DataType	   = IndComponentType.Other;
            Component[0].ShowInDynInfo = false;
            Component[0].FirstBar	   = 1;
            Component[0].Value	       = new double[Bars];

            return;
        }
Exemplo n.º 19
0
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp();
            Component[0].CompName  = "Opening price of the bar";
            Component[0].DataType  = IndComponentType.OpenPrice;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 2;
            Component[0].Value     = Open;

            return;
        }
Exemplo n.º 20
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            int iFromDay  = (int)IndParam.NumParam[0].Value;
            int iUntilDay = (int)IndParam.NumParam[1].Value;

            // Calculation
            int iFirstBar = 1;

            double[] adBars = new double[Bars];

            // Calculation of the logic
            for (int iBar = iFirstBar; iBar < Bars; iBar++)
            {
                if (iFromDay < iUntilDay)
                {
                    adBars[iBar] = Time[iBar].Day >= iFromDay && Time[iBar].Day < iUntilDay ? 1 : 0;
                }
                else if (iFromDay > iUntilDay)
                {
                    adBars[iBar] = Time[iBar].Day >= iFromDay || Time[iBar].Day < iUntilDay ? 1 : 0;
                }
                else
                {
                    adBars[iBar] = 1;
                }
            }

            // Saving the components
            Component = new IndicatorComp[2];

            Component[0]               = new IndicatorComp();
            Component[0].CompName      = "Allow long entry";
            Component[0].DataType      = IndComponentType.AllowOpenLong;
            Component[0].ChartType     = IndChartType.NoChart;
            Component[0].ShowInDynInfo = false;
            Component[0].FirstBar      = iFirstBar;
            Component[0].Value         = adBars;

            Component[1]               = new IndicatorComp();
            Component[1].CompName      = "Allow short entry";
            Component[1].DataType      = IndComponentType.AllowOpenShort;
            Component[1].ChartType     = IndChartType.NoChart;
            Component[1].ShowInDynInfo = false;
            Component[1].FirstBar      = iFirstBar;
            Component[1].Value         = adBars;
        }
Exemplo n.º 21
0
 /// <summary>
 ///     The default constructor.
 /// </summary>
 public IndicatorSlot()
 {
     SlotNumber     = 0;
     SlotType       = SlotTypes.NotDefined;
     LogicalGroup   = "";
     IsDefined      = false;
     SlotStatus     = StrategySlotStatus.Open;
     IndicatorName  = "Not defined";
     IndParam       = new IndicatorParam();
     SeparatedChart = false;
     Component      = new IndicatorComp[] {};
     SpecValue      = new double[] {};
     MinValue       = double.MaxValue;
     MaxValue       = double.MinValue;
 }
 /// <summary>
 ///  The default constructor.
 /// </summary>
 public IndicatorSlot()
 {
     SlotNumber = 0;
     SlotType = SlotTypes.NotDefined;
     LogicalGroup = "";
     IsDefined = false;
     SlotStatus = StrategySlotStatus.Open;
     IndicatorName = "Not defined";
     IndParam = new IndicatorParam();
     SeparatedChart = false;
     Component = new IndicatorComp[] {};
     SpecValue = new double[] {};
     MinValue = double.MaxValue;
     MaxValue = double.MinValue;
 }
Exemplo n.º 23
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName  = "Opening price of the bar",
                DataType  = IndComponentType.OpenPrice,
                ChartType = IndChartType.NoChart,
                FirstBar  = 2,
                Value     = Open
            };
        }
Exemplo n.º 24
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName  = "Close Price",
                DataType  = (IndParam.SlotType == SlotTypes.Open) ? IndComponentType.OpenPrice : IndComponentType.ClosePrice,
                ChartType = IndChartType.NoChart,
                FirstBar  = 2,
                Value     = Close
            };
        }
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName      = "Trailing Stop for a transferred position",
                DataType      = IndComponentType.Other,
                ShowInDynInfo = false,
                FirstBar      = 1,
                Value         = new double[Bars]
            };
        }
Exemplo n.º 26
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            int hour   = (int)IndParam.NumParam[0].Value;
            int minute = (int)IndParam.NumParam[1].Value;

            // Calculation
            const int firstBar = 2;

            double[] signal = new double[Bars];

            // Calculation of the logic
            for (int bar = firstBar; bar < Bars; bar++)
            {
                DateTime closeTime = Time[bar].AddMinutes((int)Period);
                if (closeTime.Hour == hour && closeTime.Minute == minute)
                {
                    signal[bar] = 1;
                }
            }

            // Saving the components
            Component = new IndicatorComp[2];

            Component[0] = new IndicatorComp
            {
                CompName      = "Close out long position",
                DataType      = IndComponentType.ForceCloseLong,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = true,
                FirstBar      = firstBar,
                Value         = signal
            };

            Component[1] = new IndicatorComp
            {
                CompName      = "Close out short position",
                DataType      = IndComponentType.ForceCloseShort,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = true,
                FirstBar      = firstBar,
                Value         = signal
            };
        }
Exemplo n.º 27
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Saving the components
            Component = new IndicatorComp[2];

            Component[0] = new IndicatorComp
            {
                CompName  = "Is long entry allowed",
                DataType  = IndComponentType.AllowOpenLong,
                ChartType = IndChartType.NoChart,
                FirstBar  = 0,
                Value     = new double[Bars]
            };

            Component[1] = new IndicatorComp
            {
                CompName  = "Is short entry allowed",
                DataType  = IndComponentType.AllowOpenShort,
                ChartType = IndChartType.NoChart,
                FirstBar  = 0,
                Value     = new double[Bars]
            };

            // Calculation of the logic
            switch (IndParam.ListParam[0].Text)
            {
            case "Open long positions only":
                for (int i = 0; i < Bars; i++)
                {
                    Component[0].Value[i] = 1;
                    Component[1].Value[i] = 0;
                }
                break;

            case "Open short positions only":
                for (int i = 0; i < Bars; i++)
                {
                    Component[0].Value[i] = 0;
                    Component[1].Value[i] = 1;
                }
                break;
            }
        }
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[2];

            Component[0] = new IndicatorComp();
            Component[0].CompName  = "Is long entry allowed";
            Component[0].DataType  = IndComponentType.AllowOpenLong;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 0;
            Component[0].Value     = new double[Bars];

            Component[1] = new IndicatorComp();
            Component[1].CompName  = "Is short entry allowed";
            Component[1].DataType  = IndComponentType.AllowOpenShort;
            Component[1].ChartType = IndChartType.NoChart;
            Component[1].FirstBar  = 0;
            Component[1].Value     = new double[Bars];

            // Calculation of the logic
            switch (IndParam.ListParam[0].Text)
            {
                case "Open long positions only":
                    for (int i = 0; i < Bars; i++)
                    {
                        Component[0].Value[i] = 1;
                        Component[1].Value[i] = 0;
                    }
                    break;

                case "Open short positions only":
                    for (int i = 0; i < Bars; i++)
                    {
                        Component[0].Value[i] = 0;
                        Component[1].Value[i] = 1;
                    }
                    break;

                default:
                    break;
            }

            return;
        }
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Calculation
            const int firstBar = 1;
            var       adBars   = new double[Bars];

            // Calculation of the logic
            for (int bar = 0; bar < Bars - 1; bar++)
            {
                if (Time[bar].DayOfWeek > DayOfWeek.Wednesday &&
                    Time[bar + 1].DayOfWeek < DayOfWeek.Wednesday)
                {
                    adBars[bar] = Close[bar];
                }
                else
                {
                    adBars[bar] = 0;
                }
            }

            // Check the last bar
            TimeSpan tsBarClosing = Time[Bars - 1].TimeOfDay.Add(new TimeSpan(0, (int)Period, 0));
            var      tsDayClosing = new TimeSpan(24, 0, 0);

            if (Time[Bars - 1].DayOfWeek == DayOfWeek.Friday && tsBarClosing == tsDayClosing)
            {
                adBars[Bars - 1] = Close[Bars - 1];
            }

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName      = "Week Closing",
                DataType      = IndComponentType.ClosePrice,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = firstBar,
                Value         = adBars
            };
        }
Exemplo n.º 30
0
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Saving the components
            Component = new IndicatorComp[2];

            Component[0]           = new IndicatorComp();
            Component[0].CompName  = "Is long entry allowed";
            Component[0].DataType  = IndComponentType.AllowOpenLong;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 0;
            Component[0].Value     = new double[Bars];

            Component[1]           = new IndicatorComp();
            Component[1].CompName  = "Is short entry allowed";
            Component[1].DataType  = IndComponentType.AllowOpenShort;
            Component[1].ChartType = IndChartType.NoChart;
            Component[1].FirstBar  = 0;
            Component[1].Value     = new double[Bars];

            // Calculation of the logic
            switch (IndParam.ListParam[0].Text)
            {
            case "Open long positions only":
                for (int i = 0; i < Bars; i++)
                {
                    Component[0].Value[i] = 1;
                    Component[1].Value[i] = 0;
                }
                break;

            case "Open short positions only":
                for (int i = 0; i < Bars; i++)
                {
                    Component[0].Value[i] = 0;
                    Component[1].Value[i] = 1;
                }
                break;

            default:
                break;
            }

            return;
        }
        private void CalculateForBacktester()
        {
            IndParam.ExecutionTime = ExecutionTime.AtBarClosing;

            // Calculation
            var adClosePrice = new double[Bars];

            // Calculation of the logic
            for (int bar = 0; bar < Bars - 1; bar++)
            {
                if (Time[bar].DayOfWeek > DayOfWeek.Wednesday &&
                    Time[bar + 1].DayOfWeek < DayOfWeek.Wednesday)
                {
                    adClosePrice[bar] = Close[bar];
                }
                else
                {
                    adClosePrice[bar] = 0;
                }
            }

            // Check the last bar
            TimeSpan tsBarClosing = Time[Bars - 1].TimeOfDay.Add(new TimeSpan(0, (int)Period, 0));
            var      tsDayClosing = new TimeSpan(24, 0, 0);

            if (Time[Bars - 1].DayOfWeek == DayOfWeek.Friday && tsBarClosing == tsDayClosing)
            {
                adClosePrice[Bars - 1] = Close[Bars - 1];
            }

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp
            {
                CompName      = "Week Closing",
                DataType      = IndComponentType.ClosePrice,
                ChartType     = IndChartType.NoChart,
                ShowInDynInfo = false,
                FirstBar      = 2,
                Value         = adClosePrice
            };
        }
        /// <summary>
        /// Calculates the indicator's components.
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Reading the parameters
            int percent = (int)IndParam.NumParam[0].Value;

            // Calculation
            int firstBar = 1;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0] = new IndicatorComp();
            Component[0].CompName      = "Stop to a transferred position";
            Component[0].DataType	   = IndComponentType.Other;
            Component[0].ShowInDynInfo = false;
            Component[0].FirstBar	   = firstBar;
            Component[0].Value	       = new double[Bars];

            return;
        }
        /// <summary>
        /// Calculates the indicator's components.
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Reading the parameters
            int percent = (int)IndParam.NumParam[0].Value;

            // Calculation
            int firstBar = 1;

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0]               = new IndicatorComp();
            Component[0].CompName      = "Stop to a transferred position";
            Component[0].DataType      = IndComponentType.Other;
            Component[0].ShowInDynInfo = false;
            Component[0].FirstBar      = firstBar;
            Component[0].Value         = new double[Bars];

            return;
        }
Exemplo n.º 34
0
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Calculation
            double[] adOpenPrice = new double[Bars];

            for (int iBar = 1; iBar < Bars; iBar++)
                if (Time[iBar - 1].Day != Time[iBar].Day)
                    adOpenPrice[iBar] = Open[iBar];

            // Saving the components
            Component = new IndicatorComp[1];

            Component[0]           = new IndicatorComp();
            Component[0].CompName  = "Opening price of the day";
            Component[0].DataType  = IndComponentType.OpenPrice;
            Component[0].ChartType = IndChartType.NoChart;
            Component[0].FirstBar  = 2;
            Component[0].Value     = adOpenPrice;

            return;
        }
Exemplo n.º 35
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var maMethod = (MAMethod)IndParam.ListParam[1].Index;
            var price    = (BasePrice)IndParam.ListParam[2].Index;
            var period   = (int)IndParam.NumParam[0].Value;
            var shift    = (int)IndParam.NumParam[1].Value;
            int previous = IndParam.CheckParam[0].Checked ? 1 : 0;

            // TimeExecution
            if (period == 1 && shift == 0)
            {
                if (price == BasePrice.Open)
                {
                    IndParam.ExecutionTime = ExecutionTime.AtBarOpening;
                }
                else if (price == BasePrice.Close)
                {
                    IndParam.ExecutionTime = ExecutionTime.AtBarClosing;
                }
            }

            // Calculation
            double[] movingAverage = MovingAverage(period, shift, maMethod, Price(price));
            int      firstBar      = period + shift + 1 + previous;

            // Saving the components
            if (SlotType == SlotTypes.Open || SlotType == SlotTypes.Close)
            {
                Component = new IndicatorComp[2];

                Component[1] = new IndicatorComp {
                    Value = new double[Bars]
                };

                for (int bar = firstBar; bar < Bars; bar++)
                {
                    // Covers the cases when the price can pass through the MA without a signal
                    double value   = movingAverage[bar - previous];      // Current value
                    double value1  = movingAverage[bar - previous - 1];  // Previous value
                    double tempVal = value;
                    if ((value1 > High[bar - 1] && value < Open[bar]) || // The Open price jumps above the indicator
                        (value1 <Low[bar - 1] && value> Open[bar]) ||    // The Open price jumps below the indicator
                        (Close[bar - 1] < value && value < Open[bar]) || // The Open price is in a positive gap
                        (Close[bar - 1] > value && value > Open[bar]))   // The Open price is in a negative gap
                    {
                        tempVal = Open[bar];
                    }
                    Component[1].Value[bar] = tempVal; // Entry or exit value
                }
            }
            else
            {
                Component = new IndicatorComp[3];

                Component[1] = new IndicatorComp
                {
                    ChartType = IndChartType.NoChart, FirstBar = firstBar, Value = new double[Bars]
                };

                Component[2] = new IndicatorComp
                {
                    ChartType = IndChartType.NoChart, FirstBar = firstBar, Value = new double[Bars]
                };
            }

            Component[0] = new IndicatorComp
            {
                CompName   = "MA Value",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.Red,
                FirstBar   = firstBar,
                Value      = movingAverage
            };

            switch (SlotType)
            {
            case SlotTypes.Open:
                Component[1].CompName = "Position opening price";
                Component[1].DataType = IndComponentType.OpenPrice;
                break;

            case SlotTypes.OpenFilter:
                Component[1].DataType = IndComponentType.AllowOpenLong;
                Component[1].CompName = "Is long entry allowed";
                Component[2].DataType = IndComponentType.AllowOpenShort;
                Component[2].CompName = "Is short entry allowed";
                break;

            case SlotTypes.Close:
                Component[1].CompName = "Position closing price";
                Component[1].DataType = IndComponentType.ClosePrice;
                break;

            case SlotTypes.CloseFilter:
                Component[1].DataType = IndComponentType.ForceCloseLong;
                Component[1].CompName = "Close out long position";
                Component[2].DataType = IndComponentType.ForceCloseShort;
                Component[2].CompName = "Close out short position";
                break;
            }

            if (SlotType == SlotTypes.OpenFilter || SlotType == SlotTypes.CloseFilter)
            {
                switch (IndParam.ListParam[0].Text)
                {
                case "Moving Average rises":
                    IndicatorRisesLogic(firstBar, previous, movingAverage, ref Component[1], ref Component[2]);
                    break;

                case "Moving Average falls":
                    IndicatorFallsLogic(firstBar, previous, movingAverage, ref Component[1], ref Component[2]);
                    break;

                case "The bar opens above Moving Average":
                    BarOpensAboveIndicatorLogic(firstBar, previous, movingAverage, ref Component[1], ref Component[2]);
                    break;

                case "The bar opens below Moving Average":
                    BarOpensBelowIndicatorLogic(firstBar, previous, movingAverage, ref Component[1], ref Component[2]);
                    break;

                case "The bar opens above Moving Average after opening below it":
                    BarOpensAboveIndicatorAfterOpeningBelowLogic(firstBar, previous, movingAverage, ref Component[1],
                                                                 ref Component[2]);
                    break;

                case "The bar opens below Moving Average after opening above it":
                    BarOpensBelowIndicatorAfterOpeningAboveLogic(firstBar, previous, movingAverage, ref Component[1],
                                                                 ref Component[2]);
                    break;

                case "The position opens above Moving Average":
                    Component[0].PosPriceDependence = PositionPriceDependence.BuyHigherSellLower;
                    Component[0].UsePreviousBar     = previous;
                    Component[1].DataType           = IndComponentType.Other;
                    Component[1].ShowInDynInfo      = false;
                    Component[2].DataType           = IndComponentType.Other;
                    Component[2].ShowInDynInfo      = false;
                    break;

                case "The position opens below Moving Average":
                    Component[0].PosPriceDependence = PositionPriceDependence.BuyLowerSelHigher;
                    Component[0].UsePreviousBar     = previous;
                    Component[1].DataType           = IndComponentType.Other;
                    Component[1].ShowInDynInfo      = false;
                    Component[2].DataType           = IndComponentType.Other;
                    Component[2].ShowInDynInfo      = false;
                    break;

                case "The bar closes below Moving Average":
                    BarClosesBelowIndicatorLogic(firstBar, previous, movingAverage, ref Component[1], ref Component[2]);
                    break;

                case "The bar closes above Moving Average":
                    BarClosesAboveIndicatorLogic(firstBar, previous, movingAverage, ref Component[1], ref Component[2]);
                    break;
                }
            }
        }
Exemplo n.º 36
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var    basePrice = (BasePrice)IndParam.ListParam[1].Index;
            var    period    = (int)IndParam.NumParam[0].Value;
            double level     = IndParam.NumParam[1].Value;
            int    previous  = IndParam.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int firstBar = period + 2;

            double[] adBasePrice = Price(basePrice);
            var      aroonUp     = new double[Bars];
            var      aroonDown   = new double[Bars];
            var      aroon       = new double[Bars];

            for (int bar = period; bar < Bars; bar++)
            {
                double highestHigh = double.MinValue;
                double lowestLow   = double.MaxValue;
                for (int i = 0; i < period; i++)
                {
                    int baseBar = bar - period + 1 + i;
                    if (adBasePrice[baseBar] > highestHigh)
                    {
                        highestHigh  = adBasePrice[baseBar];
                        aroonUp[bar] = 100.0 * i / (period - 1);
                    }
                    if (adBasePrice[baseBar] < lowestLow)
                    {
                        lowestLow      = adBasePrice[baseBar];
                        aroonDown[bar] = 100.0 * i / (period - 1);
                    }
                }
            }

            for (int bar = firstBar; bar < Bars; bar++)
            {
                aroon[bar] = aroonUp[bar] - aroonDown[bar];
            }

            // Saving the components
            Component = new IndicatorComp[5];

            Component[0] = new IndicatorComp
            {
                CompName  = "Aroon Histogram",
                DataType  = IndComponentType.IndicatorValue,
                ChartType = IndChartType.Histogram,
                FirstBar  = firstBar,
                Value     = aroon
            };

            Component[1] = new IndicatorComp
            {
                CompName   = "Aroon Up",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.Green,
                FirstBar   = firstBar,
                Value      = aroonUp
            };

            Component[2] = new IndicatorComp
            {
                CompName   = "Aroon Down",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.Red,
                FirstBar   = firstBar,
                Value      = aroonDown
            };

            Component[3] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            Component[4] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            // Sets the Component's type
            if (SlotType == SlotTypes.OpenFilter)
            {
                Component[3].DataType = IndComponentType.AllowOpenLong;
                Component[3].CompName = "Is long entry allowed";
                Component[4].DataType = IndComponentType.AllowOpenShort;
                Component[4].CompName = "Is short entry allowed";
            }
            else if (SlotType == SlotTypes.CloseFilter)
            {
                Component[3].DataType = IndComponentType.ForceCloseLong;
                Component[3].CompName = "Close out long position";
                Component[4].DataType = IndComponentType.ForceCloseShort;
                Component[4].CompName = "Close out short position";
            }

            // Calculation of the logic
            var indLogic = IndicatorLogic.It_does_not_act_as_a_filter;

            switch (IndParam.ListParam[0].Text)
            {
            case "Aroon Histogram rises":
                indLogic      = IndicatorLogic.The_indicator_rises;
                SpecialValues = new double[] { 0 };
                break;

            case "Aroon Histogram falls":
                indLogic      = IndicatorLogic.The_indicator_falls;
                SpecialValues = new double[] { 0 };
                break;

            case "Aroon Histogram is higher than the Level line":
                indLogic      = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
                SpecialValues = new[] { level, -level };
                break;

            case "Aroon Histogram is lower than the Level line":
                indLogic      = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
                SpecialValues = new[] { level, -level };
                break;

            case "Aroon Histogram crosses the Level line upward":
                indLogic      = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
                SpecialValues = new[] { level, -level };
                break;

            case "Aroon Histogram crosses the Level line downward":
                indLogic      = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
                SpecialValues = new[] { level, -level };
                break;

            case "Aroon Histogram changes its direction upward":
                indLogic      = IndicatorLogic.The_indicator_changes_its_direction_upward;
                SpecialValues = new double[] { 0 };
                break;

            case "Aroon Histogram changes its direction downward":
                indLogic      = IndicatorLogic.The_indicator_changes_its_direction_downward;
                SpecialValues = new double[] { 0 };
                break;
            }

            OscillatorLogic(firstBar, previous, aroon, level, -level, ref Component[3], ref Component[4], indLogic);
        }
Exemplo n.º 37
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            double shift    = IndParam.NumParam[0].Value * Point;
            int    previous = IndParam.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int firstBar = previous + 2;
            var pp       = new double[Bars];
            var r1       = new double[Bars];
            var r2       = new double[Bars];
            var r3       = new double[Bars];
            var s1       = new double[Bars];
            var s2       = new double[Bars];
            var s3       = new double[Bars];

            var high  = new double[Bars];
            var low   = new double[Bars];
            var close = new double[Bars];

            if (IndParam.ListParam[1].Text == "One bar" ||
                Period == DataPeriod.D1 || Period == DataPeriod.W1)
            {
                high  = High;
                low   = Low;
                close = Close;
            }
            else
            {
                previous = 0;

                high[0]  = 0;
                low[0]   = 0;
                close[0] = 0;

                double top    = double.MinValue;
                double bottom = double.MaxValue;

                for (int bar = 1; bar < Bars; bar++)
                {
                    if (High[bar - 1] > top)
                    {
                        top = High[bar - 1];
                    }
                    if (Low[bar - 1] < bottom)
                    {
                        bottom = Low[bar - 1];
                    }

                    if (Time[bar].Day != Time[bar - 1].Day)
                    {
                        high[bar]  = top;
                        low[bar]   = bottom;
                        close[bar] = Close[bar - 1];
                        top        = double.MinValue;
                        bottom     = double.MaxValue;
                    }
                    else
                    {
                        high[bar]  = high[bar - 1];
                        low[bar]   = low[bar - 1];
                        close[bar] = close[bar - 1];
                    }
                }

                // first Bar
                for (int bar = 1; bar < Bars; bar++)
                {
                    if (Time[bar].Day != Time[bar - 1].Day)
                    {
                        firstBar = bar;
                        break;
                    }
                }
            }

            for (int bar = firstBar; bar < Bars; bar++)
            {
                pp[bar] = (high[bar] + low[bar] + close[bar]) / 3;
                r1[bar] = 2 * pp[bar] - low[bar];
                s1[bar] = 2 * pp[bar] - high[bar];
                r2[bar] = pp[bar] + high[bar] - low[bar];
                s2[bar] = pp[bar] - high[bar] + low[bar];
                r3[bar] = high[bar] + 2 * (pp[bar] - low[bar]);
                s3[bar] = low[bar] - 2 * (high[bar] - pp[bar]);
            }

            Component = new IndicatorComp[9];

            for (int iComp = 0; iComp < 7; iComp++)
            {
                Component[iComp] = new IndicatorComp
                {
                    ChartType  = IndChartType.Dot,
                    ChartColor = Color.Violet,
                    DataType   = IndComponentType.IndicatorValue,
                    FirstBar   = firstBar
                };
            }
            Component[3].ChartColor = Color.Blue;

            Component[0].Value = r3;
            Component[1].Value = r2;
            Component[2].Value = r1;
            Component[3].Value = pp;
            Component[4].Value = s1;
            Component[5].Value = s2;
            Component[6].Value = s3;

            Component[0].CompName = "Resistance 3";
            Component[1].CompName = "Resistance 2";
            Component[2].CompName = "Resistance 1";
            Component[3].CompName = "Pivot Point";
            Component[4].CompName = "Support 1";
            Component[5].CompName = "Support 2";
            Component[6].CompName = "Support 3";

            Component[7] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            Component[8] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            if (SlotType == SlotTypes.Open)
            {
                Component[7].CompName = "Long position entry price";
                Component[7].DataType = IndComponentType.OpenLongPrice;
                Component[8].CompName = "Short position entry price";
                Component[8].DataType = IndComponentType.OpenShortPrice;
            }
            else if (SlotType == SlotTypes.Close)
            {
                Component[7].CompName = "Long position closing price";
                Component[7].DataType = IndComponentType.CloseLongPrice;
                Component[8].CompName = "Short position closing price";
                Component[8].DataType = IndComponentType.CloseShortPrice;
            }

            switch (IndParam.ListParam[0].Text)
            {
            case "Enter long at R3 (short at S3)":
            case "Exit long at R3 (short at S3)":
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    Component[7].Value[bar] = r3[bar - previous] + shift;
                    Component[8].Value[bar] = s3[bar - previous] - shift;
                }
                break;

            case "Enter long at R2 (short at S2)":
            case "Exit long at R2 (short at S2)":
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    Component[7].Value[bar] = r2[bar - previous] + shift;
                    Component[8].Value[bar] = s2[bar - previous] - shift;
                }
                break;

            case "Enter long at R1 (short at S1)":
            case "Exit long at R1 (short at S1)":
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    Component[7].Value[bar] = r1[bar - previous] + shift;
                    Component[8].Value[bar] = s1[bar - previous] - shift;
                }
                break;

            //---------------------------------------------------------------------
            case "Enter the market at the Pivot Point":
            case "Exit the market at the Pivot Point":
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    Component[7].Value[bar] = pp[bar - previous] + shift;
                    Component[8].Value[bar] = pp[bar - previous] - shift;
                }
                break;

            //---------------------------------------------------------------------
            case "Enter long at S1 (short at R1)":
            case "Exit long at S1 (short at R1)":
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    Component[7].Value[bar] = s1[bar - previous] - shift;
                    Component[8].Value[bar] = r1[bar - previous] + shift;
                }
                break;

            case "Enter long at S2 (short at R2)":
            case "Exit long at S2 (short at R2)":
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    Component[7].Value[bar] = s2[bar - previous] - shift;
                    Component[8].Value[bar] = r2[bar - previous] + shift;
                }
                break;

            case "Enter long at S3 (short at R3)":
            case "Exit long at S3 (short at R3)":
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    Component[7].Value[bar] = s3[bar - previous] - shift;
                    Component[8].Value[bar] = r3[bar - previous] + shift;
                }
                break;
            }
        }
Exemplo n.º 38
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            const int firstBar = 3;

            var adAd = new double[Bars];

            adAd[0] = (Close[0] - Low[0]) - (High[0] - Close[0]);
            if ((High[0] - Low[0]) > 0)
            {
                adAd[0] = adAd[0] / (High[0] - Low[0]) * Volume[0];
            }
            else
            {
                adAd[0] = 0;
            }

            for (int iBar = 1; iBar < Bars; iBar++)
            {
                double dDelta = 0;
                double dRange = High[iBar] - Low[iBar];

                if (dRange > 0)
                {
                    dDelta = Volume[iBar] * (2 * Close[iBar] - High[iBar] - Low[iBar]) / dRange;
                }

                adAd[iBar] = adAd[iBar - 1] + dDelta;
            }

            // Saving the components
            Component = new IndicatorComp[3];

            Component[0] = new IndicatorComp
            {
                CompName   = "Accumulation Distribution",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.Blue,
                FirstBar   = firstBar,
                Value      = adAd
            };

            Component[1] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            Component[2] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            // Sets the Component's type
            if (SlotType == SlotTypes.OpenFilter)
            {
                Component[1].DataType = IndComponentType.AllowOpenLong;
                Component[1].CompName = "Is long entry allowed";
                Component[2].DataType = IndComponentType.AllowOpenShort;
                Component[2].CompName = "Is short entry allowed";
            }
            else if (SlotType == SlotTypes.CloseFilter)
            {
                Component[1].DataType = IndComponentType.ForceCloseLong;
                Component[1].CompName = "Close out long position";
                Component[2].DataType = IndComponentType.ForceCloseShort;
                Component[2].CompName = "Close out short position";
            }

            // Calculation of the logic
            var indLogic = IndicatorLogic.It_does_not_act_as_a_filter;

            switch (IndParam.ListParam[0].Text)
            {
            case "AD rises":
                indLogic = IndicatorLogic.The_indicator_rises;
                break;

            case "AD falls":
                indLogic = IndicatorLogic.The_indicator_falls;
                break;

            case "AD changes its direction upward":
                indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
                break;

            case "AD changes its direction downward":
                indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
                break;
            }

            OscillatorLogic(firstBar, iPrvs, adAd, 0, 0, ref Component[1], ref Component[2], indLogic);
        }
Exemplo n.º 39
0
        /// <summary>
        ///     Returns signals for the logic rule "The bar opens below the Indicator after opening above it"
        /// </summary>
        protected void BarOpensBelowIndicatorAfterOpeningAboveLogic(int firstBar, int prvs, double[] adIndValue,
                                                                    ref IndicatorComp indCompLong,
                                                                    ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int baseBar = bar - 1;
                while (Math.Abs(Open[baseBar] - adIndValue[baseBar - prvs]) < sigma && baseBar > firstBar)
                {
                    baseBar--;
                }

                indCompLong.Value[bar] = Open[bar] < adIndValue[bar - prvs] - sigma &&
                                         Open[baseBar] > adIndValue[baseBar - prvs] + sigma
                                             ? 1
                                             : 0;
                indCompShort.Value[bar] = Open[bar] > adIndValue[bar - prvs] + sigma &&
                                          Open[baseBar] < adIndValue[baseBar - prvs] - sigma
                                              ? 1
                                              : 0;
            }
        }
Exemplo n.º 40
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0;

            const int firstBar = 3;
            var       adMf     = new double[Bars];

            for (int iBar = 1; iBar < Bars; iBar++)
            {
                double dAvg  = (High[iBar] + Low[iBar] + Close[iBar]) / 3;
                double dAvg1 = (High[iBar - 1] + Low[iBar - 1] + Close[iBar - 1]) / 3;
                if (dAvg > dAvg1)
                {
                    adMf[iBar] = adMf[iBar - 1] + dAvg * Volume[iBar] / 1000;
                }
                else if (dAvg < dAvg1)
                {
                    adMf[iBar] = adMf[iBar - 1] - dAvg * Volume[iBar] / 1000;
                }
                else
                {
                    adMf[iBar] = adMf[iBar - 1];
                }
            }

            // Saving the components
            Component = new IndicatorComp[3];

            Component[0] = new IndicatorComp
            {
                CompName   = "Money Flow",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.Blue,
                FirstBar   = firstBar,
                Value      = adMf
            };

            Component[1] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            Component[2] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = new double[Bars]
            };

            // Sets the Component's type
            if (SlotType == SlotTypes.OpenFilter)
            {
                Component[1].DataType = IndComponentType.AllowOpenLong;
                Component[1].CompName = "Is long entry allowed";
                Component[2].DataType = IndComponentType.AllowOpenShort;
                Component[2].CompName = "Is short entry allowed";
            }
            else if (SlotType == SlotTypes.CloseFilter)
            {
                Component[1].DataType = IndComponentType.ForceCloseLong;
                Component[1].CompName = "Close out long position";
                Component[2].DataType = IndComponentType.ForceCloseShort;
                Component[2].CompName = "Close out short position";
            }

            // Calculation of the logic
            var indLogic = IndicatorLogic.It_does_not_act_as_a_filter;

            switch (IndParam.ListParam[0].Text)
            {
            case "Money Flow rises":
                indLogic = IndicatorLogic.The_indicator_rises;
                break;

            case "Money Flow falls":
                indLogic = IndicatorLogic.The_indicator_falls;
                break;

            case "Money Flow changes its direction upward":
                indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
                break;

            case "Money Flow changes its direction downward":
                indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
                break;
            }

            OscillatorLogic(firstBar, iPrvs, adMf, 0, 0, ref Component[1], ref Component[2], indLogic);
        }
Exemplo n.º 41
0
        protected void IndicatorChangesItsDirectionUpward(int firstBar, int prvs, double[] adIndValue, ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            for (int bar = firstBar; bar < Bars; bar++)
            {
                int bar0 = bar - prvs;
                int bar1 = bar0 - 1;
                while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                    bar1--;

                int bar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                while (Math.Abs(adIndValue[bar1] - adIndValue[bar2]) < sigma && bar2 > firstBar)
                    bar2--;

                indCompLong.Value[bar] = (adIndValue[bar2] > adIndValue[bar1] && adIndValue[bar1] < adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
                indCompShort.Value[bar] = (adIndValue[bar2] < adIndValue[bar1] && adIndValue[bar1] > adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
            }
        }
Exemplo n.º 42
0
        /// <summary>
        ///     Returns signals for the logic rule "The bar opens below the Indicator"
        /// </summary>
        protected void BarOpensBelowIndicatorLogic(int firstBar, int prvs, double[] adIndValue,
                                                   ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                indCompLong.Value[bar] = Open[bar] < adIndValue[bar - prvs] - sigma ? 1 : 0;
                indCompShort.Value[bar] = Open[bar] > adIndValue[bar - prvs] + sigma ? 1 : 0;
            }
        }
Exemplo n.º 43
0
        /// <summary>
        ///     Returns signals for the logic rule "The Indicator is lower than the AnotherIndicator"
        /// </summary>
        protected void IndicatorIsLowerThanAnotherIndicatorLogic(int firstBar, int prvs, double[] adIndValue,
                                                                 double[] adAnotherIndValue,
                                                                 ref IndicatorComp indCompLong,
                                                                 ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currentBar = bar - prvs;
                indCompLong.Value[bar] = adIndValue[currentBar] < adAnotherIndValue[currentBar] - sigma ? 1 : 0;
                indCompShort.Value[bar] = adIndValue[currentBar] > adAnotherIndValue[currentBar] + sigma ? 1 : 0;
            }
        }
Exemplo n.º 44
0
        /// <summary>
        ///     Returns signals for the logic rule "The Indicator crosses AnotherIndicator upward"
        /// </summary>
        protected void IndicatorCrossesAnotherIndicatorUpwardLogic(int firstBar, int prvs, double[] adIndValue,
                                                                   double[] adAnotherIndValue,
                                                                   ref IndicatorComp indCompLong,
                                                                   ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currentBar = bar - prvs;
                int baseBar = currentBar - 1;
                while (Math.Abs(adIndValue[baseBar] - adAnotherIndValue[baseBar]) < sigma && baseBar > firstBar)
                {
                    baseBar--;
                }

                indCompLong.Value[bar] = adIndValue[currentBar] > adAnotherIndValue[currentBar] + sigma &&
                                         adIndValue[baseBar] < adAnotherIndValue[baseBar] - sigma
                                             ? 1
                                             : 0;
                indCompShort.Value[bar] = adIndValue[currentBar] < adAnotherIndValue[currentBar] - sigma &&
                                          adIndValue[baseBar] > adAnotherIndValue[baseBar] + sigma
                                              ? 1
                                              : 0;
            }
        }
Exemplo n.º 45
0
        /// <summary>
        ///     Returns signals for the logic rule "Indicator rises".
        /// </summary>
        protected void IndicatorRisesLogic(int firstBar, int prvs, double[] adIndValue, ref IndicatorComp indCompLong,
                                           ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currentBar = bar - prvs;
                int baseBar = currentBar - 1;
                bool isNoChange = true;
                bool isHigher = adIndValue[currentBar] > adIndValue[baseBar];

                while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                       baseBar > firstBar)
                {
                    isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                    baseBar--;
                }

                indCompLong.Value[bar] = adIndValue[currentBar] > adIndValue[baseBar] + sigma ? 1 : 0;
                indCompShort.Value[bar] = adIndValue[currentBar] < adIndValue[baseBar] - sigma ? 1 : 0;
            }
        }
Exemplo n.º 46
0
        /// <summary>
        ///     Calculates the logic of a No Direction Oscillator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="prvs">To use the previous bar or not.</param>
        /// <param name="adIndValue">The indicator values.</param>
        /// <param name="dLevel">The Level value.</param>
        /// <param name="indComp">Indicator component where to save the results.</param>
        /// <param name="indLogic">The chosen logic.</param>
        /// <returns>True if everything is ok.</returns>
        protected void NoDirectionOscillatorLogic(int firstBar, int prvs, double[] adIndValue, double dLevel,
                                                  ref IndicatorComp indComp, IndicatorLogic indLogic)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            switch (indLogic)
            {
                case IndicatorLogic.The_indicator_rises:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int currentBar = bar - prvs;
                        int baseBar = currentBar - 1;
                        bool isHigher = adIndValue[currentBar] > adIndValue[baseBar];
                        bool isNoChange = true;

                        while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                               baseBar > firstBar)
                        {
                            isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                            baseBar--;
                        }

                        indComp.Value[bar] = adIndValue[baseBar] < adIndValue[currentBar] - sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_falls:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int currentBar = bar - prvs;
                        int baseBar = currentBar - 1;
                        bool isHigher = adIndValue[currentBar] > adIndValue[baseBar];
                        bool isNoChange = true;

                        while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                               baseBar > firstBar)
                        {
                            isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                            baseBar--;
                        }

                        indComp.Value[bar] = adIndValue[baseBar] > adIndValue[currentBar] + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_higher_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indComp.Value[bar] = adIndValue[bar - prvs] > dLevel + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_lower_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indComp.Value[bar] = adIndValue[bar - prvs] < dLevel - sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_upward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - prvs - 1;
                        while (Math.Abs(adIndValue[baseBar] - dLevel) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indComp.Value[bar] = (adIndValue[baseBar] < dLevel - sigma &&
                                              adIndValue[bar - prvs] > dLevel + sigma)
                                                 ? 1
                                                 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_downward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - prvs - 1;
                        while (Math.Abs(adIndValue[baseBar] - dLevel) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indComp.Value[bar] = (adIndValue[baseBar] > dLevel + sigma &&
                                              adIndValue[bar - prvs] < dLevel - sigma)
                                                 ? 1
                                                 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_upward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int bar0 = bar - prvs;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                        {
                            bar1--;
                        }

                        int bar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                        while (Math.Abs(adIndValue[bar1] - adIndValue[bar2]) < sigma && bar2 > firstBar)
                        {
                            bar2--;
                        }

                        indComp.Value[bar] = (adIndValue[bar2] > adIndValue[bar1] && adIndValue[bar1] < adIndValue[bar0] &&
                                              bar1 == bar0 - 1)
                                                 ? 1
                                                 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_downward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int bar0 = bar - prvs;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                        {
                            bar1--;
                        }

                        int bar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                        while (Math.Abs(adIndValue[bar1] - adIndValue[bar2]) < sigma && bar2 > firstBar)
                        {
                            bar2--;
                        }

                        indComp.Value[bar] = (adIndValue[bar2] < adIndValue[bar1] && adIndValue[bar1] > adIndValue[bar0] &&
                                              bar1 == bar0 - 1)
                                                 ? 1
                                                 : 0;
                    }
                    break;

                default:
                    return;
            }
        }
Exemplo n.º 47
0
        /// <summary>
        ///     Calculates the logic of an Oscillator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="prvs">To use the previous bar or not.</param>
        /// <param name="adIndValue">The indicator values.</param>
        /// <param name="levelLong">The Level value for a Long position.</param>
        /// <param name="levelShort">The Level value for a Short position.</param>
        /// <param name="indCompLong">Indicator component for Long position.</param>
        /// <param name="indCompShort">Indicator component for Short position.</param>
        /// <param name="indLogic">The chosen logic.</param>
        /// <returns>True if everything is ok.</returns>
        protected void OscillatorLogic(int firstBar, int prvs, double[] adIndValue, double levelLong, double levelShort,
                                       ref IndicatorComp indCompLong, ref IndicatorComp indCompShort,
                                       IndicatorLogic indLogic)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            switch (indLogic)
            {
                case IndicatorLogic.The_indicator_rises:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int currentBar = bar - prvs;
                        int baseBar = currentBar - 1;
                        bool isHigher = adIndValue[currentBar] > adIndValue[baseBar];

                        if (!IsDiscreteValues) // Aroon oscillator uses IsDiscreteValues = true
                        {
                            bool isNoChange = true;
                            while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                                   baseBar > firstBar)
                            {
                                isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                                baseBar--;
                            }
                        }

                        indCompLong.Value[bar] = adIndValue[baseBar] < adIndValue[currentBar] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[baseBar] > adIndValue[currentBar] + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_falls:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int currentBar = bar - prvs;
                        int baseBar = currentBar - 1;
                        bool isHigher = adIndValue[currentBar] > adIndValue[baseBar];

                        if (!IsDiscreteValues) // Aroon oscillator uses IsDiscreteValues = true
                        {
                            bool isNoChange = true;
                            while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                                   baseBar > firstBar)
                            {
                                isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                                baseBar--;
                            }
                        }

                        indCompLong.Value[bar] = adIndValue[baseBar] > adIndValue[currentBar] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[baseBar] < adIndValue[currentBar] - sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_higher_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = adIndValue[bar - prvs] > levelLong + sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[bar - prvs] < levelShort - sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_lower_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = adIndValue[bar - prvs] < levelLong - sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[bar - prvs] > levelShort + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_upward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - prvs - 1;
                        while (Math.Abs(adIndValue[baseBar] - levelLong) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompLong.Value[bar] = (adIndValue[baseBar] < levelLong - sigma &&
                                                  adIndValue[bar - prvs] > levelLong + sigma)
                                                     ? 1
                                                     : 0;
                        indCompShort.Value[bar] = (adIndValue[baseBar] > levelShort + sigma &&
                                                   adIndValue[bar - prvs] < levelShort - sigma)
                                                      ? 1
                                                      : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_downward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - prvs - 1;
                        while (Math.Abs(adIndValue[baseBar] - levelLong) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompLong.Value[bar] = (adIndValue[baseBar] > levelLong + sigma &&
                                                  adIndValue[bar - prvs] < levelLong - sigma)
                                                     ? 1
                                                     : 0;
                        indCompShort.Value[bar] = (adIndValue[baseBar] < levelShort - sigma &&
                                                   adIndValue[bar - prvs] > levelShort + sigma)
                                                      ? 1
                                                      : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_upward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int bar0 = bar - prvs;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                        {
                            bar1--;
                        }

                        int iBar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                        while (Math.Abs(adIndValue[bar1] - adIndValue[iBar2]) < sigma && iBar2 > firstBar)
                        {
                            iBar2--;
                        }

                        indCompLong.Value[bar] = (adIndValue[iBar2] > adIndValue[bar1] &&
                                                  adIndValue[bar1] < adIndValue[bar0] && bar1 == bar0 - 1)
                                                     ? 1
                                                     : 0;
                        indCompShort.Value[bar] = (adIndValue[iBar2] < adIndValue[bar1] &&
                                                   adIndValue[bar1] > adIndValue[bar0] && bar1 == bar0 - 1)
                                                      ? 1
                                                      : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_downward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int bar0 = bar - prvs;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                        {
                            bar1--;
                        }

                        int iBar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                        while (Math.Abs(adIndValue[bar1] - adIndValue[iBar2]) < sigma && iBar2 > firstBar)
                        {
                            iBar2--;
                        }

                        indCompLong.Value[bar] = (adIndValue[iBar2] < adIndValue[bar1] &&
                                                  adIndValue[bar1] > adIndValue[bar0] && bar1 == bar0 - 1)
                                                     ? 1
                                                     : 0;
                        indCompShort.Value[bar] = (adIndValue[iBar2] > adIndValue[bar1] &&
                                                   adIndValue[bar1] < adIndValue[bar0] && bar1 == bar0 - 1)
                                                      ? 1
                                                      : 0;
                    }
                    break;

                default:
                    return;
            }
        }
Exemplo n.º 48
0
        /// <summary>
        ///     Calculates the logic of a band indicator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="prvs">To use the previous bar or not.</param>
        /// <param name="adUpperBand">The Upper band values.</param>
        /// <param name="adLowerBand">The Lower band values.</param>
        /// <param name="indCompLong">Indicator component for Long position.</param>
        /// <param name="indCompShort">Indicator component for Short position.</param>
        /// <param name="indLogic">The chosen logic.</param>
        protected void BandIndicatorLogic(int firstBar, int prvs, double[] adUpperBand, double[] adLowerBand,
                                          ref IndicatorComp indCompLong, ref IndicatorComp indCompShort,
                                          BandIndLogic indLogic)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            switch (indLogic)
            {
                case BandIndLogic.The_bar_opens_below_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Open[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Open[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_below_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Open[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Open[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_below_the_Upper_Band_after_opening_above_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompLong.Value[bar] = Open[bar] < adUpperBand[bar - prvs] - sigma &&
                                                 Open[baseBar] > adUpperBand[baseBar - prvs] + sigma
                                                     ? 1
                                                     : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompShort.Value[bar] = Open[bar] > adLowerBand[bar - prvs] + sigma &&
                                                  Open[baseBar] < adLowerBand[baseBar - prvs] - sigma
                                                      ? 1
                                                      : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Upper_Band_after_opening_below_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompLong.Value[bar] = Open[bar] > adUpperBand[bar - prvs] + sigma &&
                                                 Open[baseBar] < adUpperBand[baseBar - prvs] - sigma
                                                     ? 1
                                                     : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompShort.Value[bar] = Open[bar] < adLowerBand[bar - prvs] - sigma &&
                                                  Open[baseBar] > adLowerBand[baseBar - prvs] + sigma
                                                      ? 1
                                                      : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_below_the_Lower_Band_after_opening_above_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompLong.Value[bar] = Open[bar] < adLowerBand[bar - prvs] - sigma &&
                                                 Open[baseBar] > adLowerBand[baseBar - prvs] + sigma
                                                     ? 1
                                                     : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompShort.Value[bar] = Open[bar] > adUpperBand[bar - prvs] + sigma &&
                                                  Open[baseBar] < adUpperBand[baseBar - prvs] - sigma
                                                      ? 1
                                                      : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Lower_Band_after_opening_below_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompLong.Value[bar] = Open[bar] > adLowerBand[bar - prvs] + sigma &&
                                                 Open[baseBar] < adLowerBand[baseBar - prvs] - sigma
                                                     ? 1
                                                     : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                        {
                            baseBar--;
                        }

                        indCompShort.Value[bar] = Open[bar] < adUpperBand[bar - prvs] - sigma &&
                                                  Open[baseBar] > adUpperBand[baseBar - prvs] + sigma
                                                      ? 1
                                                      : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_below_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Close[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_above_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Close[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_below_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Close[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_above_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar] = Close[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                    }
                    break;

                default:
                    return;
            }
        }
        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        public override void Calculate(SlotTypes slotType)
        {
            // Reading the parameters
            int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            double[] adMFI  = new double[Bars];

            int iFirstBar = 5;

            for (int iBar = 0; iBar < Bars; iBar++)
            {
                if (Volume[iBar] > 0)
                    adMFI[iBar] = 10000 * (High[iBar] - Low[iBar]) / Volume[iBar];
                else
                    adMFI[iBar] = 10000 * (High[iBar] - Low[iBar]);
            }

            // Saving the components
            Component = new IndicatorComp[3];

            Component[0] = new IndicatorComp();
            Component[0].CompName  = "Market Facilitation Index";
            Component[0].DataType  = IndComponentType.IndicatorValue;
            Component[0].ChartType = IndChartType.Histogram;
            Component[0].FirstBar  = iFirstBar;
            Component[0].Value     = adMFI;

            Component[1] = new IndicatorComp();
            Component[1].ChartType = IndChartType.NoChart;
            Component[1].FirstBar  = iFirstBar;
            Component[1].Value     = new double[Bars];

            Component[2] = new IndicatorComp();
            Component[2].ChartType = IndChartType.NoChart;
            Component[2].FirstBar  = iFirstBar;
            Component[2].Value     = new double[Bars];

            // Sets the Component's type
            if (slotType == SlotTypes.OpenFilter)
            {
                Component[1].DataType = IndComponentType.AllowOpenLong;
                Component[1].CompName = "Is long entry allowed";
                Component[2].DataType = IndComponentType.AllowOpenShort;
                Component[2].CompName = "Is short entry allowed";
            }
            else if (slotType == SlotTypes.CloseFilter)
            {
                Component[1].DataType = IndComponentType.ForceCloseLong;
                Component[1].CompName = "Close out long position";
                Component[2].DataType = IndComponentType.ForceCloseShort;
                Component[2].CompName = "Close out short position";
            }

            // Calculation of the logic
            IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter;

            switch (IndParam.ListParam[0].Text)
            {
                case "The Market Facilitation Index rises":
                    indLogic = IndicatorLogic.The_indicator_rises;
                    break;

                case "The Market Facilitation Index falls":
                    indLogic = IndicatorLogic.The_indicator_falls;
                    break;

                case "The Market Facilitation Index changes its direction upward":
                    indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
                    break;

                case "The Market Facilitation Index changes its direction downward":
                    indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
                    break;

                default:
                    break;
            }

            NoDirectionOscillatorLogic(iFirstBar, iPrvs, adMFI, 0, ref Component[1], indLogic);
            Component[2].Value = Component[1].Value;

            return;
        }