-
Notifications
You must be signed in to change notification settings - Fork 64
/
HeikenAshi.lua
144 lines (116 loc) · 2.55 KB
/
HeikenAshi.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
Settings =
{
Name = "*HeikenAshi",
line =
{
{
Name = "HeikenAshiUP",
Color = RGB(0,255,0),
Type = TYPE_POINT,
Width =3
},
{
Name = "HeikenAshiDown",
Color = RGB(255,0,0),
Type = TYPE_POINT,
Width =3
}
}
}
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()
myHA = HA()
return #Settings.line
end
function OnCalculate(index)
return myHA(index, Settings)
end
function HA()
local cache_O={}
local cache_C={}
return function(ind, Fsettings)
local Fsettings=(Fsettings or {})
local index = ind
local openHA
local closeHA
local highHA
local lowHA
local outDown
local outUP
if index == 1 then
cache_O = {}
cache_C = {}
cache_O[index]= 0
cache_C[index]= 0
return nil
end
cache_O[index] = cache_O[index-1]
cache_C[index] = cache_C[index-1]
if not CandleExist(index) then
return nil
end
cache_O[index]=O(index)
cache_C[index]=C(index)
openHA = (cache_O[index-1] + cache_C[index-1])/2
closeHA = (O(index) + H(index) + L(index) + C(index))/4
highHA = math.max(H(index), math.max(openHA, closeHA))
lowHA = math.min(L(index), math.min(openHA, closeHA))
cache_O[index] = openHA
cache_C[index] = closeHA
if openHA < closeHA then
outDown = nil
outUP = (H(index) + L(index))/2
elseif openHA > closeHA then
outDown = (H(index) + L(index))/2
outUP = nil
end
return outUP, outDown
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