http://www.investopedia.com/terms/d/darvasboxtheory.asp
- Code: Select all
function Init()
indicator:name("Darvas Box");
indicator:description("Darvas Box");
indicator:requiredSource(core.Bar);
indicator:type(core.Indicator);
indicator.parameters:addGroup("Style");
indicator.parameters:addColor("clrUP", "UP Color", "UP Color", core.rgb(0, 255, 0));
indicator.parameters:addColor("clrDN", "DN Color", "DN Color", core.rgb(255, 0, 0));
end
local first;
local source = nil;
local BuffUP=nil;
local BuffDN=nil;
local box_top=0.;
local box_bottom=0.;
local state=1;
local i;
function Prepare()
source = instance.source;
first = source:first()+2;
local name = profile:id() .. "(" .. source:name() .. ")";
instance:name(name);
BuffUP = instance:addStream("BuffUP", core.Line, name .. ".UP", "UP", instance.parameters.clrUP, first);
BuffDN = instance:addStream("BuffDN", core.Line, name .. ".DN", "DN", instance.parameters.clrDN, first);
end
function Update(period, mode)
if (period>first) then
box_top=0.;
box_bottom=0.;
state=1;
for i=first,period,1 do
if state==1 then
box_top=source.high[i];
elseif state==2 then
if box_top<=source.high[i] then
box_top=source.high[i];
end
elseif state==3 then
if box_top>source.high[i] then
box_bottom=source.low[i];
else
box_top=source.high[i];
end
elseif state==4 then
if box_top>source.high[i] then
if box_bottom>=source.low[i] then
box_bottom=source.low[i];
end
else
box_top=source.high[i];
end
elseif state==5 then
if box_top>source.high[i] then
if box_bottom>=source.low[i] then
box_bottom=source.low[i];
end
else
box_top=source.high[i];
end
state=0;
end
BuffUP[i]=box_top;
BuffDN[i]=box_bottom;
state=state+1;
end
end
end