How to get OHLC data for Signal Candle Breakout.

Created at 17 May 2020, 17:00
VA

vamsi.rc

Joined 05.05.2020

How to get OHLC data for Signal Candle Breakout.
17 May 2020, 17:00


Hi Team,

 

I am trying to understand if breakout for the signal candle is occurred. I want to take the confirmation to place the orders.

Example :

 

In the above image, trend changed from UpTrend to DownTrend, but the trend is not confirmed. In this case, if I close the position based on 2nd, I loose next trend as 2nd one is just retracement. So to avoid this situation, I want to take confirmation.

As stated above, I want to take High and Low of 2nd Candle, and verify next candles which breakout the Signal Candle. 

In programming code language, 

If (Signal == "Buy")

{

If(Close[1] (Breakout Candle) > Signal_Candle_High) // I don't want to hardcode the index, as we don't know which candle closed above Signal Candle High.

{

Here, I need to check another candle which moves above breakout candle, here its running candle so we can use Close[0]. 

}

}

Else If (Signal == "Sell")

{

For Sell,its vice versa.

}

 

Kindly help to get this complete.

Thanks 

R C Vamsi Vardhan


@vamsi.rc
Replies

PanagiotisCharalampous
18 May 2020, 12:12

Hi Vamsi,

Here is the example you requested

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BreakoutSample : Robot
    {

        SimpleMovingAverage _sma1;
        SimpleMovingAverage _sma2;

        int _singalCandleIndex;
        bool _hasCrossedAbove;
        int _breakoutCandleIndex;

        protected override void OnStart()
        {
            _sma1 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 5);
            _sma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 20);
        }

        protected override void OnBar()
        {
            if (_sma1.Result.HasCrossedAbove(_sma2.Result, 0))
            {
                // if sma 1 has crossed above sma 2, we record the index of the bar
                _singalCandleIndex = Bars.ClosePrices.Count - 1;
                _hasCrossedAbove = true;
            }

            if (_hasCrossedAbove && Bars.HighPrices.Last(1) > Bars.HighPrices[_singalCandleIndex])
            {
                // If last bar high is above the signal bar high, we record the breakout index
                _breakoutCandleIndex = _singalCandleIndex = Bars.ClosePrices.Count - 2;
                Print("Breakout Candle Index: " + _breakoutCandleIndex);
            }

        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

vamsi.rc
19 May 2020, 18:54

RE:

Thank you very much Panagiotis. Let me try this.

 

PanagiotisCharalampous said:

Hi Vamsi,

Here is the example you requested

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BreakoutSample : Robot
    {

        SimpleMovingAverage _sma1;
        SimpleMovingAverage _sma2;

        int _singalCandleIndex;
        bool _hasCrossedAbove;
        int _breakoutCandleIndex;

        protected override void OnStart()
        {
            _sma1 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 5);
            _sma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 20);
        }

        protected override void OnBar()
        {
            if (_sma1.Result.HasCrossedAbove(_sma2.Result, 0))
            {
                // if sma 1 has crossed above sma 2, we record the index of the bar
                _singalCandleIndex = Bars.ClosePrices.Count - 1;
                _hasCrossedAbove = true;
            }

            if (_hasCrossedAbove && Bars.HighPrices.Last(1) > Bars.HighPrices[_singalCandleIndex])
            {
                // If last bar high is above the signal bar high, we record the breakout index
                _breakoutCandleIndex = _singalCandleIndex = Bars.ClosePrices.Count - 2;
                Print("Breakout Candle Index: " + _breakoutCandleIndex);
            }

        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram

 

 


@vamsi.rc

vamsi.rc
21 May 2020, 08:09

RE: RE:

Hi Panagiotis,

Is there any way to add 2 moving averages?

I tried this.

 protected override void OnStart()
        {
            // Put your initialization logic here

            ma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, Periods2);

            ma5 = Indicators.SimpleMovingAverage(Bars.ClosePrices, Periods5);   
         
            ma10 = Indicators.SimpleMovingAverage(Bars.ClosePrices, Periods10);   

         
            maCalc = (ma2 + ma5 + ma10) / 3;
        }

 

Here I am getting error:

