Australian (ASX) Stock Market Forum

Modified RSI formula in Amibroker (AB)

Joined
10 January 2011
Posts
9
Reactions
0
Hi all
I am new to AB and am wanting to scan over selected periods (eg 1 year up to 10 years) a stock or set of stocks to only return:

a. the day after RSI is less than 30 with closing price, and

b. the day after RSI is greater than 70 with opening price.

Is this possible and can anyone assist with the code?

Many thanks in advance

Marty
 
Do you mean something like the following?

COND1 = Ref(RSI(14),-1) < 30;
COND2 = Ref(RSIa(O,14),-1) > 70;
 
Do you mean something like the following?

COND1 = Ref(RSI(14),-1) < 30;
COND2 = Ref(RSIa(O,14),-1) > 70;

Hi Alterego

Thank you for your quick response.

As I said previously I new at this and not sure. I have tried to insert your suggestion into analyser and get a message to insert a Buy filter.

Any thoughts? When I tested RSI(14)< 30 what returned was every close value that meet the condition which could be quite large while the RSI stays below 30. What I am wanting is only the second day that meets the condition,(ie if condition is first met on say yesterday 10 May 2011 then I want the close for today 11 May 2011) Can this be done? And similarly for a sell (ie RSI (14) >70. So for any stock the report should have something like:

Ticker.....Trade.........Date..........Close........Open
BHP........Buy............02/2/11......45.44
RIO........Buy............05/2/11.......83.56
BHP........Sell............12/3/11.....................46.23
RIO........Sell............16/4/11.....................85.75

Many thanks

Marty
 
Any thoughts? When I tested RSI(14)< 30 what returned was every close value that meet the condition which could be quite large while the RSI stays below 30. What I am wanting is only the second day that meets the condition,(ie if condition is first met on say yesterday 10 May 2011 then I want the close for today 11 May 2011)

I'm still not quite sure what you're trying to do. The stock could stay below 30 for many days, so do you want the close of every day while it's below 30? But then you said "only the second day that meets the condition" - so are you looking for 2 consecutive days below 30??

What buy and sell conditions are you trying to do? Buy on a cross below 30, and sell on a cross above 70? If so, why are you waiting to buy on the following close after the entry signal (instead of at the open), while you're exiting on the open the day following the sell signal? Or have I misunderstood?
 
I'm still not quite sure what you're trying to do. The stock could stay below 30 for many days, so do you want the close of every day while it's below 30? But then you said "only the second day that meets the condition" - so are you looking for 2 consecutive days below 30??

What buy and sell conditions are you trying to do? Buy on a cross below 30, and sell on a cross above 70? If so, why are you waiting to buy on the following close after the entry signal (instead of at the open), while you're exiting on the open the day following the sell signal? Or have I misunderstood?

Hi again Alterego

Thanks again for following up on my query. I must not have been clear enough and as you correctly stated the "The stock could stay below 30 for many days". So what I am wanting to is to buy on the second day's close after the stock crosses below 30 on its on its RSI. And similarly I want to sell on the second day's open after the stock has crossed above 70 on its RSI. Is that any clearer?

Regards and thanks in advance

Marty
 
Well if I understand you correctly, and say Day 1 is the day that the RSI crosses below 30, and you're wanting to buy on Day 2 close, and for the sell if Day 1 is the day the RSI crosses above 70 and you want to sell the next Open, then I think you'd want something like:

SetTradeDelays(1,1,1,1);

Buy = Cross(30,RSI(14));

Sell = Cross(RSI(14),70);

BuyPrice = Close;

SellPrice = Open;
 
Well if I understand you correctly, and say Day 1 is the day that the RSI crosses below 30, and you're wanting to buy on Day 2 close, and for the sell if Day 1 is the day the RSI crosses above 70 and you want to sell the next Open, then I think you'd want something like:

SetTradeDelays(1,1,1,1);

Buy = Cross(30,RSI(14));

Sell = Cross(RSI(14),70);

BuyPrice = Close;

SellPrice = Open;

Hi again Alterego

You have done great. Unfortunately I hadn't accounted for the situation where the RSI may cross the 30 level more than once before it then crosses the 70 level. So what would be perfect is if the first buy opportunity is returned and then followed by the first sell opportunity. Your code returned every time the RSI crossed below 30 before it then crossed the 70 level. And similarly it returned every time the RSI crossed above the 70 level before it then crossed below the 30 level. I would like to eliminate all but the first occasion that the RSI crosses the 30 level and then the 70 level remembering that I want the buy to be the close of the day after the crossing the 30 level and the sell to be the open of the day after the RSI crosses the 70 level.

Hope I am clear this time. It is just isolating the first occurrences for buy and sell.

Your thoughts...

Much appreciation

Marty
 
Try adding the following 2 lines to the bottom of the code. This removes excess trade signals.

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
 
Try adding the following 2 lines to the bottom of the code. This removes excess trade signals.

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Hi Alterego

You're a genius! It works... Can you enlighten me on two points:

a. can you explain what do the ones in SetTradeDelays(1,1,1,1); actually relate to...and

b. what does ExRem(Buy,Sell); do...

Once again thank you and well done.

Much appreciated ...

Marty
 
Hi Alterego

You're a genius! It works... Can you enlighten me on two points:

a. can you explain what do the ones in SetTradeDelays(1,1,1,1); actually relate to...and

b. what does ExRem(Buy,Sell); do...

Once again thank you and well done.

Much appreciated ...

Marty

Hi Marty,

Glad it worked for you.

SetTradeDelays(1,1,1,1) delays the entry and exit by 1 day from when the trade signal is given. So the RSI cross occurs today, it enters tomorrow. The first 1 is the Buy Delay, the next is the Sell Delay, then Short Delay, then Cover Delay.

The ExRem lines remove all excess buy & sell signals. Ie, it gives you the first buy signal, followed by the next sell signal, then next buy signal, etc. So removes any extra signals in between. So it will only buy once, and won't produce another buy signal until it's had a sell signal first, and won't produce another sell signal until there's been a buy signal first.
 
Hi Marty,

Glad it worked for you.

SetTradeDelays(1,1,1,1) delays the entry and exit by 1 day from when the trade signal is given. So the RSI cross occurs today, it enters tomorrow. The first 1 is the Buy Delay, the next is the Sell Delay, then Short Delay, then Cover Delay.

The ExRem lines remove all excess buy & sell signals. Ie, it gives you the first buy signal, followed by the next sell signal, then next buy signal, etc. So removes any extra signals in between. So it will only buy once, and won't produce another buy signal until it's had a sell signal first, and won't produce another sell signal until there's been a buy signal first.

HI Alterego

Many thanks for your insights and explanation. If I can indulge and ask your thoughts on where can I get started on the programming journey for AFL given I am a newbie.

Cheers and thanks again

Marty
 
This weekly RSI divergence code is available at Amibroker library and because I have a trade in NFE at the moment which reflects the low turning point, this is what it looks like over a longer period. Since it is on the right hand side of the screen and a real money trade, whatever happens from here in reference to the divergence will stay true or be changed by the participants. ;)
 

Attachments

  • untitled.jpg
    untitled.jpg
    171.9 KB · Views: 16
Top