# example of buy on candle crosses RSI "overSold"

11

Hello

Where is the problem with the small code below.

Idea is to buy after a candle crosses "overSold".

Do you have a code which functiones (regardless of indicator). I need to find principle for "candle crossing"...

int start()
{

//----
if(Bars<=RSIPeriod) return(0);
//----

i=RSIPeriod;
//---- We here below fill buffer with RSI values

while(i>=0)
{
RSIBuffer[i]=iRSI(NULL,0,RSIPeriod,ApplyTo,i);

i--;
}

//--Below the criteria which check new bar started

if(IsItNewBar())
{

int result=0;
int OneCandleAgoCloseRSI = RSIBuffer[1];
int TwoCandlesAgoCloseRSI = RSIBuffer[2];

if(TwoCandlesAgoCloseRSI >= OverSold && OneCandleAgoCloseRSI < OverSold)
{
if(result>0)
{

OrderSelect(result,SELECT_BY_TICKET );
OrderModify(OrderTicket(),OrderOpen Price(),NormalizeDouble(TheStopLoss ,Digits),NormalizeDouble(TheTakePro fit,Digits),0,Green);
}
return(0);
}

//----closing area s new bar

return(0);

}

//----closing start area

bool IsItNewBar()
{
static datetime lastTime;
bool IsNewBar = (Time[0] != lastTime);
lastTime = Time[0];
return(IsNewBar);
157

If you are so kind to tell us what your problem is, maybe we are able to tell you where it is.
11

Actually I ran it on M1 DAX and on Tester.

Buy does not occur in whole year even with RSI overSold value 45

Thank you a lot in advance

kindest regards

Moderator
10614

Why are you calculating the RSI values every tick when you are only using them when a new bar forms?

Buffers are used in indicators, not EAs, naming an array with "Buffer" in its name can be confusing. Why use an array anyway, you only check 2 values.

You can cut out

```i=RSIPeriod;
//---- We here below fill buffer with RSI values

while(i>=0)
{
RSIBuffer[i]=iRSI(NULL,0,RSIPeriod,ApplyTo,i);

i--;
}```

completely

and just use

```int OneCandleAgoCloseRSI = iRSI(NULL,0,RSIPeriod,ApplyTo,1);
int TwoCandlesAgoCloseRSI = iRSI(NULL,0,RSIPeriod,ApplyTo,2);```

```int OneCandleAgoCloseRSI = RSIBuffer[1];
int TwoCandlesAgoCloseRSI = RSIBuffer[2];```

Your code should place trades unless you have something like incorrectly sized array

16105

1.  Play video Please edit your post. For large amounts of code, attach it.

2. Just get the two values as GumRai suggested after you have a new bar.
3. Does RSIBuffer have a size? It's not a buffer. Don't use misleading names.
4. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
11

GumRai

YOUR SOLUTION WORKS!!! Thank you a lot!!!

Pitty this forum does not have points,so I could give you highest grades

Is there anything I can do for you?

You saved me days od researching. Maybe you have email?

kindest regards