Not possible to attach files
The data file can be found on:https://docs.google.com/leaf?id=0BwbnvN ... y=CP-atuQBAnd the code I simply paste below--Indicator plots the RSI of a selected period
--and the MA of a selected period of the RSI
-- plots on the price chart where the RSI crosses it's MA
function Init()
indicator:name("RSI MA Cross");
indicator:description("RSI MA Cross plus B/S indicator");
indicator:requiredSource(core.Tick);
indicator:type(core.Oscillator);
indicator.parameters:addGroup("RSI Parameters");
indicator.parameters:addInteger("RSIFrame", "RSI Period", "No description", 14);
indicator.parameters:addColor("RSIColour", "RSI Colour", "", core.rgb(255, 0, 0));
indicator.parameters:addGroup("MA Parameters");
indicator.parameters:addInteger("MAFrame", "MA Period", "No description", 10);
indicator.parameters:addString("Method", "MA Method", "", "MVA");
indicator.parameters:addStringAlternative("Method", "MVA", "", "MVA");
indicator.parameters:addStringAlternative("Method", "EMA", "", "EMA");
indicator.parameters:addColor("MAColour", "MA Colour", "", core.rgb(100, 100, 100));
indicator.parameters:addGroup("Overbought/oversold Level");
indicator.parameters:addInteger("overbought", "Overbought Level", "", 70, 0, 100);
indicator.parameters:addInteger("oversold", "Oversold Level", "", 30, 0, 100);
end
--global variables
local RSIFrame; -- RSI timeframe - parameter
local MAFrame; --MA timeframe - parameter
local Method; --MA method- parameter
local first;
local source = nil;
-- Streams block
local RSI = nil;
local Internal={}; --for calculation
local MA_OUT = nil; -- for display
local RSI_OUT = nil; -- for display
local Long;--on price chart
local Short;--on price chart
function Prepare()
--fetch parameters from the init()ialisation of the instance
Method = instance.parameters.Method;--MA type
RSIFrame = instance.parameters.RSIFrame;-- RSI timeframe
MAFrame = instance.parameters.MAFrame;--MA timeframe
source = instance.source;
first = source:first();
--data which comes from the profile (filename) and the instance
local name = profile:id() .. "(" .. source:name() .. ", " .. RSIFrame .. ", " .. MAFrame .. "," .. Method.. ")";
instance:name(name);
--check the chosen MA is part of the library
assert(core.indicators:findIndicator(Method) ~= nil, "Please, download and install "..Method.." indicator");
--internal streams
RSI =instance:addInternalStream (first, 0);
--core.indicators:create (id, source, parameters...)
Internal["RSI"] = core.indicators:create("RSI", source, RSIFrame); --RSI external function based on the source "instance.source"
Internal["MA"] = core.indicators:create(Method, RSI, MAFrame); --MA external of the RSIbased on the source "RSI"
--instance:addStream (id, type, fullName, label, color, firstPeriod, extent)
RSI_OUT = instance:addStream("RSI", core.Line, name .. ".RSI", "RSI", instance.parameters.RSIColour, first);--create internal stream called "RSI"
MA_OUT = instance:addStream("MA", core.Line, name .. ".MA", "MA", instance.parameters.MAColour, first);--create internal stream called "MA"
--fancy graphics dots on the price chart
--instance:createTextOutput (label, id , font , size, halign, valign , color, extent)
Long = instance:createTextOutput ("Long", "Long", "Wingdings", 16, core.H_Center, core.V_Bottom, core.rgb(0, 255, 0), 0);
Short = instance:createTextOutput ("Short", "Short", "Wingdings", 16, core.H_Center, core.V_Top, core.rgb(255, 0, 0), 0);
core.host:execute ("attachTextToChart", "Long")
core.host:execute ("attachTextToChart", "Short")
end
function Update(period,mode)
if period >= first and source:hasData(period) then --draw only after the first and there is data for this period
Internal["RSI"]:update(mode); --update the data for Internal[RSI] the RSI source
if Internal["RSI"].DATA:hasData(period) then
RSI[period]=Internal["RSI"].DATA[period]; --transfer to the internal stream RSI so Internal[MA] can calculate
Internal["MA"]:update(mode);--now update the data for indicator[2] the MA of the RSI
if Internal["MA"].DATA:hasData(period) then
if period > first then
if core.crossesOver (Internal["RSI"].DATA, Internal["MA"].DATA) then--check for a crossover event
Long:set(period, source[period], "\233"); --change the symbol to something else
Short:setNoData (period); --change the symbol to blank
elseif core.crossesUnder (Internal["RSI"].DATA, Internal["MA"].DATA) then--check for a crossunder event
Short:set(period, source[period], "\234"); --change the symbol to something else
Long:setNoData (period); --change the symbol to blank
end
end
MA_OUT[period]= Internal["MA"].DATA[period];
RSI_OUT[period]= Internal["RSI"].DATA[period];
end
end
end
--check cross of RSI and MA
end