Code Help: Close order after X number of bars

Created at 17 Jul 2014, 04:18
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!
EM

emeeder

Joined 06.05.2014 Blocked

Code Help: Close order after X number of bars
17 Jul 2014, 04:18


Hi guys.

I know very little about coding and I am learning as I go by seeing how existing robots work.

I am not quite sure how to code my cbot to close an order at the end or close of the Xth bar after order was opened.

So lets say I open an order at the start of a new bar and i want to close order at the end of that same bar (or at start of next bar, which is the same thing) No matter what profit is at or anything.

Is there a simple code for this? Maybe something in the format below where 'numberofbars' is a variable?

Maybe a code that counts the number of bars since trade opened??

if (_position.TradeType == TradeType.Buy && numberofbars == 1)
                {
                    Trade.Close(_position);
                }

Any help would be very much appreciated.

Thanks


Replies

modarkat
17 Jul 2014, 09:44

here you go:

        int GetBarsAgo(Position position)
        {
            for (var i = MarketSeries.OpenTime.Count - 1; i >= 0; i--)
            {
                if (position.EntryTime > MarketSeries.OpenTime[i])
                    return MarketSeries.OpenTime.Count - 1 - i;
            }
            return -1;
        }

        protected override void OnTick()
        {
            var barsAgo = GetBarsAgo(position);

            if (barsAgo > 5)
            {
                ClosePosition(position);
            }
        }

 


@modarkat

hiba7rain
20 Jul 2014, 14:06

RE:

modarkat said:

 

hey modarkat

thank you for the comment, I have a question with regards to using bars, is it possible to close opened position if for example the bar value became less than for example moving average?

if yes how to do it?

thanks

here you go:

        int GetBarsAgo(Position position)
        {
            for (var i = MarketSeries.OpenTime.Count - 1; i >= 0; i--)
            {
                if (position.EntryTime > MarketSeries.OpenTime[i])
                    return MarketSeries.OpenTime.Count - 1 - i;
            }
            return -1;
        }

        protected override void OnTick()
        {
            var barsAgo = GetBarsAgo(position);

            if (barsAgo > 5)
            {
                ClosePosition(position);
            }
        }

 

 


@hiba7rain

Invalid
21 Jul 2014, 09:51

RE: RE:

Hi. May be next could help to get an idea

        private SimpleMovingAverage sma;

        protected override void OnStart()
        {
            sma = Indicators.SimpleMovingAverage(MarketSeries.Close, 14);
        }

        protected override void OnBar()
        {
            if (MarketSeries.Close.Last(1) < sma.Result.Last(1))
                ClosePosition(_position);
        }

 

hiba7rain said:

modarkat said:

 

hey modarkat

thank you for the comment, I have a question with regards to using bars, is it possible to close opened position if for example the bar value became less than for example moving average?

if yes how to do it?

thanks

here you go:

        int GetBarsAgo(Position position)
        {
            for (var i = MarketSeries.OpenTime.Count - 1; i >= 0; i--)
            {
                if (position.EntryTime > MarketSeries.OpenTime[i])
                    return MarketSeries.OpenTime.Count - 1 - i;
            }
            return -1;
        }

        protected override void OnTick()
        {
            var barsAgo = GetBarsAgo(position);

            if (barsAgo > 5)
            {
                ClosePosition(position);
            }
        }

 

 

 


@Invalid

hiba7rain
21 Jul 2014, 10:03

RE: RE: RE:

Invalid said:

Thank Invalid,

is the EMA codes and variables different from using SMA?

 

Hi. May by next could help to get an idea

        private SimpleMovingAverage sma;

        protected override void OnStart()
        {
            sma = Indicators.SimpleMovingAverage(MarketSeries.Close, 14);
        }

        protected override void OnBar()
        {
            if (MarketSeries.Close.Last(1) < sma.Result.Last(1))
                ClosePosition(_position);
        }

 

hiba7rain said:

modarkat said:

 

hey modarkat

thank you for the comment, I have a question with regards to using bars, is it possible to close opened position if for example the bar value became less than for example moving average?

if yes how to do it?

thanks

here you go:

        int GetBarsAgo(Position position)
        {
            for (var i = MarketSeries.OpenTime.Count - 1; i >= 0; i--)
            {
                if (position.EntryTime > MarketSeries.OpenTime[i])
                    return MarketSeries.OpenTime.Count - 1 - i;
            }
            return -1;
        }

        protected override void OnTick()
        {
            var barsAgo = GetBarsAgo(position);

            if (barsAgo > 5)
            {
                ClosePosition(position);
            }
        }

 

 

 

 


@hiba7rain

Invalid
21 Jul 2014, 10:14

RE: RE: RE: RE:

The code is the same for EMA

       private ExponentialMovingAverage ema;

        protected override void OnStart()
        {
            ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, 14);
        }

        protected override void OnBar()
        {
            if (MarketSeries.Close.Last(1) < ema.Result.Last(1))
                ClosePosition(_position);
        }

 

hiba7rain said:

Invalid said:

Thank Invalid,

is the EMA codes and variables different from using SMA?


@Invalid

hiba7rain
21 Jul 2014, 10:18

RE: RE: RE: RE: RE:

thanks again,

ill work on it and update you if succeeded.

Invalid said:

The code is the same for EMA

       private ExponentialMovingAverage ema;

        protected override void OnStart()
        {
            ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, 14);
        }

        protected override void OnBar()
        {
            if (MarketSeries.Close.Last(1) < ema.Result.Last(1))
                ClosePosition(_position);
        }

 

hiba7rain said:

Invalid said:

Thank Invalid,

is the EMA codes and variables different from using SMA?

 


@hiba7rain