Replies

dave_gryp
11 Jun 2022, 19:01

RE:

amusleh said:

Hi,

Try this:

using System;
using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ModifyStopLossTakeProfitExample : Robot
    {
        private String label = "ModTP";
        private bool variable = false;

        protected override void OnTick()
        {
            var position = Positions.Find(label, SymbolName, TradeType.Buy);

            if (position == null)
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.QuantityToVolumeInUnits(1), label, 2, 2, "Comment", false);
                variable = true;
            }

            if (variable == true)
            {
                double stopLoss, takeProfit;

                if (position.TradeType == TradeType.Buy)
                {
                    stopLoss = position.EntryPrice - (3 * Symbol.PipSize);
                    takeProfit = position.EntryPrice + (3 * Symbol.PipSize);
                }
                else
                {
                    stopLoss = position.EntryPrice + (3 * Symbol.PipSize);
                    takeProfit = position.EntryPrice - (3 * Symbol.PipSize);
                }

                ModifyPosition(position, stopLoss, takeProfit);
            }
        }
    }
}

If your position is closed immediately after modification then it means the amount of stop loss or take profit you set was very small.

Many Thanks Amusleh. That worked! I am getting used to the modify position now. The new problem that I have is I am getting this error:

08/04/2019 02:36:00.000 | → Modifying position PID2 (SL: null, TP: 1.12196) FAILED with error "TechnicalError", Position PID2

I have 2 positions open a buy and a sell. The position that has the error has been previously modified. I would just like to know if there is some more in depth reference material that I can use to trouble shoot this.

This is the Modify code that is causing my error:

                spread = Symbol.Ask - Symbol.Bid;
                halfSpread = (spread / 2);

                if (Positions.Find(Short).Pips < 0)
                {
                    shortPriceLine = Symbol.Ask - (Math.Abs(Positions.Find(Short).Pips) * pipsFactor /*0.0001*/);
                    double longShortDistance = shortPriceLine - Positions.Find(Long).EntryPrice;
                    double centralPriceLine = (longShortDistance / 2) + Positions.Find(Long).EntryPrice;

                    Positions.Find(Short).ModifyTakeProfitPrice(centralPriceLine + halfSpread);
                    Positions.Find(Long).ModifyStopLossPrice(centralPriceLine - halfSpread);

 

Many Thanks again.

 

 


@dave_gryp