Position on Closed event

Created at 09 Sep 2022, 15:53
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!
VE

velu130486

Joined 08.11.2019 Blocked

Position on Closed event
09 Sep 2022, 15:53


Dear All,

I am using the Position on Closed event in my bot and it works fine. However my concern is if multiple positions are closed for a same symbol at a same tine, same number of positions are newly executed. However my requirement is to create only 1 position for each symbol.

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var pos = args.Position;
            var sym = pos.SymbolName;
            var close = lowBar[sym].ClosePrices.LastValue;
            double vmacur = vma[sym].Result.LastValue;
            double minvolume = Symbols.GetSymbol(sym).VolumeInUnitsMin;
            if (close > vmacur)
                ExecuteMarketOrder(TradeType.Buy, sym, minvolume, "EA", 0, 0);
            else
                ExecuteMarketOrder(TradeType.Sell, sym, minvolume, "EA", 0, 0);
        }

Please help me to fix this issue

Thanks and Regards

R. Vadivelan


Replies

velu130486
12 Sep 2022, 09:34

RE:

Hello Team,

Could you please help on this topic.

Thanks and Regards

R. Vadivelan

velu130486 said:

Dear All,

I am using the Position on Closed event in my bot and it works fine. However my concern is if multiple positions are closed for a same symbol at a same tine, same number of positions are newly executed. However my requirement is to create only 1 position for each symbol.

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var pos = args.Position;
            var sym = pos.SymbolName;
            var close = lowBar[sym].ClosePrices.LastValue;
            double vmacur = vma[sym].Result.LastValue;
            double minvolume = Symbols.GetSymbol(sym).VolumeInUnitsMin;
            if (close > vmacur)
                ExecuteMarketOrder(TradeType.Buy, sym, minvolume, "EA", 0, 0);
            else
                ExecuteMarketOrder(TradeType.Sell, sym, minvolume, "EA", 0, 0);
        }

Please help me to fix this issue

Thanks and Regards

R. Vadivelan

 


PanagiotisCharalampous
12 Sep 2022, 10:10 ( Updated at: 12 Sep 2022, 10:11 )

Hi Vadivelan,

I am not sure what kind of help you need. If you are looking for suggestions on what to do, then you can set a flag and raise when the first position is opened. Then you can check that flag for subsequent executions and decide accordingly.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

velu130486
12 Sep 2022, 10:27 ( Updated at: 21 Dec 2023, 09:22 )

RE:

Hi Panagiotis,

Sorry for not explain clearly. I am using the Average method to close the positions. As you can see from the below picture multiple positions are closed at the same time for the same symbol. with the above code same number of new positions are created, however my requirement is to open only 1 new position for the symbol

Thanks and Regards

R. Vadivelan

PanagiotisCharalampous said:

Hi Vadivelan,

I am not sure what kind of help you need. If you are looking for suggestions on what to do, then you can set a flag and raise when the first position is opened. Then you can check that flag for subsequent executions and decide accordingly.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook

 


PanagiotisCharalampous
12 Sep 2022, 11:08

Hi Vadivelan,

I gave you some suggestions but I can't write the whole logic for you. If don't know how to do it, hire a programmer.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

Xammo
12 Sep 2022, 13:28

Hi Vadivelan

I'm no pro so don't take the following too seriously just thought it might help you with an idea to get to where you want to be although you probably already realise you need to think things through more about your strategy as you work things out usually two more problems/challenges turn up!

I just had a quick go at coding something up and initially thought a flag would do the job but what about a countdown timer?

It all depends what timeframe you are working on - for example if 10 trades close all at once (even if you use ClosePositionAsync they won't all close at exactly the same millisecond) how long do you want to wait after the one trade has executed to be 'in the clear' to open another trade?

If a bunch of 10 trades closed between 12:00:00 -> 12:00:16 and another closed at 12:01:00 do you want to open a trade at 12:00:00 and 12:01:00 or do you consider them to be closing at the same time - depends on your timeframe kind a thing...

Basically what is the 'wait time' between determining a 'bunch of trades closing at the same time' and a 'series of trades closing one after the other in fairly close succession that you want a new trade to be executed each time'?

Anyway here you go - not tested/could well be issues depending on how you enter trades etc./just quickfire code probably doesn't confirm to coding etiquette/hopefully gives you some ideas and of course best of luck!

 

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Count", DefaultValue = 100)]
        public int Param_int_Count { get; set; }

        private int int_Count;

        protected override void OnStart()
        {
            Positions.Closed += PositionsOnClosed;
        }

        protected override void OnTick() //change to OnBar if you want to count down bars
        {
            if (int_Count > 0)
            {
                --int_Count;
            }
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var pos = args.Position;
            var sym = pos.SymbolName;
            var close = lowBar[sym].ClosePrices.LastValue;
            double vmacur = vma[sym].Result.LastValue;
            double minvolume = Symbols.GetSymbol(sym).VolumeInUnitsMin;

            if (close > vmacur && int_Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, sym, minvolume, "EA", 0, 0);
            }
            else
            {
                ExecuteMarketOrder(TradeType.Sell, sym, minvolume, "EA", 0, 0);
            }

            int_Count = Param_int_Count;
        }

        protected override void OnStop()
        {
            Positions.Closed -= PositionsOnClosed;
        }
    }
}

 


@Xammo

velu130486
12 Sep 2022, 13:52

RE:

Thank you Xammo for your reply. I will try your code and update here if my problem is solved.

Thanks and Regards

R. Vadivelan

Xammo said:

Hi Vadivelan

I'm no pro so don't take the following too seriously just thought it might help you with an idea to get to where you want to be although you probably already realise you need to think things through more about your strategy as you work things out usually two more problems/challenges turn up!

