You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: coding_conventions/README.md
+15-25
Original file line number
Diff line number
Diff line change
@@ -18,10 +18,10 @@ The goal of these Coding Conventions is to present a set of best practices and s
18
18
-[Naming Conventions](#naming-conventions)
19
19
-[Spacing](#spacing)
20
20
-[Line Wrapping](#line-wrapping)
21
-
<br>
22
21
23
-
## Script Structure
24
22
23
+
<br>
24
+
## Script Structure
25
25
The Pine compiler is not very strict on exact positioning of specific statements or compiler directives. While many other arrangements are syntactically correct, these guidelines aim to provide a standard way of ordering elements in scripts:
26
26
27
27
1. The first line of a script should be the `//@version=X` compiler directive, where `X` is replaced by the version of Pine the script is written for. While the compiler defaults to Pine version 1 when no directive is used, scripts written using version 1 of Pine should nonetheless contain the `//@version=1` directive on their first line.
@@ -71,12 +71,11 @@ plot(signal, color = color.orange)
71
71
72
72
**[Back to top](#table-of-contents)**
73
73
74
-
<br>
75
74
75
+
<br>
76
76
## Naming Conventions
77
77
78
78
### Variable Names
79
-
80
79
We recommend using camelCase for variable names. Example: `emaLength`, `obLevel`, `showSignal2`, `aLongVariableName`.
81
80
82
81
For large projects, you may find it useful to use prefixes for a few types of variables, to make them more readily identifiable. The following prefixes can then be used:
@@ -88,22 +87,17 @@ For large projects, you may find it useful to use prefixes for a few types of va
88
87
89
88
90
89
### Function Names
91
-
92
90
For function names, we recommend using a Hungarian-style `f_` prefix in combination with the usual camelCase. The `f_` prefix guarantees disambiguation between user-defined and built-in functions. Example: `f_sinh`, `f_daysInMonth`.
93
91
94
92
### Function Parameter Names
95
-
96
93
Function parameters should be prefixed with the underscore in order to differentiate them from global scope variables. Example:
97
-
98
-
```
94
+
```js
99
95
daysInMonth(_year, _month) =>
100
96
```
101
97
102
98
### Function Dependencies
103
-
104
99
When a function requires global scope variables to perform its calculations, these dependencies should be documented in comments. Dependencies are to be avoided whenever possible, as they jeopardize function portability and make code more difficult to read.
105
-
106
-
```
100
+
```js
107
101
lenMultiplier =input(2, "Length Multiplier")
108
102
109
103
f_getSlowLength(_len) =>
@@ -115,59 +109,55 @@ f_getSlowLength(_len) =>
115
109
```
116
110
117
111
This is a preferable way to write the same function, which eliminates dependencies:
118
-
119
-
```
112
+
```js
120
113
f_getSlowLength(_len, _mult) =>
121
114
_tempLen = _len * _mult
122
115
if _tempLen <20 or _tempLen >30
123
116
_tempLen :=25
124
117
_tempLen
125
118
```
126
119
### Local Scope Variable Names
127
-
128
120
The same underscore prefix used for function parameters should also be used for all local variables. Example:
129
-
```
121
+
```js
130
122
f_getSlowLength(_len) =>
131
123
_tempLen = _len *2
132
124
if _tempLen <20 or _tempLen >30
133
125
_tempLen :=25
134
126
_tempLen
135
127
```
136
-
```
128
+
```js
137
129
if something
138
130
_myLocalVar = something
139
131
```
140
-
```
132
+
```js
141
133
for _i =0 to 100
142
134
_myLocalVar = something[_i]
143
135
```
144
136
145
137
**[Back to top](#table-of-contents)**
146
138
147
-
<br>
148
139
140
+
<br>
149
141
## Spacing
150
142
151
143
A space should be used on both sides of all operators, whether they be assignment, arithmetic (binary or unary) or logical. A space should also be used after commas. Example:
152
144
153
-
```
145
+
```js
154
146
a = close > open ?1:-1
155
147
var newLen =2
156
148
newLen :=min(20, newlen +1)
157
149
a =- b
158
150
c = d > e ? d - e : d
159
151
index = bar_index %2==0?1:2
160
152
plot(series, color =color.red)
161
-
162
153
```
163
154
164
-
<br>
165
155
156
+
<br>
166
157
## Line Wrapping
167
158
168
159
When lines need to be continued on the next, use two spaces to indent each continuation line. Example:
169
-
170
-
```
160
+
```js
171
161
plot(
172
162
series = close,
173
163
title ="Close",
@@ -177,8 +167,7 @@ plot(
177
167
```
178
168
179
169
Tabs may be used to line up elements in order to increase readability.
180
-
181
-
```
170
+
```js
182
171
plot(
183
172
series = close,
184
173
title ="Close",
@@ -187,6 +176,7 @@ plot(
187
176
)
188
177
```
189
178
179
+
<br>
190
180
## Example Scripts
191
181
192
182
Some authors use the Coding Conventions systematically:
0 commit comments