Formula Quick Reference

Market Data | Date Functions | Reference Functions | Logical Functions | Math Functions | Statistic Functions | Indicator Functions | Drawing Functions | Drawing Descriptors | Miscellaneous Functions | Formula Forum


Reference Functions

count

count(condition, n):

condition: an array of numbers,
n: period number, it can be either an integer or an integer array.

count the period number in the last n periods where condition != 0.

if n = 0, counting starts from the first period.

k :=(c-llv(l, 9))/(hhv(h, 15) - llv(l, 9))*100;
d :=sma(k, 3, 1);
j :=sma(d, 3, 1); # kdj
a :c/ma(c, 3) >= 1.05 and ma(v, 3)/ma(v, 20) > 1.5 and count(d < 20, 20) >= 1;
b := c > ref(c, 1); # up
d := c = ref(c, 1); # in balance
e := c < ref(c, 1); # down
m := 60;
f := count(b, m) + count(d, m) + count(e, m);
up_rate : 100*count(b, m)/f;
down_rate : 100*count(e, m)/f;
balance_rate : 100*count(d, m)/f;

sum

sum(x, n):

x: an array of numbers,
n: period number, it can be either an integer or an integer array.

return the total sum of x in the last n periods.

if n = 0, the calculation starts from the first period.

# obv
sum(if(close>ref(close,1),vol,if(close<ref(close,1),-vol,0)),0);
# another way of rsi
polyline(1, 30), coloraa00c8;
polyline(1, 50), linestyle1, coloraa00c8;
polyline(1, 70), coloraa00c8;
sum_of_advance:=sum(max(c-ref(c,1),0),24);
sum_of_decline:=sum(max(ref(c,1)-c,0),24);
rsi:sum_of_advance/(sum_of_advance+sum_of_decline)*100, coloraa00c8;
ref

ref(x, n):

x: an array of numbers,
n: period number, it can be either an integer or an integer array.

return the value of x n periods before the current period.

cond1:close>ref(close,1) and ref(close,1)>ref(close,2) and ref(close,2)>ref(close,3);

up:=close>ref(close,1);
cond2:up and ref(up,1) and ref(up,2);

var1:=ref(close,1);
bullish:sma(max(close-var1,0),8,1)/sma(abs(close-var1),8,1)*100;
bearish:100*(hhv(high,13)-close)/(hhv(high,13)-llv(low,13));
ma

ma(x, n):

x: an array of numbers,
n: period number, it can be either an integer or an integer array.

return the n-period simple moving averages of x

a:ma(c,30);
b:ma(a,30);
d:ma(b,30);
# institutional control
var1:=ma(close,5);
var2:=ma(close,35);
var3:=var1-var2;
var4:=if(var3>0,var3,0);
var5:=if(var3<0,var3,0);
ojs:sma(var4,34,1);
ojx:sma(var5,34,1);
control:if(var4>=ojs or var5<=ojx,var3,0),colorstick;
ema

ema(x, n):

x: an array of numbers,
n: period number, it has to be an integer.

return the n-period exponential moving averages of x

ema(c, 30); # macd
diff : ema(close,12) - ema(close,26);
dea : ema(diff,9);
macd : 2*(diff-dea), colorstick;
sma sma(x, n, m):

x: an array of numbers,
n: period number, it has to be an integer,
m: integer number, m < n.

a type of weighted moving averages, calculated using the following recursive expression:

sma(t) = (m*x(t) + (n-m)*sma(t-1))/n.

sma(close,30,1); # kdj
rsv:=(close-llv(low,9))/(hhv(high,9)-llv(low,9))*100;
k:sma(rsv,3,1);
d:sma(k,3,1);
j:3*k-2*d;
dma dma(x, a):

x: an array of numbers,
a: a single number or an array of numbers with 0 < a < 1.

a type of dynamic moving averages of x, calculated using the following recursive expression:

dma(t) = a*x(t) + (1-a)*dma(t-1).

# simplified cost moving average cyc
c5: dma(sma(close,5,1),vol/0.008/capital);
c13: dma(sma(close,8,1),vol/0.03/capital);
c34: dma(sma(close,17,1),vol/0.07/capital);
cyc: dma(close,vol/capital);
# average cost, average_price, cost line, bottom cost line

a_cost: (cost(5)+cost(95))/2;
a_price: ema(close,13);
c_line: dma(ema(close,65),sum(vol,65)/65/capital);
b_line: cost(3);
hhv
llv

hhv(x, n):
llv(x, n):

x: an array of numbers,
n: period number, it can be either an integer or an integer array.

hhv(x, n): return the highest value of x in the last n periods,

llv(x, n): return the lowerst value of x in the last n periods.

if n = 0, the calculation starts from the first period.

# William's R
rsv:= (hhv(high,14)-close)/(hhv(high,14)-llv(low,14))*100;
lwr1:sma(rsv,3,1);
lwr2:sma(lwr1,3,1);

