New

=> #

The ‘=>’ operator is used in user-defined function declarations and in switch statements.

<identifier>([<parameter_name>[=<default_value>]], ...) =>  <local_block>  <function_result>
#
// single-line function
f1(x, y) => x + y
// multi-line function
f2(x, y) => 
	sum = x + y
	sumChange = ta.change(sum, 10)
	// Function automatically returns the last expression used in it
備考 #

You can learn more about user-defined functions in the User Manual’s pages on Declaring functions and Libraries.

export #

Used in libraries to prefix the declaration of functions that will be available from other scripts importing the library.

#
//@version=5
//@description Library of debugging functions.
library("Debugging_library", overlay = true)
//@function Displays a string as a table cell for debugging purposes.
//@param txt String to display.
//@returns Void.
export print(string txt) => 
	var table t = table.new(position.middle_right, 1, 1)
	table.cell(t, 0, 0, txt, bgcolor = color.yellow)
// Using the function from inside the library to show an example on the published chart.
// This has no impact on scripts using the library.
print("Library Test")
備考 #

Each library must have at least one exported function. Exported functions cannot use variables from the global scope if they are arrays, mutable variables (reassigned with :=), or variables of ‘input’ form. Exported functions cannot use request.* functions, e.g. request.security. Exported functions must explicitly declare the type of their arguments, and all arguments must be used in the function’s body. By default, all arguments of exported functions are of the series form, unless explicitly specified as simple type in the function signature. The @description, @function, @param, and @returns directives are used to automatically generate the library’s release notes, and in the Pine Editor’s tooltips.

関連 #

library, import, simple, series

import #

Used to load an external library into a script and bind its functions to a namespace. The importing script can be an indicator, a strategy, or another library. A library must be published (privately or publicly) before it can be imported.

import {username}/{libraryName}/{libraryVersion} as {alias}
引数 #
username (literal string)User name of the library’s author.
libraryName (literal string)Name of the imported library, which corresponds to the title argument used by the author in his library script.
libraryVersion (literal int)Version number of the imported library.
alias (literal string)Namespace used to refer to the library’s functions. Optional. The default is the libraryName string.
#
//@version=5
indicator("num_methods import")
// Import the first version of the username’s "num_methods" library and assign it to the "m" namespace",
import username/num_methods/1 as m
// Call the “sinh()” function from the imported library
y = m.sinh(3.14)
// Plot value returned by the "sinh()" function",
plot(y)

series #

series is a keyword that can be used in a library’s exported functions to specify the type form required for a function’s arguments. Explicit use of the series keyword is usually unnecessary because all arguments of exported functions are automatically converted to the “series” form by default.

export <functionName>([[series] <type>] <arg1>[ = <default_value>])
#
export smaCustom(series float source, series int length) =>
	sma(source, length)

simple #

simple is a keyword that can be used in a library’s exported functions to specify the type form required for a function’s arguments. By default, all arguments of exported functions are automatically converted into the “series” type form. In some cases, this would make arguments unusable with those of built-in functions that do not support the “series” form. For these cases, the simple keyword can be used instead.

export <functionName>([[simple] <type>] <arg1>[ = <default_value>])
#
export emaWrong(float source, int length) =>
	// By default, both `source` and `length` will expect values of the `series` type form: `series float` for `source`, `series int` for `length`.
	// This function will not compile because `ema()` does not support a "series int" argument for `length`. A "simple int" is required.
	ema(source, length)

export emaRight(float source, simple int length) =>
	// This function requires an argument of "simple int" type for its `length` parameter.
	ema(source, length)

switch #

The switch operator transfers control to one of the several statements, depending on the values of a condition and expressions.

[variable_declaration = ] switch expression  value1 => local_block  value2 => local_block    => default_local_block[variable_declaration = ] switch  boolean_expression1 => local_block  boolean_expression2 => local_block    => default_local_block
戻り値 #

The value of the last expression in the local block of statements that is executed.

#
//@version=5
indicator("Switch using an expression")

string i_maType = input.string("EMA", "MA type", options = ["EMA", "SMA", "RMA", "WMA"])

float ma = switch i_maType
	"EMA" => ta.ema(close, 10)
	"SMA" => ta.sma(close, 10)
	"RMA" => ta.rma(close, 10)
	// Default used when the three first cases do not match.
	=> ta.wma(close, 10)

plot(ma)
//@version=5
strategy("Switch without an expression", overlay = true)

bool longCondition  = ta.crossover( ta.sma(close, 14), ta.sma(close, 28))
bool shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))

switch
	longCondition  => strategy.entry("Long ID", strategy.long)
	shortCondition => strategy.entry("Short ID", strategy.short)
備考 #

