If Slow MA of Williams Percent Range (WPR)>Fast MA of Williams Percent Range (WPR): Up trend,
If Slow MA of Williams Percent Range (WPR)<Fast MA of Williams Percent Range (WPR): Down trend.
- Code: Select all
function Init()
indicator:name("WPR Smoothed indicator");
indicator:description("WPR Smoothed indicator");
indicator:requiredSource(core.Bar);
indicator:type(core.Oscillator);
indicator.parameters:addGroup("Calculation");
indicator.parameters:addInteger("WPR_Period", "Period of WPR", "", 50);
indicator.parameters:addString("MA_Slow_Method", "Slow MA Method", "", "EMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "MVA", "", "MVA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "EMA", "", "EMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "Wilder", "", "Wilder");
indicator.parameters:addStringAlternative("MA_Slow_Method", "LWMA", "", "LWMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "SineWMA", "", "SineWMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "TriMA", "", "TriMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "LSMA", "", "LSMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "SMMA", "", "SMMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "HMA", "", "HMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "ZeroLagEMA", "", "ZeroLagEMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "DEMA", "", "DEMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "T3", "", "T3");
indicator.parameters:addStringAlternative("MA_Slow_Method", "ITrend", "", "ITrend");
indicator.parameters:addStringAlternative("MA_Slow_Method", "Median", "", "Median");
indicator.parameters:addStringAlternative("MA_Slow_Method", "GeoMean", "", "GeoMean");
indicator.parameters:addStringAlternative("MA_Slow_Method", "REMA", "", "REMA");
indicator.parameters:addStringAlternative("MA_Slow_Method", "ILRS", "", "ILRS");
indicator.parameters:addStringAlternative("MA_Slow_Method", "IE/2", "", "IE/2");
indicator.parameters:addStringAlternative("MA_Slow_Method", "TriMAgen", "", "TriMAgen");
indicator.parameters:addStringAlternative("MA_Slow_Method", "JSmooth", "", "JSmooth");
indicator.parameters:addInteger("MA_Slow_Period", "Period of slow MA", "", 10);
indicator.parameters:addString("MA_Fast_Method", "Fast MA Method", "", "EMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "MVA", "", "MVA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "EMA", "", "EMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "Wilder", "", "Wilder");
indicator.parameters:addStringAlternative("MA_Fast_Method", "LWMA", "", "LWMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "SineWMA", "", "SineWMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "TriMA", "", "TriMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "LSMA", "", "LSMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "SMMA", "", "SMMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "HMA", "", "HMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "ZeroLagEMA", "", "ZeroLagEMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "DEMA", "", "DEMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "T3", "", "T3");
indicator.parameters:addStringAlternative("MA_Fast_Method", "ITrend", "", "ITrend");
indicator.parameters:addStringAlternative("MA_Fast_Method", "Median", "", "Median");
indicator.parameters:addStringAlternative("MA_Fast_Method", "GeoMean", "", "GeoMean");
indicator.parameters:addStringAlternative("MA_Fast_Method", "REMA", "", "REMA");
indicator.parameters:addStringAlternative("MA_Fast_Method", "ILRS", "", "ILRS");
indicator.parameters:addStringAlternative("MA_Fast_Method", "IE/2", "", "IE/2");
indicator.parameters:addStringAlternative("MA_Fast_Method", "TriMAgen", "", "TriMAgen");
indicator.parameters:addStringAlternative("MA_Fast_Method", "JSmooth", "", "JSmooth");
indicator.parameters:addInteger("MA_Fast_Period", "Period of fast MA", "", 50);
indicator.parameters:addGroup("Style");
indicator.parameters:addColor("WPRclr", "Color of WPR", "Color of WPR", core.rgb(0, 255, 0));
indicator.parameters:addColor("MASlowclr", "Color of slow MA", "Color of slow MA", core.rgb(255, 0, 0));
indicator.parameters:addColor("MAFastclr", "Color of fast MA", "Color of fast MA", core.rgb(0, 0, 255));
indicator.parameters:addInteger("widthLinReg", "Line width", "Line width", 1, 1, 5);
indicator.parameters:addInteger("styleLinReg", "Line style", "Line style", core.LINE_SOLID);
indicator.parameters:setFlag("styleLinReg", core.FLAG_LINE_STYLE);
indicator.parameters:addColor("UP", "Color for UP", "Color for UP", core.rgb(0,255,0));
indicator.parameters:addColor("DOWN", "Color for DOWN", "Color for DOWN", core.rgb(255,0,0));
indicator.parameters:addInteger("Transparency", "Transparency", "", 50,0,100);
end
local first;
local source = nil;
local WPR_Period;
local MA_Slow_Method;
local MA_Slow_Period;
local MA_Fast_Method;
local MA_Fast_Period;
local SlowMA;
local FastMA;
local WPR;
local WPR_Buff=nil;
local SlowMA_Buff=nil;
local FastMA_Buff=nil;
local hUP;
local hDN;
local lUP;
local lDN;
function Prepare()
source = instance.source;
WPR_Period=instance.parameters.WPR_Period;
MA_Slow_Method=instance.parameters.MA_Slow_Method;
MA_Slow_Period=instance.parameters.MA_Slow_Period;
MA_Fast_Method=instance.parameters.MA_Fast_Method;
MA_Fast_Period=instance.parameters.MA_Fast_Period;
WPR = core.indicators:create("WILLIAMSPERCENTRANGE", source, WPR_Period);
SlowMA = core.indicators:create("AVERAGES", WPR.DATA, MA_Slow_Method, MA_Slow_Period, false);
FastMA = core.indicators:create("AVERAGES", WPR.DATA, MA_Fast_Method, MA_Fast_Period, false);
first = math.max(SlowMA.DATA:first(),FastMA.DATA:first())+2;
local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.WPR_Period .. ", " .. instance.parameters.MA_Slow_Method .. ", " .. instance.parameters.MA_Slow_Period .. ", " .. instance.parameters.MA_Fast_Method .. ", " .. instance.parameters.MA_Fast_Period .. ")";
instance:name(name);
WPR_Buff = instance:addStream("WPR_Buff", core.Line, name .. ".WPR", "WPR", instance.parameters.WPRclr, first);
SlowMA_Buff = instance:addStream("SlowMA_Buff", core.Line, name .. ".SlowMA", "SlowMA", instance.parameters.MASlowclr, first);
FastMA_Buff = instance:addStream("FastMA_Buff", core.Line, name .. ".FastMA", "FastMA", instance.parameters.MAFastclr, first);
WPR_Buff:setWidth(instance.parameters.widthLinReg);
WPR_Buff:setStyle(instance.parameters.styleLinReg);
SlowMA_Buff:setWidth(instance.parameters.widthLinReg);
SlowMA_Buff:setStyle(instance.parameters.styleLinReg);
FastMA_Buff:setWidth(instance.parameters.widthLinReg);
FastMA_Buff:setStyle(instance.parameters.styleLinReg);
hUP=instance:addInternalStream(0, 0);
hDN=instance:addInternalStream(0, 0);
lUP=instance:addInternalStream(0, 0);
lDN=instance:addInternalStream(0, 0);
instance:createChannelGroup("UpGroup","Up" , hUP, hDN, instance.parameters.UP, 100-instance.parameters.Transparency);
instance:createChannelGroup("DnGroup","Dn" , lUP, lDN, instance.parameters.DOWN, 100-instance.parameters.Transparency);
end
function Update(period, mode)
if (period>first) then
WPR:update(mode);
SlowMA:update(mode);
FastMA:update(mode);
WPR_Buff[period]=WPR.DATA[period];
SlowMA_Buff[period]=SlowMA.DATA[period];
FastMA_Buff[period]=FastMA.DATA[period];
if SlowMA.DATA[period]>FastMA.DATA[period] then
hUP[period]=0;
hDN[period]=-100;
lUP[period]=nil;
lDN[period]=nil;
elseif SlowMA.DATA[period]<FastMA.DATA[period] then
lUP[period]=0;
lDN[period]=-100;
hUP[period]=nil;
hDN[period]=nil;
else
hUP[period]=nil;
hDN[period]=nil;
lUP[period]=nil;
lDN[period]=nil;
end
end
end
For successful work must be installed two indicators:
1. Williams Percent Range (WPR): viewtopic.php?f=17&t=898
2. Moving Average Indicator: 20 in 1: viewtopic.php?f=17&t=2430