# kdj stock pick
var1:=(close-llv(low,9))/(hhv(high,9)-llv(low,9))*100;
var2:=sma(var1,3,1);
var3:=sma(var2,3,1);
var4:=3*var2-2*var3; # kdj
var5:=ma(close,5);
var6:=ma(close,10);
var5>ref(var5,1)*1.01 and var6>ref(var6,1)*1.005 and var3<90 and
var4>var2*1.05 and var2>var3 and var4>ref(var4,1)*1.1 and
var2>ref(var2,1)*1.05 and var3>ref(var3,1)*1.05 and
close>ref(close,1)*1.03 and vol>ref(ma(vol,20),1);

hhvbars
llvbars

hhvbars(x, n):
llvbars(x, n):

x: an array of numbers,
n: period number, it has to be an integer.

hhvbars(x, n): count the period number in the past since the period where hhv(x, n) occures;

llvbars(x, n): count the period number in the past since the period where llv(x, n) occures.

hb:hhvbars(high,20);
lb:llvbars(low,20);
a:=backset(islastbar,hhvbars(h,30)+1);
b:=count(a,30)=1;
drawicon(b,h,1),align1;
a1:=backset(islastbar,llvbars(l,30)+1);
b1:=a1>ref(a1,1);
drawicon(b1,l,2),align2;
sumbars

sumbars(x, a):

x: an array of numbers,
a: a single number.

count the period number n in the past so that sum(x, n) >= a.

day_no:sumbars(vol,capital);
prove:sum(v,day_no)/capital;
# price goes down while obv moves up. a rally is likely coming
obv:=sum(if(close>ref(close,1),vol,if(close<ref(close,1),-vol,0)),0);
hs:=sumbars(vol,capital);
aa:=if(c>llv(c,hs),1,-1);
bb:=if(obv>llv(obv,hs),1,-1);
cc:=aa*bb;
dd:if(cc=-1,1,0),colorstick;
barscount

barscount(x):

x: an array of numbers.

count the period number since the first valid period of x.

barscount(c)=1; a:backset(islastbar,10);
b:barscount(a);
d:barscount(ma(c,10));
barssince
barslast

barssince(x):
barslast(x):

x: an array of numbers.

barssince(x): count the period number since the first x != 0;

barslast(x): count the period number since the last x != 0.

a:backset(islastbar,10);
b:barssince(a);
d:barssince(ma(c,10));
barslast(close/ref(close,1)>=1.05);
backset

backset(x, n):

x: an array of numbers,
n: period number, it can be either an integer or an integer array, n != 0.

return an array y and if the current period is t and x(t) != 0, then y(t) = 1, y(t-1) = 1, y(t-2) = 1,.., y(t-n+1) = 1.

cond1:=cross(ma(c,5),ma(c,10));
cond2:=backset(cond1,6);
cond3:=(cond2>ref(cond2,1));
drawicon(cond3,l*0.95,1);
# peak trough points
n:=3;
aa:=ref(h,n)=hhv(h,2*n+1);
bb:=backset(aa,n+1);
cc:=filter(bb,n) and h=hhv(h,n+1);
drawicon(cc,h*1.02,10);
aa2:=ref(l,n)=llv(l,2*n+1);
bb2:=backset(aa2,n+1);
cc2:=filter(bb2,n) and l=llv(l,n+1);
drawicon(cc2,l*0.99,11);
filter

filter(x, n):

x: an array of numbers,
n: period number, it can be either an integer or an integer array, n != 0.

return an array y and if for the period t, x(t) != 0, x(t+1) != 0, .., x(t+m) != 0, then y(t) = 1, y(t+1) = 0, .., y(t+m) = 0, where m < n.

a:=c>ref(c,1);
b:filter(a,3)*0.5, colorbrown;
n:=5;
aa:=ref(h,n)=hhv(h,2*n+1);
bb:=backset(aa,n+1);
cc:=filter(bb,n) and h=hhv(h,n+1);
drawicon(cc,h,10), align2;
aa2:=ref(l,n)=llv(l,2*n+1);
bb2:=backset(aa2,n+1);
cc2:=filter(bb2,n) and l=llv(l,n+1);
drawicon(cc2,l,11), align1;
drawline(cc,h,islastbar,ref(h,barslast(cc)),1); # resistant line
drawline(cc2,l,islastbar,ref(l,barslast(cc2)),1); # support line

 

Function Index
abs | align | avedev | backset | barscount | barslast | barssince | between | capital | ceiling | circledot | close | color | colorstick | cost | count | cross | crossdot | date | datediff | day | dma | drawicon | drawline | drawnumber | drawtext | ema | every | exist | fillrgn | filter | finance | floor | hhv | hhvbars | high | if | intpart | iscamarket | isdown | isequal | islastbar | isup | isusmarket | last | linestick | linestyle | linethick | llv | llvbars | ln | log | longcross | low | ma | max | min | mod | month | not | open | partline | peak | peakbars | peaktrough | peaktroughbars | pointdot | polyline | pow | range | ref | relate | reverse | sar | sarturn | sgn | sigperform | slope | sma | std | stick | stickline | sum | sumbars | transparency | trough | troughbars | var | vertline | vol | volstick | weekday | winner | year | zig