EMA Crossover for closing positions

Created at 09 Jun 2020, 22:09
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!
IU

iucpxleps

Joined 01.11.2017

EMA Crossover for closing positions
09 Jun 2020, 22:09


Hi,

I'm trying to setup a rule which basically checks if price crosses 200EMA and if so closes half of the position(s). My problem is after this point if there are 3 bars below the EMA it keeps halving the position 3 times so if it was 100000, becomes 50000 and then 25000 and 12500 etc. What should I do to make it stop at first cross and close below moment for 50000 and don't do it any further? I was thinking crossbelow/above triggers come back true only once at the time crossover happens.

thanks


            //Exit half of posistion at EMA cross
            bool exitHalfLong = (MarketSeries.Close.HasCrossedBelow(BigEMa.Result, 1) && (shortPosition == null));

            // calculated volume to close half of long positions at EMA
            var positionsLong = Positions.FindAll(label, Symbol, TradeType.Buy);

            foreach (var position in positionsLong)
            {
                if (exitHalfLong)
                {
                    ClosePosition(position, position.VolumeInUnits / 2);
                }
            }

 


@iucpxleps
Replies

PanagiotisCharalampous
10 Jun 2020, 08:30

Hi iucpxleps,

You should consider a flag that will be set to true when the conditions become valid and the position is partially closed and set back to false when the MA crosses on the opposite site. Modify the position only when the flag is false.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

iucpxleps
10 Jun 2020, 17:49

RE:

PanagiotisCharalampous said:

Hi iucpxleps,

You should consider a flag that will be set to true when the conditions become valid and the position is partially closed and set back to false when the MA crosses on the opposite site. Modify the position only when the flag is false.

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis,

I'm a bit confused about the difference between 

"set to true when the conditions become valid and the position is partially closed" and "Modify the position only when the flag is false."

how these are different? When conditions are valid and i close the position partially, doesnt it mean modifying it anyway as well?

 

thanks


@iucpxleps

PanagiotisCharalampous
11 Jun 2020, 08:33

Hi iucpxleps,

When you partially close the position, your flag will be set to true meaning that if you check if the flag is false before the partial closing, the partial closing will only run once. Then it will wait until the MAs cross on the opposite side before resetting to false and allow the partial closing to be executed again.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

iucpxleps
11 Jun 2020, 21:14

RE:

PanagiotisCharalampous said:

Hi iucpxleps,

When you partially close the position, your flag will be set to true meaning that if you check if the flag is false before the partial closing, the partial closing will only run once. Then it will wait until the MAs cross on the opposite side before resetting to false and allow the partial closing to be executed again.

Best Regards,

Panagiotis 

Join us on Telegram

Hi Panagiotis,

I think now I have managed to do it thanks :)


@iucpxleps