Stop cBot triggering on the same Bar/candle once a trade has been closed

Created at 01 Sep 2017, 14:06
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!
CT

ctid362975

Joined 31.08.2017

Stop cBot triggering on the same Bar/candle once a trade has been closed
01 Sep 2017, 14:06


Hey Guys,
This is a bit of a bumb question.  But can anyone show me some code as to how you would stop a cBot from re-entering a trade on the same bar once the previous trade had been closed?

So.  I am looking the close of the previous bar.  

if my signal is true based upon the previous bar.  I immdieatley enter the market.  if the exit criteria are met, I then try to take profit.  

If I take profit on the same bar as which I opened on and close my position on, presently the logic still looks at the previous bar and I re-enter the trade but this time much higher up / lower down which means all the profit I made on the good entry is lost and I loose more to exit.
I want to control the system so that once it has traded it needs to wait for a new signal before I can go enter.  To this point I do not follow a long to short signal.  I could be long long and then short.

If any of that is unclear (probably is), please ping me and I will try to re-explain myself.

Thanks,
ctid


@ctid362975
Replies

Spotware
01 Sep 2017, 14:28

Dear Trader,

See an example below that might help. The logic of the example is to check enter criteria inside OnBar() event so that you enter the marker only once in each bar and to check exit criteria inside OnTick() so that you can exit the marker when your exit criteria are met. You can adjust the code accordingly.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private bool _criteriaToEnter;
        private bool _criteriaToExit;
        protected override void OnStart()
        {
            // Put your initialization logic here
        }
        protected override void OnBar()
        {
            if (_criteriaToEnter)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000);
            }
        }
        protected override void OnTick()
        {
            if (_criteriaToExit)
            {
                ClosePosition(Positions[0]);
            }
        }

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

Let us know if this is what you are looking for.

Best Regards,

cTrader Team 


@Spotware

ctid362975
01 Sep 2017, 17:12

RE:

Spotware said:

Dear Trader,

See an example below that might help. The logic of the example is to check enter criteria inside OnBar() event so that you enter the marker only once in each bar and to check exit criteria inside OnTick() so that you can exit the marker when your exit criteria are met. You can adjust the code accordingly.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private bool _criteriaToEnter;
        private bool _criteriaToExit;
        protected override void OnStart()
        {
            // Put your initialization logic here
        }
        protected override void OnBar()
        {
            if (_criteriaToEnter)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000);
            }
        }
        protected override void OnTick()
        {
            if (_criteriaToExit)
            {
                ClosePosition(Positions[0]);
            }
        }

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

Let us know if this is what you are looking for.

Best Regards,

cTrader Team 

Thanks Guys,

Something so simple.... I should have just stopped to think!  Awesome!


@ctid362975