How to set a timeframe for a condition

Hello everybody.

I’m a newbie in AFL coding, I recently readed the Bandy’s introduction to Amibroker, and I try to figure out how can I set a timeframe for a condition.

For instance:

RSI < 30 during 2 weeks then buy

I tried this, but doesn’t work…


RSII= RSI (14);
RISMAJOR= RSII<(30) AND 2*inWeekly 

I did some “research” in this forum and I do not find something that can help me.

Thanks a lof for your answers.

what exactly do you mean by

Read this https://www.amibroker.com/guide/h_timeframe.html

I mean, the condition (rsi<30) must be existing for 2 weeks.

like a oversold stock,

THANKS!!

I read it all, but I’m not sure what to do… :sweat_smile:

maybe:

TimeFrameSet (inWeekly);
RSII= RSI (14);
RISMAJOR= RSII<(30);
TimeFrameRestore();

but if I want to set that condition for 2 weeks, what should I do?

TimeFrameSet (inWeekly);
RSII= RSI (14);
TimeFrameRestore();

yes this one works.

If you want to compare “2weeks” then you’ll need to reference the array with

ref(rsii, -2);//

make sure you avoid future leaks in your code.

Dio

Thanks a lot Dio.

But I don’t know what is where do you find the leak in my code, in fact I don’t know what is a leak… :sweat_smile:

very newbie … sorry.

Then, how I link the code line you show me?

perhaps:

buy = RSII AND ref

ok, what you could do is this as an example:

RSI_daily=RSI(20);

TimeFrameSet(inWeekly);//
RSI_inweekly= RSI(20);//
TimeFrameRestore();

condition= RSI_daily <20 AND Ref(RSI_inweekly,-2) < 20;///

what this says is that the current daily RSI is below 20 and the RSI in weekly, 2 weeks ago was also below 20…

I hope it helps you get started.
Future leaks ARE VERY IMPORTANT to understand. if your code addresses in any way something in future, your results will be off because basically you are having foresight, a glass ball that you don’t actually have. Read up on it and ask again.

Dio

@Dionysos,

Your code is not correct.

  1. You don't expand after compressing to TF
  2. What he is looking for is having RSI being lower than value 30 for two weeks in a row

So either....

If he really means weekly RSI (and lower TF):

numweeks = 2;

tmfrm = inWeekly;

TimeFrameSet(tmfrm);
RSII = RSI(14);
RISMAJOR = Sum( RSII<30, numweeks ) == numweeks;
TimeFrameRestore();

// EXPANDING!!
RISMAJOR = TimeFrameExpand( RISMAJOR, tmfrm, expandLast);

Buy = RISMAJOR;

Or...

if he wants to have daily bars with daily RSI being lower than 30 for two weeks (aka 10 business days):

// if EOD data

numdays = 10;// number of business days

RSII = RSI(14);
RISMAJOR = Sum( RSII<30, numdays ) == numdays;

Buy = RISMAJOR;

Or....

2 Likes

Thanks a lot for your answer Dio, you help me a lot, and I understand, now, perfectly what you mean by leaks.

Hello fxshrat.

Just to clarify what I'm looking for, I work in daily bars, and I want to buy when the RSI have been less than 30 the last two weeks, or the last 10 days, doesn't matter.

I'm going to explain in a chart, maybe it's more easy to understand.

56

the “buy point” it’s the little green arrow. :joy: