Partial Volume close after 2RR not working

Created at 20 Jan 2022, 02: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!
IC

icollocollo

Joined 26.11.2021

Partial Volume close after 2RR not working
20 Jan 2022, 02:18


 

{
                var allPositions = Positions.FindAll(MyLabel, SymbolName);
                foreach (Position p in allPositions)
                {
                    var Volume_units = (int)Symbol.NormalizeVolumeInUnits(Account.Balance * Risk / 100, RoundingMode.ToNearest);
                    var Quantity = p.NetProfit / Volume_units;
                    double newVol = Symbol.NormalizeVolumeInUnits((p.VolumeInUnits / 2), RoundingMode.Up);

                    if (Quantity == 2)
                    {

                        ClosePosition(p, newVol);
                        Print("Partial closing " + newVol + " of " + p + "2RR Achieved!");

                    }


                    // Close trades without stoploss or takeprofit

                    if (p.StopLoss == null || p.TakeProfit == null)
                    {

                        ClosePosition(p);

                    }
                }

            }

 


@icollocollo
Replies

amusleh
20 Jan 2022, 08:43

Hi,

Can you provide more context and what you are after?

If you want to close a position when it reach 2 Risk:Reward, then you can use the Position Pips, if it's double the amount of stop loss in pips then close it.

You don't have to use volume to calculate it.


@amusleh

icollocollo
20 Jan 2022, 09:02

RE:

amusleh said:

Hi,

Can you provide more context and what you are after?

If you want to close a position when it reach 2 Risk:Reward, then you can use the Position Pips, if it's double the amount of stop loss in pips then close it.

You don't have to use volume to calculate it.

Reason i am trying to use Volume is because my SL varies from one to the other. And TP is a multiple of SL, say SL*5. Closing partials at SL * 2 and modifying the remaining to break even at +2 pips.


@icollocollo

amusleh
20 Jan 2022, 09:17

RE: RE:

icollocollo said:

amusleh said:

Hi,

Can you provide more context and what you are after?

If you want to close a position when it reach 2 Risk:Reward, then you can use the Position Pips, if it's double the amount of stop loss in pips then close it.

You don't have to use volume to calculate it.

Reason i am trying to use Volume is because my SL varies from one to the other. And TP is a multiple of SL, say SL*5. Closing partials at SL * 2 and modifying the remaining to break even at +2 pips.

Hi,

It doesn't matter if your SLs varies or not, you can get each position current SL in pips, for example if your position current pips is 10 and your stop loss is 5 pips then it means your position is reached 2 RR, and you can close half of its volume, no need for calculating volume.

 


@amusleh

icollocollo
20 Jan 2022, 16:31 ( Updated at: 20 Jan 2022, 19:11 )

RE: RE: RE:

amusleh said:

icollocollo said:

amusleh said:

Hi,

Can you provide more context and what you are after?

If you want to close a position when it reach 2 Risk:Reward, then you can use the Position Pips, if it's double the amount of stop loss in pips then close it.

You don't have to use volume to calculate it.

Reason i am trying to use Volume is because my SL varies from one to the other. And TP is a multiple of SL, say SL*5. Closing partials at SL * 2 and modifying the remaining to break even at +2 pips.

Hi,

It doesn't matter if your SLs varies or not, you can get each position current SL in pips, for example if your position current pips is 10 and your stop loss is 5 pips then it means your position is reached 2 RR, and you can close half of its volume, no need for calculating volume.

 

UPDATE! Got the code below working,,,

{
            var allPositions = Positions.FindAll(MyLabel, SymbolName);
            foreach (Position p in allPositions)
            {
                double entryPrice = p.EntryPrice;
                double distance = p.TradeType == TradeType.Buy ? (Symbol.Bid - entryPrice) / Symbol.PipSize : (entryPrice - Symbol.Ask) / Symbol.PipSize;
                double newVol = Symbol.NormalizeVolumeInUnits((p.VolumeInUnits / 2), RoundingMode.Up);
                var sl = p.TradeType == TradeType.Buy ? (entryPrice - p.StopLoss) / Symbol.PipSize : (p.StopLoss - entryPrice) / Symbol.PipSize;


                if (p.TradeType == TradeType.Buy)
                {
                    if (distance / sl == 2)
                    {
                        ModifyPosition(p, newVol);
                        Print("Partial closing " + newVol + " of " + p + "2RR Achieved!");
                    }

                }
                if (p.TradeType == TradeType.Sell)
                {
                    if (distance / sl == 2)
                    {
                        ModifyPosition(p, newVol);
                        Print("Partial closing " + newVol + " of " + p + "2RR Achieved!");
                    }

                }


                // Close trades without stoploss or takeprofit

                if (p.StopLoss == null || p.TakeProfit == null)
                {

                    ClosePosition(p);

                }
            }

        }

 


