Hello,
Would someone please convert this from Ninja to Thinkorswim?
I appreciate it.
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: