forked from nick-nh/qlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tema.lua
168 lines (140 loc) · 3.47 KB
/
tema.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Settings =
{
Name = "*TEMA",
periodTEMA = 112,
vTypeTEMA = "C",
periodEMA = 64,
vTypeEMA = "C",
showTEMA = 1,
showEMA = 1,
line =
{
{
Name = "TEMA",
Color = RGB(255,0,0),
Type = TYPE_LINE,
Width =2
},
{
Name = "EMA",
Color = RGB(128,0,0),
Type = TYPE_LINE,
Width =2
}
}
}
function dValue(i,param)
local v = param or "C"
if not CandleExist(i) then
return nil
end
if v == "O" then
return O(i)
elseif v == "H" then
return H(i)
elseif v == "L" then
return L(i)
elseif v == "C" then
return C(i)
elseif v == "V" then
return V(i)
elseif v == "M" then
return (H(i) + L(i))/2
elseif v == "T" then
return (H(i) + L(i)+C(i))/3
elseif v == "W" then
return (H(i) + L(i)+2*C(i))/4
elseif v == "ATR" then
local previous = math.max(i-1, 1)
if not CandleExist(previous) then
previous = FindExistCandle(previous)
end
if previous == 0 then
return 0
end
return math.max(math.abs(H(i) - L(i)), math.abs(H(i) - C(previous)), math.abs(C(previous) - L(i)))
else
return C(i)
end
end
function FindExistCandle(I)
local out = I
while not CandleExist(out) and out > 0 do
out = out -1
end
return out
end
function Init()
myTEMA = TEMA()
return #Settings.line
end
function OnCalculate(index)
return myTEMA(index, Settings)
end
function TEMA()
local cache_EMA={}
local cache_TEMA1={}
local cache_TEMA2={}
local cache_TEMA3={}
return function(ind, Fsettings)
local Fsettings=(Fsettings or {})
local index = ind
local periodTEMA = Fsettings.periodTEMA or 112
local showTEMA = Fsettings.showTEMA or 1
local vTypeTEMA = Fsettings.vTypeTEMA or "C"
local periodEMA = Fsettings.periodEMA or 63
local showEMA = Fsettings.showEMA or 1
local vTypeEMA = Fsettings.vTypeEMA or "C"
local kTEMA = 2/(periodTEMA+1)
local kEMA = 2/(periodEMA+1)
local valueTEMA = 0
local valueEMA = 0
local outTEMA = nil
local outEMA = nil
if index == 1 then
cache_EMA = {}
cache_TEMA1 = {}
cache_TEMA2 = {}
cache_TEMA3 = {}
if CandleExist(index) then
cache_EMA[index]= dValue(index, vTypeEMA)
cache_TEMA1[index]= dValue(index, vTypeTEMA)
cache_TEMA2[index]= dValue(index, vTypeTEMA)
cache_TEMA3[index]= dValue(index, vTypeTEMA)
else
cache_EMA[index]= 0
cache_TEMA1[index]= 0
cache_TEMA2[index]= 0
cache_TEMA3[index]= 0
end
return nil
end
cache_EMA[index] = cache_EMA[index-1]
cache_TEMA1[index] = cache_TEMA1[index-1]
cache_TEMA2[index] = cache_TEMA2[index-1]
cache_TEMA3[index] = cache_TEMA3[index-1]
if not CandleExist(index) then
return nil
end
valueTEMA = dValue(index, vTypeTEMA)
cache_TEMA1[index]=kTEMA*valueTEMA+(1-kTEMA)*cache_TEMA1[index-1]
cache_TEMA2[index]=kTEMA*cache_TEMA1[index]+(1-kTEMA)*cache_TEMA2[index-1]
cache_TEMA3[index]=kTEMA*cache_TEMA2[index]+(1-kTEMA)*cache_TEMA3[index-1]
valueEMA = dValue(index, vTypeEMA)
cache_EMA[index]=kEMA*valueEMA+(1-kEMA)*cache_EMA[index-1]
if showTEMA == 1 then
outTEMA = 3*cache_TEMA1[index] - 3*cache_TEMA2[index] + cache_TEMA3[index]
end
if showEMA == 1 then
outEMA = cache_EMA[index]
end
return outTEMA, outEMA
end
end
function round(num, idp)
if idp and num then
local mult = 10^(idp or 0)
if num >= 0 then return math.floor(num * mult + 0.5) / mult
else return math.ceil(num * mult - 0.5) / mult end
else return num end
end