Daily market Open Price Reference in cBot

Created at 30 Sep 2022, 18:16
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!
JW

jwandia2022

Joined 30.09.2022

Daily market Open Price Reference in cBot
30 Sep 2022, 18:16


Hi All,

Is there any way to write a logic that allows a cbot execute a buy/sell trade once the Daily Market Open Price is crossed?

Joyce.


@jwandia2022
Replies

ctid3999979
01 Oct 2022, 10:53

RE:

jwandia2022 said:

Hi All,

Is there any way to write a logic that allows a cbot execute a buy/sell trade once the Daily Market Open Price is crossed?

Joyce.

If I'm understanding your query correctly, you could use the MarketData.GetBars() method to get the daily candles, then write some code such as 

dailyBars = MarketData.GetBars("Daily");

protected override void OnBar()
        {

            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ExecuteMarketOrder()
            }
        }

 


@ctid3999979

jwandia2022
03 Oct 2022, 10:53

RE: RE:

ctid3999979 said:

jwandia2022 said:

Hi All,

Is there any way to write a logic that allows a cbot execute a buy/sell trade once the Daily Market Open Price is crossed?

Joyce.

If I'm understanding your query correctly, you could use the MarketData.GetBars() method to get the daily candles, then write some code such as 

dailyBars = MarketData.GetBars("Daily");

protected override void OnBar()
        {

            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ExecuteMarketOrder()
            }
        }

 

Thanks. Will try working with this solution and see what happens.


@jwandia2022

jwandia2022
03 Oct 2022, 12:50

RE: RE:

ctid3999979 said:

jwandia2022 said:

Hi All,

Is there any way to write a logic that allows a cbot execute a buy/sell trade once the Daily Market Open Price is crossed?

Joyce.

If I'm understanding your query correctly, you could use the MarketData.GetBars() method to get the daily candles, then write some code such as 

dailyBars = MarketData.GetBars("Daily");

protected override void OnBar()
        {

            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ExecuteMarketOrder()
            }
        }

 

Hi Again,

Which syntax should I use before "dailyBars = MarketData.GetBars("Daily")" to call the method?


@jwandia2022

jwandia2022
03 Oct 2022, 14:02

RE: RE: RE:

jwandia2022 said:

ctid3999979 said:

jwandia2022 said:

Hi All,

Is there any way to write a logic that allows a cbot execute a buy/sell trade once the Daily Market Open Price is crossed?

Joyce.

If I'm understanding your query correctly, you could use the MarketData.GetBars() method to get the daily candles, then write some code such as 

dailyBars = MarketData.GetBars("Daily");

protected override void OnBar()
        {

            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ExecuteMarketOrder()
            }
        }

 

Hi Again,

Which syntax should I use before "dailyBars = MarketData.GetBars("Daily")" to call the method?

Below is a sample of the code

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 New : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            dailyBars = MarketData.GetBars("Daily");
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ///Execute Buy Trade
            }

            if (Bars.Last(0).Close < dailyBars.Last(0).Close)
            {
                ///Execute Sell Trade
            }
        }


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

 


@jwandia2022

ctid3999979
03 Oct 2022, 16:01

RE: RE: RE: RE:

jwandia2022 said:

jwandia2022 said:

ctid3999979 said:

jwandia2022 said:

Hi All,

Is there any way to write a logic that allows a cbot execute a buy/sell trade once the Daily Market Open Price is crossed?

Joyce.

If I'm understanding your query correctly, you could use the MarketData.GetBars() method to get the daily candles, then write some code such as 

dailyBars = MarketData.GetBars("Daily");

protected override void OnBar()
        {

            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ExecuteMarketOrder()
            }
        }

 

Hi Again,

Which syntax should I use before "dailyBars = MarketData.GetBars("Daily")" to call the method?

Below is a sample of the code

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 New : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            dailyBars = MarketData.GetBars("Daily");
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ///Execute Buy Trade
            }

            if (Bars.Last(0).Close < dailyBars.Last(0).Close)
            {
                ///Execute Sell Trade
            }
        }


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

 

You forgot to declare the variable for the dailyBars. Above the OnStart() method you just need private Bars dailyBars;

 

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 New : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        private Bars dailyBars;

        protected override void OnStart()
        {
            // Put your initialization logic here
            dailyBars = MarketData.GetBars("Daily");
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ///Execute Buy Trade
            }

            if (Bars.Last(0).Close < dailyBars.Last(0).Close)
            {
                ///Execute Sell Trade
            }
        }


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

 


@ctid3999979

jwandia2022
05 Oct 2022, 10:16

RE: RE: RE: RE: RE:

Thanks. I had not noticed that beforehand.


@jwandia2022

jwandia2022
05 Oct 2022, 11:44 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE: RE: RE: RE:

You forgot to declare the variable for the dailyBars. Above the OnStart() method you just need private Bars dailyBars;

 

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 New : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        private Bars dailyBars;

        protected override void OnStart()
        {
            // Put your initialization logic here
            dailyBars = MarketData.GetBars("Daily");
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            if (Bars.Last(0).Close > dailyBars.Last(0).Close)
            {
                ///Execute Buy Trade
            }

            if (Bars.Last(0).Close < dailyBars.Last(0).Close)
            {
                ///Execute Sell Trade
            }
        }


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

 

I'm still not able to get the sample code above to build on CTrader. Error messages:

 

Kindly assist.

Joyce.


@jwandia2022