Modify Position

Created at 04 Oct 2020, 21:56
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!
LU

luca.tocchi

Joined 25.03.2020

Modify Position
04 Oct 2020, 21:56


hi I'm trying to learn how to change the stoploss. Why doesn't it work?

I would like to try to put the stoploss at +30 pips when the position reaches +50 pips

thanks

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Valido : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Stop Loss", Group = "Protection", DefaultValue = 100)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 100)]
        public int TakeProfit { get; set; }

        protected override void OnTick()
        {
            var longPosition = Positions.Find("Miaposizione", SymbolName, TradeType.Buy);

            if (longPosition == null)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, 100000, "Miaposizione", StopLoss, TakeProfit);
            }
            else
            {
                if (longPosition.Pips >= Symbol.PipSize * 50)
                    ModifyPosition(longPosition, 30, TakeProfit);
            }
        }
    }
}


@luca.tocchi
Replies

PanagiotisCharalampous
05 Oct 2020, 08:32

Hi Luca,

Here is the correct way to do this

                if (longPosition.Pips >= 50)
                    ModifyPosition(longPosition, longPosition.EntryPrice + Symbol.PipSize * 30, longPosition.TakeProfit);

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luca.tocchi
05 Oct 2020, 13:41

RE:

PanagiotisCharalampous said:

Hi Luca,

Here is the correct way to do this

                if (longPosition.Pips >= 50)
                    ModifyPosition(longPosition, longPosition.EntryPrice + Symbol.PipSize * 30, longPosition.TakeProfit);

Best Regards,

Panagiotis 

Join us on Telegram

thanks a lot!


@luca.tocchi

luca.tocchi
05 Oct 2020, 14:35

RE:

PanagiotisCharalampous said:

Hi Luca,

Here is the correct way to do this

                if (longPosition.Pips >= 50)
                    ModifyPosition(longPosition, longPosition.EntryPrice + Symbol.PipSize * 30, longPosition.TakeProfit);

Best Regards,

Panagiotis 

Join us on Telegram

if once the stop loss is at +30, I want it to become trail?


@luca.tocchi

PanagiotisCharalampous
05 Oct 2020, 14:43

Hi Luca,

Use ModifyTrailingStop.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

luca.tocchi
05 Oct 2020, 18:33

RE:

PanagiotisCharalampous said:

Hi Luca,

Use ModifyTrailingStop.

Best Regards,

Panagiotis 

Join us on Telegram

yes, however, being in onTick () if I put longPosition.ModifyTrailingStop every time it will put the stop loss back to +30

 

if (longPosition.Pips >= 50)

ModifyPosition(longPosition, longPosition.EntryPrice + Symbol.PipSize * 30, longPosition.TakeProfit);

longPosition.ModifyTrailingStop(true);

so i don't think it works

 

 


@luca.tocchi

PanagiotisCharalampous
06 Oct 2020, 07:36

Hi Luca,

You need to check if the stop loss was modified again before or not before modifying it. You can use some flags for this.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous