Ever felt like your sales data is a wild sea monster – all over the place and difficult to grasp? Fear not, intrepid analyst! In our last adventure, we slayed the trend obscurity beast with the trusty Simple Moving Average (SMA). But what if we crave more? What if we yearn to understand not just the direction of the current, but also the very nature of the waves – their size, their fury? Buckle up, mateys, because today we unveil a powerful new weapon in our arsenal: Bollinger Bands!
Think of Bollinger Bands as an extension of your data taming toolkit. They add a layer of volatility analysis, giving you insights into whether your sales are currently high or low compared to their recent history. It's like having a built-in "normalcy meter" for your data, offering a dynamic perspective that goes beyond basic trend lines.
Decoding the Bollinger Band Magic
Just like SMAs, crafting Bollinger Bands in BigQuery is a breeze. We'll follow a similar treasure map:
Gather the Booty (Daily Sales): Aggregate your sales data by day, just like we did with SMAs.
Chart a Course (Simple Moving Average): Remember the trusty SMA from our previous voyage? We'll use it again here as a baseline, typically using a 20-day window (think of it as a 20-day average sales anchor).
Measure the Ocean's Fury (Standard Deviation): Now, things get exciting! We'll calculate the standard deviation of sales for the past 20 days. This unveils the typical fluctuation of your sales around the average.
Unfurl the Bollinger Bands: Imagine the SMA as a center line. We'll use the standard deviation to dynamically create upper and lower bands around this center. The further the standard deviation, the wider the bands, indicating higher potential volatility. Conversely, narrow bands suggest calmer seas.
Here's a special treasure map (SQL code) to help you conjure the Bollinger Bands:
WITH
daily_sales AS (
SELECT DATE(created_at) AS date
,SUM(net_revenue) AS sales
FROM `project_id.schema_id.orders`
GROUP BY date),
simple_moving_average AS (
SELECT date
,sales
,AVG(sales) OVER (ORDER BY date ROWS BETWEEN 20 PRECEDING AND CURRENT ROW) AS sales_sma_20
FROM daily_sales),
standard_deviation AS (
SELECT *
,STDDEV(sales) OVER (ORDER BY date ROWS BETWEEN 20 PRECEDING AND CURRENT ROW) AS sales_stddev_20
FROM simple_moving_average),
bollinger_bands AS (
SELECT * EXCEPT(sales_stddev_20)
,sales_sma_20 + (2 * sales_stddev_20) AS sales_sma_upper_20
,sales_sma_20 - (2 * sales_stddev_20) AS sales_sma_lower_20
FROM standard_deviation)
SELECT *
FROM bollinger_bands
ORDER BY date
Explanation:
This code calculates the magic behind the Bollinger Bands. It builds upon the SMA we created earlier and then:
Calculates the standard deviation of sales for the past 20 days.
Uses the SMA and standard deviation to define the upper and lower bands. A common approach is to add/subtract twice the standard deviation from the SMA to create these bands.
Setting Sail with Bollinger Bands
By plotting your sales data alongside the Bollinger Bands, you gain a powerful tool to assess not just trends, but also volatility. Imagine the 20-day SMA you used for trend analysis acting as a middle ground. Bollinger Bands add another layer by dynamically adjusting upper and lower bands based on the standard deviation of sales over that same period.
Wider Bands, Wavier Seas: When the Bollinger Bands expand, it suggests a period of higher volatility. Prices may fluctuate more dramatically, so be prepared for potential swings.
Narrow Bands, Calmer Waters: Conversely, narrow bands indicate lower volatility. This could signal a more predictable market, potentially offering better timing for buying or selling decisions.
Reaching for the Edges: If your sales consistently push above the upper Bollinger Band, it might hint at a potential breakout from a trading range. The opposite (dipping below the lower band) could suggest a breakdown.
Remember, Bollinger Bands are a valuable tool for understanding volatility, but they shouldn't be used in isolation. They work best when combined with other technical indicators to form a well-rounded picture of the market.
So, the next time you're navigating the data seas, remember the power of Bollinger Bands! With them by your side, you can transform that data kraken into a more manageable creature, revealing not just trends, but also the underlying rhythm of the market.