Supertrend bot

Created at 25 Jan 2023, 17:34
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!
MR

mrlukesprague

Joined 17.09.2021

Supertrend bot
25 Jan 2023, 17:34


Hey gang, 

Seeking some wisdom on this bot. 

using the Supertrend indicator it should open and reverse trades in line with the UpTrend or DownTrend properties  - but when i go to backtest it just doesn't open a position - any help appreciated 

 

 

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SupertrendSample : Robot
    {
        private double _volumeInUnits;

        private Supertrend _supertrend;

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

        [Parameter("Label", DefaultValue = "Sample")]
        public string Label { get; set; }

        [Parameter("Period", DefaultValue = 10)]
        public int Period { get; set; }

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

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            _supertrend = Indicators.Supertrend(Period, Multiplier);
        }

        protected override void OnBar()
        {
            var position = Positions.Find(Label);
            if (position != null)
            {
                if ((position.TradeType == TradeType.Buy && _supertrend.Trend.Last(1) == -1) ||
                    (position.TradeType == TradeType.Sell && _supertrend.Trend.Last(1) == 1))
                {
                    ReversePosition(position);
                }
            }
            else
            {
                if (_supertrend.Trend.Last(1) == 1)
                {
                    ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, null, null);
                }
                else if (_supertrend.Trend.Last(1) == -1)
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, null, null);
                }
            }
        }
    }
}
 


@mrlukesprague