comparing candles

 

I am thinking about creating a method that just compares 2 candles passed to it. These candles may be history candles or current candle. I just want to be able to compare them for higher highs/higher lows, lower highs/lower lows, engulfing (high of first candle is higher than second candle, low of first candle is lower than low of second candle). I would like to return an int where the following rules would apply:

1 = (up) higher highs and higher lows are true between the candles

2 = (down) lowever highs and lower lows are true between the candles

3 = (engulfing) high of first candle is higher than second candle, low of first candle is lower than low of second candle is true

Can you show me a method/function that does this? Are there other candle conditions that I need to consider? What are they and how would that code look? If you already have a code snippet that does this, would you share it with me?

 

I use the following function to determine two consecutive short or long candles. The output of this function is returned to an OrderModify which actively checks this condition below a certan point. You could probably code a time function if you want to get candle info at a certain time:


int CheckBars()

{
double bar0=Close[1]-Open[1];
double bar1=Close[2]-Open[2];

string textfile=StringConcatenate(Symbol(),"_logging.txt");
int logging=FileOpen(textfile,FILE_READ|FILE_WRITE);
FileSeek(logging, 0, SEEK_END);

if(bar0>0 && bar1>0)
{
FileWrite(logging,"Two consecutive green bars...Pending close");
FileWrite(logging," ");
FileClose(logging);
logging=0;
return(1);
}
if(bar1<0 && bar1<0)
{
FileWrite(logging,"Two consecutive red bars...Pending close");
FileWrite(logging," ");
FileClose(logging);
logging=0;
return(0);
}
}
 
LEHayes wrote >>

I am thinking about creating a method that just compares 2 candles passed to it. These candles may be history candles or current candle. I just want to be able to compare them for higher highs/higher lows, lower highs/lower lows, engulfing (high of first candle is higher than second candle, low of first candle is lower than low of second candle). I would like to return an int where the following rules would apply:

1 = (up) higher highs and higher lows are true between the candles

2 = (down) lowever highs and lower lows are true between the candles

3 = (engulfing) high of first candle is higher than second candle, low of first candle is lower than low of second candle is true

Can you show me a method/function that does this? Are there other candle conditions that I need to consider? What are they and how would that code look? If you already have a code snippet that does this, would you share it with me?

I was going to remove this thread earlier, got busy. I found someones code snippet that did the following:

int IsMorningStar()
{int retval=0;
if(
(Body(3) > Body(2)) && // Star body smaller than the previous one

(Body(1) > Body(2)) && // Body of star smaller than bodies of first and last candles

(Close[3] < Open[3]) && // First is a down candle

(Close[1] > Open[1]) && // Third is an up candle

(Close[1] > (BodyLo(3) + Body(3)*0.5)) // The third candle closes above the midpoint of the first candle

)
retval=1;
return (retval);
}
int IsEveningStar()
{int retval=0;
if(
(Body(3) > Body(2)) && // Star body smaller than the previous one

(Body(1) > Body(2)) && // Body of star smaller than bodies of first and last candles

(Close[3] > Open[3]) && // First is an up candle

(Close[1] < Open[1]) && // Third is a down candle

(Close[1] < (BodyHi(3) - Body(3)*0.5)) // The third candle closes below the midpoint of the first candle

)
retval=1;
return (retval);
}
int IsBullishEngulfing()
{int retval=0;
if(
(Close[2] < Open[2]) && // First is a down candle

(Close[1] > Open[1]) && // Second is an up candle

(Body(2) < Body(1)) // First engulfed by second
)
retval=1;
return (retval);
}
int IsBearishEngulfing()
{int retval=0;
if(
(Close[2] > Open[2]) && // First is an up candle

(Close[1] < Open[1]) && // Second is a down candle

(Body(2) < Body(1)) // First engulfed by second
)
retval=1;
return (retval);
}
int IsDojiCandle()
{int retval=0;
if(
(Body(1) < ((High[1] - Low[1])/iDoji))
)
retval=1;
return (retval);
}

double Body (int iCandle)
{ double CandleOpen, CandleClose;
CandleOpen=Open[iCandle];
CandleClose=Close[iCandle];
return (MathMax(CandleOpen, CandleClose)-(MathMin(CandleOpen, CandleClose)));
}

double BodyLo (int iCandle)
{
return (MathMin(Open[iCandle], Close[iCandle]));
}

double BodyHi (int iCandle)
{
return (MathMax(Open[iCandle], Close[iCandle]));
}
 
mixtermind wrote >>

I use the following function to determine two consecutive short or long candles. The output of this function is returned to an OrderModify which actively checks this condition below a certan point. You could probably code a time function if you want to get candle info at a certain time:

int CheckBars()

{
double bar0=Close[1]-Open[1];
double bar1=Close[2]-Open[2];

string textfile=StringConcatenate(Symbol(),"_logging.txt");
int logging=FileOpen(textfile,FILE_READ|FILE_WRITE);
FileSeek(logging, 0, SEEK_END);

if(bar0>0 && bar1>0)
{
FileWrite(logging,"Two consecutive green bars...Pending close");
FileWrite(logging," ");
FileClose(logging);
logging=0;
return(1);
}
if(bar1<0 && bar1<0)
{
FileWrite(logging,"Two consecutive red bars...Pending close");
FileWrite(logging," ");
FileClose(logging);
logging=0;
return(0);
}
}

There is something wrong with this code snippet. The second if condition refers to "bar1<0 && bar1<0", I think meant something like "bar0<0 && bar1<0"

 
LEHayes:

There is something wrong with this code snippet. The second if condition refers to "bar1<0 && bar1<0", I think meant something like "bar0<0 && bar1<0"

Thanks for picking up on that, I corrected it in my code.

Reason: