//============================================================== // Forex Strategy Builder // Copyright © Miroslav Popov. All rights reserved. //============================================================== // THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE. //============================================================== using System; using System.Drawing; using ForexStrategyBuilder.Infrastructure.Entities; using ForexStrategyBuilder.Infrastructure.Enums; using ForexStrategyBuilder.Infrastructure.Interfaces; namespace ForexStrategyBuilder.Indicators.Store { public class AOA : Indicator { public AOA() { IndicatorName = "Angle Of Averages AOA"; PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter; SeparatedChart = true; SeparatedChartMinValue = -90; SeparatedChartMaxValue = 90; IndicatorAuthor = "Jim Totaro, original by mladen"; IndicatorVersion = "1.01"; IndicatorDescription = "Angle of Averages."; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; IndParam.ListParam[0].ItemList = new[] { "AOA line rises", "AOA line falls", "AOA line is higher than the Level line", "AOA line is lower than the Level line", "AOA line crosses the Level line upward", "AOA line crosses the Level line downward", "AOA line changes its direction upward", "AOA line changes its direction downward" }; IndParam.ListParam[0].Index = 0; IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[IndParam.ListParam[0].Index]; IndParam.ListParam[0].Enabled = true; IndParam.ListParam[0].ToolTip = "Logic of application of the indicator."; IndParam.ListParam[1].Caption = "MA Smoothing method"; IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod)); IndParam.ListParam[1].Index = (int)MAMethod.Smoothed; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The Moving Average method used for smoothing AOA value."; IndParam.ListParam[2].Caption = "Base price"; IndParam.ListParam[2].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[2].Index = (int)BasePrice.Close; IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index]; IndParam.ListParam[2].Enabled = true; IndParam.ListParam[2].ToolTip = "The price AOA is based on."; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "MA Smoothing period"; IndParam.NumParam[0].Value = 34; IndParam.NumParam[0].Min = 5; IndParam.NumParam[0].Max = 200; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "The period for smoothing of the moving average."; IndParam.NumParam[1].Caption = "Bars for Angle Calculation"; IndParam.NumParam[1].Value = 6; IndParam.NumParam[1].Min = 5; IndParam.NumParam[1].Max = 200; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "The number of bars to calculate AOA value."; IndParam.NumParam[2].Caption = "Level"; IndParam.NumParam[2].Value = 8; IndParam.NumParam[2].Min = 0; IndParam.NumParam[2].Max = 90; IndParam.NumParam[2].Enabled = true; IndParam.NumParam[2].ToolTip = "Angle signal level in degrees."; // The CheckBox parameters IndParam.CheckParam[0].Caption = "Use previous bar value"; IndParam.CheckParam[0].Enabled = true; IndParam.CheckParam[0].ToolTip = "Use the indicator value from the previous bar."; } public override void Calculate(IDataSet dataSet) { DataSet = dataSet; var Pi = 3.14159265358979323846264338327950288; // Reading the parameters var maMethod = (MAMethod)IndParam.ListParam[1].Index; var basePrice = (BasePrice)IndParam.ListParam[2].Index; var period = (int)IndParam.NumParam[0].Value; var AngleBars = (int)IndParam.NumParam[1].Value; double AngleLevel = IndParam.NumParam[2].Value; int previous = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = period + previous; double[] price = Price(basePrice); var pos = new double[Bars]; var neg = new double[Bars]; var AOA = new double[Bars]; var angleh = new double[Bars]; var anglec = new double[Bars]; var angle = new double[Bars]; double[] ma = MovingAverage(period, 0, maMethod, price); for (int i = firstBar; i < Bars; i++) { if (i <= AngleBars) continue; double change = ma[i] - ma[i - AngleBars]; double range = 0; for (int k = 0; k < 20 * AngleBars && (i - k - 1) >= 0; k++) { range += Math.Max(High[i - k], Close[i - k - 1]) - Math.Min(Low[i - k], Close[i - k - 1]); } range /= (double)AngleBars * 20.0; if (range != 0) angle[i] = Math.Atan(change / (range * AngleBars)) * 180.0 / Pi; else angle[i] = 0; angleh[i] = angle[i]; } // Saving the components Component = new IndicatorComp[3]; Component[0] = new IndicatorComp(); Component[0].CompName = "Angle of Average"; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Histogram; Component[0].FirstBar = firstBar; Component[0].Value = angleh; Component[1] = new IndicatorComp(); Component[1].ChartType = IndChartType.NoChart; Component[1].FirstBar = firstBar; Component[1].Value = new double[Bars]; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = firstBar; 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 var logicRule = IndicatorLogic.It_does_not_act_as_a_filter; switch (IndParam.ListParam[0].Text) { case "AOA line rises": logicRule = IndicatorLogic.The_indicator_rises; SpecialValues = new double[] { 0 }; break; case "AOA line falls": logicRule = IndicatorLogic.The_indicator_falls; SpecialValues = new double[] { 0 }; break; case "AOA line is higher than the Level line": logicRule = IndicatorLogic.The_indicator_is_higher_than_the_level_line; SpecialValues = new[] { AngleLevel, - AngleLevel }; break; case "AOA line is lower than the Level line": logicRule = IndicatorLogic.The_indicator_is_lower_than_the_level_line; SpecialValues = new[] { AngleLevel, - AngleLevel }; break; case "AOA line crosses the Level line upward": logicRule = IndicatorLogic.The_indicator_crosses_the_level_line_upward; SpecialValues = new[] { AngleLevel, - AngleLevel }; break; case "AOA line crosses the Level line downward": logicRule = IndicatorLogic.The_indicator_crosses_the_level_line_downward; SpecialValues = new[] { AngleLevel, - AngleLevel }; break; case "AOA line changes its direction upward": logicRule = IndicatorLogic.The_indicator_changes_its_direction_upward; SpecialValues = new double[] { 0 }; break; case "AOA line changes its direction downward": logicRule = IndicatorLogic.The_indicator_changes_its_direction_downward; SpecialValues = new double[] { 0 }; break; } OscillatorLogic(firstBar, previous, angleh, AngleLevel, - AngleLevel, ref Component[1], ref Component[2], logicRule); } public override void SetDescription() { string longLevel = IndParam.NumParam[1].ValueToString; string shortLevel = IndParam.NumParam[1].AnotherValueToString(100 - IndParam.NumParam[1].Value); EntryFilterLongDescription = ToString() + " "; EntryFilterShortDescription = ToString() + " "; ExitFilterLongDescription = ToString() + " "; ExitFilterShortDescription = ToString() + " "; switch (IndParam.ListParam[0].Text) { case "AOA rises": EntryFilterLongDescription += "rises"; EntryFilterShortDescription += "falls"; ExitFilterLongDescription += "rises"; ExitFilterShortDescription += "falls"; break; case "AOA falls": EntryFilterLongDescription += "falls"; EntryFilterShortDescription += "rises"; ExitFilterLongDescription += "falls"; ExitFilterShortDescription += "rises"; break; case "AOA is higher than the Level line": EntryFilterLongDescription += "is higher than the Level " + longLevel; EntryFilterShortDescription += "is lower than the Level " + shortLevel; ExitFilterLongDescription += "is higher than the Level " + longLevel; ExitFilterShortDescription += "is lower than the Level " + shortLevel; break; case "AOA is lower than the Level line": EntryFilterLongDescription += "is lower than the Level " + longLevel; EntryFilterShortDescription += "is higher than the Level " + shortLevel; ExitFilterLongDescription += "is lower than the Level " + longLevel; ExitFilterShortDescription += "is higher than the Level " + shortLevel; break; case "AOA crosses the Level line upward": EntryFilterLongDescription += "crosses the Level " + longLevel + " upward"; EntryFilterShortDescription += "crosses the Level " + shortLevel + " downward"; ExitFilterLongDescription += "crosses the Level " + longLevel + " upward"; ExitFilterShortDescription += "crosses the Level " + shortLevel + " downward"; break; case "AOA crosses the Level line downward": EntryFilterLongDescription += "crosses the Level " + longLevel + " downward"; EntryFilterShortDescription += "crosses the Level " + shortLevel + " upward"; ExitFilterLongDescription += "crosses the Level " + longLevel + " downward"; ExitFilterShortDescription += "crosses the Level " + shortLevel + " upward"; break; case "AOA changes its direction upward": EntryFilterLongDescription += "changes its direction upward"; EntryFilterShortDescription += "changes its direction downward"; ExitFilterLongDescription += "changes its direction upward"; ExitFilterShortDescription += "changes its direction downward"; break; case "AOA changes its direction downward": EntryFilterLongDescription += "changes its direction downward"; EntryFilterShortDescription += "changes its direction upward"; ExitFilterLongDescription += "changes its direction downward"; ExitFilterShortDescription += "changes its direction upward"; break; } } public override string ToString() { return IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (") + IndParam.ListParam[1].Text + ", " + // Smoothing method IndParam.ListParam[2].Text + ", " + // Base price IndParam.NumParam[0].ValueToString + ")"; // Smoothing period } } }