@@ -10,9 +10,10 @@ High-precision OHLC transformation with strict anchor time alignment.
1010
1111## Features
1212
13- - ** Strict Anchor Alignment** : Ensures candles align with specific sessions (e.g., 4h at 23:00 or 23:30 UTC).
1413- ** High Performance** : Batch processing optimized for thousands of candles at once.
15- - ** Flexible Timeframes** : Supports ` m ` , ` h ` , ` d ` , ` w ` , ` M ` (e.g. ` 15m ` , ` 4h ` , ` 1d ` , ` 1w ` , ` 1M ` ).
14+ - ** Strict Anchor Alignment** : Ensures candles align with specific sessions (e.g., 4h at 23:00 or 23:30 UTC).
15+ - ** Flexible Timeframes** : Supports ` m ` , ` h ` , ` d ` , ` w ` , ` M ` (fixed) and ` 1W ` (calendar week), ` 1Mc ` (calendar month).
16+ - ** Input validation** : Optional ` validate: true ` to check time, OHLC, duplicates, and order before transform.
1617
1718## Installation
1819
@@ -42,62 +43,85 @@ const data = [
4243]
4344
4445// Convert to 4-hour chart (Default Anchor 23:00 UTC)
45- const h4 = Transform .from (data ).to (' 4h' )
46+ const h4 = Transform .fromCandles (data ).toTimeframe (' 4h' )
4647console .log (h4 ) // Output: [ { time: 1704063600000, open: 1, high: 2, low: 0.5, close: 1.5, ... }, ... ]
4748
4849// Convert to 1-day chart with custom anchor (e.g., 00:00 UTC)
49- const daily = Transform .from (data ).anchor (0 ).to (' 1d' )
50+ const daily = Transform .fromCandles (data ).setAnchorTime (0 ).toTimeframe (' 1d' )
5051
5152// Anchor with hour and minute (e.g., 23:30 UTC)
52- const session = Transform .from (data ).anchor (23 , 30 ).to (' 4h' )
53+ const session = Transform .fromCandles (data ).setAnchorTime (23 , 30 ).toTimeframe (' 4h' )
54+
55+ // Calendar week (Monday start) and calendar month
56+ const weekly = Transform .fromCandles (data ).toTimeframe (' 1W' )
57+ const monthly = Transform .fromCandles (data ).toTimeframe (' 1Mc' )
58+
59+ // Optional validation before transform
60+ const safe = Transform .fromCandles (data ).toTimeframe (' 4h' , { validate: true })
5361```
5462
5563## API Reference
5664
57- ### Transform.from
65+ ### Transform.fromCandles
5866
5967``` typescript
60- Transform .from (data )
68+ Transform .fromCandles (data )
6169```
6270
6371- ` data ` ` <CandleData[]> ` : Array of source OHLC candles
6472- Returns: ` Transform `
6573- Description: Creates a transformation instance for fluent chaining.
6674
67- ### Transform.prototype.anchor
75+ ### Transform.prototype.setAnchorTime
6876
6977``` typescript
70- transform .anchor (hour , minute )
78+ transform .setAnchorTime (hour , minute )
7179```
7280
7381- ` hour ` ` <number> ` : Anchor hour in UTC (0–23). Defaults to ` 23 ` .
7482- ` minute ` ` <number> ` : (Optional) Anchor minute (0–59). Defaults to ` 0 ` .
7583- Returns: ` this `
7684- Description: Sets the anchor time for bucket alignment (e.g. 23:30 UTC).
7785
78- ### Transform.prototype.to
86+ ### Transform.prototype.toTimeframe
7987
8088``` typescript
81- transform .to (timeframe )
89+ transform .toTimeframe (timeframe , options )
8290```
8391
84- - ` timeframe ` ` <string> ` : Target timeframe (e.g. ` '15m' ` , ` '4h' ` , ` '1d' ` , ` '1w' ` , ` '1M' ` ).
92+ - ` timeframe ` ` <string> ` : Target (e.g. ` '4h' ` , ` '1d' ` , ` '1W' ` , ` '1Mc' ` ).
93+ - ` options ` ` <object> ` : (Optional) ` { validate?: boolean } ` — run input validation before transform.
8594- Returns: ` CandleData[] `
8695- Description: Runs the transformation and returns aggregated candles.
8796
97+ ### Transform.prototype.setWeekStartDay
98+
99+ ``` typescript
100+ transform .setWeekStartDay (weekStartDay )
101+ ```
102+
103+ - ` weekStartDay ` ` <number> ` : Week start (0=Sun, 1=Mon, …, 6=Sat). Default ` 1 ` (Monday). Used for ` '1W' ` .
104+ - Returns: ` this `
105+ - Description: Sets week start day for calendar week timeframe.
106+
88107### Transform.execute
89108
90109``` typescript
91- Transform .execute (candles , timeframe , anchorHour , anchorMinute )
110+ Transform .execute (candles , timeframe , anchorHour , anchorMinute , options )
92111```
93112
94113- ` candles ` ` <CandleData[]> ` : Source candle array
95- - ` timeframe ` ` <string> ` : Target timeframe (e.g. ` '4h' ` , ` '1d' ` )
114+ - ` timeframe ` ` <string> ` : Target (e.g. ` '4h' ` , ` '1d' ` , ` '1W' ` , ` '1Mc '` )
96115- ` anchorHour ` ` <number> ` : (Optional) Anchor hour (0–23). Defaults to ` 23 ` .
97116- ` anchorMinute ` ` <number> ` : (Optional) Anchor minute (0–59). Defaults to ` 0 ` .
117+ - ` options ` ` <object> ` : (Optional) ` { weekStartDay?: 0|1|…|6, validate?: boolean } `
98118- Returns: ` CandleData[] `
99119- Description: Runs batch transformation without a fluent instance.
100120
121+ ### Time (constants)
122+
123+ Static readonly: ` msPerMinute ` , ` msPerHour ` , ` msPerDay ` , ` msPerWeek ` , ` msPerMonth ` (milliseconds per unit), ` defaultAnchorOffset ` (23h in ms).
124+
101125### Time.alignTime
102126
103127``` typescript
@@ -111,19 +135,43 @@ Time.alignTime(timestamp, intervalMs, anchorHour, anchorMinute)
111135- Returns: ` number `
112136- Description: Aligns timestamp to the bucket open time on the anchor grid.
113137
138+ ### Time.getBucketStart
139+
140+ ``` typescript
141+ Time .getBucketStart (timestamp , timeframe , anchorHour , anchorMinute , weekStartDay )
142+ ```
143+
144+ - ` timestamp ` ` <number> ` : Input time in ms
145+ - ` timeframe ` ` <string> ` : e.g. ` '4h' ` , ` '1d' ` , ` '1W' ` , ` '1Mc' `
146+ - ` anchorHour ` / ` anchorMinute ` : Used for fixed intervals only
147+ - ` weekStartDay ` ` <number> ` : (Optional) 0–6 for ` '1W' ` . Default ` 1 ` (Monday)
148+ - Returns: ` number `
149+ - Description: Bucket start for any timeframe (fixed or calendar).
150+
114151### Time.parseTimeframe
115152
116153``` typescript
117- Time .parseTimeframe (tf )
154+ Time .parseTimeframe (timeframeStr )
118155```
119156
120- - ` tf ` ` <string> ` : Timeframe string (e.g. ` '15m' ` , ` '4h' ` , ` '1d' ` , ` '1w' ` , ` '1M' ` )
157+ - ` timeframeStr ` ` <string> ` : Fixed timeframe only (e.g. ` '15m' ` , ` '4h' ` , ` '1d' ` , ` '1w' ` , ` '1M' ` ). Use ` Time.getBucketStart ` for ` '1W' ` / ` '1Mc' ` .
121158- Returns: ` number `
122159- Description: Parses timeframe string to duration in milliseconds.
123160
161+ ### Validator.validateCandles
162+
163+ ``` typescript
164+ Validator .validateCandles (candles , options )
165+ ```
166+
167+ - ` candles ` ` <CandleData[]> ` : Array to validate
168+ - ` options ` ` <object> ` : (Optional) ` { allowDuplicates?, allowUnordered?, strictOHLC? } `
169+ - Returns: ` { valid: boolean, errors: string[] } `
170+ - Description: Validates time (finite, non-negative), OHLC, duplicates, and order.
171+
124172## Note
125173
126- - ` 1w ` = 7 days; ` 1M ` = 30-day period (fixed length, not calendar month) .
174+ - ` 1w ` = 7 days (fixed) ; ` 1M ` = 30-day period (fixed). ` 1W ` = calendar week (default Monday); ` 1Mc ` = calendar month (first of month UTC). Validation is opt-in via ` toTimeframe(..., { validate: true }) ` or ` Transform.execute(..., { validate: true }) ` .
127175
128176## License
129177
0 commit comments