Блог им. autotrade

--[[
вопросы к автору: https://smart-lab.ru/profile/autotrade/
Индикатор: адаптивная среднияя, задается минимаьный период,
на котором не может быть больше одного пересечения с ценой
параметры:
per - период поиска пересечений
SLP - стоплосс
sign - признак показывать сигналы
prf - признак показывать профит
tar - признак тариф
--]]
Settings={
Name="ADAPT_MA_PER_v1",
per=30,
-- SLP=2.0,
sign=0,
prf=0,
tar=0.03,
line=
{
{
Name = "cur1",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0,255)
},
{
Name = "cur2",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0,0,255)
},
{
Name = "TRIANGLE_DOWN",
Type =TYPE_TRIANGLE_DOWN,
Width = 5,
Color = RGB(255, 0, 0)
},
{
Name = "TRIANGLE_UP",
Type =TYPE_TRIANGLE_UP,
Width = 5,
Color = RGB(0, 0, 255)
},
{
Name = "cur5",
Type =TYPE_POINT,
Width = 5,
Color = RGB(255, 0, 255)
},
{
Name = "cur6",
Type =TYPE_POINT,
Width = 5,
Color = RGB(0, 0, 255)
},
{
Name = "cur7",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0,0)
},
{
Name = "cur8",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0,0,0)
}
}
}
function Init()
return 8
end
function OnCalculate(index)
--[[ --]]
per = Settings.per
if index == 1 then
sma = {}
sma[index] = O(index)
else
sma[index] = sma[index-1] + O(index)
end
pi = 10
if index > per then
for p = 20, 100, 5 do
cnt = 0
if index > p then
--y = (sma[index] - sma[index-p])/p
if index-p-per > 2 then
for i = index-1, index-per, -1 do
if i > p then
y = (sma[i] - sma[i-p])/p
y1 = (sma[i-1] - sma[i-p-1])/p
if O(i) > y and O(i-1) <= y1 or
O(i) < y and O(i-1) >= y1 then
cnt = cnt + 1 -- intersection
end
end
end
end
end
if cnt < 2 then
pi = p
break
end
end
end
ln2 = pi
if index == 1 then
sum01 = {}
sum01[index] = O(index)
ma01 = {}
else
sum01[index] = sum01[index-1] + O(index)
if index > ln2 then
ma01[index] = (sum01[index] - sum01[index-ln2])/ln2
else
ma01[index] = O(index)
end
end
SLP = Settings.SLP
-- trades
if index == 1 then
prof = {}
pos = {}
SLV = {}
deal = {}
dealx = {}
prof[index] = 0
pos[index] = 0
SLV[index] = 0
deal[index] = 0
dealx[index] = 1
else
prof[index] = prof[index-1]
pos[index] = pos[index-1]
SLV[index] = SLV[index-1]
deal[index] = deal[index-1]
dealx[index] = dealx[index-1]
if
(
O(index) > ma01[index] and pos[index] ~= 1
)
then -- long
pos[index] = 1
deal[index] = O(index)
dealx[index] = index
SLV[index] = SLP*O(index)/100
end
if
(
O(index) < ma01[index] and pos[index] ~= -1
)
then -- short
pos[index] = -1
deal[index] = O(index)
dealx[index] = index
SLV[index] = SLP*O(index)/100
end
end
--[[--]]
if deal[index] ~= 0 and pos[index] == 1 and
deal[index] - O(index) > SLV[index]--SLP*deal[index]/100
then -- cash
pos[index] = 0
deal[index] = 0
dealx[index] = 1
else
if deal[index] ~= 0 and pos[index] == -1 and
O(index) - deal[index] > SLV[index]--SLP*deal[index]/100
then -- cash
pos[index] = 0
deal[index] = 0
dealx[index] = 1
end
end
--[[
--]]
if index == Size() then
if Settings.sign == 1 then
for i = 2, Size() do
if pos[i] ~= nil and pos[i-1] ~= nil then
if pos[i-1] ~= 1 and pos[i] == 1 then
SetValue(i, 4, O(i))
else
if pos[i-1] ~= -1 and pos[i] == -1 then
SetValue(i, 3, O(i))
else
SetValue(i, 3, nil)
SetValue(i, 4, nil)
end
if pos[i] ~= pos[i-1] and pos[i] == 0 then
if pos[i-1] == 1 then
SetValue(i, 5, O(i))
else
SetValue(i, 6, O(i))
end
end
end
end
end
end
end
--[[--]]
-- profit
if Settings.prf==1 then
if pos[index-1] ~= nil then
if pos[index-1] == 1 then
prof[index] = prof[index-1] + (O(index) - O(index-1))
end
if pos[index-1] == -1 then
prof[index] = prof[index-1] + (O(index-1) - O(index))
end
if pos[index-1] == 0 then
prof[index] = prof[index-1]
end
if pos[index] ~= pos[index-1] then
if pos[index] == 0 or pos[index-1] == 0 then
prof[index] = prof[index] - Settings.tar*O(index)/100
else
prof[index] = prof[index] - 2*Settings.tar*O(index)/100
end
end
end
end
if Settings.prf==1 then
return prof[index] --per1[index], per2[index]
else
if Settings.sign==1 then
return nil
else
return ma01[index]
end
end
end
Это Арно-Леру? Или другая?
Но уже понятно, что другая. Сколько этих ма-шек развелось, и не сосчитать ))
--[[
Адаптивная среднияя, задается минимаьный период,
на котором не может быть больше одного пересечения с ценой
--]]
Settings = {
Name = "*AdapMAv1",
per = 30, — [ период поиска пересечений ]
sign = 0, -- [ 1 = показывать сигналы Buy/Sell ]
line = {
{Name=«MA», Type=TYPE_LINE, Width=1, Color=RGB(128,192,222)},
{Name=«Buy», Type=_G.TYPE_TRIANGLE_UP, Width=3, Color=RGB(0,200,0)},
{Name=«Sell», Type=_G.TYPE_TRIANGLE_DOWN, Width=3, Color=RGB(255,50,50)}
}
}
local cachedN, cachedPer = nil, nil
local arrMA, arrBuy, arrSell
local function rebuild(N, per)
arrMA, arrBuy, arrSell = {}, {}, {}
— [ накопительная сумма Open ]
local sumO = {}
for i = 1, N do
sumO[i] = (sumO[i-1] or 0) + (O(i) or 0)
end
— [ кэш SMA(p) для всех p = 20, 25, ..., 100 ]
local smaCache = {}
for p = 20, 100, 5 do
local arr = {}
for i = p, N do
arr[i] = (sumO[i] — (sumO[i-p] or 0)) / p
end
smaCache[p] = arr
end
local prevMA = nil
local pos = 0
for index = 1, N do
— [ выбор адаптивного периода pi ]
local pi = per
if index > per then
for p = 20, 100, 5 do
local smaP = smaCache[p]
local cnt = 0
if index > p + per + 2 then
for i = index — 1, index — per, -1 do
local y = smaP[i]
local y1 = smaP[i-1]
local oi = O(i)
local oi1 = O(i-1)
if y and y1 and oi and oi1 then
if (oi > y and oi1 <= y1) or (oi < y and oi1 >= y1) then
cnt = cnt + 1
end
end
end
end
if cnt < 2 then
pi = p
break
end
end
end
— [ MA с текущим pi ]
local ma
if index >= pi then
ma = (sumO[index] — (sumO[index-pi] or 0)) / pi
elseif prevMA then
ma = prevMA
else
ma = O(index) or 0
end
arrMA[index] = ma
— [ направление MA ]
local newPos = 0
if prevMA then
if ma > prevMA then newPos = 1
elseif ma < prevMA then newPos = -1
end
end
— [ сигналы на смене направления ]
if pos ~= 1 and newPos == 1 then
arrBuy[index] = ma
end
if pos ~= -1 and newPos == -1 then
arrSell[index] = ma
end
prevMA = ma
pos = newPos
end
end
function Init()
cachedN, cachedPer = nil, nil
arrMA, arrBuy, arrSell = {}, {}, {}
return 3
end
function OnCalculate(i)
local N = Size()
local per = tonumber(Settings.per) or 30
local sign = tonumber(Settings.sign) or 0
if cachedN ~= N or cachedPer ~= per then
cachedN, cachedPer = N, per
rebuild(N, per)
end
if not arrMA or i < 1 or i > N then
return nil, nil, nil
end
— [ линия 1: MA ]
local line1 = arrMA[i]
— [ линии 2-3: сигналы Buy/Sell ]
local line2, line3 = nil, nil
if sign == 1 then
line2 = arrBuy[i]
line3 = arrSell[i]
end
return line1, line2, line3
end