Replies

grandaislucas
24 Jan 2025, 17:32 ( Updated at: 14 Feb 2025, 18:18 )

RE: ctrader can't get tp and sl and use them when my code says the opposite

firemyst said: 

Maybe it's a language barrier, but nowhere in the code you posted does it show you trying to get the SL or TP from the position that was opened. 

If you are trying to set the SL or TP of the current position, nowhere in the code you posted are you doing that either.

You need to do something like :

ModifyPosition(_positionToModify, _positionToModify.VolumeInUnits, slLevel, tpLevel, ....)

Oh thanks I forgot to send this part of the code !

And sorry for the french comments in the code…

I really appreciate your help !

protected override void OnTimer()
        {
            if (_positionToModify != null && _positionToModify.VolumeInUnits > 0)
            {
                double entryPrice = _positionToModify.EntryPrice;

                // Calcul du SL et du TP
                double slPips = SlPercentage * Symbol.PipSize;
                double tpPips = TpPercentage * Symbol.PipSize;

                double slLevel = _positionToModify.TradeType == TradeType.Buy
                    ? entryPrice - slPips
                    : entryPrice + slPips;

                double tpLevel = _positionToModify.TradeType == TradeType.Buy
                    ? entryPrice + tpPips
                    : entryPrice - tpPips;

                // Appliquer le SL et TP
                var modifyResult = ModifyPosition(_positionToModify, slLevel, tpLevel, ProtectionType.None);

                if (!modifyResult.IsSuccessful)
                {
                    Print("Erreur lors de l'application du SL ou TP : {0}", modifyResult.Error);
                }
                else
                {
                    Print("SL et TP appliqués avec succès : SL = {0}, TP = {1}", slLevel, tpLevel);
                }
            }
        }

@grandaislucas