модификация индикатора в плане наличия функция отключения шорта
предыдущий блог по этой теме:
smart-lab.ru/blog/1090831.php
--[[
вопросы к автору: https://t.me/autotradering
параметры:
--]]
Settings={
Name="sber_imoex2_SL_v2",
prf=0, -- =1 - показать профит
sign=0, -- =1 - показать сигнал
Len1=40, -- длина средней
Len2=30, -- длина средней
Len3=5, -- длина средней
SLP=0.5, -- stop loss в %
shrt=0, -- 1-short
line=
{
{
Name = "line1",
Type =TYPE_LINE,
Width = 1,
Color = RGB(0,0,255)
},
{
Name = "line2",
Type =TYPE_LINE,
Width = 1,
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)
}
}
}
function Init()
prof = {}
sl = {}
pos = {}
otn = {}
return 6
end
function OnCalculate(index)
Len1 = Settings.Len1
Len2 = Settings.Len2
Len3 = Settings.Len3
if index == 1 then
brs = Size()
imoex2 = {}
sber = {}
otn = {}
end
if index == 1 or brs ~= Size() then
brs = Size()
imoex2_id = "imoex2"
number_of_candles_imoex2 = getNumCandles(imoex2_id)
imoex2_from_graph, a, b = getCandlesByIndex(imoex2_id, 0, 0, number_of_candles_imoex2)
for i = 1, number_of_candles_imoex2 do
imoex2[i] = imoex2_from_graph[i-1].open
if i > 1 and imoex2[i] == 0 then
imoex2[i] = imoex2[i-1]
end
end
sber_id = "sber"
number_of_candles_sber = getNumCandles(sber_id)
sber_from_graph, a, b = getCandlesByIndex(sber_id, 0, 0, number_of_candles_sber)
for i = 1, number_of_candles_sber do
sber[i] = sber_from_graph[i-1].open
if i > 1 and sber[i] == 0 then
sber[i] = sber[i-1]
end
end
else
imoex2[index] = imoex2_from_graph[index-1].open
if index-2 >= 0 and imoex2[index] ~= nil then
if imoex2[index] == 0 then
if imoex2[index-1] ~= nil then
imoex2[index] = imoex2[index-1]
end
end
else
imoex2[index] = imoex2[index-1]
end
sber[index] = sber_from_graph[index-1].open
if index-2 >= 0 and sber[index] ~= nil then
if sber[index] == 0 then
if sber[index-1] ~= nil then
sber[index] = sber[index-1]
end
end
else
sber[index] = sber[index-1]
end
end
if imoex2[index]~= 0 then
otn[index] = sber[index]/imoex2[index]
else
otn[index] = 0
end
if imoex2[index]~= nil and sber[index]~= nil then
-- SMA1
if index == 1 then
sum1 = {}
sma1 = {}
n1 = {}
sum1[index] = imoex2[index]
n1[index]=1
else
sum1[index] = imoex2[index] + sum1[index-1]
n1[index]= n1[index-1] + 1
if index > Len1 then
sum1[index] = sum1[index-1] + imoex2[index]
sum1[index] = sum1[index] - imoex2[index-Len1]
n1[index] = n1[index-1]
end
end
-- SMA2
if index == 1 then
sum2 = {}
sma2 = {}
n2 = {}
sum2[index] = sber[index]
n2[index]=1
else
sum2[index] = sber[index] + sum2[index-1]
n2[index]= n2[index-1] + 1
if index > Len2 then
sum2[index] = sum2[index-1] + sber[index]
sum2[index] = sum2[index] - sber[index-Len2]
n2[index] = n2[index-1]
end
end
-- SMA3
if index == 1 then
sum3 = {}
sma3 = {}
n3 = {}
sum3[index] = otn[index]
n3[index]=1
else
if sum3[index-1] == nil then
sum3[index] = 0
n3[index] = 1
else
sum3[index] = otn[index] + sum3[index-1]
n3[index]= n3[index-1] + 1
if index > Len3 then
sum3[index] = sum3[index-1] + otn[index]
sum3[index] = sum3[index] - otn[index-Len3]
n3[index] = n3[index-1]
end
end
end
if n1[index] ~= nil then
if n1[index] ~= 0 then
sma1[index] = sum1[index]/n1[index]
end
end
if n2[index] ~= nil then
if n2[index] ~= 0 then
sma2[index] = sum2[index]/n2[index]
end
end
if n3[index] ~= nil then
if n3[index] ~= 0 then
sma3[index] = sum3[index]/n3[index]
end
end
end
--[[ --]]
-- return otn[index], sma3[index]
if index == 1 then
prof = {}
pos = {}
f = {}
SLV = {}
prof[index] = 0
pos[index] = 0
f[index] = 0
SLV[index] = 0
else
prof[index] = prof[index-1]
pos[index] = pos[index-1]
f[index] = f[index-1]
SLV[index] = SLV[index-1]
end
if sber[index] ~= nil and imoex2[index] ~= nil and
sber[index-1] ~= nil and imoex2[index-1] ~= nil and
sma1[index] ~= nil and sma2[index] ~= nil and
sma1[index-1] ~= nil and sma2[index-1] ~= nil and
f[index-1] ~= nil and SLV[index-1] ~= nil
then
kf2 = imoex2[index]/sber[index]
kf3 = imoex2[index]/otn[index]
f[index] = (imoex2[index] - sma1[index]) +
kf2*(sber[index] - sma2[index]) +
kf3*(otn[index] - sma3[index])
if
f[index] > SLV[index] and f[index-1] <= SLV[index-1]
and
pos[index] ~= 1
then -- long
SLV[index] = -kf2*Settings.SLP*sber[index]/100
pos[index] = 1
else
if
f[index] < SLV[index] and f[index-1] >= SLV[index-1]
and
pos[index] ~= -1
then -- short
SLV[index] = kf2*Settings.SLP*sber[index]/100
if Settings.shrt == 1 then
pos[index] = -1
else
pos[index] = 0
end
end
end
if SLV[index] < 0 then
if f[index] > -SLV[index] and pos[index] == 1 then
SLV[index] = 0
end
else
if SLV[index] > 0 then
if f[index] < -SLV[index] and pos[index] == -1 then
SLV[index] = 0
end
end
end
end
-- signals
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, sber[i])
else
if pos[i-1] ~= -1 and pos[i] == -1 then
SetValue(i, 3, sber[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, sber[i])
else
SetValue(i, 6, sber[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] + sber[index] - sber[index-1]
end
if pos[index-1] == -1 then
prof[index] = prof[index-1] + sber[index-1] - sber[index]
end
if pos[index-1] == 0 then
prof[index] = prof[index-1]
end
end
return prof[index]
else
if Settings.sign == 1 then
return nil
else
return f[index] , SLV[index]
end
end
end