SuperTrend Robot

Created at 21 Jun 2013, 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!
virtuesoft's avatar

virtuesoft

Joined 21.06.2013

SuperTrend Robot
21 Jun 2013, 17:34


Hello, 

I've spent the past 2 days trying to create a robot that uses the SuperTrend indicator. However, I've had no luck and I'm really at the end of my tether with it. If any of you fine people could help me out I would greatly appreciate it.

I would like the robot to Buy when the SuperTrend is Green (i.e. when the price is above the SuperTrend), and Sell when the SuperTrend is Red (i.e when the price is below the SuperTrend). When a Buy or Sell position is opened, I would also like to close any existing positions opened in the opposite direction.

I am using the SuperTrend indicator located here...

/algos/indicators/show/136


@virtuesoft
Replies

fzlogic
21 Jun 2013, 18:03

Why don't you post the robot in the robots section so that we can fix/modify  it. Also add as much of the trading strategy logic as possible.


@fzlogic

emeeder
20 Aug 2014, 18:59

I am trying to figure out the same thing.

What Output should the Cbot be looking for?

I have tried the results of these outputs  UpTrend/DownTrend/_upBuffer/_downBuffer being equal to 0 or 1 or -1 but none of those seem to work? I changed the 'private' to 'public' when i tried using the _upBuffer and _downBuffer parameters. But that did not work anyways.

Is it a True or False result i am looking for??

Can anyone have a look at the code below and maybe assist me with this?

Thanks a lot!

 

 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class Supertrend : Indicator
    {
        [Parameter(DefaultValue = 10)]
        public int Period { get; set; }

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

        [Output("UpTrend", Color = Colors.Green, PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries UpTrend { get; set; }

        [Output("DownTrend", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 3)]
        public IndicatorDataSeries DownTrend { get; set; }

        private IndicatorDataSeries _upBuffer;
        private IndicatorDataSeries _downBuffer;
        private AverageTrueRange _averageTrueRange;
        private int[] _trend;
        private bool _changeofTrend;

        protected override void Initialize()
        {
            _trend = new int[1];
            _upBuffer = CreateDataSeries();
            _downBuffer = CreateDataSeries();
            _averageTrueRange = Indicators.AverageTrueRange(Period, MovingAverageType.WilderSmoothing);
        }

        public override void Calculate(int index)
        {
            // Init
            UpTrend[index] = double.NaN;
            DownTrend[index] = double.NaN;

            double median = (MarketSeries.High[index] + MarketSeries.Low[index]) / 2;
            double atr = _averageTrueRange.Result[index];

            _upBuffer[index] = median + Multiplier * atr;
            _downBuffer[index] = median - Multiplier * atr;


            if (index < 1)
            {
                _trend[index] = 1;
                return;
            }

            Array.Resize(ref _trend, _trend.Length + 1);

            // Main Logic
            if (MarketSeries.Close[index] > _upBuffer[index - 1])
            {
                _trend[index] = 1;
                if (_trend[index - 1] == -1)
                    _changeofTrend = true;
            }
            else if (MarketSeries.Close[index] < _downBuffer[index - 1])
            {
                _trend[index] = -1;
                if (_trend[index - 1] == -1)
                    _changeofTrend = true;
            }
            else if (_trend[index - 1] == 1)
            {
                _trend[index] = 1;
                _changeofTrend = false;
            }
            else if (_trend[index - 1] == -1)
            {
                _trend[index] = -1;
                _changeofTrend = false;
            }

            if (_trend[index] < 0 && _trend[index - 1] > 0)
                _upBuffer[index] = median + (Multiplier * atr);
            else if (_trend[index] < 0 && _upBuffer[index] > _upBuffer[index - 1])
                _upBuffer[index] = _upBuffer[index - 1];

            if (_trend[index] > 0 && _trend[index - 1] < 0)
                _downBuffer[index] = median - (Multiplier * atr);
            else if (_trend[index] > 0 && _downBuffer[index] < _downBuffer[index - 1])
                _downBuffer[index] = _downBuffer[index - 1];

            // Draw Indicator
            if (_trend[index] == 1)
            {
                UpTrend[index] = _downBuffer[index];
                if (_changeofTrend)
                {
                    UpTrend[index - 1] = DownTrend[index - 1];
                    _changeofTrend = false;
                }
            }
            else if (_trend[index] == -1)
            {
                DownTrend[index] = _upBuffer[index];
                if (_changeofTrend)
                {
                    DownTrend[index - 1] = UpTrend[index - 1];
                    _changeofTrend = false;
                }
            }
        }
    }
}

 


emeeder
21 Aug 2014, 17:19

virtuesoft, Did you get a cbot to work with the supertrend indicator?

 


virtuesoft
21 Aug 2014, 17:35

RE:

emeeder said:

virtuesoft, Did you get a cbot to work with the supertrend indicator?

 

No, I gave up on it and moved on to other things. Sorry.


@virtuesoft