Skip to content

Commit 8301447

Browse files
authored
Update README.md
1 parent 6f0e2d9 commit 8301447

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

faq_and_code/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,39 @@ The short answer is no and the reason is because of the rollback process that ha
15341534

15351535
To time the duration of an event, we would need to detect its beginning, save the time at that point (e.g., in a `timeSaved` variable), and for each subsequent execution of the script, check if the condition is still true and if so, use `timenow - timeSaved` to determine the number of milliseconds for which the condition has been true. This, however, cannot be done in Pine because the value of all variables are rolled back to their starting values at the beginning of the realtime bar before each realtime bar iteration of the script. This makes it impossible for the saved time in our `timeSaved` variable to perpetuate across subsequent iterations of the script in the same realtime bar. Only its value at the realtime bar's close, so the script's last iteration in the realtime bar, is preserved.
15361536

1537+
1538+
### How can I identify the nth occurrence of a weekday in the month?
1539+
This shows how to use our `f_nthDayofweekInMonth(_nth, _dayNo)` function to detect the first Monday of the month:
1540+
1541+
```js
1542+
//@version=4
1543+
study("", "", true)
1544+
1545+
// The default inputs look for the first Monday of the month.
1546+
// Note that days must be trading days for the function to work correctly.
1547+
int i_nth = input(1, "nth Occurence of the day", minval = 1, maxval = 5)
1548+
int i_dayNo = input(2, "Day number", minval = 1, maxval = 7, tooltip = "1 is Sunday")
1549+
1550+
// ————— Function returning `true` on the `_nth` occurrence of `_dayNo` in the month.
1551+
f_nthDayofweekInMonth(_nth, _dayNo) =>
1552+
// int _nth : The occurrence required. Use 1 for the first occurrence.
1553+
// int _dayNo: The day of the week number (Sunday is 1, Saturday is 7). Days not found on the chart are not accounted for.
1554+
var int _occurrence = 0
1555+
bool _newMonth = change(time("M")) > 0
1556+
bool _newDay = dayofweek == _dayNo and dayofweek[1] != _dayNo
1557+
if _newMonth
1558+
if _newDay
1559+
_occurrence := 1
1560+
else
1561+
_occurrence := 0
1562+
else if _newDay
1563+
_occurrence += 1
1564+
bool _return = _occurrence == _nth and dayofweek == _dayNo
1565+
1566+
plotchar(f_nthDayofweekInMonth(i_nth, i_dayNo), "Day", "", location.top, size = size.tiny)
1567+
```
1568+
1569+
15371570
**[Back to top](#table-of-contents)**
15381571

15391572

0 commit comments

Comments
 (0)