//============================================================== // 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 WaddahAttarExplosion : Indicator { public WaddahAttarExplosion() { IndicatorName = "Waddah Attar Explosion"; PossibleSlots = SlotTypes.OpenFilter; SeparatedChart = true; IndicatorAuthor = "Footon"; IndicatorVersion = "2.0"; IndicatorDescription = "Custom"; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; if (SlotType == SlotTypes.OpenFilter) IndParam.ListParam[0].ItemList = new[] { "Long if buy signal", "Long if sell signal" }; else IndParam.ListParam[0].ItemList = new[] { "Not Defined" }; 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 = "Smoothing method"; IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod)); IndParam.ListParam[1].Index = (int)MAMethod.Exponential; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The smoothing method of Moving Averages."; 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 the Moving Averages and BB are based on"; IndParam.ListParam[3].Caption = "BB method"; IndParam.ListParam[3].ItemList = Enum.GetNames(typeof(MAMethod)); IndParam.ListParam[3].Index = (int)MAMethod.Simple; IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index]; IndParam.ListParam[3].Enabled = true; IndParam.ListParam[3].ToolTip = "BB method"; // The NumericUpDown parameters IndParam.NumParam[0].Caption = "Sensitivity"; IndParam.NumParam[0].Value = 150; IndParam.NumParam[0].Min = 1; IndParam.NumParam[0].Max = 1000; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "Sensitivity"; IndParam.NumParam[1].Caption = "DeadZonePip"; IndParam.NumParam[1].Value = 30; IndParam.NumParam[1].Min = 1; IndParam.NumParam[1].Max = 200; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "DeadZonePip"; IndParam.NumParam[2].Caption = "ExplosionPower"; IndParam.NumParam[2].Value = 15; IndParam.NumParam[2].Min = 1; IndParam.NumParam[2].Max = 200; IndParam.NumParam[2].Enabled = true; IndParam.NumParam[2].ToolTip = "ExplosionPower"; IndParam.NumParam[3].Caption = "TrendPower"; IndParam.NumParam[3].Value = 15; IndParam.NumParam[3].Min = 1; IndParam.NumParam[3].Max = 200; IndParam.NumParam[3].Enabled = true; IndParam.NumParam[3].ToolTip = "TrendPower"; // 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; // Reading the parameters MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; MAMethod slMethod = (MAMethod )IndParam.ListParam[3].Index; BasePrice basePrice = (BasePrice)IndParam.ListParam[2].Index; int nSlow = 40; int Sensetive = (int)IndParam.NumParam[0].Value; int nFast = 20; int DeadZonePip = (int)IndParam.NumParam[1].Value; int ExplosionPower = (int)IndParam.NumParam[2].Value; int TrendPower = (int)IndParam.NumParam[3].Value; int previous = IndParam.CheckParam[0].Checked ? 1 : 0; // Calculation int firstBar = Math.Max(nSlow, Math.Max(nFast, 20)) + 3; double point = (Digits == 5 || Digits == 3) ? 10 * Point : Point; double[] adMACD = new double[Bars]; double[] buy = new double[Bars]; double[] sell = new double[Bars]; double[] ind_buffer1 = new double[Bars]; double[] ind_buffer2 = new double[Bars]; double[] ind_buffer3 = new double[Bars]; double[] ind_buffer4 = new double[Bars]; double Trend1 = 0; double Trend2 = 0; double Explo1 = 0; double Explo2 = 0; double Dead = point * DeadZonePip; double pwrt = 0; double pwre = 0; double[] adMASlow = MovingAverage(nSlow, 0, maMethod, Price(basePrice)); double[] adMAFast = MovingAverage(nFast, 0, maMethod, Price(basePrice)); double[] price = Price(basePrice); double[] ma = MovingAverage(20, 0, slMethod, price); double[] upperBand = new double[Bars]; double[] lowerBand = new double[Bars]; for (int bar = firstBar; bar < Bars; bar++) { double sum = 0; for (int i = 0; i < 20; i++) { double delta = (price[bar - i] - ma[bar]); sum += delta * delta; } double stdDev = Math.Sqrt(sum / 20); upperBand[bar] = ma[bar] + 2.0 * stdDev; lowerBand[bar] = ma[bar] - 2.0 * stdDev; adMACD[bar] = adMAFast[bar] - adMASlow[bar]; Trend1 = (adMACD[bar-1] - adMACD[bar-2]) * Sensetive; Trend2 = (adMACD[bar-3] - adMACD[bar-4]) * Sensetive; Explo1 = upperBand[bar-1] - lowerBand[bar-1]; Explo2 = upperBand[bar-2] - lowerBand[bar-2]; if(Trend1 >= 0) ind_buffer1[bar] = Trend1; if(Trend1 < 0) ind_buffer2[bar] = (-1*Trend1); ind_buffer3[bar] = Explo1; ind_buffer4[bar] = Dead; if(Trend1 > 0 && Trend1 > Explo1 && Trend1 > Dead && Explo1 > Dead && Explo1 > Explo2 && Trend1 > Trend2) { pwrt = 100*(Trend1 - Trend2) / Trend1; pwre = 100*(Explo1 - Explo2) / Explo1; if(pwre >= ExplosionPower && pwrt >= TrendPower) { if (IndParam.ListParam[0].Index == 1) sell[bar] = 1; else buy[bar] = 1; } } if(Trend1 < 0 && Math.Abs(Trend1) > Explo1 && Math.Abs(Trend1) > Dead && Explo1 > Dead && Explo1 > Explo2 && Math.Abs(Trend1) > Math.Abs(Trend2)) { pwrt = 100*(Math.Abs(Trend1) - Math.Abs(Trend2)) / Math.Abs(Trend1); pwre = 100*(Explo1 - Explo2) / Explo1; if(pwre >= ExplosionPower && pwrt >= TrendPower) { if (IndParam.ListParam[0].Index == 1) buy[bar] = 1; else sell[bar] = 1; } } } // Saving the components Component = new IndicatorComp[6]; Component[0] = new IndicatorComp { CompName = "Histogram", DataType = IndComponentType.IndicatorValue, ChartType = IndChartType.Histogram, ChartColor = Color.Blue, FirstBar = firstBar, Value = ind_buffer1 }; Component[1] = new IndicatorComp { CompName = "Line", DataType = IndComponentType.IndicatorValue, ChartType = IndChartType.Line, ChartColor = Color.Gold, FirstBar = firstBar, Value = ind_buffer3 }; Component[2] = new IndicatorComp { CompName = "Line", DataType = IndComponentType.IndicatorValue, ChartType = IndChartType.Line, ChartColor = Color.Blue, FirstBar = firstBar, Value = ind_buffer4 }; Component[3] = new IndicatorComp { CompName = "Allow entry", DataType = IndComponentType.AllowOpenLong, ChartType = IndChartType.NoChart, FirstBar = firstBar, Value = buy }; Component[4] = new IndicatorComp { CompName = "Allow entry", DataType = IndComponentType.AllowOpenShort, ChartType = IndChartType.NoChart, FirstBar = firstBar, Value = sell }; Component[5] = new IndicatorComp { CompName = "Histogram", DataType = IndComponentType.IndicatorValue, ChartType = IndChartType.Histogram, ChartColor = Color.Blue, FirstBar = firstBar, Value = ind_buffer2 }; } public override void SetDescription() { EntryFilterLongDescription = "Long if buy signal"; EntryFilterShortDescription = "Short if sell signal"; ExitFilterLongDescription = "Close long if exit long signal"; ExitFilterShortDescription = "Close short if exit sell signal"; } public override string ToString() { string sString = IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (") + IndParam.ListParam[1].Text + ", " + // Method IndParam.ListParam[2].Text + ", " + // Price IndParam.ListParam[3].Text + ", " + // Signal MA Method IndParam.NumParam[0].ValueToString + ", " + // Slow MA period IndParam.NumParam[1].ValueToString + ", " + // Fast MA period IndParam.NumParam[2].ValueToString + ")"; // Signal MA period return sString; } } }