AdvancedExitTimeStepped by Naya
566 downloads / 283 views / Created: 14.06.2025 Average Rating: 0
Indicator Description
The Advance Exit Time indicator is designed to generate periodic exit signals at customizable intervals, starting from a specified anchor time. This powerful tool helps traders implement disciplined, time-based exit strategies for systematic position management.
Key Features:
- Anchor Time setting (hours and minutes) determines the starting point for periodic exits
- Adjustable Exit Period (in minutes) controls the frequency of exit signals
- Handles time calculations seamlessly, including midnight transitions
- Works effectively across various timeframes, with optimal performance on smaller timeframes
Use Cases:
1. Systematic profit-taking by closing trades at regular intervals
2. Session-based trading strategies with timed exits
3. Automated trading systems requiring scheduled position closures
4. Reducing emotional decision-making with predefined exit times
How It Works:
The indicator calculates exit points by measuring time intervals from the user-defined anchor time. For example, with an anchor time of 00:00 and a 60-minute period, exit signals will occur at the top of every hour (1:00, 2:00, etc.). The calculation properly accounts for daily time rollovers.
Benefits:
- Enforces trading discipline through structured exit rules
- Provides flexibility for different trading styles and timeframes
- Removes emotional bias from exit decisions
- Complements various trading strategies with reliable timing
Ideal For:
- Day traders and scalpers
- Systematic trading approaches
- Traders using time-based strategies
- Automated trading systems
Comments
using System;
using System.Collections.Generic;
using ForexStrategyBuilder.Infrastructure.Entities;
using ForexStrategyBuilder.Infrastructure.Enums;
using ForexStrategyBuilder.Infrastructure.Interfaces;
namespace ForexStrategyBuilder.Indicators.Custom
{
public class AdvancedExitTimeStepped : Indicator
{
public AdvancedExitTimeStepped()
{
IndicatorName = "Advanced Exit Time Stepped";
PossibleSlots = SlotTypes.CloseFilter;
IsDeafultGroupAll = true;
IsGeneratable = false;
WarningMessage = "The indicator generates exit signals at periodic intervals starting from the specified anchor time." +
Environment.NewLine +
"Ensure the strategy time frame allows closing at these specific times.";
IndicatorAuthor = "NAYA +237674724684";
IndicatorVersion = "1.21";
IndicatorDescription = "Generates periodic exit signals based on specified time steps from an anchor time using bar close time.";
}
public override void Initialize(SlotTypes slotType)
{
SlotType = slotType;
IndParam.IsAllowLTF = false;
IndParam.ExecutionTime = ExecutionTime.AtBarClosing;
// Logic Parameter
IndParam.ListParam[0].Caption = "Logic";
IndParam.ListParam[0].ItemList = new[] { "Exit the market at periodic intervals" };
IndParam.ListParam[0].Index = 0;
IndParam.ListParam[0].Text = IndParam.ListParam[0].ItemList[0];
IndParam.ListParam[0].Enabled = true;
IndParam.ListParam[0].ToolTip = "Defines the core behavior of the indicator.";
// Anchor Hour
IndParam.ListParam[1].Caption = "Anchor Hour";
var hours = new List();
for (int i = 0; i <= 23; i += 1)
hours.Add(i.ToString());
IndParam.ListParam[1].ItemList = hours.ToArray();
IndParam.ListParam[1].Index = 0;
IndParam.ListParam[1].Text = IndParam.ListParam[1].ItemList[0];
IndParam.ListParam[1].Enabled = true;
IndParam.ListParam[1].ToolTip = "The hour component (0-23) of the anchor time.";
// Anchor Minute
IndParam.ListParam[2].Caption = "Anchor Minute";
var minutes = new List();
for (int i = 0; i <= 30; i += 30)
minutes.Add(i.ToString());
IndParam.ListParam[2].ItemList = minutes.ToArray();
IndParam.ListParam[2].Index = 0;
IndParam.ListParam[2].Text = IndParam.ListParam[2].ItemList[0];
IndParam.ListParam[2].Enabled = true;
IndParam.ListParam[2].ToolTip = "The minute component (0, 15, 30, 45) of the anchor time.";
// Exit Period
IndParam.ListParam[3].Caption = "Exit Period (minutes)";
var periods = new List();
for (int i = 30; i <= 720; i += 30)
periods.Add(i.ToString());
IndParam.ListParam[3].ItemList = periods.ToArray();
IndParam.ListParam[3].Index = 0;
IndParam.ListParam[3].Text = IndParam.ListParam[3].ItemList[0];
IndParam.ListParam[3].Enabled = true;
IndParam.ListParam[3].ToolTip = "The interval (in minutes) between exit signals, starting from the anchor time. (15 to 1440 in steps of 15)";
}
public override void Calculate(IDataSet dataSet)
{
DataSet = dataSet;
int anchorHour = int.TryParse(IndParam.ListParam[1].Text, out anchorHour) ? anchorHour : 0;
int anchorMinute = int.TryParse(IndParam.ListParam[2].Text, out anchorMinute) ? anchorMinute : 0;
int exitPeriod = int.TryParse(IndParam.ListParam[3].Text, out exitPeriod) ? Math.Max(15, exitPeriod) : 15;
const int firstBar = 2;
double[] signal = new double[Bars];
int anchorTotalMinutes = anchorHour * 60 + anchorMinute;
for (int bar = firstBar; bar < Bars; bar++)
{
// Use bar close time (same as AdvanceExitTime)
DateTime closeTime = Time[bar].AddMinutes((int)Period);
int closeTotalMinutes = closeTime.Hour * 60 + closeTime.Minute;
// Calculate minutes since anchor time
int minutesSinceAnchor = (closeTotalMinutes - anchorTotalMinutes + 1440) % 1440;
// Check if current time is a multiple of exitPeriod from anchor time
signal[bar] = (minutesSinceAnchor % exitPeriod == 0) ? 1 : 0;
}
Component = new IndicatorComp[2];
Component[0] = new IndicatorComp
{
CompName = "Close out long position",
DataType = IndComponentType.ForceCloseLong,
ChartType = IndChartType.NoChart,
ShowInDynInfo = true,
FirstBar = firstBar,
Value = signal
};
Component[1] = new IndicatorComp
{
CompName = "Close out short position",
DataType = IndComponentType.ForceCloseShort,
ChartType = IndChartType.NoChart,
ShowInDynInfo = true,
FirstBar = firstBar,
Value = signal
};
}
public override void SetDescription()
{
string anchorHourText = IndParam.ListParam[1].Text.PadLeft(2, '0');
string anchorMinuteText = IndParam.ListParam[2].Text.PadLeft(2, '0');
string exitPeriodText = IndParam.ListParam[3].Text;
string description = "Exit the market every {exitPeriodText} minutes starting from {anchorHourText}:{anchorMinuteText}";
ExitFilterLongDescription = description;
ExitFilterShortDescription = description;
}
public override string ToString()
{
string anchorHourText = IndParam.ListParam[1].Text.PadLeft(2, '0');
string anchorMinuteText = IndParam.ListParam[2].Text.PadLeft(2, '0');
string exitPeriodText = IndParam.ListParam[3].Text;
return "{IndicatorName} ({anchorHourText}:{anchorMinuteText}, {exitPeriodText} min)";
}
}
}
//+------------------------------------------------------------------+ //| AdvancedExitTimeStepped.mqh | //| Copyright (C) 2025 NAYA +237674724684 | //| NAYA +237674724684 | //+------------------------------------------------------------------+ #property copyright "Copyright (C) 2025 NAYA +237674724684" #property link "NAYA +237674724684" #property version "1.21" #property strict #include <Forexsb.com/Indicator.mqh> #include <Forexsb.com/Enumerations.mqh> //+------------------------------------------------------------------+ //| Class AdvancedExitTimeStepped | //+------------------------------------------------------------------+ class AdvancedExitTimeStepped : public Indicator { public: AdvancedExitTimeStepped(SlotTypes slotType); virtual void Calculate(DataSet &dataSet); virtual string ToString(); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ void AdvancedExitTimeStepped::AdvancedExitTimeStepped(SlotTypes slotType) { SlotType = slotType; IndicatorName = "Advanced Exit Time Stepped"; IsAllowLTF = false; ExecTime = ExecutionTime_AtBarClosing; WarningMessage = "The indicator generates exit signals at periodic intervals starting from the specified anchor time. Ensure the strategy time frame allows closing at these specific times."; IsSeparateChart = false; IsDiscreteValues = false; IsDefaultGroupAll = true; } //+------------------------------------------------------------------+ //| Calculate indicator values | //+------------------------------------------------------------------+ void AdvancedExitTimeStepped::Calculate(DataSet &dataSet) { Data = GetPointer(dataSet); if(ArraySize(ListParam) < 4) { PrintFormat("%s Error: ListParam array size is %d, expected at least 4.", IndicatorName, ArraySize(ListParam)); return; } string anchorHourText = ListParam[1].Text; string anchorMinuteText = ListParam[2].Text; string exitPeriodText = ListParam[3].Text; int anchorHour = (int)StringToInteger(anchorHourText); int anchorMinute = (int)StringToInteger(anchorMinuteText); int exitPeriod = (int)StringToInteger(exitPeriodText); anchorHour = MathMax(0, MathMin(23, anchorHour)); anchorMinute = MathMax(0, MathMin(59, anchorMinute)); exitPeriod = MathMax(15, MathMin(1440, exitPeriod)); if (exitPeriod <= 0) { PrintFormat("%s Error: Exit Period must be greater than 0. Using default value of 15.", IndicatorName); exitPeriod = 15; } const int firstBar = 2; double signal[]; ArrayResize(signal, Data.Bars); ArrayInitialize(signal, 0); int anchorTotalMinutes = anchorHour * 60 + anchorMinute; for(int bar = firstBar; bar < Data.Bars; bar++) { // Calculate bar close time (similar to C# version) datetime closeTime = Data.Time[bar] + Data.Period * 60; MqlDateTime mqlCloseTime; TimeToStruct(closeTime, mqlCloseTime); int closeTotalMinutes = mqlCloseTime.hour * 60 + mqlCloseTime.min; // Calculate minutes since anchor time int minutesSinceAnchor = (closeTotalMinutes - anchorTotalMinutes + 1440) % 1440; // Check if current time is a multiple of exitPeriod from anchor time signal[bar] = (minutesSinceAnchor % exitPeriod == 0) ? 1 : 0; } ArrayResize(Component, 2); Component[0].DataType = IndComponentType_ForceCloseLong; Component[0].CompName = "Close out long position"; Component[0].FirstBar = firstBar; ArrayResize(Component[0].Value, Data.Bars); ArrayCopy(Component[0].Value, signal); Component[1].DataType = IndComponentType_ForceCloseShort; Component[1].CompName = "Close out short position"; Component[1].FirstBar = firstBar; ArrayResize(Component[1].Value, Data.Bars); ArrayCopy(Component[1].Value, signal); } //+------------------------------------------------------------------+ //| Returns string representation of the indicator | //+------------------------------------------------------------------+ string AdvancedExitTimeStepped::ToString() { if(ArraySize(ListParam) < 4) return IndicatorName; string anchorHourText = ListParam[1].Text; string anchorMinuteText = ListParam[2].Text; string exitPeriodText = ListParam[3].Text; string anchorTimeFullText = StringFormat("%02d:%02d", (int)StringToInteger(anchorHourText), (int)StringToInteger(anchorMinuteText)); return StringFormat("%s (%s, %s min)", IndicatorName, anchorTimeFullText, exitPeriodText); } //+------------------------------------------------------------------+
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.;