Only one of the local_block instances or the default_local_block can be executed. The default_local_block is introduced with the => token alone and is only executed when none of the preceding blocks are executed. If the result of the switch statement is assigned to a variable and a default_local_block is not specified, the statement returns na if no local_block is executed. When assigning the result of the switch statement to a variable, all local_block instances must return the same type of value.

関連 #

if, ?:

while #

The while statement allows the conditional iteration of a local code block.

variable_declaration = while boolean_expression    continue    break    return_expression
#
//@version=5
indicator("")
// This is a simple example of calculating a factorial using a while loop.
int i_n = input.int(10, "Factorial Size", minval=0)
int counter   = i_n
int factorial = 1
while counter > 0
	factorial := factorial * counter
	counter   := counter - 1

plot(factorial)
備考 #

The local code block after the initial while line must be indented with four spaces or a tab. For the while loop to terminate, the boolean expression following while must eventually become false, or a break must be executed.

indicator #

The function sets a number of indicator properties.

indicator(title, shorttitle, overlay, format, precision, scale, max_bars_back, timeframe, timeframe_gaps, explicit_plot_zorder, max_lines_count, max_labels_count, max_boxes_count)  void
引数 #
title (const string)indicator title that would be seen in Indicators widget. Argument IS REQUIRED.
shorttitle (const string)indicator short title that would be seen in the chart legend. Argument is optional.
overlay (const bool)if true the indicator will be added as an overlay for the main series. If false - it would be added on a separate chart pane. Default is false.
format (const string)type of formatting indicator values on the price axis. Possible values are: format.inherit, format.price, format.volume. Default is format.inherit.
precision (const int)number of digits after the floating point for indicator values on the price axis. Must be a non negative integer and not greater than 16. If omitted, using formatting from parent series. If format is format.inherit and this argument is set, then format becomes format.price.
scale (scale_type)インジケーターが割り当てられる価格スケール。可能な値は scale.right, scale.left, scale.none です。値 scale.none は、‘overlay = true’ の設定との組み合わせでのみ適用できます。省略された場合は、メインスケールが使用されます。
max_bars_back (const int)Maximum number of bars available for a indicator for historical reference. This parameter is applied to every built-in or user variable in the script if there is a reference to historical data of a variable in the script code (‘[]’ operator is used). Variable buffer sizes in the Pine Script are typically autodetected. This however is not possible in certain cases which is why the parameter allows a user to manually set the lower bound of this value. NOTE: using of the max_bars_back function instead of the parameter is optimal because it applies to only one variable.
timeframe (const string)custom resolution of the indicator, which defines indicator input and behavior like a indicator body in the security context. If you specify the empty string resolution, it will appear the same as on the chart. Argument is optional.
max_lines_count (const int)表示される直近のライン描画の数。デフォルト値は50で、許可される最大値は500です。
max_labels_count (const int)表示される直近のラベル描画の数。デフォルト値は50で、許可される最大値は500です。
timeframe_gaps (const bool)リクエストされたデータのマージ方法(要求されたデータは自動的にメイン系列のOHLCデータとマージされます)。可能な値は false, true です。true - リクエストされたデータは、ギャップありでマージされます。false -リクエストされたデータは、ギャップなしで連続してマージされ、すべてのギャップは以前の最も近い既存の値で埋められます。デフォルト値は true です。
max_boxes_count (const int)表示する直近のボックス描画の数。デフォルト値は50で、最大許容値は500です。
explicit_plot_zorder (const bool)インジケーターのプロット、塗りつぶし、水平線のレンダリング順を指定します。trueの場合、プロットはインジケータのコード上の順序に基づいて描画され、新しいプロットは前のプロットの上に描画されます。これは plot*() 関数、fill、hline にのみ適用されます。オプション引数。デフォルトは faLse です。
#
indicator(title='MyScriptindicator')
indicator(title="MyScriptindicator", shorttitle="MSS", precision=6, overlay=true)
備考 #

Every script must have one indicator call.
間接的な作用のある関数呼び出しは、resolution パラメーターがどの値でもエラーが発生します。

関連 #

request.security

library #

Declaration statement identifying a script as a library.

library(title, overlay)  void
引数 #
title (const string)The title of the library and its identifier. It cannot contain spaces, special characters or begin with a digit. It is used as the publication’s default title, and to uniquely identify the library in the import statement, when another script uses it. It is also used as the script’s name on the chart.
overlay (const bool)If true, the library will be added over the chart. If false, it will be added in a separate pane. Optional. The default is false.
#
//@version=5
// @description Math library
library("num_methods", overlay = true)
// Calculate "sinh()" from the float parameter `x`
export sinh(float x) =>
	(math.exp(x) - math.exp(-x)) / 2.0
plot(sinh(0))
関連 #

import, export, indicator, strategy

© - 2021 - TradingViewの教科書