Not able to double the volume in unsuccessful trade and half the volume in successful trade with a minimum threshold value

Created at 03 May 2023, 10:37
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!
AS

ashish.sah.np

Joined 19.12.2022

Not able to double the volume in unsuccessful trade and half the volume in successful trade with a minimum threshold value
03 May 2023, 10:37


I have coded a supertrend strategy. But not able to double the volume if my last trade was unsuccessful and half the volume if last trade was successful.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BuiltInSuperTrendTrading : Robot
    {
        [Parameter("ATR Period", DefaultValue = 14)]
        public int AtrPeriod { get; set; }

        [Parameter("Multiplier", DefaultValue = 3.0)]
        public double Multiplier { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 20)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 0)]
        public int Volume { get; set; }

        [Parameter("Lowest Volume Threshold", DefaultValue = 1000, MinValue = 0)]
        public int LowestVolumeThreshold { get; set; }

        private Supertrend _superTrend;
        private TradeType? _lastTradeType;

        protected override void OnStart()
        {
            _superTrend = Indicators.Supertrend(AtrPeriod, Multiplier);
            Positions.Closed += OnPositionsClosed;
        }

        protected override void OnBar()
        {
            if (_superTrend.UpTrend.LastValue > 0 && (!_lastTradeType.HasValue || _lastTradeType.Value == TradeType.Sell))
            {
                ClosePreviousPositions();
                OpenLongPosition();
                _lastTradeType = TradeType.Buy;
            }
            else if (_superTrend.DownTrend.LastValue > 0 && (!_lastTradeType.HasValue || _lastTradeType.Value == TradeType.Buy))
            {
                ClosePreviousPositions();
                OpenShortPosition();
                _lastTradeType = TradeType.Sell;
            }
        }

        private void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            if (obj.Position.Label == "BuiltInSuperTrendTrading")
            {
                if (obj.Position.NetProfit < 0)
                {
                    // Close all other positions
                    foreach (var position in Positions)
                    {
                        if (position.SymbolName == SymbolName && position.Label == "BuiltInSuperTrendTrading")
                        {
                            ClosePosition(position);
                        }
                    }
                }

                // Adjust volume based on the net profit of the closed position
                SetNewVolume(obj.Position.NetProfit > 0);
            }
        }

        private void ClosePreviousPositions()
        {
            foreach (var position in Positions)
            {
                if (position.Label == "BuiltInSuperTrendTrading")
                {
                    ClosePosition(position);
                }
            }
        }

        private void OpenLongPosition()
        {
            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "BuiltInSuperTrendTrading", StopLoss, TakeProfit);
        }

        private void OpenShortPosition()
        {
            ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "BuiltInSuperTrendTrading", StopLoss, TakeProfit);
        }

        private void SetNewVolume(bool lastTradeWasSuccessful)
        {
            if (lastTradeWasSuccessful)
            {
                Volume = (int)Math.Ceiling(Volume / 2.0);
            }
            else
            {
                Volume = (int)Math.Ceiling(Volume * 2.0);
            }
            if (Volume < LowestVolumeThreshold)
            {
                Volume = LowestVolumeThreshold;
            }
        }
    }
}

 


@ashish.sah.np