Candle Prices at specific time

Created at 17 Apr 2020, 22:44
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!
SA

samuelbreezey

Joined 10.04.2019

Candle Prices at specific time
17 Apr 2020, 22:44


Hi all

I am creating a breakout bot which opens a pending order at 7am on the hourly timeframe. I want the submitted price of the buy order to be at the previous candles high and the sell order at previous candles low. 

See code below:

 

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 BreakoutBot : Robot
    {
        [Parameter("Instance Name", DefaultValue = "Breakout")]
        public string InstanceName { get; set; }
        [Parameter("Start Time", DefaultValue = 7, MinValue = 0, MaxValue = 24)]
        public double startTime { get; set; }
        [Parameter("Expiry in hours", DefaultValue = 2, MinValue = 0, MaxValue = 24)]
        public double expHrs { get; set; }


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

        protected override void OnTick()
        {


//Last closed bar open price
            var open = MarketSeries.Open.Last(1);

//Last closed bar close price
            var close = MarketSeries.Close.Last(1);

//Last closed bar high price
            var high = MarketSeries.High.Last(1);

//Las closed bar low price
            var low = MarketSeries.Low.Last(1);

            var buyPrice = high;
            var sellPrice = low;

            var expiredtime = Server.Time.AddHours(expHrs);


            if (Positions.Count == 0 && PendingOrders.Count(item => item.OrderType == PendingOrderType.Stop && item.TradeType == TradeType.Buy) == 0)
            {
                if (Server.Time.Hour == startTime)
                {
                    PlaceStopOrder(TradeType.Sell, SymbolName, 1000, sellPrice, InstanceName, 50, 50, expiredtime);
                    PlaceStopOrder(TradeType.Buy, SymbolName, 1000, buyPrice, InstanceName, 50, 50, expiredtime);
                }
            }


            if (Positions.Count == 1)
            {
                foreach (var order in PendingOrders)
                {
                    CancelPendingOrder(order);
                }

            }

        }

    }
}

Any help would be greatly appreciated

Samuel


@samuelbreezey
Replies

samuelbreezey
17 Apr 2020, 22:54

RE:

samuelbreezey said:

Hi all

I am creating a breakout bot which opens a pending order at 7am on the hourly timeframe. I want the submitted price of the buy order to be at the previous candles high and the sell order at previous candles low. 

See code below:

 

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 BreakoutBot : Robot
    {
        [Parameter("Instance Name", DefaultValue = "Breakout")]
        public string InstanceName { get; set; }
        [Parameter("Start Time", DefaultValue = 7, MinValue = 0, MaxValue = 24)]
        public double startTime { get; set; }
        [Parameter("Expiry in hours", DefaultValue = 2, MinValue = 0, MaxValue = 24)]
        public double expHrs { get; set; }


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

        protected override void OnTick()
        {


//Last closed bar open price
            var open = MarketSeries.Open.Last(1);

//Last closed bar close price
            var close = MarketSeries.Close.Last(1);

//Last closed bar high price
            var high = MarketSeries.High.Last(1);

//Las closed bar low price
            var low = MarketSeries.Low.Last(1);

            var buyPrice = high;
            var sellPrice = low;

            var expiredtime = Server.Time.AddHours(expHrs);


            if (Positions.Count == 0 && PendingOrders.Count(item => item.OrderType == PendingOrderType.Stop && item.TradeType == TradeType.Buy) == 0)
            {
                if (Server.Time.Hour == startTime)
                {
                    PlaceStopOrder(TradeType.Sell, SymbolName, 1000, sellPrice, InstanceName, 50, 50, expiredtime);
                    PlaceStopOrder(TradeType.Buy, SymbolName, 1000, buyPrice, InstanceName, 50, 50, expiredtime);
                }
            }


            if (Positions.Count == 1)
            {
                foreach (var order in PendingOrders)
                {
                    CancelPendingOrder(order);
                }

            }

        }

    }
}

Any help would be greatly appreciated

Samuel

Sorry it looks like I have figured it out:

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 BreakoutBot : Robot
    {
        [Parameter("Instance Name", DefaultValue = "Breakout")]
        public string InstanceName { get; set; }
        [Parameter("Start Time", DefaultValue = 7, MinValue = 0, MaxValue = 24)]
        public double startTime { get; set; }
        [Parameter("Expiry in hours", DefaultValue = 2, MinValue = 0, MaxValue = 24)]
        public double expHrs { get; set; }
        [Parameter("Look Back Period (bars)", DefaultValue = 1, MinValue = 1, MaxValue = 10)]
        public int lookBack { get; set; }


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

        protected override void OnTick()
        {


//Last closed bar open price
            var open = MarketSeries.Open.Last(lookBack);

//Last closed bar close price
            var close = MarketSeries.Close.Last(lookBack);

//Last closed bar high price
            var high = MarketSeries.High.Last(lookBack);

//Las closed bar low price
            var low = MarketSeries.Low.Last(lookBack);

            var buyPrice = high;
            var sellPrice = low;

            var expiredtime = Server.Time.AddHours(expHrs);


            if (Positions.Count == 0 && PendingOrders.Count(item => item.OrderType == PendingOrderType.Stop && item.TradeType == TradeType.Buy) == 0)
            {
                if (Server.Time.Hour == startTime)
                {
                    PlaceStopOrder(TradeType.Sell, SymbolName, 1000, sellPrice, InstanceName, 50, 50, expiredtime);
                    PlaceStopOrder(TradeType.Buy, SymbolName, 1000, buyPrice, InstanceName, 50, 50, expiredtime);
                }
            }


            if (Positions.Count == 1)
            {
                foreach (var order in PendingOrders)
                {
                    CancelPendingOrder(order);
                }

            }

        }
        /*      foreach (var position in Positions)
            {
                if (Positions.Count == 1)
                {
                    ClosePosition(position);

                } */



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

 


@samuelbreezey