Skip to content

Usage

First the imports

import torch
from torchtrader.ta.ma import MovingAverage

Then lets JIT it and create the window size of the Moving Average

window_size = 3     # Parameter of MovingAverage
ma = torch.jit.script(MovingAverage(window_size))
data = torch.tensor(3)

Now lets call the function with the x value which is to be interpreted as the input data, the actual value of the series.

print(ma(data))
print(ma(data))
print(ma(data))

And the output should look as follows.

tensor([1.])
tensor([2.])
tensor([3.])
As it's been inputting a value of 3 for a window size of 3, the computed output should be increasing or decreasing in n steps until it reaches the input value. Note that the steps number is window size.

MovingAverage

Bases: torch.nn.Module

Computes the moving average for a sequence of values.

Parameters:

Name Type Description Default
window_size int

The number of values to use in the moving average calculation.

3

__init__(window_size=3)

Initialize a new instance of MovingAverage.

Parameters:

Name Type Description Default
window_size int

The number of values to use in the moving average calculation.

3

forward(value, window_size)

Compute the moving average with a new value and return the result.

Parameters:

Name Type Description Default
value Tensor

The new value to add to the moving average.

required
window_size int

The number of values to use in the moving average calculation.

required

Returns:

Type Description
Tensor

The current value of the moving average.

get()

Get the current moving average.

Returns:

Type Description
Tensor

The current value of the moving average.

update(value)

Update the moving average with a new value.

Parameters:

Name Type Description Default
value Tensor

The new value to add to the moving average.

required