Convert Fibonacci SuperTrend from NinjaTrader?

J

john3

Active member
2019 Donor
Hello,

Would someone please convert this from Ninja to Thinkorswim?

Code:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    /// <summary>
    /// A modification of the SuperTrend indicator that is based on fibonacci retracements
    ///
    /// Code Written by Tyler Moore
    /// www.FreeIndicators.Com
    ///
    /// For modifications to this code or to have your own custom code written
    /// please visit www.MooreTechLLC.com   
    /// </summary>
    [Description("Fibonacci SuperTrend Indicator\n\nwww.FreeIndicators.com")]
    public class FibonacciSuperTrend : Indicator
    {
        #region Variables
        // Wizard generated variables
            private int length = 14; // Default setting for Lenght
            private double retrace = 38.2; // Default setting for Multiplier
            private bool showArrows = true;
            private string alertFile = @"Alert1.wav";
            private bool useHighLow = true;
            private bool colorBars = true;
            private bool audioAlerts = true;
        // User defined variables (add any user defined variables below)
            private int trend;
            private BoolSeries Trend;
            private double hh = 0, ll = 0;
            private MIN min;
            private MAX max;
        #endregion

        /// <summary>
        /// This method is used to configure the indicator and is called once before any bar data is loaded.
        /// </summary>
        protected override void Initialize()
        {
            Add(new Plot(Color.Green, PlotStyle.Line, "UpTrend"));
            Add(new Plot(Color.Red, PlotStyle.Line, "DownTrend"));
            
            CalculateOnBarClose    = true;
            Overlay                = true;
            PriceTypeSupported    = false;
            Trend = new BoolSeries(this);
        }
        
        protected override void OnStartUp()
        {
            IDataSeries h = useHighLow ? High : Close;
            IDataSeries l = useHighLow ? Low : Close;
            
            min=MIN(l,length);
            max=MAX(h,length);
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar<1)
                return;
            
            double h = useHighLow ? High[0] : Close[0];
            double l = useHighLow ? Low[0] : Close[0];
            
            if (h>max[1])
            {
                hh=h;
                trend=1;
            }
            else if (l<min[1])
            {
                ll=l;
                trend=-1;
            }
                
            Trend.Set(trend==1);
            
            if(Trend[0] && !Trend[1])
            {
                UpTrend.Set(SellPrice);
                UpTrend.Set(1, DownTrend[1]);
                if (ShowArrows)
                    DrawArrowUp(CurrentBar.ToString(), true, 0, UpTrend[0] - TickSize, UpColor);
                if (audioAlerts)
                    PlaySound(alertFile);   
            }
            else if (!Trend[0]  && Trend[1])
            {
                DownTrend.Set(BuyPrice);
                DownTrend.Set(1, UpTrend[1]);
                if (ShowArrows)
                    DrawArrowDown(CurrentBar.ToString(), true, 0, DownTrend[0] + TickSize, DownColor);
                if (audioAlerts)
                    PlaySound(alertFile);   
            }
            else if (Trend[0])
                UpTrend.Set(SellPrice > UpTrend[1] ? SellPrice : UpTrend[1]);
            else
                DownTrend.Set(BuyPrice < DownTrend[1] ? BuyPrice : DownTrend[1]);
                                                    
            // Color bars
            if (colorBars)
            {
                if (trend==1)
                {
                    if (colorBars)
                        BarColor=UpColor;               
                }
                else if (trend==-1)
                {
                    if (colorBars)
                        BarColor=DownColor;               
                }
                else
                    BarColor=Color.Empty;
            }           
        }
        
        private Color UpColor
        {
            get { return Plots[0].Pen.Color; }
        }
        
        private Color DownColor
        {
            get { return Plots[1].Pen.Color; }
        }
        
        private double MyRange
        {
            get { return hh-ll; }                       
        }
    
        private double BuyPrice
        {
            get { return ll+Retracement*MyRange; }                       
        }
        
        private double SellPrice
        {
            get { return hh-Retracement*MyRange; }           
        }
        
        private double Retracement
        {
            get {return retrace/100;}           
        }

        #region Properties
        [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
        [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
        public DataSeries UpTrend
        {
            get { return Values[0]; }
        }

        [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
        [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
        public DataSeries DownTrend
        {
            get { return Values[1]; }
        }

        [Description("ATR Period")]
        [Category("Parameters")]
        public int Length
        {
            get { return length; }
            set { length = Math.Max(1, value); }
        }
        [Description("Show Arrows when Trendline is violated")]
        [Category("Parameters")]
        public bool ShowArrows
        {
            get { return showArrows; }
            set { showArrows = value; }
        }

        [Description("ATR Multiplier")]
        [Category("Parameters")]
        public double Retrace
        {
            get { return retrace; }
            set { retrace = Math.Max(0.01, value); }
        }
        
        [Description("")]
        [Category("Parameters")]
        public string AlertFile
        {
            get { return alertFile; }
            set { alertFile = value; }
        }
        
        [Description("")]
        [Category("Parameters")]
        public bool UseHighLow
        {
            get { return useHighLow; }
            set { useHighLow = value; }
        }
        
        [Description("")]
        [Category("Parameters")]
        public bool ColorBars
        {
            get { return colorBars; }
            set { colorBars = value; }
        }
        
        [Description("")]
        [Category("Parameters")]
        public bool AudioAlerts
        {
            get { return audioAlerts; }
            set { audioAlerts = value; }
        }
        #endregion
    }
}

#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
    public partial class Indicator : IndicatorBase
    {
        private FibonacciSuperTrend[] cacheFibonacciSuperTrend = null;

        private static FibonacciSuperTrend checkFibonacciSuperTrend = new FibonacciSuperTrend();

        /// <summary>
        /// Fibonacci SuperTrend Indicator\n\nwww.FreeIndicators.com
        /// </summary>
        /// <returns></returns>
        public FibonacciSuperTrend FibonacciSuperTrend(string alertFile, bool audioAlerts, bool colorBars, int length, double retrace, bool showArrows, bool useHighLow)
        {
            return FibonacciSuperTrend(Input, alertFile, audioAlerts, colorBars, length, retrace, showArrows, useHighLow);
        }

        /// <summary>
        /// Fibonacci SuperTrend Indicator\n\nwww.FreeIndicators.com
        /// </summary>
        /// <returns></returns>
        public FibonacciSuperTrend FibonacciSuperTrend(Data.IDataSeries input, string alertFile, bool audioAlerts, bool colorBars, int length, double retrace, bool showArrows, bool useHighLow)
        {
            if (cacheFibonacciSuperTrend != null)
                for (int idx = 0; idx < cacheFibonacciSuperTrend.Length; idx++)
                    if (cacheFibonacciSuperTrend[idx].AlertFile == alertFile && cacheFibonacciSuperTrend[idx].AudioAlerts == audioAlerts && cacheFibonacciSuperTrend[idx].ColorBars == colorBars && cacheFibonacciSuperTrend[idx].Length == length && Math.Abs(cacheFibonacciSuperTrend[idx].Retrace - retrace) <= double.Epsilon && cacheFibonacciSuperTrend[idx].ShowArrows == showArrows && cacheFibonacciSuperTrend[idx].UseHighLow == useHighLow && cacheFibonacciSuperTrend[idx].EqualsInput(input))
                        return cacheFibonacciSuperTrend[idx];

            lock (checkFibonacciSuperTrend)
            {
                checkFibonacciSuperTrend.AlertFile = alertFile;
                alertFile = checkFibonacciSuperTrend.AlertFile;
                checkFibonacciSuperTrend.AudioAlerts = audioAlerts;
                audioAlerts = checkFibonacciSuperTrend.AudioAlerts;
                checkFibonacciSuperTrend.ColorBars = colorBars;
                colorBars = checkFibonacciSuperTrend.ColorBars;
                checkFibonacciSuperTrend.Length = length;
                length = checkFibonacciSuperTrend.Length;
                checkFibonacciSuperTrend.Retrace = retrace;
                retrace = checkFibonacciSuperTrend.Retrace;
                checkFibonacciSuperTrend.ShowArrows = showArrows;
                showArrows = checkFibonacciSuperTrend.ShowArrows;
                checkFibonacciSuperTrend.UseHighLow = useHighLow;
                useHighLow = checkFibonacciSuperTrend.UseHighLow;

                if (cacheFibonacciSuperTrend != null)
                    for (int idx = 0; idx < cacheFibonacciSuperTrend.Length; idx++)
                        if (cacheFibonacciSuperTrend[idx].AlertFile == alertFile && cacheFibonacciSuperTrend[idx].AudioAlerts == audioAlerts && cacheFibonacciSuperTrend[idx].ColorBars == colorBars && cacheFibonacciSuperTrend[idx].Length == length && Math.Abs(cacheFibonacciSuperTrend[idx].Retrace - retrace) <= double.Epsilon && cacheFibonacciSuperTrend[idx].ShowArrows == showArrows && cacheFibonacciSuperTrend[idx].UseHighLow == useHighLow && cacheFibonacciSuperTrend[idx].EqualsInput(input))
                            return cacheFibonacciSuperTrend[idx];

                FibonacciSuperTrend indicator = new FibonacciSuperTrend();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
                indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
                indicator.Input = input;
                indicator.AlertFile = alertFile;
                indicator.AudioAlerts = audioAlerts;
                indicator.ColorBars = colorBars;
                indicator.Length = length;
                indicator.Retrace = retrace;
                indicator.ShowArrows = showArrows;
                indicator.UseHighLow = useHighLow;
                Indicators.Add(indicator);
                indicator.SetUp();

                FibonacciSuperTrend[] tmp = new FibonacciSuperTrend[cacheFibonacciSuperTrend == null ? 1 : cacheFibonacciSuperTrend.Length + 1];
                if (cacheFibonacciSuperTrend != null)
                    cacheFibonacciSuperTrend.CopyTo(tmp, 0);
                tmp[tmp.Length - 1] = indicator;
                cacheFibonacciSuperTrend = tmp;
                return indicator;
            }
        }
    }
}

// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
    public partial class Column : ColumnBase
    {
        /// <summary>
        /// Fibonacci SuperTrend Indicator\n\nwww.FreeIndicators.com
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.FibonacciSuperTrend FibonacciSuperTrend(string alertFile, bool audioAlerts, bool colorBars, int length, double retrace, bool showArrows, bool useHighLow)
        {
            return _indicator.FibonacciSuperTrend(Input, alertFile, audioAlerts, colorBars, length, retrace, showArrows, useHighLow);
        }

        /// <summary>
        /// Fibonacci SuperTrend Indicator\n\nwww.FreeIndicators.com
        /// </summary>
        /// <returns></returns>
        public Indicator.FibonacciSuperTrend FibonacciSuperTrend(Data.IDataSeries input, string alertFile, bool audioAlerts, bool colorBars, int length, double retrace, bool showArrows, bool useHighLow)
        {
            return _indicator.FibonacciSuperTrend(input, alertFile, audioAlerts, colorBars, length, retrace, showArrows, useHighLow);
        }
    }
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    public partial class Strategy : StrategyBase
    {
        /// <summary>
        /// Fibonacci SuperTrend Indicator\n\nwww.FreeIndicators.com
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.FibonacciSuperTrend FibonacciSuperTrend(string alertFile, bool audioAlerts, bool colorBars, int length, double retrace, bool showArrows, bool useHighLow)
        {
            return _indicator.FibonacciSuperTrend(Input, alertFile, audioAlerts, colorBars, length, retrace, showArrows, useHighLow);
        }

        /// <summary>
        /// Fibonacci SuperTrend Indicator\n\nwww.FreeIndicators.com
        /// </summary>
        /// <returns></returns>
        public Indicator.FibonacciSuperTrend FibonacciSuperTrend(Data.IDataSeries input, string alertFile, bool audioAlerts, bool colorBars, int length, double retrace, bool showArrows, bool useHighLow)
        {
            if (InInitialize && input == null)
                throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

            return _indicator.FibonacciSuperTrend(input, alertFile, audioAlerts, colorBars, length, retrace, showArrows, useHighLow);
        }
    }
}
#endregion

