@rivascva/dt-idl 1.1.183 → 1.1.184

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/go/utils/lambda.go +83 -0
  2. package/package.json +1 -1
@@ -0,0 +1,83 @@
1
+ package utils
2
+
3
+ import (
4
+ "fmt"
5
+ "strconv"
6
+ "strings"
7
+ "time"
8
+
9
+ "github.com/RivasCVA/dt-idl/go/models"
10
+ )
11
+
12
+ // SleepThroughMarketTransition sleeps until the buffer window passes if the current time falls within market open or close transition periods.
13
+ //
14
+ // The buffer window is the window around each transition period.
15
+ // For example, if the buffer window is 15 seconds and the market opens at 9:30:00 and closes at 16:00:00:
16
+ //
17
+ // - Market open transition period: 9:29:45 - 9:30:15
18
+ // - The function will sleep until the current time is 9:30:15
19
+ //
20
+ // - Market close transition period: 15:59:45 - 16:00:15
21
+ // - The function will sleep until the current time is 16:00:15
22
+ //
23
+ // The goal is to allow enough time for the market data to be updated before running asynchronous operations that depend on this data.
24
+ func SleepThroughMarketTransition(now time.Time, bufferWindow time.Duration) error {
25
+ // load the market timezone
26
+ loc, err := time.LoadLocation(models.MarketTimezone)
27
+ if err != nil {
28
+ return fmt.Errorf("unable to load the market timezone: %w", err)
29
+ }
30
+
31
+ // convert the current time to the market timezone
32
+ now = now.In(loc)
33
+ today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc)
34
+
35
+ // parse the open time
36
+ openTimeParts := strings.Split(models.MarketOpenTime, ":")
37
+ openHour, err := strconv.Atoi(openTimeParts[0])
38
+ if err != nil {
39
+ return fmt.Errorf("unable to parse the open hour: %w", err)
40
+ }
41
+ openMinute, err := strconv.Atoi(openTimeParts[1])
42
+ if err != nil {
43
+ return fmt.Errorf("unable to parse the open minute: %w", err)
44
+ }
45
+
46
+ // parse the close time
47
+ closeTimeParts := strings.Split(models.MarketCloseTime, ":")
48
+ closeHour, err := strconv.Atoi(closeTimeParts[0])
49
+ if err != nil {
50
+ return fmt.Errorf("unable to parse the close hour: %w", err)
51
+ }
52
+ closeMinute, err := strconv.Atoi(closeTimeParts[1])
53
+ if err != nil {
54
+ return fmt.Errorf("unable to parse the close minute: %w", err)
55
+ }
56
+
57
+ // windows is a list of time windows that represent the market transition periods
58
+ windows := []struct {
59
+ start time.Time
60
+ end time.Time
61
+ }{
62
+ {
63
+ // market open window
64
+ start: today.Add(time.Duration(openHour)*time.Hour + time.Duration(openMinute)*time.Minute - bufferWindow),
65
+ end: today.Add(time.Duration(openHour)*time.Hour + time.Duration(openMinute)*time.Minute + bufferWindow),
66
+ },
67
+ {
68
+ // market close window
69
+ start: today.Add(time.Duration(closeHour)*time.Hour + time.Duration(closeMinute)*time.Minute - bufferWindow),
70
+ end: today.Add(time.Duration(closeHour)*time.Hour + time.Duration(closeMinute)*time.Minute + bufferWindow),
71
+ },
72
+ }
73
+
74
+ // sleep until the current time is after the market transition period (if applicable)
75
+ for _, window := range windows {
76
+ if !now.Before(window.start) && now.Before(window.end) {
77
+ time.Sleep(window.end.Sub(now))
78
+ break
79
+ }
80
+ }
81
+
82
+ return nil
83
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivascva/dt-idl",
3
- "version": "1.1.183",
3
+ "version": "1.1.184",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",