Stop Loss and Take Profit using previous candles

Created at 28 Nov 2024, 10: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!
CA

cassezek

Joined 28.11.2024

Stop Loss and Take Profit using previous candles
28 Nov 2024, 10:43


Hi,
I'm new with cBots/C#/algos and still learning how to program a bot. I need help with a basic bot that I am trying to make. But I am stuck at setting the correct SL and TP. Not sure as well if I have setup my entries correctly. So here is what I want the bot to do.

Buy: When a down fractal forms, and the high of the subsequent bar is higher than the high of the preceding bar, a buy order is placed.
Sell: When an up fractal forms, and the low of the subsequent bar is lower than the low of the preceding bar, a sell order is placed.
Risk Management:

Stop Loss: Set to the low (for buys) or high (for sells) of the fractal bar.
Take Profit: Set to the high (for buys) or low (for sells) of the bar preceding the fractal bar.

Below is the code.
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;   

using System.Threading.Tasks;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None, AddIndicators = true)]   

    public class FractalStrategy : Robot
    {
        [Parameter("Periods", DefaultValue = 5, Group = "Fractal", MinValue = 5)]
        public int Periods { get; set; }

        [Parameter("Volume (Lots)", DefaultValue = 0.01)]
        public double VolumeInLots { get; set; }
        
        [Parameter("Label", DefaultValue = "FractalStrategy")]
        public string Label { get; set; }

        private Fractals _fractals;
        private double _volumeInUnits;

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(Label);
            }
        }


        protected override void OnStart()
        {
            _fractals = Indicators.Fractals(Periods);
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
        }

        protected override void OnBar()
        {
            var currentBarIndex = Bars.Count - 1;
          

            // Check for down fractal and buy condition
            if (_fractals.DownFractal.Last(0) < Bars.LowPrices[currentBarIndex] &&
               Bars.HighPrices[currentBarIndex-1] < Bars.HighPrices[currentBarIndex-3])
               //Bars.LowPrices.Last(2)
               //MarketSeries.Low.Last(2)
            {
                var stopLossPrice1 = Bars.LowPrices.Last(2) - Symbol.PipSize;
                var StopLoss1 = Math.Round((Symbol.Ask - stopLossPrice1) / Symbol.PipSize);
                var takeProfitPrice1 = Bars.HighPrices.Last(3) - Symbol.PipSize;
                var TakeProfit1 = Math.Round((Symbol.Ask - takeProfitPrice1) / Symbol.PipSize);
                
                ClosePositions(TradeType.Sell);
                
                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, "FractalBuy", StopLoss1, TakeProfit1);
            }

            // Check for up fractal and sell condition
            if (_fractals.UpFractal.Last(0) > Bars.HighPrices[currentBarIndex] &&
                Bars.LowPrices[currentBarIndex-1] > Bars.LowPrices[currentBarIndex-3])
            {
                
                var stopLossPrice2 = Bars.HighPrices.Last(2) - Symbol.PipSize;
                var StopLoss2 = Math.Round((Symbol.Ask - stopLossPrice2) / Symbol.PipSize);
                var takeProfitPrice2 = Bars.LowPrices.Last(3) - Symbol.PipSize;
                var TakeProfit2 = Math.Round((Symbol.Ask - takeProfitPrice2) / Symbol.PipSize);

                
                ClosePositions(TradeType.Buy);
                
                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, "FractalSell", StopLoss2, TakeProfit2);
            }
        }
        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;

                ClosePosition(position);
            }
        }
    }
}

 

Thanks,

cassezek


@cassezek
Replies

PanagiotisCharalampous
29 Nov 2024, 07:12

Hi there,

Did you test your cBot? Does is not do what you expect it to do? What does it do instead?

Best regards,

Panagiotis


@PanagiotisCharalampous