Need Help with Code

 

Hello,

I am currently reading Codersguru's MQL4 primer. I am writing my first code.

My first project is an algorithm that analyzesa custom indicator I created with the help of another programmer called Stochastic RSI. It basically does a stochastic of the RSI Indicator.

The code I have written is a bit of a tester program to make sure I am on the right track.

The problem I am having is that I have some nested If....Else statements and one If...ELse is being totally skipped. Below is the Stochastic RSI code with the code I added to analyze in Bold.

The section that is being skipped is the Lines after "if (S==1)

I was wondering if you could check it out and tell me why this section is being skipped.

Thank You!

//+------------------------------------------------------------------+

//| Stoch_RSI.mq4 |

//| Copyright © 2013, Gehtsoft USA LLC |

//| FxCodeBase |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2013, Gehtsoft USA LLC"

#property link "http://fxcodebase.com"

#property indicator_separate_window

#property indicator_buffers 4 //K[],D[],RSI[],SKI[]

#property indicator_color1 Green //K Line

#property indicator_color2 Red //D Line

//--------------------------------------Setting settings and buffers---

extern int RSI_Length=34;

extern int K_Stochastic_Length=21;

extern int K_Slowing_Length=13;

extern int D_Slowing_Stochastic_Length=13;

extern int Price=0; // Applied price

// 0 - Close

// 1 - Open

// 2 - High

// 3 - Low

// 4 - Median

// 5 - Typical

// 6 - Weighted

double K[], D[];

double RSI[], SKI[];

//------------------------------------------

//Set Line characteristics

//------------------------------------------

int init()

{

IndicatorShortName("Stochastic RSI");

IndicatorDigits(Digits);

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,K);

SetIndexStyle(1,DRAW_LINE);

SetIndexBuffer(1,D);

SetIndexStyle(2,DRAW_NONE);

SetIndexBuffer(2,RSI);

SetIndexStyle(3,DRAW_NONE);

SetIndexBuffer(3,SKI);

return(0);

}

//------------------------------------------

int deinit()

{

return(0);

}

//------------------------------------------

//Primary StochRSI Code

//------------------------------------------

int start() //Special Function Start()

{

if(Bars<=3) return(0); //if 3 bars or less, end program

int ExtCountedBars=IndicatorCounted(); //Number of Counted Bars - At start Counted bars = 0

if (ExtCountedBars<0) return(-1);

int limit=Bars-2;

if(ExtCountedBars>2) limit=Bars-ExtCountedBars-1; //limit = Index of the first uncounted bar

int pos;

pos=limit; //transfer value of limit to pos (position?)

//-------------------------------------------------

//Calculate RSI for bar array

//-------------------------------------------------

while(pos>=0) //Loop for uncounted bars

{

RSI[pos]=iRSI(NULL, 0, RSI_Length, Price, pos); //Calculate RSI for pos bar

pos--; //Calculate index of the next bar

}

//------------------------------------------------------------------------

//Calculate Stochastic for the RSI

//Stochastic =

// %K = (Current Close - Lowest Low)/(Highest High - Lowest Low) * 100

// %D = 3-day SMA of %K

// Lowest Low = lowest low for the look-back period

// Highest High = highest high for the look-back period

// %K is multiplied by 100 to move the decimal point two places

//------------------------------------------------------------------------

double Min, Max;

pos=limit;

while(pos>=0)

{

Min=RSI[ArrayMinimum(RSI, K_Stochastic_Length, pos)]; //Calculate Lowest Low of RSI for Lookback period

Max=RSI[ArrayMaximum(RSI, K_Stochastic_Length, pos)]; //Calculate Highest High of RSI for Lookback Period

if (Min==Max) //if min=max then denominator is 0, therefore

{ //Can't divide a number by 0, therefore

SKI[pos]=100.; //Stochastic = 100

}

else

{

SKI[pos]=100.*(RSI[pos]-Min)/(Max-Min); //Stochastic Formula

}

pos--; //Calculate Stochastic of index of next bar

}

//-------------------------------------------------------------------------

//Calculate K lines

//-------------------------------------------------------------------------

pos=limit;

while(pos>=0)

{

K[pos]=iMAOnArray(SKI, 0, K_Slowing_Length, 0, MODE_SMA, pos);

pos--;

}

//-------------------------------------------------------------------------

//Calculate D lines

//-------------------------------------------------------------------------

pos=limit;

while(pos>=0)

{

D[pos]=iMAOnArray(K, 0, D_Slowing_Stochastic_Length, 0, MODE_SMA, pos);

pos--;

}

//---------------------------------------------------------------------------

//1st CrossOver Analysis

//--------------------------------------------------------------------------

int C, L, S, T, Bar ;

Bar=1;

C=0; L=0; S=0; T=0;

while(Bar>=0)

{

if (K>D)

{

Alert("Bar", Bar, ": K is greater than D. K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

L=1; S=0; Bar++;

continue;

}

else

if (L==1)

{

C++;

if (K<25 && D<25)

{ Alert("We have our first Long Crossover below 25 at Bar", Bar, "K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

break;}

else

{

T++;

Alert("Bad long crossover at", Bar,"! Change settings and start over. K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

break;

}

}

else

if (K<D)

{

Alert("Bar", Bar, ": D is greater than K. K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

L=0; S=1; Bar++;

}

else

//-------This Section Being Skipped v

if (S==1)

{ C++; Alert("I'm Here! K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

if (K>75 && D>75)

{ Alert("We have our first Short Crossover Above 75 at Bar", Bar, "K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

break;}

else

{T++;

Alert("Bad short crossover at", Bar,"! Change settings and start over. K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

break;

}

}

else

{ Alert ("Go to Zero Algorithm. K=",K,"D=",D,"C=",C,"T=",T,"L=",L,"S=",S);

break;}

}

return(0);

}

 

I made a bunch of changes. I think I'm much closer, but still not working right. Any suggestions?

I've added a lot of comments to make the code clear.

I've attached the mq4 file.

Thank You

ThemBonez

Files:
 

adaptive_stoch_rsi_1.mq4

ThemBonez:
I made a bunch of changes. I think I'm much closer, but still not working right. Any suggestions?

I've added a lot of comments to make the code clear.

I've attached the mq4 file.

Thank You

ThemBonez

Hi Thembonez,

Zerro error compiling, try it now.

I don't know if this is what you'r asking for.

Sincerely.

Tomcat98

Files:
 
Tomcat98:
adaptive_stoch_rsi_1.mq4

Hi Thembonez,

Zerro error compiling, try it now.

I don't know if this is what you'r asking for.

Sincerely.

Tomcat98

Hi,

What is that? ThemBonez

Reason: