//============================================================== // 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 TimeGrid : Indicator { public TimeGrid() { IndicatorName = "Time Grid"; PossibleSlots = SlotTypes.OpenFilter; IndicatorAuthor = "Miroslav Popov"; IndicatorVersion = "1.0"; IndicatorDescription = "Allows entry at every round hour"; } public override void Initialize(SlotTypes slotType) { SlotType = slotType; // Setting up the indicator parameters IndParam.IndicatorType = TypeOfIndicator.DateTime; IndParam.IsAllowLTF = false; // The ComboBox parameters IndParam.ListParam[0].Caption = "Logic"; IndParam.ListParam[0].ItemList = new string[] { "Bar opens at round 1 min", "Bar opens at round 5 min", "Bar opens at round 15 min", "Bar opens at round 30 min", "Bar opens at round 60 min", }; 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.NumParam[0].Caption = "From hour (incl.)"; IndParam.NumParam[0].Value = 8; IndParam.NumParam[0].Min = 0; IndParam.NumParam[0].Max = 23; IndParam.NumParam[0].Point = 0; IndParam.NumParam[0].Enabled = true; IndParam.NumParam[0].ToolTip = "The beginning of the trading period"; IndParam.NumParam[1].Caption = "Until hour (excl.)"; IndParam.NumParam[1].Value = 16; IndParam.NumParam[1].Min = 0; IndParam.NumParam[1].Max = 24; IndParam.NumParam[1].Point = 0; IndParam.NumParam[1].Enabled = true; IndParam.NumParam[1].ToolTip = "The the of the trading period."; } public override void Calculate(IDataSet dataSet) { int logic = IndParam.ListParam[0].Index; int minutes = 1; if (logic == 1) minutes = 5; if (logic == 2) minutes = 15; if (logic == 3) minutes = 30; if (logic == 4) minutes = 0; DataSet = dataSet; // Reading the parameters int fromHour = (int)IndParam.NumParam[0].Value; int toHour = (int)IndParam.NumParam[1].Value; // Calculation int firstBar = 2; double[] signal = new double[Bars]; // Calculation of the logic for (int bar = firstBar; bar < Bars; bar++) { if (minutes == 0 && Time[bar].Minute != 0) { signal[bar] = 0; continue; } if (minutes != 0 && Time[bar].Minute % minutes != 0) { signal[bar] = 0; continue; } if (fromHour < toHour) signal[bar] = Time[bar].Hour >= fromHour && Time[bar].Hour < toHour ? 1 : 0; else if (fromHour > toHour) signal[bar] = Time[bar].Hour >= fromHour || Time[bar].Hour < toHour ? 1 : 0; else signal[bar] = 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 = firstBar; Component[0].Value = signal; 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 = firstBar; Component[1].Value = signal; } public override void SetDescription() { int fromHour = (int)IndParam.NumParam[0].Value; int toHour = (int)IndParam.NumParam[1].Value; int logic = IndParam.ListParam[0].Index; int minutes = 1; if (logic == 1) minutes = 5; if (logic == 2) minutes = 15; if (logic == 3) minutes = 30; if (logic == 3) minutes = 60; EntryFilterLongDescription = "the bar opens at round " + minutes + " hour from " + fromHour + " (incl.) to " + toHour + " (excl.)"; EntryFilterShortDescription = "the bar opens at round " + minutes + " hour from "+ fromHour + " (incl.) to " + toHour + " (excl.)"; } public override string ToString() { int fromHour = (int)IndParam.NumParam[0].Value; int toHour = (int)IndParam.NumParam[1].Value; return IndicatorName + " (" + fromHour + ", " + toHour + ")"; } } }