When an indicator or signal generates MultiCharts alerts, those alerts can fire on every bar on which the alert condition happens. This may not always be practical, however, especially when we don’t trade every hour the market is open. So how do we programmatically generate alerts only during our trading hours?

In this article:

Generating MultiCharts alerts during a specific time window

Firing MultiCharts alerts programmatically is done with the Alert() keyword (MultiCharts Wiki, 2012a), but for that to work we need to enable the script’s alert setting. Then, depending on how we’ve configured the alert, the alerts appear as a notification window, audio alert, and/or email alert (MultiCharts Wiki, 2013).

We enable alerts with the ‘Enable Alerts’ checkbox, and the ‘Alert Conditions Check’ setting just below it configures how often the alert fires per bar:

Enabling the alerts of a MultiCharts indicator or strategy by hand

But there’s no option to configure when alerts fire. Instead, the script generates an alert each time the alert condition happens – regardless of whether that’s during regular trading hours or when we’re sleeping. Even though we don’t act on alerts outside our trading hours, they still clutter the ‘Alerts’ tab of the Order and Position Tracker and can generate a lot of unnecessary email alerts. So how do we only generate alerts during a particular time period?

One approach is using the bar’s time. But there are two disadvantages with that. First, the time of a bar is the bar’s closing time. That gives us a loss of precision. For instance, when we trade an hourly chart of an instrument that opens at 9:00, we cannot generate alerts between 9:15 and 9:45 since the bars will have timestamps of 10:00, 11:00, 12:00, and so on. The second disadvantage is that the bar’s time can be in the exchange time’s zone. That means if the same script is applied to different instruments, we need to convert each instrument’s exchange time to the local time zone in which we’re trading. Even though we probably know our trading hours better in local time zone than in several exchange time zones.

Luckily, two PowerLanguage keywords return the current time of the computer (so local time zone). These keywords are also much more precise than using the bar’s closing time (PowerLanguage Keyword Reference, 2016):

  • The CurrentTime keyword returns the computer time as a numerical value in 24-hour HHmm format. That means when it’s 9:12 in the morning, CurrentTime returns 912 while 15:34 in the afternoon is returned as 1534.
  • The CurrentTime_s keyword also returns a numerical value with the computer’s current time, but adds seconds precision through using the 24-hour HHmmss format. So at 10:17:47 in the morning, CurrentTime_s returns 101747 and 21:29:50 becomes 212950.

Let’s look at a programming example to see how CurrentTime can be used to only generate alerts during a certain time period.

Example: only generate alerts during a certain time period

In the example indicator below we generate an alert whenever there are three consecutive bars with higher highs or lower lows. With two input options we define a custom time period. And before generating an alert we’ll check with CurrentTime whether the computer’s time is within that time period.

The image below shows how an alert generated by the script looks like. After discussing the code, we’ll take a closer look at the script’s behaviour.

Example alert generated by the MultiCharts script

Inputs:
    StartTime(800),
    EndTime(1215);

Variables:
    inTimePeriod(false),
    higherHighs(false),
    lowerLows(false);

// Determine if we're currently in the specified time range
if (StartTime < EndTime) then
    inTimePeriod = (CurrentTime >= StartTime) and (CurrentTime <= EndTime)
else
    inTimePeriod = (CurrentTime > StartTime) or (CurrentTime < EndTime);

if (CheckAlert and inTimePeriod) then begin

    // Check for the alert conditions
    higherHighs = (High > High[1]) and (High[1] > High[2]) and
        (High[2] > High[3]);
    lowerLows = (Low < Low[1]) and (Low[1] < Low[2]) and
        (Low[2] < Low[3]);

    // Generate alerts
    if (higherHighs) then
        Alert("Three higher highs in a row")

    else if (lowerLows) then
        Alert("Three consecutive lower lows");

end;

We start with creating two input options and three variables:


Inputs:
    StartTime(800),
    EndTime(1215);

Variables:
    inTimePeriod(false),
    higherHighs(false),
    lowerLows(false);

The StartTime and EndTime numerical input options define the begin and end time of our custom time period. Since we’re going to use the CurrentTime keyword in the script, we set these inputs to the same time format as that keyword uses. And so the default value of StartTime is 800 (meaning 8:00) and EndTime 1215 (12:15 hour). With these inputs we can easily and quickly configure the time period, without having to edit and recompile the code.

