Thanks gidien! it's amazing!
feedback a sample for dwon trend line list below.
if want to draw up trend line just apply the oppisite rule.
Hope everyone could enhanse if there had more efficient algorithm.
-- The indicator corresponds to the Down Trend Line in Richard Trading System
-- Usage: core.indicators:create("DTL", source, begin)
-- Description:darw the outter trend line of latest 10 period within the range begin to last
-- initializes the indicator
function Init()
indicator:name(resources:get("name"));
indicator:description(resources:get("description") );
indicator:requiredSource(core.Tick);
indicator:type(core.Indicator);
indicator.parameters:addInteger("N", resources:get("param_N_name"), resources:get("param_N_description"), 100, 2, 500);
indicator.parameters:addColor("clrDTL", resources:get("param_clrDTL_name"), resources:get("param_clrDTL_description"), core.rgb(255, 255, 255));
end
local first = 0;
local n = 0;
local source = nil;
local out = nil;
local sourcesize = 0;
local sourcepip = 0;
local pivot = nil;
local delta = nil;
local value1 = 0;
local value2 = 0;
local position1 = 0;
local position2 = 0;
-- initializes the instance of the indicator
function Prepare()
n = instance.parameters.N;
source = instance.source;
sourcesize = source:size() - 1;
sourcepip = source

ipSize();
first = sourcesize - n + 1;
local name = profile:id() .. "(" .. source:name() .. "," .. n .. ")";
instance:name(name);
pivot = instance:addInternalStream(0, 0);
delta = instance:addInternalStream(0, 0);
out = instance:addStream("DTL", core.Line, name, "DTL", instance.parameters.clrDTL, first)
end
-- calculate the value
function Update(period)
local rangeU = nil;
local rangeT = nil;
if (period == sourcesize) then
position1, position2 = DTLFindPeak(first, sourcesize);
out[position1] = position1;
out[position2] = position2;
--draw only the latest 10
rangeT = core.rangeTo(sourcesize, 10);
core.drawLine(out, rangeT, source[position1], position1, source[position2], position2);
end
end
-- find the most peak two point
function DTLFindPeak(pos1, pos2)
local i = 0;
local val = 0;
local rangeA = nil;
local rangeU = nil;
local peak = 0;
local valley = 0;
local mval = 0;
local peak1 = 0;
local peak2 = 0;
rangeA = core.range(source:first(), sourcesize);
rangeU = core.range(pos1, pos2);
-- find the trend start point
if source[pos1] > source[pos2] then
val, pos1 = core.max(source, rangeU);
else
val, pos1 = core.min(source, rangeU);
end
-- set base slope line
core.drawLine(pivot, rangeA, source[pos1], pos1, source[pos2], pos2);
-- find peak and valley
for i = source:first(), sourcesize do
if i >= pos1 then
val = (source[i] - pivot[i]);
if val > 0 then
if mval < 0 then
delta[valley] = mval;
mval = 0;
end
if val > mval then
mval = val;
peak = i;
end
elseif val < 0 then
if mval > 0 then
delta[peak] = mval;
mval = 0;
end
if val < mval then
mval = val;
valley = i;
end
elseif val == 0 then
if mval > 0 then
delta[peak] = mval;
mval = 0;
else
delta[valley] = mval;
mval = 0;
end
end
else
delta[i] = 0;
end
end
rangeU = core.range(pos1, pos2);
val, peak1, mval, peak2 = core.max2(delta, rangeU);
return peak1, peak2;
end