Add indicator for coloring bars according to trend as defined as closing above / below swing highs / lows
This commit is contained in:
parent
558321e2c5
commit
9149fad567
164
indicators/Trend.cs
Normal file
164
indicators/Trend.cs
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
#region Using declarations
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
using NinjaTrader.Cbi;
|
||||||
|
using NinjaTrader.Gui;
|
||||||
|
using NinjaTrader.Gui.Chart;
|
||||||
|
using NinjaTrader.Gui.SuperDom;
|
||||||
|
using NinjaTrader.Gui.Tools;
|
||||||
|
using NinjaTrader.Data;
|
||||||
|
using NinjaTrader.NinjaScript;
|
||||||
|
using NinjaTrader.Core.FloatingPoint;
|
||||||
|
using NinjaTrader.NinjaScript.DrawingTools;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//This namespace holds Indicators in this folder and is required. Do not change it.
|
||||||
|
namespace NinjaTrader.NinjaScript.Indicators
|
||||||
|
{
|
||||||
|
public class Trend : Indicator
|
||||||
|
{
|
||||||
|
protected override void OnStateChange()
|
||||||
|
{
|
||||||
|
if (State == State.SetDefaults)
|
||||||
|
{
|
||||||
|
Description = @"Colors bars based on closes above previous highs or below previous lows";
|
||||||
|
Name = "Trend";
|
||||||
|
Calculate = Calculate.OnPriceChange;
|
||||||
|
IsOverlay = true;
|
||||||
|
DisplayInDataBox = true;
|
||||||
|
DrawOnPricePanel = true;
|
||||||
|
DrawHorizontalGridLines = true;
|
||||||
|
DrawVerticalGridLines = true;
|
||||||
|
PaintPriceMarkers = true;
|
||||||
|
ScaleJustification = ScaleJustification.Right;
|
||||||
|
IsSuspendedWhileInactive = true;
|
||||||
|
UptrendColor = Brushes.LimeGreen;
|
||||||
|
DowntrendColor = Brushes.Red;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnBarUpdate()
|
||||||
|
{
|
||||||
|
if (CurrentBar < 2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (IsFirstTickOfBar)
|
||||||
|
{
|
||||||
|
double previousHigh = High[2];
|
||||||
|
double previousLow = Low[2];
|
||||||
|
double currentClose = Close[1];
|
||||||
|
|
||||||
|
if (currentClose > previousHigh)
|
||||||
|
{
|
||||||
|
BarBrushes[0] = UptrendColor;
|
||||||
|
CandleOutlineBrushes[0] = UptrendColor;
|
||||||
|
}
|
||||||
|
else if (currentClose < previousLow)
|
||||||
|
{
|
||||||
|
BarBrushes[0] = DowntrendColor;
|
||||||
|
CandleOutlineBrushes[0] = DowntrendColor;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BarBrushes[0] = BarBrushes[1];
|
||||||
|
CandleOutlineBrushes[0] = CandleOutlineBrushes[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string DisplayName
|
||||||
|
{
|
||||||
|
get { return Name; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[NinjaScriptProperty]
|
||||||
|
[XmlIgnore]
|
||||||
|
[Display(Name = "Uptrend Color", GroupName = "Trend", Order = 1)]
|
||||||
|
public Brush UptrendColor { get; set; }
|
||||||
|
|
||||||
|
[Browsable(false)]
|
||||||
|
public string UptrendColorSerialization
|
||||||
|
{
|
||||||
|
get { return Serialize.BrushToString(UptrendColor); }
|
||||||
|
set { UptrendColor = Serialize.StringToBrush(value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
[NinjaScriptProperty]
|
||||||
|
[XmlIgnore]
|
||||||
|
[Display(Name = "Downtrend Color", GroupName = "Trend", Order = 2)]
|
||||||
|
public Brush DowntrendColor { get; set; }
|
||||||
|
|
||||||
|
[Browsable(false)]
|
||||||
|
public string DowntrendColorSerialization
|
||||||
|
{
|
||||||
|
get { return Serialize.BrushToString(DowntrendColor); }
|
||||||
|
set { DowntrendColor = Serialize.StringToBrush(value); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region NinjaScript generated code. Neither change nor remove.
|
||||||
|
|
||||||
|
namespace NinjaTrader.NinjaScript.Indicators
|
||||||
|
{
|
||||||
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
||||||
|
{
|
||||||
|
private Trend[] cacheTrend;
|
||||||
|
public Trend Trend(Brush uptrendColor, Brush downtrendColor)
|
||||||
|
{
|
||||||
|
return Trend(Input, uptrendColor, downtrendColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Trend Trend(ISeries<double> input, Brush uptrendColor, Brush downtrendColor)
|
||||||
|
{
|
||||||
|
if (cacheTrend != null)
|
||||||
|
for (int idx = 0; idx < cacheTrend.Length; idx++)
|
||||||
|
if (cacheTrend[idx] != null && cacheTrend[idx].UptrendColor == uptrendColor && cacheTrend[idx].DowntrendColor == downtrendColor && cacheTrend[idx].EqualsInput(input))
|
||||||
|
return cacheTrend[idx];
|
||||||
|
return CacheIndicator<Trend>(new Trend(){ UptrendColor = uptrendColor, DowntrendColor = downtrendColor }, input, ref cacheTrend);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
||||||
|
{
|
||||||
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
||||||
|
{
|
||||||
|
public Indicators.Trend Trend(Brush uptrendColor, Brush downtrendColor)
|
||||||
|
{
|
||||||
|
return indicator.Trend(Input, uptrendColor, downtrendColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Indicators.Trend Trend(ISeries<double> input , Brush uptrendColor, Brush downtrendColor)
|
||||||
|
{
|
||||||
|
return indicator.Trend(input, uptrendColor, downtrendColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace NinjaTrader.NinjaScript.Strategies
|
||||||
|
{
|
||||||
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
||||||
|
{
|
||||||
|
public Indicators.Trend Trend(Brush uptrendColor, Brush downtrendColor)
|
||||||
|
{
|
||||||
|
return indicator.Trend(Input, uptrendColor, downtrendColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Indicators.Trend Trend(ISeries<double> input , Brush uptrendColor, Brush downtrendColor)
|
||||||
|
{
|
||||||
|
return indicator.Trend(input, uptrendColor, downtrendColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
Loading…
Reference in New Issue
Block a user