multiple take profit levels

Created at 22 Nov 2020, 01: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!
DU

duketv

Joined 16.09.2020

multiple take profit levels
22 Nov 2020, 01:09


Hello,
I am curious if anybody has an idea on how to add multiple take profit levels. Perhaps there already is a snppet or a thread here, but I haven't been able to find it.
Would appreciate any tips :)
Thank You

Duke


@duketv
Replies

paanoik@seznam.cz
22 Nov 2020, 02:10

RE:

Hi,

I don't get what do you mean, example what you want to achieve could help. Position can obviously has only one TP level. If it hit the level, position is closed. If you want tu change TP value, you can use method

ModifyPosition(position, SL, TP)

T.


@paanoik@seznam.cz

xabbu
22 Nov 2020, 13:18

hey duketv,

you can devide your initial position in x smaller sub-portions and add different TP levels on each off them. This is how I manage it the NNFX way off doing fx...

 


@xabbu

prosteel1
28 Nov 2020, 06:38

RE:

duketv said:

Hello,
I am curious if anybody has an idea on how to add multiple take profit levels. Perhaps there already is a snppet or a thread here, but I haven't been able to find it.
Would appreciate any tips :)
Thank You

Duke

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleRSIcBot : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", Group = "RSI", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("Partial Close offset", DefaultValue = 10, MinValue = 10, Step = 10)]
        public int Offset { get; set; }

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }
        protected override void OnStop()
        {
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
        }

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < 30)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
            if (rsi.Result.LastValue < 30 + Offset)
            {
                PartialClose(TradeType.Sell);
            }
            if (rsi.Result.LastValue > 70 - Offset)
            {
                PartialClose(TradeType.Buy);
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
                ClosePosition(position);
        }

        private void PartialClose(TradeType tradeType)
        {

            foreach (var position in Positions.FindAll("SampleRSI", SymbolName, tradeType))
            {
                double newVol = Symbol.NormalizeVolumeInUnits((position.VolumeInUnits / 2), RoundingMode.Up);
                if (position.Quantity == Quantity)
                {
                    ClosePosition(position, newVol);
                    Print("Partial closing " + newVol + " of " + position + " due to RSI = " + rsi.Result.LastValue);
                }
            }
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SampleRSI", SymbolName, tradeType);
            var volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI");
        }
    }
}

 


@prosteel1

duketv
01 Dec 2020, 21:43

RE: RE:

paanoik@seznam.cz said:

Hi,

I don't get what do you mean, example what you want to achieve could help. Position can obviously has only one TP level. If it hit the level, position is closed. If you want tu change TP value, you can use method

ModifyPosition(position, SL, TP)

T.

Hi Paanoik,

My idea was to set two different take profit levels, so sell half of my position when it hits TP level 1 and the other half when it hits TP level 2. It looks like Prosteel1 has given a good example


@duketv

duketv
01 Dec 2020, 21:47

RE:

xabbu said:

hey duketv,

you can devide your initial position in x smaller sub-portions and add different TP levels on each off them. This is how I manage it the NNFX way off doing fx...

 

Hi Xabbu,

You discovered my reason for wanting this in the first place ;-) I figured out later that I could circumvent this by opening two positions, but it does make it messier in the results. Anyway good idea. 

ps. I tried the Bressert Inidicator yesterday and worked quite well :)


@duketv

duketv
01 Dec 2020, 21:50

RE: RE:

@prosteel1

YOU ARE A GENIUS

exactly what I was looking for (and actually very simple). Many thanks


@duketv