Блог им. autotrade

--[[
вопросы к автору: https://smart-lab.ru/profile/autotrade/
Индикатор: адаптивная среднияя по мультипрофиту от средних
параметры:
cnt - колич средних
SLP - стоплосс
sign - признак показывать сигналы
prf - признак показывать профит
tar - признак тариф
--]]
Settings={
Name="ADAPT_MA_MULT_PROF_v1",
cnt=100,
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)
--[[ --]]
cnt = Settings.cnt
tar = Settings.tar
tar = 0.03
str = 20
if index == 1 then
indprm = {}
sma = {{},{}}
pftm = {{},{}}
sma[index] = {}
pftm[index] = {}
for i = str, cnt do
sma[index][i] = O(index)
pftm[index][i] = 0
end
indprm[index] = str
else
indprm[index] = indprm[index-1]
sma[index] = {}
pftm[index] = {}
for i = str, cnt do
sma[index][i] = sma[index-1][i] + O(index)
pftm[index][i] = pftm[index-1][i]
if index-1 > cnt then
ma1 = (sma[index][i] - sma[index-i][i])/i
ma2 = (sma[index-1][i] - sma[index-i-1][i])/i
tar0 = 0
if ma1 >= O(index) and ma2 < O(index-1) then
pftm0 = O(index) - O(index-1)
tar0 = 2*O(index)*tar/100
else
if ma1 <= O(index) and ma2 > O(index-1) then
pftm0 = O(index-1) - O(index)
tar0 = 2*O(index)*tar/100
else
if ma1 <= O(index) then
pftm0 = O(index) - O(index-1)
else
pftm0 = O(index-1) - O(index)
end
end
end
pftm[index][i] = pftm[index-1][i] + pftm0 - tar0
end
end
end
ind0 = 5*cnt
if index > ind0 then
maxp = 0.2
for i = str, cnt do
dprft = (pftm[index][i] - pftm[index-5*i][i])/(5*i)
dprft = 100*dprft/O(index)
--[[
0.1*(pftm[index][i] - pftm[index-10][i]) +
0.2*(pftm[index][i] - pftm[index-20][i]) +
0.3*(pftm[index][i] - pftm[index-50][i]) +
0.4*(pftm[index][i] - pftm[index-90][i])
--]]
if dprft > maxp then
maxp = dprft
indprm[index] = i
end
end
end
indm = indprm[index]
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]
ma1 = O(index)
if index > indm then
ma1 = (sma[index][indm] - sma[index-indm][indm])/indm
end
if
(
O(index) >= ma1 and pos[index] ~= 1
) and
(
deal[index] == 0 or
deal[index] ~= 0 and
(
O(index) < deal[index]*(1 - 2*SLP/100) or
O(index) > deal[index]*(1 + 0.5*SLP/100)
)
)
then -- long
pos[index] = 1
deal[index] = O(index)
dealx[index] = index
SLV[index] = SLP*O(index)/100
end
if
(
O(index) < ma1 and pos[index] ~= -1
) and
(
deal[index] == 0 or
deal[index] ~= 0 and
(
O(index) > deal[index]*(1 + 2*SLP/100) or
O(index) < deal[index]*(1 - 0.5*SLP/100)
)
)
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 ma1
end
end
end