July cohort is now open: How to secure your spot:

Can you forecast future price trends?

Forecast future price trends (with projections). VBT can also forecast these price segments into the future!

In a previous newsletter, we used VectorBT PRO (VBT) to detect price patterns. VBT can also forecast these price segments into the future!

Having a high-confidence estimate of where the price of an asset may move in the future can help with risk management and real-time analysis.

Are you ready to learn how?

Let’s go!

Forecast future price trends (with projections)

Imports and set up

Given the self-contained design of VBT, a single import is enough for the analysis.

import vectorbtpro as vbt
vbt.settings.set_theme("dark")

Let’s define a set of variables for our analysis and grab data from Yahoo Finance.

SYMBOL = "BTC-USD"
TIMEFRAME = "1 hour"
START = "one year ago"

LAST_N_BARS = 66
PRED_N_BARS = 12

GIF_FNAME = "projections.gif"
GIF_N_BARS = 72
GIF_FPS = 4
GIF_PAD = 0.01

data = vbt.YFData.pull(SYMBOL, timeframe=TIMEFRAME, start=START)

Find and plot projections

Let’s write a function that analyzes the most recent price trend and employs it as a pattern to identify similar price movements in historical data

def find_patterns(data):
    price = data.hlc3
    pattern = price.values[-LAST_N_BARS:]
    pattern_ranges = price.vbt.find_pattern(
        pattern=pattern,
        rescale_mode="rebase",
        overlap_mode="allow",
        wrapper_kwargs=dict(freq=TIMEFRAME)
    )
    pattern_ranges = pattern_ranges.status_closed
    return pattern_ranges

pattern_ranges = find_patterns(data)

This pattern recognition function will focus exclusively on segments of price history having a comparable percentage change from their respective starting points.

We identified 14 price segments that closely resemble the latest price trend.

def plot_projections(data, pattern_ranges, **kwargs):
    projection_ranges = pattern_ranges.with_delta(
        PRED_N_BARS,
        open=data.open,
        high=data.high,
        low=data.low,
        close=data.close,
    )
    projection_ranges = projection_ranges.status_closed
    return projection_ranges.plot_projections(
        plot_past_period=LAST_N_BARS, 
        **kwargs,
    )

plot_projections(data, pattern_ranges, plot_bands=True).show_png()

The function that extracts the price data immediately after each segment and plots these as extensions of the price trend. The result is a series of price projections.

Forecast future price trends (with projections). VBT can also forecast these price segments into the future!

As we can see, similar price movements have historically branched into many paths.

For a statistically robust forecast, we display the confidence bands around the projections, with 60% of these projections falling between the upper and lower bands

Generate an animated GIF

Lastly, we will generate an animated GIF that iterates through a specified range of bars, applying the procedure to each bar within that range.

def plot_frame(frame_index, **kwargs):
    sub_data = data.loc[:frame_index[-1]]
    pattern_ranges = find_patterns(sub_data)
    if pattern_ranges.count() < 3:
        return None
    return plot_projections(sub_data, pattern_ranges, **kwargs)

vbt.save_animation(
    GIF_FNAME,
    data.index[-GIF_N_BARS:],
    plot_frame,
    plot_projections=False,
    delta=1,
    fps=GIF_FPS,
    writer_kwargs=dict(loop=0),
    yaxis_range=[
        data.low.iloc[-GIF_N_BARS:].min() * (1 - GIF_PAD), 
        data.high.iloc[-GIF_N_BARS:].max() * (1 + GIF_PAD)
    ],
)

The result is an illustrated depiction of the real time price forecasts.

Forecast future price trends (with projections). VBT can also forecast these price segments into the future!
Forecast future price trends (with projections). VBT can also forecast these price segments into the future!

Next steps

There are two action items you can follow to take advantage of this powerful forecasting feature. First, is use it to set limit orders and stop losses during live trading.

The second is to generate a backtest using these price predictions in your strategy.