@icollocollo

icollocollo
25 Apr 2022, 01:37 ( Updated at: 25 Apr 2022, 01:41 )

RE: RE: RE: RE:

icollocollo said:

amusleh said:

icollocollo said:

amusleh said:

Hi,

Can you provide more context and what you are after?

If you want to close a position when it reach 2 Risk:Reward, then you can use the Position Pips, if it's double the amount of stop loss in pips then close it.

You don't have to use volume to calculate it.

Reason i am trying to use Volume is because my SL varies from one to the other. And TP is a multiple of SL, say SL*5. Closing partials at SL * 2 and modifying the remaining to break even at +2 pips.

Hi,

It doesn't matter if your SLs varies or not, you can get each position current SL in pips, for example if your position current pips is 10 and your stop loss is 5 pips then it means your position is reached 2 RR, and you can close half of its volume, no need for calculating volume.

 

UPDATE! Got the code below working,,,

{
            var allPositions = Positions.FindAll(MyLabel, SymbolName);
            foreach (Position p in allPositions)
            {
                double entryPrice = p.EntryPrice;
                double distance = p.TradeType == TradeType.Buy ? (Symbol.Bid - entryPrice) / Symbol.PipSize : (entryPrice - Symbol.Ask) / Symbol.PipSize;
                double newVol = Symbol.NormalizeVolumeInUnits((p.VolumeInUnits / 2), RoundingMode.Up);
                var sl = p.TradeType == TradeType.Buy ? (entryPrice - p.StopLoss) / Symbol.PipSize : (p.StopLoss - entryPrice) / Symbol.PipSize;


                if (p.TradeType == TradeType.Buy)
                {
                    if (distance / sl == 2)
                    {
                        ModifyPosition(p, newVol);
                        Print("Partial closing " + newVol + " of " + p + "2RR Achieved!");
                    }

                }
                if (p.TradeType == TradeType.Sell)
                {
                    if (distance / sl == 2)
                    {
                        ModifyPosition(p, newVol);
                        Print("Partial closing " + newVol + " of " + p + "2RR Achieved!");
                    }

                }


                // Close trades without stoploss or takeprofit

                if (p.StopLoss == null || p.TakeProfit == null)
                {

                    ClosePosition(p);

                }
            }

        }

 

Hello @Amusleh here is a problem with this code. I want to prevent a position from being modified twice. Like the position after partials is closed. Should either hit SL or TP. But since price moves up and down when price distance is equal to 2 again. The same percentage of the remaining volume is calculated again. 

E.g 50 % of 1000 is 500. When condition is true again 50% of 500 = 250 is closed again then 125 then 62.5 theb 31.5 until tp or sl is hit. 

Please help

 

 

 

 


@icollocollo

amusleh
26 Apr 2022, 09:21

Hi,

You can store the partially closed position ID on a collection like a List and check if it's already partially closed or not.


@amusleh

icollocollo
26 Apr 2022, 12:00

RE:

amusleh said:

Hi,

You can store the partially closed position ID on a collection like a List and check if it's already partially closed or not.

Thanks amusleh for the reply, i have seen that list offers great way for doing that. Unfortunately am not really experienced with it. Do you have an example of an indicator or cbot that fully exploits linq that can be help in illustrating this?. Also need lists for a supply and demand indicator


@icollocollo

amusleh
27 Apr 2022, 08:22

Hi,

List is part of .NET BCL collections, you can find all you need here: List<T> Class (System.Collections.Generic) | Microsoft Docs

For Linq: Language-Integrated Query (LINQ) (C#) | Microsoft Docs


@amusleh