-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathta_price_transforms.sql
More file actions
40 lines (29 loc) · 1.29 KB
/
Copy pathta_price_transforms.sql
File metadata and controls
40 lines (29 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{#
Price-transform expressions (issue #109). These are plain column
expressions — no window functions — so they can be used at any stage.
#}
{% macro ta_typical_price(high='high', low='low', close='close') %}
(({{ high }} + {{ low }} + {{ close }}) / 3)
{% endmacro %}
{% macro ta_median_price(high='high', low='low') %}
(({{ high }} + {{ low }}) / 2)
{% endmacro %}
{% macro ta_weighted_close(high='high', low='low', close='close') %}
(({{ high }} + {{ low }} + 2 * {{ close }}) / 4)
{% endmacro %}
{% macro ta_true_range(high='high', low='low', prev_close='prev_close') %}
{#- prev_close is expected to be a precomputed LAG(close) column.
Falls back to plain high-low on the first bar (prev_close NULL),
matching TA-Lib behavior. -#}
GREATEST(
{{ high }} - {{ low }},
COALESCE(ABS({{ high }} - {{ prev_close }}), {{ high }} - {{ low }}),
COALESCE(ABS({{ low }} - {{ prev_close }}), {{ high }} - {{ low }})
)
{% endmacro %}
{% macro ta_daily_return(close='close', prev_close='prev_close') %}
SAFE_DIVIDE({{ close }} - {{ prev_close }}, NULLIF({{ prev_close }}, 0))
{% endmacro %}
{% macro ta_log_return(close='close', prev_close='prev_close') %}
SAFE.LN(SAFE_DIVIDE({{ close }}, NULLIF({{ prev_close }}, 0)))
{% endmacro %}