Last Bullish and Bearish candle

Created at 13 Nov 2013, 11:27
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
RK

rkokerti

Joined 28.06.2012

Last Bullish and Bearish candle
13 Nov 2013, 11:27


Hello!

Can anyone help me, how to define the actual last bullish and last bearish candle? Even if the latest candle change of direction.

Thanks in advance!


@rkokerti
Replies

rkokerti
13 Nov 2013, 13:24

Like code below... But it is not too smart / elegant, and it is investigate only 3 candle backward. 

I would like to ivnestigate more (min 20) candles. So some FOR/DO/WHILE cycle would be better.

Please help!

    public override void Calculate(int index)
    {

    if(MarketSeries.Open[index] < MarketSeries.Close[index]) 
    {
    ChartObjects.DrawText("LL","LastLong",index,MarketSeries.High[index],VerticalAlignment.Top,HorizontalAlignment.Center,Colors.LimeGreen);
    }
    else if(MarketSeries.Open[index-1] < MarketSeries.Close[index-1])
    {
    ChartObjects.DrawText("LL","LastLong",index-1,MarketSeries.High[index-1],VerticalAlignment.Top,HorizontalAlignment.Center,Colors.LimeGreen);    
    }
    else if(MarketSeries.Open[index-2] < MarketSeries.Close[index-2])
    {
    ChartObjects.DrawText("LL","LastLong",index-2,MarketSeries.High[index-2],VerticalAlignment.Top,HorizontalAlignment.Center,Colors.LimeGreen);    
    }
    else if(MarketSeries.Open[index-3] < MarketSeries.Close[index-3])
    {
    ChartObjects.DrawText("LL","LastLong",index-3,MarketSeries.High[index-3],VerticalAlignment.Top,HorizontalAlignment.Center,Colors.LimeGreen);    
    }


    if(MarketSeries.Open[index] > MarketSeries.Close[index]) 
    {
    ChartObjects.DrawText("LS","LastShort",index,MarketSeries.Low[index],VerticalAlignment.Bottom,HorizontalAlignment.Center,Colors.Red);
    }
    else if(MarketSeries.Open[index-1] > MarketSeries.Close[index-1]) 
    {
    ChartObjects.DrawText("LS","LastShort",index-1,MarketSeries.Low[index-1],VerticalAlignment.Bottom,HorizontalAlignment.Center,Colors.Red);
    }
    else if(MarketSeries.Open[index-2] > MarketSeries.Close[index-2]) 
    {
    ChartObjects.DrawText("LS","LastShort",index-2,MarketSeries.Low[index-2],VerticalAlignment.Bottom,HorizontalAlignment.Center,Colors.Red);
    }
    else if(MarketSeries.Open[index-3] > MarketSeries.Close[index-3]) 
    {
    ChartObjects.DrawText("LS","LastShort",index-3,MarketSeries.Low[index-3],VerticalAlignment.Bottom,HorizontalAlignment.Center,Colors.Red);
    }    

}

 


@rkokerti

fzlogic
13 Nov 2013, 14:49

            for (int i = index; i > index - Period; i--)

                if (MarketSeries.Open[i] < MarketSeries.Close[i])
                {
                    ChartObjects.DrawText("LL", "LastLong", index, MarketSeries.High[i], VerticalAlignment.Top,
                                          HorizontalAlignment.Center, Colors.LimeGreen);
                    break;
                }
        }

 


@fzlogic

fzlogic
13 Nov 2013, 14:56

RE:

fzlogic said:

            for (int i = index; i > index - Period; i--)

                if (MarketSeries.Open[i] < MarketSeries.Close[i])
                {
                    ChartObjects.DrawText("LL", "LastLong", index, MarketSeries.High[i], VerticalAlignment.Top,
                                          HorizontalAlignment.Center, Colors.LimeGreen);
                    break;
                }
        }

 

That one was only for last long.

            double lastLong = 0;
            double lastShort = 0;

            for (int i = index; i > index - Period; i--)
            {
                if (MarketSeries.Open[i] < MarketSeries.Close[i])
                {
                    lastLong = MarketSeries.High[i];
                    ChartObjects.DrawText("LL", "LastLong", i, lastLong, VerticalAlignment.Top,
                                          HorizontalAlignment.Center, Colors.LimeGreen);
                }
                else if (MarketSeries.Open[i] > MarketSeries.Close[i])
                {
                    lastShort = MarketSeries.Low[i];
                    ChartObjects.DrawText("LS", "LastShort", i, lastShort, VerticalAlignment.Bottom,
                                          HorizontalAlignment.Center, Colors.Red);
                }
                if (lastLong > 0 && lastShort > 0) 
                    break;
            }

 


@fzlogic

fzlogic
13 Nov 2013, 14:59

Sorry, that one was also wrong.

This one is correct:

            double lastLong = 0;
            double lastShort = 0;

            for (int i = index; i > index - Period; i--)
            {
                if (lastLong == 0 && MarketSeries.Open[i] < MarketSeries.Close[i])
                {
                    lastLong = MarketSeries.High[i];
                    ChartObjects.DrawText("LL", "LastLong", i, lastLong, VerticalAlignment.Top,
                                          HorizontalAlignment.Center, Colors.LimeGreen);
                }
                else if (lastShort == 0 && MarketSeries.Open[i] > MarketSeries.Close[i])
                {
                    lastShort = MarketSeries.Low[i];
                    ChartObjects.DrawText("LS", "LastShort", i, lastShort, VerticalAlignment.Bottom,
                                          HorizontalAlignment.Center, Colors.Red);
                }
                if (lastLong > 0 && lastShort > 0)
                    break;
            }

 


@fzlogic

rkokerti
13 Nov 2013, 19:26

Dear fzlogic!

Nice Job! Thanks for the quick reply!

 


@rkokerti