I appreciate it.
 
Last edited by a moderator:
B

barbaros

Active member
I am also interested in this if anyone can convert it.

I don't understand how the hh and ll in the NT script is working. It is also interesting that max and min values are initialized on startup and not updated.
 
Last edited:
B

barbaros

Active member
@john3 Try this. I'm not sure how close i was able to get it. Let me know how far it is off. I don't have NinjaTrader to compare.

Python:
# Fibonacci SuperTrend
# converted from www.FreeIndicators.com
# 
# 20200615 - barbaros

input length = 14;
input retrace = 38.2;
input useHighLow = yes;
input showArrows = yes;
input colorBars = yes;
input audioAlerts = yes;

def h = if useHighLow then high else close;
def l = if useHighLow then low else close;
def minL = Lowest(l, length);
def maxH = Highest(h, length);

def hh = if h > maxH[1] then h else hh[1];
def ll = if l < minL[1] then l else ll[1];
def trend = if h > maxH[1] then 1 else if l < minL[1] then -1 else trend[1];
def isTrend = trend == 1;

def MyRange = hh-ll;
def Retracement = retrace/100;
def BuyPrice = ll + (Retracement * MyRange);
def SellPrice = hh - (Retracement * MyRange);

def UpTrend = if isTrend and !isTrend[1] then SellPrice else if isTrend and SellPrice > UpTrend[1] then SellPrice else Uptrend[1];
def DownTrend = if !isTrend && isTrend[1] then BuyPrice else if !isTrend and BuyPrice < DownTrend[1] then BuyPrice else DownTrend[1];

# Plots

plot TrendLine = if isTrend then UpTrend else DownTrend;
TrendLine.assignValueColor(if isTrend then Color.GREEN else Color.RED);

plot UpArrow = if showArrows and isTrend and !isTrend[1] then yes else no;
UpArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

plot DownArrow = if showArrows and !isTrend && isTrend[1] then yes else no;
DownArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

# Color Bars

AssignPriceColor(if colorBars and isTrend then Color.GREEN else if colorBars then Color.RED else Color.CURRENT);

# Alerts

Alert(isTrend and !isTrend[1], "Up Trend", Alert.BAR, Sound.Ding);
Alert(!isTrend && isTrend[1], "Down Trend", Alert.BAR, Sound.Ding);
 
Last edited:
J

john3

Active member
2019 Donor
@barbaros Thank you very much, but it doesn't seem to match the Ninja's version. Maybe the issue is that changing the settings in the code doesn't seem to change anything. For example, I've changed "length" and "useHighLow," but it didn't seem to do anything.
 
B

barbaros

Active member
@john3 I might have messed something up in the translation. I updated the previous post. May be try that version.
 
Last edited:
MattATM

MattATM

Active member
VIP
Could you show a screenshot as I don't have NT yet but should be getting it
 

Similar threads

Top