Adding Stop Loss Problem

Created at 26 Jul 2023, 13:43
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!
LA

lataicmarket

Joined 26.07.2023

Adding Stop Loss Problem
26 Jul 2023, 13:43


Hello,

First time I'm making cBot and I have a problem. How can I add stoploss and take profit to my code ? Below I'm sending my code, could You check please ?

 

using cAlgo.API;
using cAlgo.API.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("Quantity (Lots)", Group = "TakeProfit", DefaultValue = 20)]
        public int TakeProfit { get; set; }
        
        [Parameter("Quantity (Lots)", Group = "StopLoss", DefaultValue = 20)]
        public int StopLoss { get; set; }

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

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

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

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

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

        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" );
                
          
           
                
            
        }
    }
}


@lataicmarket
Replies

PanagiotisChar
27 Jul 2023, 05:11

Hi there,

See below

ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI", StopLoss, TakeProfit);

Aieden Technologies

Need help? Join us on Telegram


 


@PanagiotisChar

lataicmarket
27 Jul 2023, 05:21

RE: Adding Stop Loss Problem

PanagiotisChar said: 

Hi there,

See below

ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "SampleRSI", StopLoss, TakeProfit);

Aieden Technologies

Need help? Join us on Telegram


 

Thank You for the answer. But where exactly should I add this ExecuteMarketOrder which You send me ? Which part of my code ?


@lataicmarket