millionchart
فارسی

Modules

A module exports names, and other scripts import it by name and version.

A module is a script whose job is to be imported. It draws nothing itself. This page shows a module and a script that imports it.

Module: running statistics#

A module that exports a record type and two functions.

Cuprel
// A module: a running mean kept in a record. Other scripts import it by name and version.
module Stats
    edition = 1

export type Stats
    n: int = 0
    total: float = 0

export fn Stats.add(self: Stats, x: float)
    self.n += 1
    self.total += x

export fn Stats.mean(self: Stats): float = if self.n > 0 then self.total / self.n else na

Importing a module#

An indicator that imports the module and keeps a record in a state.

Cuprel
// Using the module: the running mean of every close so far, drawn next to the close.
indicator "Running mean"
    edition = 1
    display = price

import Me/Stats@1 as mu
state stats = mu.Stats(n = 0)
stats.add(close)
plot(stats.mean(), name = "Mean of every close so far", color = color.orange)
plot(close, name = "Close", color = color.gray)