Calculation:
Momentum is calculated as a ratio of today’s price to the price several (N) periods ago.
MOMENTUM = CLOSE(i)/CLOSE(i-N)*100
Where:
CLOSE(i) — is the closing price of the current bar;
CLOSE(i-N) — is the closing bar price N periods ago.
- Code: Select all
function Init()
indicator:name("Momentum");
indicator:description("Momentum");
indicator:requiredSource(core.Tick);
indicator:type(core.Oscillator);
indicator.parameters:addInteger("N", "N", "Period", 14);
indicator.parameters:addColor("clrMom", "Color of momentum", "Color of momentum", core.rgb(0, 255, 0));
end
local first;
local source = nil;
local N;
function Prepare()
source = instance.source;
N=instance.parameters.N;
first = source:first();
local name = profile:id() .. "(" .. source:name() .. ", " .. N .. ")";
instance:name(name);
Momentum = instance:addStream("Momentum", core.Line, name .. ".Momentum", "Momentum", instance.parameters.clrMom, first);
end
function Update(period, mode)
if (period>first+N) then
Momentum[period]=source[period]*100./source[period-N];
end
end
Simple momentum strategy.
viewtopic.php?f=31&t=65737&p=117765#p117765