Day of Week Exit by Popov
44945 downloads / 6453 views / Created: 01.09.2013




Average Rating: 0
Indicator Description
Indicator author: Krog
Original Request -- indicator to specify closing day, to be more flexible than Week Closing (last day only)
Feature: can specify day-time range for exiting positions
Notes: use filters so you don't enter a position during the exit time. Or else, equity curve goes down quickly because it will open and close positions on each bar.
Please verify it works for you on a demo account before using on real account. If any bugs or unexpected behavior, please advise.
Forum link: Day of Week Exit
Original Request -- indicator to specify closing day, to be more flexible than Week Closing (last day only)
Feature: can specify day-time range for exiting positions
Notes: use filters so you don't enter a position during the exit time. Or else, equity curve goes down quickly because it will open and close positions on each bar.
Please verify it works for you on a demo account before using on real account. If any bugs or unexpected behavior, please advise.
Forum link: Day of Week Exit
Comments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
//==============================================================
// 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
{
///
/// Day of Week Exit Indicator
///
public class DayOfWeekExit : Indicator
{
public DayOfWeekExit()
{
IndicatorName = "Day of Week Exit";
PossibleSlots = SlotTypes.CloseFilter;
IndicatorAuthor = "Krog";
IndicatorVersion = "2.0";
IndicatorDescription = "Custom indicator for FSB / FST.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
IndParam.IndicatorType = TypeOfIndicator.DateTime;
// The ComboBox parameters
IndParam.ListParam[0].Caption = "Logic";
IndParam.ListParam[0].ItemList = new string[]
{
"Exit the market between the specified day at the specified time"
};
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 = "Indicators' logic.";
IndParam.ListParam[1].Caption = "From (incl.)";
IndParam.ListParam[1].ItemList = Enum.GetNames(typeof(DayOfWeek));
IndParam.ListParam[1].Index = (int)DayOfWeek.Friday;
IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[IndParam.ListParam[1].Index];
IndParam.ListParam[1].Enabled = true;
IndParam.ListParam[1].ToolTip = "Day of beginning for the exit period.";
IndParam.ListParam[2].Caption = "To (excl.)";
IndParam.ListParam[2].ItemList = Enum.GetNames(typeof(DayOfWeek));
IndParam.ListParam[2].Index = (int)DayOfWeek.Saturday;
IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[IndParam.ListParam[2].Index];
IndParam.ListParam[2].Enabled = true;
IndParam.ListParam[2].ToolTip = "End day for the exit period.";
// The NumericUpDown parameters
IndParam.NumParam[0].Caption = "From hour (incl.)";
IndParam.NumParam[0].Value = 0;
IndParam.NumParam[0].Min = 0;
IndParam.NumParam[0].Max = 23;
IndParam.NumParam[0].Enabled = true;
IndParam.NumParam[0].ToolTip = "Beginning of the exit period.";
IndParam.NumParam[1].Caption = "From min (incl.)";
IndParam.NumParam[1].Value = 0;
IndParam.NumParam[1].Min = 0;
IndParam.NumParam[1].Max = 59;
IndParam.NumParam[1].Enabled = true;
IndParam.NumParam[1].ToolTip = "Beginning of the exit period.";
IndParam.NumParam[2].Caption = "Until hour (excl.)";
IndParam.NumParam[2].Value = 24;
IndParam.NumParam[2].Min = 0;
IndParam.NumParam[2].Max = 24;
IndParam.NumParam[2].Enabled = true;
IndParam.NumParam[2].ToolTip = "End of the exit period.";
IndParam.NumParam[3].Caption = "Until min( excl.)";
IndParam.NumParam[3].Value = 0;
IndParam.NumParam[3].Min = 0;
IndParam.NumParam[3].Max = 59;
IndParam.NumParam[3].Enabled = true;
IndParam.NumParam[3].ToolTip = "End of the exit period.";
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
// Reading the parameters
int iFromDay = (int)IndParam.ListParam[1].Index;
int iFromHour = (int)IndParam.NumParam[0].Value;
int iFromMin = (int)IndParam.NumParam[1].Value;
int iUntilDay = (int)IndParam.ListParam[2].Index;
int iUntilHour = (int)IndParam.NumParam[2].Value;
int iUntilMin = (int)IndParam.NumParam[3].Value;
TimeSpan tsFromTime = new TimeSpan(iFromDay, iFromHour, iFromMin, 0);
TimeSpan tsUntilTime = new TimeSpan(iUntilDay, iUntilHour, iUntilMin, 0);
// Calculation
int iFirstBar = 1;
double[] adBars = new double[Bars];
// Calculation of the logic
for (int iBar = iFirstBar; iBar < Bars; iBar++)
{
TimeSpan tsBar = new TimeSpan((int)Time[iBar].DayOfWeek, Time[iBar].Hour, Time[iBar].Minute, 0);
adBars[iBar] = tsBar >= tsFromTime && tsBar < tsUntilTime ? 1 : 0;
}
// Saving the components
Component = new IndicatorComp[1];
Component[0] = new IndicatorComp();
Component[0].CompName = "Force close position";
Component[0].DataType = IndComponentType.ForceClose;
Component[0].ChartType = IndChartType.NoChart;
Component[0].ShowInDynInfo = false;
Component[0].FirstBar = iFirstBar;
Component[0].Value = adBars;
}
public override void SetDescription()
{
DayOfWeek dowFromDay = (DayOfWeek)IndParam.ListParam[1].Index;
int iFromHour = (int)IndParam.NumParam[0].Value;
int iFromMin = (int)IndParam.NumParam[1].Value;
string sFromMin = (iFromMin < 10) ? "0" + iFromMin.ToString() : iFromMin.ToString();
DayOfWeek dowUntilDay = (DayOfWeek)IndParam.ListParam[2].Index;
int iUntilHour = (int)IndParam.NumParam[2].Value;
int iUntilMin = (int)IndParam.NumParam[3].Value;
string sUntilMin = (iUntilMin < 10) ? "0" + iUntilMin.ToString() : iUntilMin.ToString();
ExitFilterLongDescription = "between " + dowFromDay + " " + iFromHour.ToString() + ":" + sFromMin + " and " + dowUntilDay + " " + iUntilHour.ToString() + ":" + sUntilMin;
ExitFilterShortDescription = "between " + dowFromDay + " " + iFromHour.ToString() + ":" + sFromMin + " and " + dowUntilDay + " " + iUntilHour.ToString() + ":" + sUntilMin;
}
public override string ToString()
{
DayOfWeek dowFromDay = (DayOfWeek)IndParam.ListParam[1].Index;
int iFromHour = (int)IndParam.NumParam[0].Value;
int iFromMin = (int)IndParam.NumParam[1].Value;
string sFromMin = (iFromMin < 10) ? "0" + iFromMin.ToString() : iFromMin.ToString();
DayOfWeek dowUntilDay = (DayOfWeek)IndParam.ListParam[2].Index;
int iUntilHour = (int)IndParam.NumParam[2].Value;
int iUntilMin = (int)IndParam.NumParam[3].Value;
string sUntilMin = (iUntilMin < 10) ? "0" + iUntilMin.ToString() : iUntilMin.ToString();
string sString = IndicatorName + " (" +
dowFromDay + " " + // Day
iFromHour.ToString() + ":" + // Hour
sFromMin + "-" + // Minute
dowUntilDay + " " + // Day
iUntilHour.ToString() + ":" + // Hour
sUntilMin + ")"; // Minute
return sString;
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92//+--------------------------------------------------------------------+ //| Copyright: (C) 2016 Forex Software Ltd. | //| Website: http://forexsb.com/ | //| Support: http://forexsb.com/forum/ | //| License: Proprietary under the following circumstances: | //| | //| This code is a part of Forex Strategy Builder. It is free for | //| use as an integral part of Forex Strategy Builder. | //| One can modify it in order to improve the code or to fit it for | //| personal use. This code or any part of it cannot be used in | //| other applications without a permission. | //| The contact information cannot be changed. | //| | //| NO LIABILITY FOR CONSEQUENTIAL DAMAGES | //| | //| In no event shall the author be liable for any damages whatsoever | //| (including, without limitation, incidental, direct, indirect and | //| consequential damages, damages for loss of business profits, | //| business interruption, loss of business information, or other | //| pecuniary loss) arising out of the use or inability to use this | //| product, even if advised of the possibility of such damages. | //+--------------------------------------------------------------------+ #property copyright "Copyright (C) 2016 Forex Software Ltd." #property link "http://forexsb.com" #property version "2.00" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class DayOfWeekExit : public Indicator { public: DayOfWeekExit(SlotTypes slotType); virtual void Calculate(DataSet &dataSet); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void DayOfWeekExit::DayOfWeekExit(SlotTypes slotType) { SlotType = slotType; IndicatorName = "Day of Week Exit"; WarningMessage = ""; IsAllowLTF = true; ExecTime = ExecutionTime_DuringTheBar; IsSeparateChart = false; IsDiscreteValues = false; IsDefaultGroupAll = true; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void DayOfWeekExit::Calculate(DataSet &dataSet) { Data=GetPointer(dataSet); int fromHour = (int) NumParam[0].Value; int fromMin = (int) NumParam[1].Value; int untilHour = (int) NumParam[2].Value; int untilMin = (int) NumParam[3].Value; int fromDay = ListParam[1].Index; int untilDay = ListParam[2].Index; const int firstBar=2; double adBars[]; ArrayResize(adBars,Data.Bars);ArrayInitialize(adBars,0); int fromTime = fromHour*3600+fromMin*60; int untilTime = untilHour*3600+untilMin*60; for(int bar=firstBar; bar<Data.Bars; bar++) { MqlDateTime mqlTime; TimeToStruct(Data.Time[bar],mqlTime); int barTime=mqlTime.hour*3600+mqlTime.min*60; int time=mqlTime.day_of_week; adBars[bar]= time>=fromDay && time<untilDay && barTime>=fromTime && barTime<untilTime ? 1 : 0; } ArrayResize(Component[0].Value,Data.Bars); Component[0].CompName = "Force close position"; Component[0].DataType = IndComponentType_ForceClose; Component[0].ShowInDynInfo=false; Component[0].FirstBar=firstBar; ArrayCopy(Component[0].Value,adBars); } //+------------------------------------------------------------------+
Risk warning: Forex, spread bets and CFD are leveraged products. They may not be suitable for you as they carry a high degree of risk to your capital and you can lose more than your initial investment. You should ensure you understand all of the risks.
Copyright © 2006 - 2025, Forex Software Ltd.;
Copyright © 2006 - 2025, Forex Software Ltd.;