Note: We use CurrentTime since we don’t need the second precision that CurrentTime_s provides. If you do use CurrentTime_s, then the default values for these inputs have to be 80000 (that is, 8:00:00) and 121500 (12:15:00).

Next we create three true/false variables, each with a default value of false. The first, inTimePeriod, will be used to indicate whether the computer’s current time falls inside the specified time range or not. With higherHighs and lowerLows we’ll temporarily hold the conditions used for generating an alert programmatically.

But first we determine whether the computer’s current time falls inside the time range or not:


if (StartTime < EndTime) then
    inTimePeriod = (CurrentTime >= StartTime) and (CurrentTime <= EndTime)
else
    inTimePeriod = (CurrentTime > StartTime) or (CurrentTime < EndTime);

A time range can be specified in two ways. The first is a same-day time range (like from 9:00 till 17:00). The other is a time range that crosses midnight, for instance from 22:00 till 8:00. With this if/else statement, we address both options.

First is the if portion of the if/else statement. With its condition we check whether the StartTIme input is less than (<) EndTime. That happens when we have a same-day time range, like when the inputs have their default values of 8:00 and 12:15. In that situation, we set the inTimePeriod true/false variable to a condition that combines two expressions with the and logical keyword. That keyword returns true when the value on its left and the value on its right are both true too; should one or both values be false, then and returns false too (MultiCharts Wiki, 2012b).

The first expression that determines the value of inTimePeriod checks whether the computer’s time (CurrentTime) is greater than or equal to (>=) the StartTime input option. With the second expression we require that the computer time is less than or equal to (<=) the EndTime input. Combined, this means the computer time has to fall somewhere between StartTime and EndTime to set the inTimePeriod variable to true; otherwise, that variable is assigned false.

Now when StartTime is not less than EndTime, these inputs specify an overnight session and the else portion of this if/else statement executes. That code also updates the inTimePeriod variable, but this time we determine its new value by combining two expressions with the or logical keyword. That keyword returns true when the value on its left, the value on its right, or both are true. Only when both the left and right value are false will the result combined with or be false too (MultiCharts Wiki, 2012c).

With the first of those expressions we check whether the computer’s current time is greater than (>) the StartTime input option, while the other evaluates if the computer’s current time is less than (<) EndTime. The reasoning behind this is best explained with an example.

Say we set StartTime to 2200 and EndTime to 800. We can think of this time range as two smaller time periods because the clock ‘resets’ at midnight (that is, goes from 23:59 to 0:01). From that standpoint, there’s one time period from 22:00 till 0:00 while the other covers 0:00 till 8:00. And so for the time to be in this overnight session, the time either has to be in the late evening (to cover the period after 22:00) or early morning (for the period before 8:00).

And so we require here that the CurrentTime keyword returns a value that’s either greater than StartTime (which covers 22:00 till 0:00) or less than EndTime (which covers 0:00 till 8:00). Now whenever the computer time falls into one of those sessions, we set the inTimePeriod variable to true. Otherwise, that variable is set to false.

After figuring out how the computer’s time relates to the input options, we place the remaining code of the indicator inside the following if statement:


if (CheckAlert and inTimePeriod) then begin

    // ...

end;

This if statement’s condition evaluates two expressions: the CheckAlert keyword and inTimePeriod true/false variable. By combining them with the and logical keyword, both need to be true before the combined condition is true too.

The CheckAlert keyword returns true when the script’s alert setting is enabled and when the current bar is the last of the data series (PowerLanguage Keyword Reference, 2016). By adding this keyword to the if statement’s condition, we make sure that the if statement’s code only runs when the script is actually in a position to fire an alert.

The second expression is the inTimePeriod true/false variable, which holds true when the computer’s time falls inside the range set with the StartTime and EndTime input options. This makes any alert that we’re going to generate inside the if statement only fire during that specified time window.

Before generating alerts though, we first program the alert conditions inside the if statement:


higherHighs = (High > High[1]) and (High[1] > High[2]) and
    (High[2] > High[3]);
lowerLows = (Low < Low[1]) and (Low[1] < Low[2]) and
    (Low[2] < Low[3]);

We set the higherHighs and lowerLows variables here to a true/false value based on three expressions that are combined with and. That the variable is only set to true when all three expressions are true too.