I just had a quick go at coding something up and initially thought a flag would do the job but what about a countdown timer?

It all depends what timeframe you are working on - for example if 10 trades close all at once (even if you use ClosePositionAsync they won't all close at exactly the same millisecond) how long do you want to wait after the one trade has executed to be 'in the clear' to open another trade?

If a bunch of 10 trades closed between 12:00:00 -> 12:00:16 and another closed at 12:01:00 do you want to open a trade at 12:00:00 and 12:01:00 or do you consider them to be closing at the same time - depends on your timeframe kind a thing...

Basically what is the 'wait time' between determining a 'bunch of trades closing at the same time' and a 'series of trades closing one after the other in fairly close succession that you want a new trade to be executed each time'?

Anyway here you go - not tested/could well be issues depending on how you enter trades etc./just quickfire code probably doesn't confirm to coding etiquette/hopefully gives you some ideas and of course best of luck!

 

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Count", DefaultValue = 100)]
        public int Param_int_Count { get; set; }

        private int int_Count;

        protected override void OnStart()
        {
            Positions.Closed += PositionsOnClosed;
        }

        protected override void OnTick() //change to OnBar if you want to count down bars
        {
            if (int_Count > 0)
            {
                --int_Count;
            }
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var pos = args.Position;
            var sym = pos.SymbolName;
            var close = lowBar[sym].ClosePrices.LastValue;
            double vmacur = vma[sym].Result.LastValue;
            double minvolume = Symbols.GetSymbol(sym).VolumeInUnitsMin;

            if (close > vmacur && int_Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, sym, minvolume, "EA", 0, 0);
            }
            else
            {
                ExecuteMarketOrder(TradeType.Sell, sym, minvolume, "EA", 0, 0);
            }

            int_Count = Param_int_Count;
        }

        protected override void OnStop()
        {
            Positions.Closed -= PositionsOnClosed;
        }
    }
}

 

 


velu130486
14 Sep 2022, 14:09

RE: RE:

Hello Xammo,

I tried your code in my Cbot, but it still not worked. It still create same number of new positions when multiple positions are closed.

By the way is there any reason for keeping int_count by default as 100

Thanks and Regards

R. Vadivelan

velu130486 said:

Thank you Xammo for your reply. I will try your code and update here if my problem is solved.

Thanks and Regards

R. Vadivelan

Xammo said:

Hi Vadivelan

I'm no pro so don't take the following too seriously just thought it might help you with an idea to get to where you want to be although you probably already realise you need to think things through more about your strategy as you work things out usually two more problems/challenges turn up!

I just had a quick go at coding something up and initially thought a flag would do the job but what about a countdown timer?

It all depends what timeframe you are working on - for example if 10 trades close all at once (even if you use ClosePositionAsync they won't all close at exactly the same millisecond) how long do you want to wait after the one trade has executed to be 'in the clear' to open another trade?

If a bunch of 10 trades closed between 12:00:00 -> 12:00:16 and another closed at 12:01:00 do you want to open a trade at 12:00:00 and 12:01:00 or do you consider them to be closing at the same time - depends on your timeframe kind a thing...

Basically what is the 'wait time' between determining a 'bunch of trades closing at the same time' and a 'series of trades closing one after the other in fairly close succession that you want a new trade to be executed each time'?

Anyway here you go - not tested/could well be issues depending on how you enter trades etc./just quickfire code probably doesn't confirm to coding etiquette/hopefully gives you some ideas and of course best of luck!

 

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Count", DefaultValue = 100)]
        public int Param_int_Count { get; set; }

        private int int_Count;

        protected override void OnStart()
        {
            Positions.Closed += PositionsOnClosed;
        }

        protected override void OnTick() //change to OnBar if you want to count down bars
        {
            if (int_Count > 0)
            {
                --int_Count;
            }
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            var pos = args.Position;
            var sym = pos.SymbolName;
            var close = lowBar[sym].ClosePrices.LastValue;
            double vmacur = vma[sym].Result.LastValue;
            double minvolume = Symbols.GetSymbol(sym).VolumeInUnitsMin;

            if (close > vmacur && int_Count == 0)
            {
                ExecuteMarketOrder(TradeType.Buy, sym, minvolume, "EA", 0, 0);
            }
            else
            {
                ExecuteMarketOrder(TradeType.Sell, sym, minvolume, "EA", 0, 0);
            }

            int_Count = Param_int_Count;
        }

        protected override void OnStop()
        {
            Positions.Closed -= PositionsOnClosed;
        }
    }
}

 

 

 


Xammo
14 Sep 2022, 15:25

Hi

No that was just a random number - it depends how long you want to wait/how you are running things in your code/strategy

Do you want to wait 100 ticks before a closing trade(s) has allowed one single trade to open before allowing another closing trade(s) to allow the next single trade to open or 100 bars - or 10000 ticks etc.

Basically what is the delay between a bunch of trades closing that allowed the one trade you wanted open to open that you will allow the next bunch of closing trades to allow a single trade to be opened?

Either that or my suggested approach isn't going to work for you I dunno but yeh it was more to give you an idea how you could go about getting to where you want to be but I would guess you haven't much experience (fine no problem we've all been there etc.) if you don't know you can set the 100 to whatever you want so I would also guess you need to get clue'd up more on how things work and really think on what it is you want your strategy to do - for example it might work in one way you are thinking but many things can happen and you need to think what you would want to do if x y z happened and how it could all fit in the code if that makes sense... best o luck!

Oh also if/when you reply -> to prevent the thread here getting majorly long just use the reply button at the top then you don't get all the previous messages displayed in your post - or just delete all the previous messages unless you wanted to quote something or whatever - cheers


@Xammo