by appress » Sat Jun 04, 2011 1:42 pm
I reply to my own question, here is an indicator which uses DMI to identify the crossing of the two lines DI+ and DI- (how the heck do I attach file??)
--Indicator compares the outputs of indicator DMI
--
--and gives a buy or sell signal upon the following conditions
--
--DM+ cross above DM- BUY
--DM+ cross below DM- SELL
function Init()
indicator:name("DMI Cross");
indicator:description("DMI Cross B/S indicator");
indicator:requiredSource(core.Bar);
indicator:type(core.Oscillator);
indicator.parameters:addGroup("Calculation");
indicator.parameters:addInteger("DMI_N", "Number of periods", "No description", 14, 1, 1000);
end
--global variables
local first;
local DIM; --variables from the DMI indicator
local DIP; --variables from the DMI indicator
local DMI_N;
local source = nil;
local Long;
local Short;
-- Streams block
local Internal={}; --for calculation
local BUYSELL = nil; -- for display
function Prepare()
--fetch parameters from the init()ialisation of the instance
DMI_N = instance.parameters.DMI_N;--MA type
source = instance.source;
--create the DMI indicator in this indicator
Internal["DMI"] = core.indicators:create("DMI", source, DMI_N); --DMI external standard function based on the source "instance.source"
--set the first valid condition
first= Internal["DMI"].DATA:first(); -- set the first to the DMI first;
--"localise" the DMI streams
DIM=Internal["DMI"].DIM; --minus stream
DIP=Internal["DMI"].DIP; --plus stream
--data which comes from the profile (filename) and the instance
local name = profile:id() .. "( " .. source:name() .. ", " .. DMI_N.." )";
instance:name(name);
--instance:addStream (id, type, fullName, label, color, firstPeriod, extent)
BUYSELL = instance:addStream("BuySell", core.Line, "BuySell", "BuySell", core.rgb(0, 0, 255), first);--create display stream
end
function Update(period,mode)
--draw only after the first and there is data for this period
if period < first or not source:hasData(period) then
return;
end
--update the data for Internal[DMI] the DMI source
Internal["DMI"]:update(mode);
--data exists?
if not Internal["DMI"].DATA:hasData(period) or not Internal["DMI"].DATA:hasData(period-1) or not Internal["DMI"].DATA:hasData(period-2) then
return;
end
--DMI cross
--check for a crossover event
BUYSELL[period] = 0;
if core.crossesOver (DIM, DIP, period-1) then
BUYSELL[period] = 1;
--check for a crossunder event
elseif core.crossesUnder (DIM, DIP, period-1) then
BUYSELL[period] = -1;
end
end