Originally Posted by gidien
Hi reginad,
here is the code for a full automatic PIVOT LEVEL indicator. The only thing you can change is the time, when the pivot levels were calculated.
For explample if parameter Stunden to 6 then the pivot calculate from six a clock yesterday to six a clock today.
The code also includes the FIBO PIVOT Level calculation.
You do not have to think about which timeframe your are use, the indicator check this for you, and set the right parameter.
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
-- TODO: Add minimal and maximal value of numeric parameters and default color of the streams
function Init()
indicator:name("PIVOT");
indicator:description("PIVOT LINES");
indicator:requiredSource(core.Bar);
indicator:type(core.Indicator);
indicator.parameters:addInteger("HH", "Stunden", "Stunden", 0,0,24);
indicator.parameters:addColor("R3_color", "R3", "R3", core.rgb(0,0,128));
indicator.parameters:addColor("R2_color", "R2", "R2", core.rgb(0,0,255));
indicator.parameters:addColor("R1_color", "R1", "R1", core.rgb(0,128,255));
indicator.parameters:addColor("S3_color", "S3", "S3", core.rgb(128,0,0));
indicator.parameters:addColor("S2_color", "S2", "S2", core.rgb(255,0,0));
indicator.parameters:addColor("S1_color", "S1", "S1", core.rgb(255,128,128));
indicator.parameters:addColor("PP_color", "PP", "PP", core.rgb(255,255,0));
indicator.parameters:addColor("MIN_color", "LOW PREV", "LOW PREV", core.rgb(255,255,0));
indicator.parameters:addColor("MAX_color", "MAX PREV", "MAX PREV", core.rgb(255,255,0));
indicator.parameters:addColor("CLOSE_color", "CLOSE PREV", "CLOSE PREV", core.rgb(255,255,0));
end
-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- TODO: Refine the first period calculation for each of the output streams.
-- TODO: Calculate all constants, create instances all subsequent indicators and load all required libraries
-- Parameters block
local first;
local source = nil;
local TF = nil;
local N = nil;
local HH = nil;
local MM = nil;
local SS = nil;
local MAX = nil;
local MIN = nil;
-- Streams block
local HIGH = nil;
-- Routine
function Prepare()
HH = instance.parameters.HH;
MM = instance.parameters.MM;
SS = instance.parameters.SS;
TF = instance.parameters.TF;
source = instance.source;
-- CHECK which TIMEFRAME is used
TF = source:barSize();
if TF == "m5" then
N = 304;
end
if TF == "m15" then
N = 104;
end
if TF == "m30" then
N = 52;
end
if TF == "h1" then
N = 26;
end
first = N + source:first();
first_COUNT = source:first();
local name = profile:id() .. "(" .. source:name() .. ")";
instance:name(name);
-- Internal DATE Stream
DATE = instance:addInternalStream (first_COUNT);
-- STREAM OUTPUT
MAX = instance:addStream("MAX", core.Line, name, "MAX", instance.parameters.MAX_color,first+2*N-1);
MIN = instance:addStream("MIN", core.Line, name, "MIN", instance.parameters.MIN_color,first+2*N-1);
CLOSE_P = instance:addStream("CLOSE_P", core.Line, name, "CLOSE_P", instance.parameters.CLOSE_color,first+2*N-1);
R3 = instance:addStream("R3", core.Line, name, "R3", instance.parameters.R3_color,first+2*N-1);
R2 = instance:addStream("R2", core.Line, name, "R2", instance.parameters.R2_color,first+2*N-1);
R1 = instance:addStream("R1", core.Line, name, "R1", instance.parameters.R1_color,first+2*N-1);
S1 = instance:addStream("S1", core.Line, name, "S1", instance.parameters.S1_color,first+2*N-1);
S2 = instance:addStream("S2", core.Line, name, "S2", instance.parameters.S2_color,first+2*N-1);
S3 = instance:addStream("S3", core.Line, name, "S3", instance.parameters.S3_color,first+2*N-1);
PP = instance:addStream("PP", core.Line, name, "PP", instance.parameters.PP_color,first+2*N-1);
end
-- Indicator calculation routine
-- TODO: Add your code for calculation output values
function Update(period)
-- GET DATE of the PERIOD
DATE[period] = source:date(period);
CC = core.dateToTable(DATE[period]);
if period >= first and source:hasData(period)then
if CC.hour == HH and CC.min == 0 and CC.sec == 0 then
MAX[period] = core.max(source.high, core.rangeTo(period,N));
MIN[period] = core.min(source.low, core.rangeTo(period,N));
CLOSE_P[period] = source.close[period];
else
MAX[period] = MAX[period-1];
MIN[period] = MIN[period-1];
CLOSE_P[period] = CLOSE_P[period-1];
end
-- PIVOT LEVEL
PP[period] = (MAX[period]+MIN[period]+CLOSE_P[period]) / 3;
R3[period] = MAX[period] + 2*(PP[period]-MIN[period]);
R2[period] = PP[period] + (MAX[period]-MIN[period]);
R1[period] = 2*PP[period] - MIN[period];
S1[period] = 2*PP[period] - MAX[period];
S2[period] = PP[period] - (MAX[period] - MIN[period]);
S3[period] = MIN[period] - 2*(MAX[period] - PP[period]);
-- PIVOT LEVEL
end
-- FIBONACCI LEVEL
-- PP[period] = ((MAX[period] - MIN[period]) * 0.500) + MIN[period];
-- R3[period] = MAX[period];
-- R2[period] = ((MAX[period] - MIN[period]) * 0.764) + MIN[period];
-- R1[period] = ((MAX[period] - MIN[period]) * 0.618) + MIN[period];
-- S1[period] = ((MAX[period] - MIN[period]) * 0.382) + MIN[period];
-- S2[period] = ((MAX[period] - MIN[period]) * 0.236) + MIN[period];
-- S3[period] = MIN[period];
-- FIBONACCI LEVEL
end
|