Global Stop Loss for position with same pair and direction.

Created at 07 Sep 2022, 17:41
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!
MA

marcin.kamaj

Joined 15.01.2022

Global Stop Loss for position with same pair and direction.
07 Sep 2022, 17:41


Hi traders,

 

i'd like ask you that smb met extra fucntion fo cTrader to give possibility to set global Stop Loss if we have opened several trade of same currecny pair in same direction ?

 

Thank you in advance for help.

 

Best Regards,

Marcin


@marcin.kamaj
Replies

PanagiotisCharalampous
08 Sep 2022, 08:32

Hi Marcin,

What do you mean with global stop loss? Same number of pips? Same price?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

Xammo
12 Sep 2022, 13:53

Hi Marcin

Just a few suggestions that might help/give you some ideas - obviously you'd most likely want to knit them into your logic rather than having them as hard set parameters and checking every OnTick with ClosePositionAsync creates additional headaches trying to work out how to not try and close a position that is already in the process of closing! (same goes for ExecuteMarketOrderAsync - I've still yet to work out a graceful/rock solid way to deal with Async stuff!)  but yeh hopefully it helps and best of luck!

 

using cAlgo.API;
using System.Collections.Generic;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("StopLoss_Amount", DefaultValue = 100)]
        public double Param_double_StopLoss_Amount { get; set; }

        [Parameter("StopLoss_Pips", DefaultValue = -10)]
        public double Param_double_StopLoss_Pips { get; set; }

        [Parameter("StopLoss_Price_Sell")]
        public double Param_double_StopLoss_Price_Sell { get; set; }

        [Parameter("StopLoss_Price_Buy")]
        public double Param_double_StopLoss_Price_Buy { get; set; }

        protected override void OnTick()
        {
            Check_StopLoss(TradeType.Sell);
            Check_StopLoss(TradeType.Buy);
        }

        private void Check_StopLoss(TradeType tradeType)
        {
            IEnumerable<Position> pos_All_TradeType = Positions.Where(x => x.SymbolName == SymbolName && x.TradeType == tradeType);

            //THIS
            if (pos_All_TradeType.Sum(x => x.NetProfit) <= Param_double_StopLoss_Amount)
            {
                CloseAll_TradeType(pos_All_TradeType);
            }

            //OR THIS
            if (pos_All_TradeType.Sum(x => x.Pips) <= Param_double_StopLoss_Pips)
            {
                CloseAll_TradeType(pos_All_TradeType);
            }

            //OR THIS
            if (tradeType == TradeType.Sell)
            {
                if (Symbol.Ask > Param_double_StopLoss_Price_Sell)
                {
                    CloseAll_TradeType(pos_All_TradeType);
                }
            }
            else
            {
                if (Symbol.Bid < Param_double_StopLoss_Price_Buy)
                {
                    CloseAll_TradeType(pos_All_TradeType);
                }
            }
        }

        private void CloseAll_TradeType(IEnumerable<Position> pos_All_TradeType)
        {
            foreach (Position pos in pos_All_TradeType)
            {
                ClosePositionAsync(pos);
            }
        }
    }
}

 


@Xammo