RSI and MA(RSI) Indicator

Moderator: admin

RSI and MA(RSI) Indicator

Postby appress » Sun Apr 24, 2011 5:52 am

Hi, based this on work by Apprentice :) Indicator "Smoothed RSI" - thanks, good work!

It's an indicator which shows two lines: RSI and MA(RSI)

It should also point out when they cross, but I am having difficulty:
1) too many triggers: the number of arrows does not correspond to the number of crosses. For one occasion where the RSI (14) crosses the MA(RSI)30 at period x I expect one arrow at period x but now there are likely to be any number of other arrows period x+-10. The data file has 8 prices per day (see attached) with each cycle through the Update function if just one of those crosses it triggers a buy or sell arrow on the chart. I need a way to make this happen only on the close.

2) Error - [string "C:\Gehtsoft\IndicoreSDK\indicators\RSI MA Cro..."]:103: Index is out of range. This looks like it's trying to accomplish a lookup on a period which does not exist, but I'm not sure how to solve.


It does show two lines and they do look like the RSI and the MA. I'm pleased that I've managed to use the existing RSI & MA indicators to make my own.


You are of course welcome to use this!
appress
 
Posts: 13
Joined: Fri Mar 18, 2011 2:47 pm

Re: RSI and MA(RSI) Indicator

Postby Apprentice » Sun Apr 24, 2011 8:11 am

Can you post the Code/File of these indicators.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36495
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: RSI and MA(RSI) Indicator

Postby appress » Sun Apr 24, 2011 10:41 am

Not possible to attach files

The data file can be found on:
https://docs.google.com/leaf?id=0BwbnvN ... y=CP-atuQB

And 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
appress
 
Posts: 13
Joined: Fri Mar 18, 2011 2:47 pm

Re: RSI and MA(RSI) Indicator

Postby appress » Mon Apr 25, 2011 2:16 am

ok, half the problem is still here: "103 index is out of range" and when I implement the solution below on the same data, the number of messages grows from 8 to 17. Still working on this...

Have however solved half the problem: the multiple arrows on the indicator have now disappeared, instead of testing on the current bar I must test on the previous bar. Comments on why this is would be appreciated, also why the data format is such that it gives 8 prices per day (I expected 4 OHLC)

New code in Update():

if period > first then
if core.crossesOver (Internal["RSI"].DATA, Internal["MA"].DATA,period-1) 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,period-1) 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
appress
 
Posts: 13
Joined: Fri Mar 18, 2011 2:47 pm

Re: RSI and MA(RSI) Indicator

Postby Apprentice » Mon Apr 25, 2011 4:46 am

Something like this .
RSI MA Cross.lua
(4.9 KiB) Downloaded 800 times


I would advise you that as a source, you use the bar.
The only reason, then you can place the arrows on the top bottom of the candle,
not in the middle.


You have not defined the parameter Period for CrossOver/CrossesUnder

--if core.crossesOver (Internal["RSI"].DATA, Internal["MA"].DATA) then--check for a crossover event
if core.crossesOver (Internal["RSI"].DATA, Internal["MA"].DATA, period) then--check for a crossover event
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36495
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: RSI and MA(RSI) Indicator

Postby appress » Mon Apr 25, 2011 6:18 am

Much obliged.

Tried to base on a Bar source instead of Tick source, as suggested, but the following error appears..
An error occurred during the calculation of the indicator 'RSI MA CROSS_APPRENTICE'. The error details: [string "RSI MA Cross_apprentice.lua"]:85: [string "RSI.lua"]:81: attempt to perform arithmetic on field '?' (a nil value).


I think now it needs some refining to be tradeable, but will try and test it as strategy, see how quickly I can zero my test balance.
appress
 
Posts: 13
Joined: Fri Mar 18, 2011 2:47 pm



Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 64 guests