Skip to content

Commit a7d03f0

Browse files
committed
Indicators
1 parent d221526 commit a7d03f0

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

roffild.lua

+107
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,113 @@ function roffild.dumpTablesToFolder(path, table_names, last)
728728
end
729729
end
730730

731+
--#region Indicators
732+
733+
---DataSource for Indicator
734+
---@return fnCreateDataSourceReturn
735+
function roffild.indicToDataSource()
736+
return {
737+
["O"] = function (self, index) return O(index) end,
738+
["H"] = function (self, index) return H(index) end,
739+
["L"] = function (self, index) return L(index) end,
740+
["C"] = function (self, index) return C(index) end,
741+
["V"] = function (self, index) return V(index) end,
742+
["T"] = function (self, index) return T(index) end,
743+
["Size"] = function (self) return Size() end,
744+
["Close"] = function (self) return true end,
745+
["SetEmptyCallback"] = function (self) return true end,
746+
["SetUpdateCallback"] = function (self, callback_function) return true end,
747+
}
748+
end
749+
750+
---Возвращает цену.
751+
---@param datasource fnCreateDataSourceReturn DataSource
752+
---@param index number Индексы свечек начинаются с 1
753+
---@param pricetype string Тип цены: "O", "H", "L", "C", "V", "M"(edian), "T"(ypical), "W"(eighted)
754+
---@return number?
755+
function roffild.indicPrice(datasource, index, pricetype)
756+
if pricetype == nil or #pricetype == 0 then
757+
return nil
758+
end
759+
local t = string.upper(string.sub(pricetype, 1, 1))
760+
if t == "O" then
761+
return datasource:O(index)
762+
elseif t == "H" then
763+
return datasource:H(index)
764+
elseif t == "L" then
765+
return datasource:L(index)
766+
elseif t == "C" then
767+
return datasource:C(index)
768+
elseif t == "V" then
769+
return datasource:V(index)
770+
elseif t == "M" then
771+
return (datasource:H(index) + datasource:L(index)) / 2.0
772+
elseif t == "T" then
773+
return (datasource:H(index) + datasource:L(index) + datasource:C(index)) / 3.0
774+
elseif t == "W" then
775+
return (datasource:H(index) + datasource:L(index) + (datasource:C(index) * 2.0)) / 4.0
776+
end
777+
return nil
778+
end
779+
780+
---Moving Average
781+
---@param datasource fnCreateDataSourceReturn DataSource
782+
---@param period number Период
783+
---@param shift number Сдвиг в барах
784+
---@param pricetype string Тип цены: "O", "H", "L", "C", "V", "M"(edian), "T"(ypical), "W"(eighted)
785+
---@return number?
786+
function roffild.indicMovingAverage(datasource, period, shift, pricetype)
787+
if shift == nil then
788+
shift = 0
789+
end
790+
local stop = datasource:Size() - shift
791+
local start = stop - (period - 1)
792+
local result = 0.0
793+
local ip = roffild.indicPrice
794+
if start < 1 or stop < 1 then
795+
return nil
796+
end
797+
for x = start, stop, 1 do
798+
result = result + ip(datasource, x, pricetype)
799+
end
800+
return result / period
801+
end
802+
803+
---Money Flow Index
804+
---@param datasource fnCreateDataSourceReturn DataSource
805+
---@param period number Период
806+
---@param shift number Сдвиг в барах
807+
---@return number?
808+
function roffild.indicMoneyFlowIndex(datasource, period, shift)
809+
if shift == nil then
810+
shift = 0
811+
end
812+
local stop = datasource:Size() - shift
813+
local start = stop - period
814+
if start < 1 or stop < 1 then
815+
return nil
816+
end
817+
local pos = 0.0
818+
local neg = 0.0
819+
local last = (datasource:H(start) + datasource:L(start) + datasource:C(start)) / 3.0
820+
for x = start+1, stop, 1 do
821+
local tp = (datasource:H(x) + datasource:L(x) + datasource:C(x)) / 3.0
822+
if tp >= last then
823+
pos = pos + (tp * datasource:V(x))
824+
else
825+
neg = neg + (tp * datasource:V(x))
826+
end
827+
last = tp
828+
end
829+
if neg ~= 0.0 then
830+
return 100.0 - (100.0 / (1.0 + (pos / neg)))
831+
else
832+
return 100.0
833+
end
834+
end
835+
836+
--#endregion
837+
731838
--#region Logger
732839

733840
---@class roffildLogger

0 commit comments

Comments
 (0)