//============================================================== // Forex Strategy Builder // Copyright (c) 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 EmaPredictiveCrossover : Indicator { public EmaPredictiveCrossover() { IndicatorName = "EmaPredictive Moving Averages Crossover"; PossibleSlots = SlotTypes.OpenFilter | SlotTypes.CloseFilter; IndicatorAuthor = "Footon"; IndicatorVersion = "2.0"; IndicatorDescription = "Footon's indi corner: custom indicators for FSB and FST."; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; IndParam.ListParam[0].ItemList = new string[] { "The Fast MA crosses the Slow MA upward", "The Fast MA crosses the Slow MA downward", "The Fast MA is higher than the Slow MA", "The Fast MA is lower than the Slow MA", }; 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 = "Slow Base price"; IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[1].Index = (int)BasePrice.Close; IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index]; IndParam.ListParam[1].Enabled = true; IndParam.ListParam[1].ToolTip = "The price Moving Average is based on."; IndParam.ListParam[3].Caption = "Fast Base price"; IndParam.ListParam[3].ItemList = Enum.GetNames(typeof(BasePrice)); IndParam.ListParam[3].Index = (int)BasePrice.Close; IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[IndParam.ListParam[3].Index]; IndParam.ListParam[3].Enabled = true; IndParam.ListParam[3].ToolTip = "The price Moving Average is based on.";*/ // The NumericUpDown parameters IndParam.NumParam[0].Caption = "Fast LongPeriod"; IndParam.NumParam[0].Value = 25.0; IndParam.NumParam[0].Min = 0.1; IndParam.NumParam[0].Max = 200; IndParam.NumParam[0].Point = 1; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "The Moving Average period."; IndParam.NumParam[1].Caption = "Fast ShortPeriod"; IndParam.NumParam[1].Value = 8.0; IndParam.NumParam[1].Min = 0.1; IndParam.NumParam[1].Max = 200; IndParam.NumParam[1].Point = 1; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "ShortPeriod"; IndParam.NumParam[2].Caption = "Fast ExtraTimeForward"; IndParam.NumParam[2].Value = 1.0; IndParam.NumParam[2].Min = 0.1; IndParam.NumParam[2].Max = 200; IndParam.NumParam[2].Point = 1; IndParam.NumParam[2].Enabled = true; IndParam.NumParam[2].ToolTip = "ExtraTimeForward"; IndParam.NumParam[3].Caption = "Slow LongPeriod"; IndParam.NumParam[3].Value = 50.0; IndParam.NumParam[3].Min = 0; IndParam.NumParam[3].Max = 200; IndParam.NumParam[3].Point = 1; IndParam.NumParam[3].Enabled = true; IndParam.NumParam[3].ToolTip = "The Moving Average period."; IndParam.NumParam[4].Caption = "Slow ShortPeriod"; IndParam.NumParam[4].Value = 16.0; IndParam.NumParam[4].Min = 0.1; IndParam.NumParam[4].Max = 200; IndParam.NumParam[4].Point = 1; IndParam.NumParam[4].Enabled = true; IndParam.NumParam[4].ToolTip = "ShortPeriod"; IndParam.NumParam[5].Caption = "Slow ExtraTimeForward"; IndParam.NumParam[5].Value = 1.0; IndParam.NumParam[5].Min = 0; IndParam.NumParam[5].Max = 200; IndParam.NumParam[5].Point = 1; IndParam.NumParam[5].Enabled = true; IndParam.NumParam[5].ToolTip = "ExtraTimeForward"; // 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."; return; } public override void Calculate(IDataSet dataSet) { DataSet = dataSet; // Reading the parameters /*MAMethod maMethod = (MAMethod )IndParam.ListParam[1].Index; BasePrice price = (BasePrice)IndParam.ListParam[2].Index;*/ double LongPeriod = IndParam.NumParam[0].Value; double ShortPeriod = IndParam.NumParam[1].Value; double ExtraTimeForward = IndParam.NumParam[2].Value; double slowLongPeriod = IndParam.NumParam[3].Value; double slowShortPeriod = IndParam.NumParam[4].Value; double slowExtraTimeForward = IndParam.NumParam[2].Value; //int iDeviation = (int)IndParam.NumParam[3].Value; int iPrvs = IndParam.CheckParam[0].Checked ? 1 : 0; //int firstbar = Convert.ToInt32(LongPeriod + ShortPeriod + ExtraTimeForward); int iFirstBar = 3;//firstbar; //LongPeriod + ShortPeriod + ExtraTimeForward + 3; double[] ExtBuffer = new double[Bars]; double[] ExtBuffer1 = new double[Bars]; double[] adMAOscillator = new double[Bars]; double ma1 = 0; double ma3 = 0; double p1 = 0; double p3 = 0; double t1 = 0; double t3 = 0; double t = 0; double ma11 = 0; double ma31 = 0; double p11 = 0; double p31 = 0; double t11 = 0; double t31 = 0; double t2 = 0; p1=2.0/(LongPeriod+1.0); p3=2.0/(ShortPeriod+1.0); p11=2.0/(slowLongPeriod+1.0); p31=2.0/(slowShortPeriod+1.0); //---- t1=(LongPeriod-1.0)/2.0; t3=(ShortPeriod-1.0)/2.0; t=ShortPeriod + ExtraTimeForward; t11=(slowLongPeriod-1.0)/2.0; t31=(slowShortPeriod-1.0)/2.0; t2=slowShortPeriod + slowExtraTimeForward; ma1=1; ma3=ma1; ma11=1; ma31=ma1; for (int iBar = iFirstBar; iBar < Bars; iBar++) { double val=Close[iBar]; double val1=Close[iBar]; ma1=p1*val + (1.0-p1)*ma1; ma3=p3*val + (1.0-p3)*ma3; ma11=p11*val1 + (1.0-p11)*ma11; ma31=p31*val1 + (1.0-p31)*ma31; //---- double slope1=(ma3-ma1)/(t1-t3); double slope11=(ma31-ma11)/(t11-t31); //---- double predict=ma3 + slope1*t; ExtBuffer[iBar]=predict; double predict1=ma31 + slope11*t2; ExtBuffer1[iBar]=predict1; } for (int iBar = iFirstBar; iBar < Bars; iBar++) adMAOscillator[iBar] = ExtBuffer1[iBar] - ExtBuffer[iBar]; // Saving the components Component = new IndicatorComp[4]; Component[0] = new IndicatorComp(); Component[0].CompName = "Fast Moving Average"; Component[0].ChartColor = Color.Goldenrod; Component[0].DataType = IndComponentType.IndicatorValue; Component[0].ChartType = IndChartType.Line; Component[0].FirstBar = iFirstBar; Component[0].Value = ExtBuffer; Component[1] = new IndicatorComp(); Component[1].CompName = "Slow Moving Average"; Component[1].ChartColor = Color.IndianRed; Component[1].DataType = IndComponentType.IndicatorValue; Component[1].ChartType = IndChartType.Line; Component[1].FirstBar = iFirstBar; Component[1].Value = ExtBuffer1; Component[2] = new IndicatorComp(); Component[2].ChartType = IndChartType.NoChart; Component[2].FirstBar = iFirstBar; Component[2].Value = new double[Bars]; Component[3] = new IndicatorComp(); Component[3].ChartType = IndChartType.NoChart; Component[3].FirstBar = iFirstBar; Component[3].Value = new double[Bars]; // Sets the Component's type if (SlotType == SlotTypes.OpenFilter) { Component[2].DataType = IndComponentType.AllowOpenLong; Component[2].CompName = "Is long entry allowed"; Component[3].DataType = IndComponentType.AllowOpenShort; Component[3].CompName = "Is short entry allowed"; } else if (SlotType == SlotTypes.CloseFilter) { Component[2].DataType = IndComponentType.ForceCloseLong; Component[2].CompName = "Close out long position"; Component[3].DataType = IndComponentType.ForceCloseShort; Component[3].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 Fast MA crosses the Slow MA upward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward; break; case "The Fast MA crosses the Slow MA downward": indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward; break; case "The Fast MA is higher than the Slow MA": indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line; break; case "The Fast MA is lower than the Slow MA": indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line; break; default: break; } OscillatorLogic(iFirstBar, iPrvs, adMAOscillator, 0, 0, ref Component[2], ref Component[3], indLogic); return; } /// /// Sets the indicator logic description /// public override void SetDescription() { EntryFilterLongDescription = ToString() + "; the Fast MA "; EntryFilterShortDescription = ToString() + "; the Fast MA "; ExitFilterLongDescription = ToString() + "; the Fast MA "; ExitFilterShortDescription = ToString() + "; the Fast MA "; switch (IndParam.ListParam[0].Text) { case "The Fast MA crosses the Slow MA upward": EntryFilterLongDescription += "crosses the Slow MA upward"; EntryFilterShortDescription += "crosses the Slow MA downward"; ExitFilterLongDescription += "crosses the Slow MA upward"; ExitFilterShortDescription += "crosses the Slow MA downward"; break; case "The Fast MA crosses the Slow MA downward": EntryFilterLongDescription += "crosses the Slow MA downward"; EntryFilterShortDescription += "crosses the Slow MA upward"; ExitFilterLongDescription += "crosses the Slow MA downward"; ExitFilterShortDescription += "crosses the Slow MA upward"; break; case "The Fast MA is higher than the Slow MA": EntryFilterLongDescription += "is higher than the Slow MA"; EntryFilterShortDescription += "is lower than the Slow MA"; ExitFilterLongDescription += "is higher than the Slow MA"; ExitFilterShortDescription += "is lower than the Slow MA"; break; case "The Fast MA is lower than the Slow MA": EntryFilterLongDescription += "is lower than the Slow MA"; EntryFilterShortDescription += "is higher than the Slow MA"; ExitFilterLongDescription += "is lower than the Slow MA"; ExitFilterShortDescription += "is higher than the Slow MA"; break; default: break; } return; } /// /// Indicator to string /// public override string ToString() { string sString = IndicatorName + (IndParam.CheckParam[0].Checked ? "* (" : " (") + IndParam.ListParam[1].Text + ", " + // Price IndParam.ListParam[3].Text + ", " + // Fast MA Method IndParam.ListParam[4].Text + ", " + // Slow MA Method IndParam.NumParam[0].ValueToString + ", " + // Fast MA period IndParam.NumParam[1].ValueToString + ", " + // Slow MA period IndParam.NumParam[2].ValueToString + ", " + // Fast MA shift IndParam.NumParam[3].ValueToString + ")"; // Slow MA shift return sString; } } }