ninjatrader/indicators/RateOfChange.cs

131 lines
4.0 KiB
C#
Raw Normal View History

2024-06-03 19:23:34 +00:00
#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;
using NinjaTrader.Gui.PropertiesTest;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class RateOfChange : Indicator
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @" Basic Rate of Change indicator";
Name = "Rate of Change";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = false;
Period = 14;
AddPlot(new Stroke(Brushes.Yellow, 3), PlotStyle.Line, "Rate of Change");
AddLine(new Stroke(Brushes.Gray, 3), 0, "Origin");
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Period)
return;
double inputBarsAgo = Input[Math.Min(CurrentBar, Period)];
if (inputBarsAgo == 0.0)
Value[0] = 0.0;
else
Value[0] = ((Input[0] - inputBarsAgo) / inputBarsAgo) * 100;
}
public override string DisplayName
{
get { return Name; }
}
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "Rate of Change Period", GroupName = "Rate of Change", Order = 1)]
public int Period { get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private RateOfChange[] cacheRateOfChange;
public RateOfChange RateOfChange(int period)
{
return RateOfChange(Input, period);
}
public RateOfChange RateOfChange(ISeries<double> input, int period)
{
if (cacheRateOfChange != null)
for (int idx = 0; idx < cacheRateOfChange.Length; idx++)
if (cacheRateOfChange[idx] != null && cacheRateOfChange[idx].Period == period && cacheRateOfChange[idx].EqualsInput(input))
return cacheRateOfChange[idx];
return CacheIndicator<RateOfChange>(new RateOfChange(){ Period = period }, input, ref cacheRateOfChange);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.RateOfChange RateOfChange(int period)
{
return indicator.RateOfChange(Input, period);
}
public Indicators.RateOfChange RateOfChange(ISeries<double> input , int period)
{
return indicator.RateOfChange(input, period);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.RateOfChange RateOfChange(int period)
{
return indicator.RateOfChange(Input, period);
}
public Indicators.RateOfChange RateOfChange(ISeries<double> input , int period)
{
return indicator.RateOfChange(input, period);
}
}
}
#endregion