How to stop/pause closing an existing position countinuously if given criteria have already been met once?

Created at 04 Dec 2023, 07: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!
JXTA's avatar

JXTA

Joined 23.08.2023

How to stop/pause closing an existing position countinuously if given criteria have already been met once?
04 Dec 2023, 07:44


I am trying to write a formula/code to partially close half of my current position when it hit a winning target, but then I realized that it could sometimes falls into scenario in P1, in P1 the winning pip have successfully hit LongPartiallyC_TargetinPip it triggered partially close the first time, then it fell down below my EntryPrice and bounce up again successfully hit LongPartiallyC_TargetinPip  again causing it partially close 2nd time

P1(showing the traces of how the unedited code partially close my existing position)

foreach (var position in Positions.FindAll(LongLabel, SymbolName, TradeType.Buy))
            {
                if
                (
                
                //Check if the current position net profit more than N pips
                position.Pips > LongPartiallyC_TargetinPip
                
                )
                {
                    if
                    (
                    
                    // The LogicCode Should be write in here //
                  
                    )
                    {
                    // Leave blank to ensure it wont execute PartialClose(TradeType.Buy) again;
                    }
                    else
                    {
                    PartialClose(TradeType.Buy);
                    }
                }
            }

private void PartialClose(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(LongLabel, SymbolName, tradeType))
            {
                double halfVol = position.Quantity / 2;
                if (position.TradeType != tradeType) continue;
                {
                    ClosePosition(position, halfVol);
                    Print("Partial closing " + halfVol + " of " + position);
                }
            }
        }

I dont know how to check if there was a PartialClose(TradeType.Buy) been done before or any way I could do to ensure it wont partially close continuously....if anyone could help, much appreciated !


@JXTA
Replies

PanagiotisCharalampous
04 Dec 2023, 09:11

Hi there,

You should create your own structure where you should save the status of your position and keep track of what happened e.g.   

		public class PositionDetails
        {
            public Position position;
			public bool PartialTPTriggered;
        }    

Then keep a list with one entry per position. When the partial tp is triggered set the PartialTPTriggered for that position to true. If it is triggered, do not trigger it again for that position.

Best regards,

Panagiotis
          
         


@PanagiotisCharalampous

JXTA
04 Dec 2023, 11:11

RE: How to stop/pause closing an existing position countinuously if given criteria have already been met once?

PanagiotisCharalampous said: 

Hi there,

You should create your own structure where you should save the status of your position and keep track of what happened e.g.   

		public class PositionDetails        {            public Position position;			public bool PartialTPTriggered;        }    

Then keep a list with one entry per position. When the partial tp is triggered set the PartialTPTriggered for that position to true. If it is triggered, do not trigger it again for that position.

Best regards,

Panagiotis
          
         

Hi Panagiotis, thanks for your quick reply, tho I understand(maybe) the concept of what you are trying to tell me, but I don't have advanced knowledge in programming, it would be great if you could please show me detail example of the code or any link I could refer to? Really appreciate it.


@JXTA

PanagiotisCharalampous
04 Dec 2023, 11:24

Hi there,

Unfortunately it is a lot of work to create a comprehensive example. If I find some time later, I will.

Best regards,

Panagiotis


@PanagiotisCharalampous

JXTA
04 Dec 2023, 12:57

RE: How to stop/pause closing an existing position countinuously if given criteria have already been met once?

PanagiotisCharalampous said: 

Hi there,

Unfortunately it is a lot of work to create a comprehensive example. If I find some time later, I will.

Best regards,

Panagiotis

Okay, many thanks in advanced ! PanagiotisCharalampous let me know when there are any example I can refer to, I will pay attention to any update of this link, have a great day ahead!


@JXTA

firemyst
05 Dec 2023, 01:23

RE: RE: How to stop/pause closing an existing position countinuously if given criteria have already been met once?

JINGXUAN said: 

PanagiotisCharalampous said: 

Hi there,

Unfortunately it is a lot of work to create a comprehensive example. If I find some time later, I will.

Best regards,

Panagiotis

Okay, many thanks in advanced ! PanagiotisCharalampous let me know when there are any example I can refer to, I will pay attention to any update of this link, have a great day ahead!

I keep track of every position I open internally using Dictionary objects:

 private Dictionary<int, double> _positionEntryPricePoints;
 private Dictionary<int, double> _positionEntryVolumes;
 private Dictionary<int, DateTime> _positionEntryPriceTimes;
 private Dictionary<int, double> _increasePositionSizePoints;
       

The “key” is the position number and the “value” is the name of the Dictionary.

So for instance, if I open a German40 position with 3 lots @ 15:00 at the 12345 point, then:

price points would be (1,12345)

entry volume wouldbe (1,3)

entry price times would be (1, 2023/12/05 15:00)

Now if I increase my size by 1.5 lots at 12400 around 16:00, then:

price points would be (1,12345), (2,12400)

entry volume wouldbe (1,3), (2,1.5)

entry price times would be (1, 2023/12/05 15:00), (2, 2023/12/05 16:00)

increase position size points would now be (2,12400)

You get the gist.

I continuously update my Dictionary objects as appropriate so I know exactly where I'm at and what I've done.

You can do something similar for your closings.

 


@firemyst

JXTA
06 Dec 2023, 02:00 ( Updated at: 06 Dec 2023, 06:16 )

RE: RE: RE: How to stop/pause closing an existing position countinuously if given criteria have already been met once?

firemyst said: 

JINGXUAN said: 

PanagiotisCharalampous said: 

Hi there,

Unfortunately it is a lot of work to create a comprehensive example. If I find some time later, I will.

Best regards,

Panagiotis

Okay, many thanks in advanced ! PanagiotisCharalampous let me know when there are any example I can refer to, I will pay attention to any update of this link, have a great day ahead!

I keep track of every position I open internally using Dictionary objects:

 private Dictionary<int, double> _positionEntryPricePoints; private Dictionary<int, double> _positionEntryVolumes; private Dictionary<int, DateTime> _positionEntryPriceTimes; private Dictionary<int, double> _increasePositionSizePoints;       

The “key” is the position number and the “value” is the name of the Dictionary.

So for instance, if I open a German40 position with 3 lots @ 15:00 at the 12345 point, then:

price points would be (1,12345)

entry volume wouldbe (1,3)

entry price times would be (1, 2023/12/05 15:00)

Now if I increase my size by 1.5 lots at 12400 around 16:00, then:

price points would be (1,12345), (2,12400)

entry volume wouldbe (1,3), (2,1.5)

entry price times would be (1, 2023/12/05 15:00), (2, 2023/12/05 16:00)

increase position size points would now be (2,12400)

You get the gist.

I continuously update my Dictionary objects as appropriate so I know exactly where I'm at and what I've done.

You can do something similar for your closings.

 

Hi firemyst, thanks for your patient answer also very detailed explain, I will try it out and update when I get what I want! Thanks again! Very appreciated it


@JXTA