Modify Positions is modiying takeprofit when it shouldn't

Created at 27 Jan 2024, 12:24
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!
MI

mihlali700

Joined 22.12.2023

Modify Positions is modiying takeprofit when it shouldn't
27 Jan 2024, 12:24


The strategy I'm using uses a parabolic SAR stop loss but since its not always consistent I have the initial stoploss be at a certain price and for the takeprofit I use pips so I take the currentprice +/- TakeProfit(in pips) to get the takeprofit price for it .

 

                       double AClose3 = Bars.ClosePrices.Last(3);
                        double StopLoss1 = Aclose3; 
                       double takeprofits = Symbol.Ask + TakeProfit;
                        OpenTrade(TradeType.Buy,StopLoss1,takeprofits);
                        
                        
                        double AClose3 = Bars.ClosePrices.Last(3);
                        double StopLoss1 = AClose3;
                        double takeprofits = Symbol.Ask - TakeProfit;
                    
                      
                        OpenTrade(TradeType.Sell,StopLoss1,takeprofits);

 

Here is the opentrade. : 

private void OpenTrade(TradeType type, double StopLoss, double TakeProfit1)
        {
            // calculate volume from lots
            var volume = Symbol.QuantityToVolumeInUnits(Quantity);
            volume = Symbol.NormalizeVolumeInUnits(volume, RoundingMode.Down);
            
            
            
            ExecuteMarketOrder(type, SymbolName, volume, StrategyName, StopLoss, TakeProfit1);
        }

 

But for some weird reason the parabolic SAR code just modifies the take profit when it clearly shouldn't .

       private void SetSARTrailingStop() 
        {
        
        var buyPositions = Positions.FindAll(StrategyName, SymbolName, TradeType.Buy);
        var sellPositions = Positions.FindAll(StrategyName, SymbolName, TradeType.Sell);
        
        foreach ( Position position in buyPositions) 
        {
         
          double newStopLoss = parabolicSAR.Result.LastValue;
          bool isProtected = position.StopLoss.HasValue;
         
           if ( isProtected)
                {
                    if (newStopLoss > Bid)
                        return;
                    if (newStopLoss - position.StopLoss < Symbol.TickSize)
                        return;
                }
          var takeprofits1 = position.TakeProfit;
          ModifyPosition(position, newStopLoss, takeprofits1);
 
        } 
       
       foreach (Position position in sellPositions)
            {
              
              double newStopLoss = parabolicSAR.Result.LastValue;
              bool isProtected = position.StopLoss.HasValue;
            
            if ( isProtected)
                {
                    if (newStopLoss < Bid)
                        return;
                    if (position.StopLoss - newStopLoss < Symbol.TickSize)
                        return;
                }
                var takeprofits1 = position.TakeProfit;

                ModifyPosition(position, newStopLoss, takeprofits1);

            }
        
        
     }

 

 

 

As you can see the TP is different even though no code in there modifies the takeprofit .
I also noticed that the tp price is over 2k pips away when I set the pips to 10, why does that happen ? Anyone know why ? 
This also happens for the stoploss , i don't know why because I set the initial stoploss at a definite price 


@mihlali700
Replies

ctrader.guru
28 Jan 2024, 06:35

Try changing in the foreach statement the return; with the continue;


@ctrader.guru

AlgoCreators
28 Jan 2024, 09:14

Hi, you need to use this command to change the stop loss


Position.ModifyStopLossPips(newStopLoss);

@AlgoCreators

mihlali700
29 Jan 2024, 12:58

RE: Modify Positions is modiying takeprofit when it shouldn't

ctrader.guru said: 

Try changing in the foreach statement the return; with the continue;

The Trailing stop is working, what im trying to figure out is why I get a stop loss  thats more than 1k pips away . 


@mihlali700

mihlali700
29 Jan 2024, 12:58

RE: Modify Positions is modiying takeprofit when it shouldn't

ctrader.guru said: 

Try changing in the foreach statement the return; with the continue;

The Trailing stop is working, what im trying to figure out is why I get a stop loss  thats more than 1k pips away . 


@mihlali700

mihlali700
29 Jan 2024, 12:58

RE: Modify Positions is modiying takeprofit when it shouldn't

ctrader.guru said: 

Try changing in the foreach statement the return; with the continue;

The Trailing stop is working, what im trying to figure out is why I get a stop loss  thats more than 1k pips away . 


@mihlali700

ctrader.guru
29 Jan 2024, 16:23

RE: RE: Modify Positions is modiying takeprofit when it shouldn't

mihlali700 said: 

ctrader.guru said: 

Try changing in the foreach statement the return; with the continue;

The Trailing stop is working, what im trying to figure out is why I get a stop loss  thats more than 1k pips away . 

It would be better if you shared the whole source code, we will be able to help you better.


@ctrader.guru