Operator '+' cannot be applied to operands of type 'MovingAverage' and 'MovingAverage'.

How to achieve this functionality?
 

 

vamsi.rc said:

Thank you very much Panagiotis. Let me try this.

 

PanagiotisCharalampous said:

Hi Vamsi,

Here is the example you requested

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BreakoutSample : Robot
    {

        SimpleMovingAverage _sma1;
        SimpleMovingAverage _sma2;

        int _singalCandleIndex;
        bool _hasCrossedAbove;
        int _breakoutCandleIndex;

        protected override void OnStart()
        {
            _sma1 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 5);
            _sma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 20);
        }

        protected override void OnBar()
        {
            if (_sma1.Result.HasCrossedAbove(_sma2.Result, 0))
            {
                // if sma 1 has crossed above sma 2, we record the index of the bar
                _singalCandleIndex = Bars.ClosePrices.Count - 1;
                _hasCrossedAbove = true;
            }

            if (_hasCrossedAbove && Bars.HighPrices.Last(1) > Bars.HighPrices[_singalCandleIndex])
            {
                // If last bar high is above the signal bar high, we record the breakout index
                _breakoutCandleIndex = _singalCandleIndex = Bars.ClosePrices.Count - 2;
                Print("Breakout Candle Index: " + _breakoutCandleIndex);
            }

        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram

 

 

 


@vamsi.rc

PanagiotisCharalampous
21 May 2020, 08:43

Hi Vamsi,

Moving averages have a Result property which is a DataSeries that contains the values of the indicator. You will need to add these values individually e.g. 

 maCalc = (ma2.Result.LastValue + ma5.Result.LastValue + ma10.Result.LastValue) / 3;

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

vamsi.rc
21 May 2020, 10:31

RE:

Thanks for your reply Panagiotis.

I am trying to code as below in cBot. Kindly help.

private IndicatorDataSeries maCalc;

private MovingAverage ma20, ma50, ma200;

protected override void OnStart()
        {
            // Put your initialization logic here

            ma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, Periods2);

            ma5 = Indicators.SimpleMovingAverage(Bars.ClosePrices, Periods5);   
         
            ma10 = Indicators.SimpleMovingAverage(Bars.ClosePrices, Periods10);   

            maCalc = (ma2.Result.LastValue + ma5.Result.LastValue + ma10.Result.LastValue) / 3;
        }

 

Now I am looking for the crossover candle for this maCalc line as explained below.

 

if(Bars.OpenPrices < maCalc && Bars.ClosePrices > maCalc && Bars.HighPrices > maCalc && Bars.LowPrices < maCalc) (This is one condition of candle CrossOver). 

{

           _signalCandleIndex = Bars.ClosePrices.Count - 1;
           _hasCrossedAbove = true;

}

From this I can get the CrossOver Candle index to proceed further. (But i am not succeed by trying this way)

Later, I need to find out the Breakout of that signal candle High or Low based on TradeType.

if (_hasCrossedAbove && Bars.HighPrices.Last(1) > Bars.HighPrices[_signalCandleIndex])
            {
                // If last bar high is above the signal bar high, we record the breakout index
                _breakoutCandleIndex = _singalCandleIndex = Bars.ClosePrices.Count - 2;
                //Print("Breakout Candle Index: " + _breakoutCandleIndex);
            }

Once breakout candle close, I need to take position either on High or Low.

If (Bars.ClosePrices[0] > Bars.HighPrices.Last(_breakoutCandleIndex))

{

             ExecuteMarketOrder();

}

To achieve this, I am spending hours of time to complete this task, lack of programming experience in cAlgo especially and unable to get desired result.

I request you for your help to complete this task. Kindly help.

Thanks 

R C Vamsi Vardhan

 

PanagiotisCharalampous said:

Hi Vamsi,

Moving averages have a Result property which is a DataSeries that contains the values of the indicator. You will need to add these values individually e.g. 

 maCalc = (ma2.Result.LastValue + ma5.Result.LastValue + ma10.Result.LastValue) / 3;

Best Regards,

Panagiotis 

Join us on Telegram

 


@vamsi.rc

... Deleted by UFO ...