Category Trend  Published on 25/12/2019

UT BOT

Description

Follow my cTrader Telegram group at https://t.me/cTraderCommunity; everyone can talk about cTrader indicators and algorithm without restrictions, though it is not allowed to spam commercial indicators to sell them. There's also a Discord Server now @ https://discord.gg/5GAPMtp and an Instagram page https://www.instagram.com/ctrader_community/

This is the conversion of UT Bot from tradingview.

 


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class UTBOT : Indicator
    {
        [Parameter("Key", DefaultValue = 1)]
        public double keyvalue { get; set; }
        [Parameter("ATR Period", DefaultValue = 10)]
        public int atrperiod { get; set; }

        private AverageTrueRange xATR;

        [Output("UpTrend", PlotType = PlotType.DiscontinuousLine, LineColor = "Green")]
        public IndicatorDataSeries XATRTrailingStopGreen { get; set; }
        [Output("Continuos", PlotType = PlotType.DiscontinuousLine, LineColor = "Green")]
        public IndicatorDataSeries XATRTrailingStop { get; set; }
        [Output("DownTrend", PlotType = PlotType.DiscontinuousLine, LineColor = "Red")]
        public IndicatorDataSeries XATRTrailingStopRed { get; set; }

        protected override void Initialize()
        {
            xATR = Indicators.AverageTrueRange(atrperiod, MovingAverageType.Exponential);
        }

        public override void Calculate(int index)
        {
            double nLoss = keyvalue * xATR.Result[index];
            XATRTrailingStop[index] = (MarketSeries.Close[index] > XATRTrailingStop[index - 1] && MarketSeries.Close[index - 1] > XATRTrailingStop[index - 1]) ? Math.Max(XATRTrailingStop[index - 1], MarketSeries.Close[index] - nLoss) : (MarketSeries.Close[index] < XATRTrailingStop[index - 1] && MarketSeries.Close[index - 1] < XATRTrailingStop[index - 1]) ? Math.Min(XATRTrailingStop[index - 1], MarketSeries.Close[index] + nLoss) : (MarketSeries.Close[index] > XATRTrailingStop[index - 1]) ? MarketSeries.Close[index] - nLoss : MarketSeries.Close[index] + nLoss;
            XATRTrailingStopGreen[index] = XATRTrailingStop[index] < MarketSeries.Close[index] ? XATRTrailingStop[index] : double.NaN;
            XATRTrailingStopRed[index] = XATRTrailingStop[index] > MarketSeries.Close[index] ? XATRTrailingStop[index] : double.NaN;
        }
    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: UT BOT.algo
  • Rating: 0
  • Installs: 6447
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
PH
phaniophrero · 2 years ago

Hi , could you tell me how can you add those Buy and  Sell labels too ? ,or at least to insert the Buy and Sell text the way is shown in TradingView ?

Thank you.

CT
ctraderbeta · 4 years ago

Hi,
I am trying to get this to work on multiple time frames.  But it only seems to work on the current time frame.
Any help would be appreciated 

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class UTBOTMT : Indicator
    {
        [Parameter("Key", DefaultValue = 1)]
        public double Keyvalue { get; set; }
        [Parameter("ATR Period", DefaultValue = 10)]
        public int Atrperiod { get; set; }

        [Parameter("Atr TimeFrame")]
        public TimeFrame AtrTimeFrame { get; set; }

        private AverageTrueRange xATR;

        [Output("Continuos", PlotType = PlotType.DiscontinuousLine, LineColor = "Yellow")]
        public IndicatorDataSeries XATRTrailingStop { get; set; }
        [Output("UpTrend", PlotType = PlotType.DiscontinuousLine, LineColor = "Green")]
        public IndicatorDataSeries XATRTrailingStopGreen { get; set; }
        [Output("DownTrend", PlotType = PlotType.DiscontinuousLine, LineColor = "Red")]
        public IndicatorDataSeries XATRTrailingStopRed { get; set; }


        protected override void Initialize()
        {
            xATR = Indicators.AverageTrueRange(MarketData.GetBars(AtrTimeFrame), Atrperiod, MovingAverageType.Exponential);
        }

        public override void Calculate(int index)
        {
            double nLoss = Keyvalue * xATR.Result[index];

            XATRTrailingStop[index] = (MarketData.GetBars(AtrTimeFrame).ClosePrices[index] > XATRTrailingStop[index - 1] && MarketData.GetBars(AtrTimeFrame).ClosePrices[index - 1] > XATRTrailingStop[index - 1]) ? Math.Max(XATRTrailingStop[index - 1], MarketData.GetBars(AtrTimeFrame).ClosePrices[index] - nLoss) : (MarketData.GetBars(AtrTimeFrame).ClosePrices[index] < XATRTrailingStop[index - 1] && MarketData.GetBars(AtrTimeFrame).ClosePrices[index - 1] < XATRTrailingStop[index - 1]) ? Math.Min(XATRTrailingStop[index - 1], MarketData.GetBars(AtrTimeFrame).ClosePrices[index] + nLoss) : (MarketData.GetBars(AtrTimeFrame).ClosePrices[index] > XATRTrailingStop[index - 1]) ? MarketData.GetBars(AtrTimeFrame).ClosePrices[index] - nLoss : MarketData.GetBars(AtrTimeFrame).ClosePrices[index] + nLoss;

            XATRTrailingStopGreen[index] = XATRTrailingStop[index] < MarketData.GetBars(AtrTimeFrame).ClosePrices[index] ? XATRTrailingStop[index] : double.NaN;
            XATRTrailingStopRed[index] = XATRTrailingStop[index] > MarketData.GetBars(AtrTimeFrame).ClosePrices[index] ? XATRTrailingStop[index] : double.NaN;

        }
    }
}