!=
Not Equal. Suitable for any type of expression.
Return value
boolean or series
%
Modulo(remainder after division). Suitable for numeric expression.
Return value
integer or float or series
*
Multiplication. Suitable for numeric expression.
Return value
integer or float or series
+
Addition or unary plus operator。Suitable for numeric expression or string。
Return value
Used to concatenation operator for string.
For numeric operation, if one of the type is a series, then it will return series, followed by if one of the type is float then it will rturn float, and if both type is integer it will result in integer.
Can be used as unary operation.
Note
You can use arithmetic operators with numbers and series. In this case, all item in series will be run the operation.
-
Substraction or unary minus operator. Suitable for numeric expression.
Return value
integer or float or series
/
Division。Suitable for numeric expression.
Return value
integer or float or series
**
Exponent operator。Suitable for numeric expression.
Return value
integer or float or series
%=
Modulo assignment(save remainder after division to a variable). Suitable for numeric expression.
Equilavent to
Note
expr1 must be defined before being assigned.
*=
Mulitiplication assignment. Suitable for numeric expression.
Equilavent to
Note
expr1 must be defined before being assigned.
+=
Addition assignment. Suitable for numeric expression.
Equilavent to
Note
expr1 must be defined before being assigned.
-=
Subtraction assignment. Suitable for numeric expression.
Equilavent to
Note
expr1 must be defined before being assigned.
/=
Division assignment. Suitable for numeric expression.
Equilavent to
Note
expr1 must be defined before being assigned.
<
Less Than. Suitable for numeric expression.
Return value
integer or float or series
>
More Than. Suitable for numeric expression.
Return value
integer or float or series
<=
Less Than or Equal To. Suitable for numeric expression.
Return value
integer or float or series
>=
Greater Than or Equal To。Suitable for numeric expression.
Return value
integer or float or series
==
Equal. Suitable for numeric expression.
Return value
integer or float or series
?:
Conditional ternary operator.
Not recommended
result = close > open ? highest(close,10) : lowest(close,10)
Recommended way
hi= highest(close,10)
lo = lowest(close,10)
result = close > open ? hi : lo
Or
study(title="Demo")
myInput = input(2,type="int")
result = myInput == 2 ? highest(close,10) : lowest(close,10)
value
if expr1 is evaluated true,then expr2,otherwise expr3.
[]
Tuple member reference.
def fun(i,j):
return i,j
(a,b) = fun(1,2)
r = fun(1,2)
(c, d) = r
e = r[0]
f = r[1]
Return value
integer or float or series
and
Logical Conjunction. Suitable for boolean expression。
Return value
boolean or series
or
Logical Disjunction。Suitable for boolean expression。
Return value
boolean or series
not
Negation。Suitable for boolean expression。
Return value
boolean or series
for
For statement allows repeated execution of some instructions. When the loop is not cancelled by break, it will execute the following else statement block
sum = 0
for i in range(0,10,2):
sum += i
else:
sum = 20
if
The if statement define a block of statement to be executed when the expression condition is met.
sum = 0
if close >open:
sum+=1
elif close == open:
sum += 2
else :
sum+= 3
Recommended way to use when construct with ema, sma etc.
sum = 0
ema1 = ema(close,2)
ema2 = ema(close,9)
ema3 = ema(close,19)
if close >open:
sum+=ema1
elif close == open:
sum += ema2
else :
sum+= ema3
Not recommended
sum = 0
if close >open:
sum+=ema(close,2)
elif close == open:
sum += ema(close,9)
else :
sum+= ema(close,19)
def
def to define function
def my_fun(i,j=open):
return i,j
(x,y) = my_fun(close)
result = my_fun(close,open)
(a,b) = result
m = result[0]
n = result[1]
print(a)
print(m)
open
Current opening price
type
float
high
Current high price
type
float
low
Current low price
type
float
close
Current closing price
type
float
time
Current time
type
float
volume
Current volume
type
float
plot
Plot a series of data on the chart
plot(series, title, color, linewidth, style) → plot
plot(high+low, title="Title", color="#00ffaa", linewidth=2, style="area",histbase=0,offset=-10)
p1 = plot(open)
p2 = plot(close)
Return value
A plot object, that can be used in fill
Parameters
series (series)Data series to be drawn. Required.
title (const string)Title.
color (color)Color of drawn line. You can use 'color = "red"' or 'color ="#ff001a"' or complex expression like 'color = close >= open ? "green" : "red"'. Optional.
linewidth (input integer)Width of the drawn line, use a vlua from 1 to 4. Default value is 1. Not applicable to every style.
style (input integer) Drawing type. Possible value:"line" "histogram","cross","area". Default value is "line".
histbase (input float)When rendering the drawing using the "area" style,this specifies the price axis’ starting point for the columns。 Default value is 0.0。
offset (integer)Specifies if the K-lines should shift to the left (with negative values) or to the right (with positive values). Default value is 0。
fill
Plot a series of data on the chart
fill(plot1, plot2, color, title) → void
p1 = plot(open)
p2 = plot(close)
fill(p1, p2, color=color("green",50))
Return value
Fills background between two plots with a given color.
Parameters
plot1 (plot)First plot series to be drawn。 Required.
plot2 (plot)Second plot series to be drawn. Required.
color (color)Color of drawn line. You can use 'color = "red"' or 'color ="#ff001a"' or complex expression like 'color = close >= open ? "green" : "red"'. Optional.
title (const string)Title of the filled object created. Optional.
timestamp
The timestamp function returns the specified date and time in UNIX time.
timestamp(year, month, day, hour, minute, second) → integer
Example
show = timestamp(2016, 01, 19, 9, 30) > time
plot(time>timestamp(2020, 7, 19, 9, 30, 15)?2:1)
Return value
Unix时间。
Parameter
year (integer) Year
month (integer) Month
day (integer) Day
hour (integer) Hour
minute (integer) Minute
second (integer) (Optional)Second. Default is 0
change
The difference between the current value and the previous value, x - x[y].
change(source, length) → series[float]
Return value
Series differences
Parameter
source (series)
length (integer) Offset from the current K-line to the previous K-line
rma
The moving average used in RSI. It is an exponentially weighted moving average line, alpha's weighted value = 1 / length.
rma(source, length) → series[float]
plot(rma(close, 15))
def my_rma(src, length):
alpha = length
sum = 0.0
sum = na(prev(sum,1)) ? sma(src, length) : (src + (alpha - 1) * nz(prev(sum,1))) / alpha
return sum
plot(my_rma(close, 15))
Return value
Exponential moving average of x = 1 / y.
Parameters
source (series)Data series
length (integer) Number of K-lines (length).
prev
To query previous source series data, length is number of previous K-lines.
prev(source, length) → series[float]
a = 0.0
a = prev(a,1)
if high == low
a = low
Return value
series of values
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
lowest
Lowest value of a given series
lowest(source, length) → series[float]
Return value
Lowest value
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
highest
Highest value of a given series
highest(source, length) → series[float]
Return value
Highest value.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
mom
Momentum of price x and price x of a given series. This is just the difference between x - x[y].
mom(source, length) → series[float]
Return value
Momentum of price x and price x of a given series.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
cci
CCI (Commodity Path Index) is calculated by dividing the difference between the typical price and its simple moving average by the mean deviation. The index is scaled by an inverse factor of 0.015, to provide more readable value.
cci(source, length) → series[float]
Return value
x commdity path index of the past y line
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
dev
Deviation between series and sma
dev(source, length) → series[float]
plot(dev(close, 10))
def my_dev(source, length):
mean = sma(source, length)
sum = 0.0
for i in range(0, length - 1):
val = prev(source,i)
sum = sum + abs(val - mean)
dev = sum/length
return dev
plot(my_dev(close, 10))
Return value
Deviation of y and x
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
lowestbars
The offset of the lowest value from a given number of bars.
lowestbars(source, length) → series[float]
plot(lowestbars(close, 15))
Return value
Offset to the lowest k-line.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
highestbars
The offset of the highest value from a given number of bars.
highestbars(source, length) → series[float]
plot(highestbars(close, 15))
Return value
Offset to the highest k-line.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
rising
Test if x series rise along y axis.
rising(source, length) → series[float]
Return value
If the current x is greater than any previous x on the past y line, return true, otherwise return false.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
falling
Test if x series fall along y axis.
falling(source, length) → series[float]
Return value
If the current x is less than any previous x on the past y line, return true, otherwise return false.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
variance
Variance is an expectation of a series of mean deviations(sma), and informally measures the degree to which a set of numbers is evenly distributed
variance(source, length) → series[float]
plot(variance(close, 15))
def my_variance(x, len):
sm = sma(x, len)
return sma(x*x,len) - sm*sm
plot(my_variance(close, 15))
Return value
The x variance of the past y line.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
vwma
The vwma function returns volume weighted moving average. It's the same as: sma(x * volume, y) / sma(volume, y)
vwma(source, length) → series[float]
plot(vwma(close, 15))
def my_vwma(x, y):
return sma(x * volume, y) / sma(volume, y)
plot(my_vwma(close, 15))
Return value
The x volume weighted moving average of the past y line.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
sma
The sma function returns the simple moving average, which is the last y value of x, divided by y.
sma(source, length) → series[float]
plot(sma(close, 15))
def my_sma(x, y):
sum = 0.0
for i in range(0, y - 1):
sum = sum + prev(x,i) / y
return sum
plot(my_sma(close, 15))
Return value
The simple moving average of x for the past y line.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
ema
The ema function returns the exponentially weighted moving average. The weighting factor in ema decreased exponentially. 它It is calculated by the formula: EMA = alpha * x + (1-alpha) * EMA[1], where alpha = 2 / (y + 1)
ema(source, length) → series[float]
plot(ema(close, 15))
def my_ema(src, length):
alpha = 2 / (length + 1)
sum = 0.0
sum = na(prev(sum,1)) ? sma(src, length) : alpha * src + (1 - alpha) * nz(prev(sum,1))
return sum
plot(my_ema(close,15))
Return value
Exponential moving average of x where alpha = 2 / (y + 1)
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
wma
The wma function will get back the weighted moving average of x on the y line. In wma, the weighting factor is reduced in the arithmetic process.
wma(source, length) → series[float]
plot(wma(close, 15))
def my_wma(x, y):
norm = 0.0
sum = 0.0
for i in range(0, y - 1):
weight = (y - i) * y
norm = norm + weight
sum = sum + prev(x,i) * weight
return sum / norm
plot(my_wma(close, 15))
Return value
The x-weighted moving average of the past y line.
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
rsi
Relative strength index. It is calculated based on the upward and downward changes of x's rma.
rsi(source, length) → series[float]
plot(rsi(close, 7))
def my_rsi(x, y):
u = max(x - prev(x,1), 0)
d = max(prev(x,1) - x, 0)
rs = rma(u, y) / rma(d, y)
res = 100 - 100 / (1 + rs)
return res
plot(my_rsi(close, 7))
Return value
Relative strength index (RSI)
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
stdev
Standard deviation
stdev(source, length) → series[float]
plot(stdev(close, 5))
def isZero(val, eps):
return abs(val) <= eps
def SUM(fst, snd):
EPS = 0.0000000001
res = fst + snd
result = 0
if isZero(res, EPS):
result = 0
else:
if not isZero(res, 0.00001):
result = res
else:
result = 15
return result
def my_stdev(src, length):
avg = sma(src, length)
sumOfSquareDeviations = 0.0
for i in range(0, length - 1):
sum = SUM(prev(src,i), -avg)
sumOfSquareDeviations = sumOfSquareDeviations + sum * sum
stdev = sqrt(sumOfSquareDeviations / length)
return stdev
plot(my_stdev(close, 5))
Return value
Standard deviation
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
sum
The sum function returns the sliding sum of last y values of x.
sum(source, length) → series[float]
Return value
Sum of x of the past y line,
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
hma
The hma function returns the Hull Moving Average
hma(source, length) → series[float]
src = input(defval=close, type="source", title="Source")
length = input(defval=9, type="int", title="Length")
hmaBuildIn = hma(src, length)
plot(hmaBuildIn, title="Hull MA", color="#674EA7")
Return value
return the Hull Moving Average given a "length" from the "source"
Parameters
source (series)Data series
length (integer) Number of K-lines (length)
na
Test a value if it is a NaN
Return value
If x is not a valid number, it is true (x is NaN), otherwise it is false.
nz
Replace NaN values with zeros (or specified numbers) in the series.
Return value
Two parameter versions: if it is a valid (non-NaN) number, then return x, otherwise y
One parameter version: if it is a valid (non-NaN) number, x is returned, otherwise 0
Parameters
x (series) Data series
y (float)Value to replace NaN
color
Specify the transparency of a given color.
color(color, transp) → color
plot(close-open,color=color("red",60))
Return value
Colors with specific transparency
Parameters
color (color)
transp (integer) Values are from 0 (opaque) to 100 (not visible)
fixnan
For a given series, replace the NaN value with the previous non-NaN value.
Return value
series without NaN gap
Parameters
x (series)
abs
If x>=0, the absolute value of x is x, otherwise it is -x.
Return value
Absolute of x
acos
The acos function returns the arccosine of a number (expressed in radians), such as cos(acos(y)) = y in the y range [-1, 1].
Return value
Arccosine value。If y is out of the range [-1,1], the return angle is within the range of [0, Pi] or NaN.
asin
If the asin function returns the arcsine of a number (in radians), sine (asin(y)) = y is in the range of y [-1, 1].
Return value
Arcsine value. If y is out of the range [-1,1], the return angle is within the range of [-Pi/2, Pi/2] or na.
atan
The atan function returns the arctangent of a number (expressed in radians), tan(atan(y)) = any y in y.
Return value
Arctangent value; The return angle is within the range of [-Pi / 2, Pi / 2].
cos
The cos function returns the trigonometric cosine of the angle.
Return value
The trigonometric cosine of an angle.
cum
The cumulative (total) sum of x. In other words, this is the sum of all the elements of x.
Return value
Sum of a series
exp
The exp function of x is e^x, where x is the parameter and e is the Euler number.
result = exp(100)
print(result)
Return value
A number representing e^x
sqrt
Square root of any x >= 0 is the unique y >= 0 such that y^2 = x
result = sqrt(100)
print(result)
Return value
Square root of x
floor
returns closest integer value which is less than or equal to specified expression
result = ceil(100.2)
print(result)
Return value
A number representing the largest integer less than or equal to the specified number.
ceil
Always rounds a number up to the next largest integer
result = ceil(100.2)
print(result)
Return value
The smallest integer greater than or equal to the given number.
round
The number rounded to the nearest integer.
result = round(100.2)
print(result)
Return value
The value of the given number rounded to the nearest integer
log
Natural logarithm of x>0 is unique "y", such as e ^ y = x
result = log(100)
print(result)
Return value
The natural logarithm of x.
max
Return the largest of multiple values
result = max(close,open)
plot(result)
Return value
The largest of multiple values.
min
Return the smallest of multiple values
result = min(close,open)
plot(result)
Return value
The smallest of multiple values.
avg
Calculate the average of all series (corresponding elements).
avg(x1, x2, a, b) -> float
result = avg(close,open)
plot(result)
Return value
Average
sign
If x is zero, the sign of x (signature function) is 0, if x is greater than zero, it is 1.0, and if x is less than zero, it is -1.0.
Return value
Parameter flag
dmi
The dmi function returns the trend index DMI.
dmi(diLength, adxSmoothing) → (series[float], series[float], series[float])
Return value
Three tuples of DMI series: rising index (+DI), falling index (-DI) and average trend index (ADX).
Parameters
diLength (integer)DI Period。
adxSmoothing (integer)ADX smoothing period
pivothigh
This function returns the price of the pivot high. If there is no pivot high, "NaN" is returned.
pivothigh(source, leftbars, rightbars) → series[float]
leftBars = input(2,type="int")
rightBars=input(2,type="int")
ph = pivothigh(high,leftBars, rightBars)
plot(ph, style="cross", linewidth=3, color= "red", offset=-rightBars)
Return value
The price at pivot high or'NaN'.
Parameters
source (series)Data series
leftbars (series)Left bars
rightbars (series)Right bars
pivotlow
This function returns the price of the pivot low. If there is no pivot low, return "NaN".
pivotlow(source, leftbars, rightbars) → series[float]
leftBars = input(2,type="int")
rightBars=input(2,type="int")
ph = pivotlow(high,leftBars, rightBars)
plot(ph, style="cross", linewidth=3, color= "red", offset=-rightBars)
Return value
The price of the pivot low or'NaN'.
Parameters
source (series)Data series
leftbars (series)Left bars
rightbars (series)Right bars
macd
MACD(Moving Average Convergence Divergence)。 It should reveal changes in the strength, direction, momentum, and duration of stock price trends.
macd(source, fastlen, slowlen, siglen) → (series[float], series[float], series[float])
(macdLine, signalLine, histLine) = macd(close, 12, 26, 9)
plot(macdLine, color="blue")
plot(signalLine, color="orange")
plot(histLine, color=histLine>0?"green":"red", style="histogram")
Return value
Three tuples of MACD series: MACD line, signal line and histogram line.
Parameters
source (series)Data series
lfastlen (integer)Fast line
slowlen (integer) Slow length
siglen (integer)Signal length
stoch
Stochastic indicators. Calculation equation: 100 * (close - lowest(low, length)) / (highest(high, length) - lowest(low, length))
stoch(source, high, low, length) → series[float]
plot(stoch(close, high, low, 10))
Return value
Stochastic.
Parameters
source (series)Source series
lfastlen (integer)High series
slowlen (integer) Low series
siglen (integer)Length (number of K lines)
valuewhen
When the most recent nth condition is true, return the source
valuewhen(condition, source, occurrence) → series[float]
slow = sma(close, 7)
fast = sma(close, 14)
valuewhen(cross(slow, fast), close, 1)
Return value
Source value when the situation is true
linreg
Linear regression curve. The curve that best fits the price within the time interval specified by the user. Use the least square method to calculate. The result of this function is calculated using the following formula: linreg = intercept + slope *(length-1-offset), where length is the second parameter, offset is the thrid parameter, intercept and slope are the values calculated by the source series least square method (first parameter)。
linreg(source, length, offset) → series[float]
plot(linreg(open , 10, 10))
Return value
Linear regression curve
Parameters
source (series)Source series
length (integer)Length
offset (integer) Offset
sar
Parabolic SAR (parabolic stop and reversal) is a method designed by J. Welles Wilder, Jr. To find a potential reversal of the price direction of the trading market.
sar(start, inc, max) → series[float]
plot(sar(0.2, 0.2, .2), style="cross", linewidth=3)
Return value
Parabolic SAR
Parameters
start (float)Start
inc (float)Increment
max (float) Max
atr
The function ATR (Average True Range) returns the RMA of the true range. The true fluctuation range is max(high-low, abs(high-close[1]), abs(low-close[1]))
atr(length) → series[float]
plot(atr(14))
def my_atr(length):
trueRange = na(prev(high,1))? high-low : max(max(high - low, abs(high - prev(close,1))), abs(low - prev(close,1)))
return rma(trueRange, length)
plot(my_atr(14))
Return value
Average true range (ATR)
Parameters
length (integer)Length
alma
Arnaud Legoux Moving Average. It uses Gaussian distribution as the weight of the moving average.
alma(series, length, offset, sigma) → series[float]
plot(alma(close, 9, 0.85, 6))
def my_alma(series, windowsize, offset, sigma):
m = floor(offset * (windowsize - 1))
s = windowsize / sigma
norm = 0.0
sum = 0.0
for i in range(0,windowsize - 1):
weight = exp(-1 * ((i - m)** 2) / (2 * (s**2)))
norm = norm + weight
sum = sum + prev(series,windowsize - i - 1) * weight
return sum / norm
plot(my_alma(close, 9, 0.85, 6))
Return value
Arnaud Legoux Moving Average
Parameters
series (series)Data series
length (integer)Number of K-lines (length)
offset (float)Control the trade-off between smoothness (closer to 1) and responsiveness (closer to 0)。
sigma (float) Change the smoothness of ALMA. The larger the Sigma, the smoother the ALMA.
dayofweek
Return value
Provides the day of the week (exchange time zone) in UNIX time.
Parameters
time (series) Unix time in milliseconds.
dayofmonth
dayofmonth(time) → series
Return value
Provides the day of the month (exchange time zone) in UNIX time.
Parameters
time (series) Unix time in milliseconds.
cross
cross(x, y) → series[bool]
ema1 = ema(close,20)
ema2 = ema(close, 10)
plot(cross(ema1,ema2)?-1:1, color="red",style="histogram")
plot(sign(ema2- ema1))
Return value
If the two series cross each other, it is 1, otherwise it is 0.
Parameters
x (series)
y (series)
crossunder
The `x`-series is defined as having crossed under `y`-series if the value of `x` is less than the value of `y` and the value of `x` was greater than the value of `y` on the bar immediately preceding the current bar.
crossunder(x, y) → series[bool]
ema1 = ema(close,20)
ema2 = ema(close, 10)
plot(crossunder(ema1,ema2)?-1:1, color="red",style="histogram")
Return value
If "x" crosses under "y", it is true, otherwise it is false.
Parameters
x (series) Data series`x`。
y (series) Data series`y`。
crossover
The `x`-series is defined as having crossed over `y`-series if the value of `x` is greater than the value of `y` and the value of `x` was less than the value of `y` on the bar immediately preceding the current bar.
crossover(x, y) → series[bool]
ema1 = ema(close,20)
ema2 = ema(close, 10)
plot(crossover(ema1,ema2)?-1:1, color="red",style="histogram")
Return value
True if "x" crosses over "y", otherwise false
Parameters
x (series) Data series`x`。
y (series) Data series`y`。