Declares a function that is called with dot syntax on a value of its first parameter's type, and may be overloaded per type.
method name(Type this, ...) => block defines a function whose first parameter has an explicit type. You call it as value.name(...), so a method on float reads like a built-in: close.clamp(low, high). The first parameter is the receiver and is passed automatically.
Methods with the same name can exist for different receiver types, and the one matching the receiver is called. That is how you add behaviour to your own types and also to built-in ones such as float, string or array<float>. The first parameter, the receiver, must have an explicit type, and a method can be exported from a library.
Syntax
[export] method <functionName>(<paramType> <paramName> [= <defaultValue>], …) => <functionBlock>Remarks
- On Sidechair: methods are supported, including those defined on your own types.
Example
//@version=6
indicator("Method", overlay = true)
method clamp(float this, float lo, float hi) =>
math.min(math.max(this, lo), hi)
upper = ta.highest(close, 20)
lower = ta.lowest(close, 20)
plot(close.clamp(lower, upper), "Clamped", color.blue)
plot(ta.sma(close, 20).clamp(lower, upper), "SMA clamped", color.orange)See also
Runs on Sidechair