This is the implementation of Olivier Seban's Supertrend indicator.
- Code: Select all
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
indicator:name("SuperTrend Indicator");
indicator:description("The indicator displays the buying and selling with the colors. The indicator is initially developed by Jason Robinson for MT4");
indicator:requiredSource(core.Bar);
indicator:type(core.Indicator);
indicator.parameters:addInteger("N", "Number of periods", "No description", 10);
indicator.parameters:addDouble("M", "Multiplier", "No description", 1.5);
indicator.parameters:addColor("UP_color", "Color of UP", "Color of UP", core.rgb(0, 255, 0));
indicator.parameters:addColor("DN_color", "Color of DN", "Color of DN", core.rgb(255, 0, 0));
end
-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- Parameters block
local N;
local M;
local first;
local source = nil;
local ATR = nil;
-- Streams block
local TrUP = nil;
local TrDN = nil;
local UP = nil;
local DN = nil;
local TR = nil;
-- Routine
function Prepare()
N = instance.parameters.N;
M = instance.parameters.M;
source = instance.source;
ATR = core.indicators:create("ATR", source, N);
first = ATR.DATA:first();
local name = profile:id() .. "(" .. source:name() .. ", " .. N .. ", " .. M .. ")";
instance:name(name);
UP = instance:addInternalStream(first, 0);
DN = instance:addInternalStream(first, 0);
TR = instance:addInternalStream(first, 0);
TrUP = instance:addStream("UP", core.Line, name .. ".UP", "UP", instance.parameters.UP_color, first);
TrDN = instance:addStream("DN", core.Line, name .. ".DN", "DN", instance.parameters.DN_color, first);
end
-- Indicator calculation routine
function Update(period, mode)
ATR:update(mode);
TR[period] = 1;
if period >= first then
local median, atr, change;
atr = ATR.DATA[period];
median = (source.high[period] + source.low[period]) / 2;
UP[period] = median + atr * M;
DN[period] = median - atr * M;
if period >= first + 1 then
change = false;
if source.close[period] > UP[period - 1] then
TR[period] = 1;
if TR[period - 1] == -1 then
change = true;
end
elseif source.close[period] < DN[period - 1] then
TR[period] = -1;
if TR[period - 1] == 1 then
change = true;
end
else
TR[period] = TR[period - 1];
end
local flag, flagh;
if TR[period] < 0 and TR[period - 1] > 0 then
flag = 1;
else
flag = 0;
end
if TR[period] > 0 and TR[period - 1] < 0 then
flagh = 1;
else
flagh = 0;
end
if TR[period] > 0 and DN[period] < DN[period - 1] then
DN[period] = DN[period - 1];
end
if TR[period] < 0 and UP[period] > UP[period - 1] then
UP[period] = UP[period - 1];
end
if flag == 1 then
UP[period] = median + atr * M;
end
if flagh == 1 then
DN[period] = median - atr * M;
end
if TR[period] == 1 then
TrUP[period] = DN[period];
if change then
TrUP[period - 1] = TrDN[period - 1];
end
end
if TR[period] == -1 then
TrDN[period] = UP[period];
if change then
TrDN[period - 1] = TrUP[period - 1];
end
end
end
end
end
Single Stream Version
Indicator-based strategy.
https://fxcodebase.com/code/viewtopic.php?f=31&t=75138