For the higherHighs variable, we check if the current bar’s high (High) is greater than (>) the previous bar’s high (High[1]). With the second expression we evaluate whether the previous bar had a high above the bar before that (High[1] > High[2]), and then check if the high of two bars ago was higher than the bar before that (High[2] > High[3]). When all three situations happened, the higherHighs variable is set to true and becomes false otherwise.

Similarly, the lowerLows variable is set to true when the current bar’s low (Low) is less than (<) the low of the previous bar (Low[1]), with the previous bar also having a lower low (Low[1] < Low[2]) while the bar before that achieved a lower low too (Low[2] < Low[3]). When these three expressions evaluate to true, lowerLows becomes true too (and false otherwise`).

After we create the alert conditions, we use an if/else statement to generate the alerts:


if (higherHighs) then
    Alert("Three higher highs in a row")

else if (lowerLows) then
    Alert("Three consecutive lower lows");

The if portion of this if/else statement checks whether the higherHighs variable evaluates to true, which it does with three higher highs in a row. In that situation, we generate an alert message with Alert() (PowerLanguage Keyword Reference, 2016). The message that we specify inside that keyword’s parentheses mentions the three higher highs.

The else if part of the if/else statement evaluates if lowerLows is true. That happens when there are three lower lows in a row, in which case we generate an alert message that says so.

Since this if/else statement is inside the if statement that checks whether the computer time falls inside the time window, the alerts only generate when the time is between the StartTime and EndTime input options. That way alerts only trigger inside that time period, but not outside it.

Examples: generating alerts only in a certain time period

Let’s see how the example indicator behaves and how its alerts look like. To have the script fire alerts, we use the following manual alert settings:

Configuring the alert settings of the MultiCharts script

Given that the script only generates an alert when the time falls in a time window, the ‘Once Per Bar’ setting is actually a ‘once per bar during the custom time range’ kind of alert.

The input options that we gave our indicator look like:

Input options of the MultiCharts example indicator

Now when there are three lower lows in a row, like here:

Example chart with the MultiCharts indicator applied

Then the indicator generates the following alert message (provided that the lower lows happen inside the specified time window):

Example alert for three lower lows in the defined time range

Likewise, the alerts looks like this when there are three higher highs:

Example MultiCharts alert of reaching three higher highs

Placing a maximum on the number of alerts an indicator or signal generates is also possible by limiting the number of alerts a script generates.

Summary

We programmatically MultiCharts alerts with Alert() but for this the script’s ‘Enable Alerts’ option needs to be enabled. Once turned on, the indicator or signal can generate an alert with every bar – even when we don’t intend to trade at that time. By restricting MultiCharts alerts to fire only during a certain time period, we prevent unneeded alert messages from cluttering the MultiCharts program or our email inbox. To do that we can use CurrentTime and CurrentTime_s as filters. The CurrentTime keyword returns the computer’s time in 24-hour HHmm format (like 910 and 1624 for 9:10 and 16:24). And CurrentTime_s adds seconds precision and returns the computer’s current time in 24-hour HHmmss format (91034 and 162459 for 9:10:34 and 16:24:59). When we check if a particular time falls inside a certain time period, we need to address two time periods: those that remain on the same day (9:00 till 17:30) and those that cross midnight (23:15 till 8:30). For the first, the current time has to be greater than or equal to the start time while also being less than or equal to the end time. When the custom time range crosses midnight, the current time needs to be beyond the start time or before the end time.

Learn more:


References

MultiCharts Wiki (2012a, February 7). Alert. Retrieved on September 5, 2016, from http://www.multicharts.com/trading-software/index.php/Alert

MultiCharts Wiki (2012b, February 19). And. Retrieved on September 5, 2016, from http://www.multicharts.com/trading-software/index.php/And

MultiCharts Wiki (2012c, February 19). Or. Retrieved on September 5, 2016, from http://www.multicharts.com/trading-software/index.php/Or

MultiCharts Wiki (2013, May 10). Using Alerts. Retrieved on September 5, 2016, from https://www.multicharts.com/trading-software/index.php/Using_Alerts

PowerLanguage Keyword Reference (2016). Retrieved on May 25, 2016, from http://www.multicharts.com/trading-software/images/c/c6/PowerLanguage_Keyword_Reference.pdf