-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
{{ config(severity="warn") }} | ||
|
||
with trade_counts as ( | ||
select | ||
date(ledger_closed_at) as close_date | ||
, count(*) as trade_count | ||
from {{ ref('stg_history_trades') }} | ||
where date(ledger_closed_at) >= date_sub(current_date(), interval 90 day) | ||
and date(ledger_closed_at) < current_date() | ||
group by close_date | ||
with | ||
trade_counts as ( | ||
select | ||
date(ledger_closed_at) as close_date | ||
, count(*) as trade_count | ||
from {{ ref('stg_history_trades') }} | ||
where | ||
date(ledger_closed_at) >= date_sub(current_date(), interval 90 day) | ||
and date(ledger_closed_at) < current_date() | ||
group by close_date | ||
) | ||
|
||
, bounds as ( | ||
select | ||
(avg(trade_count) - (3 * stddev(trade_count))) as lower_bound | ||
, (avg(trade_count) + (3 * stddev(trade_count))) as upper_bound | ||
from trade_counts | ||
, bounds as ( | ||
select | ||
(avg(trade_count) - (3 * stddev(trade_count))) as lower_bound | ||
, (avg(trade_count) + (3 * stddev(trade_count))) as upper_bound | ||
from trade_counts | ||
) | ||
|
||
select | ||
close_date | ||
, trade_count | ||
from trade_counts, bounds | ||
where close_date = date_sub(current_date(), interval 1 day) | ||
where | ||
close_date = date_sub(current_date(), interval 1 day) | ||
and trade_count >= upper_bound | ||
or trade_count <= lower_bound | ||
or trade_count <= lower_bound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
{{ config(severity="warn") }} | ||
|
||
with base_trades as ( | ||
select | ||
date(ledger_closed_at) as close_date | ||
, sum(buying_amount) as amount | ||
from {{ ref('stg_history_trades') }} | ||
where date(ledger_closed_at) >= date_sub(current_date(), interval 90 day) | ||
and date(ledger_closed_at) <= current_date() | ||
group by close_date | ||
) | ||
with | ||
base_trades as ( | ||
select | ||
date(ledger_closed_at) as close_date | ||
, sum(buying_amount) as amount | ||
from {{ ref('stg_history_trades') }} | ||
where | ||
date(ledger_closed_at) >= date_sub(current_date(), interval 90 day) | ||
and date(ledger_closed_at) <= current_date() | ||
group by close_date | ||
) | ||
|
||
, counter_trades as ( | ||
select | ||
date(ledger_closed_at) as close_date | ||
, sum(selling_amount) as amount | ||
from {{ ref('stg_history_trades') }} | ||
where date(ledger_closed_at) >= date_sub(current_date(), interval 90 day) | ||
and date(ledger_closed_at) < current_date() | ||
group by close_date | ||
) | ||
, counter_trades as ( | ||
select | ||
date(ledger_closed_at) as close_date | ||
, sum(selling_amount) as amount | ||
from {{ ref('stg_history_trades') }} | ||
where | ||
date(ledger_closed_at) >= date_sub(current_date(), interval 90 day) | ||
and date(ledger_closed_at) < current_date() | ||
group by close_date | ||
) | ||
|
||
, data_table as ( | ||
select | ||
close_date | ||
, sum(amount) as amount | ||
from ( | ||
select * | ||
from base_trades | ||
union all | ||
select * | ||
from counter_trades | ||
, data_table as ( | ||
select | ||
close_date | ||
, sum(amount) as amount | ||
from ( | ||
select * | ||
from base_trades | ||
union all | ||
select * | ||
from counter_trades | ||
) | ||
group by close_date | ||
) | ||
group by close_date | ||
) | ||
|
||
, bounds as ( | ||
select | ||
(avg(amount) - (3 * stddev(amount))) as lower_bound | ||
, (avg(amount) + (3 * stddev(amount))) as upper_bound | ||
from data_table | ||
) | ||
, bounds as ( | ||
select | ||
(avg(amount) - (3 * stddev(amount))) as lower_bound | ||
, (avg(amount) + (3 * stddev(amount))) as upper_bound | ||
from data_table | ||
) | ||
|
||
select | ||
close_date | ||
, amount | ||
from data_table, bounds | ||
where close_date = date_sub(current_date(), interval 1 day) | ||
where | ||
close_date = date_sub(current_date(), interval 1 day) | ||
and amount >= upper_bound | ||
or amount <= lower_bound | ||
or amount <= lower_bound |