ninjatrader/indicators/KnowSureThing.cs

280 lines
11 KiB
C#

#region Using declarations
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using System.Xml.Serialization;
#endregion
namespace NinjaTrader.NinjaScript.Indicators
{
[CategoryOrder("Rate of Change", 1)]
[CategoryOrder("Moving Average", 2)]
[CategoryOrder("Weight", 3)]
[CategoryOrder("Visual", 4)]
public class KnowSureThing : Indicator
{
public static int DefaultRoC1 = 10;
public static int DefaultRoC2 = 15;
public static int DefaultRoC3 = 20;
public static int DefaultRoC4 = 30;
public static int DefaultMA1 = 10;
public static int DefaultMA2 = 10;
public static int DefaultMA3 = 10;
public static int DefaultMA4 = 15;
public static int DefaultEMAPeriod = 9;
public static int DefaultW1 = 1;
public static int DefaultW2 = 2;
public static int DefaultW3 = 3;
public static int DefaultW4 = 4;
public static Brush DefaultPositiveValue = Brushes.LimeGreen;
public static Brush DefaultNegativeValue = Brushes.Red;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Know Sure Thing (KST) oscillator";
Name = "Know Sure Thing";
Calculate = Calculate.OnPriceChange;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
RoC1 = DefaultRoC1;
RoC2 = DefaultRoC2;
RoC3 = DefaultRoC3;
RoC4 = DefaultRoC4;
MA1 = DefaultMA1;
MA2 = DefaultMA2;
MA3 = DefaultMA3;
MA4 = DefaultMA4;
EMAPeriod = DefaultEMAPeriod;
W1 = DefaultW1;
W2 = DefaultW2;
W3 = DefaultW3;
W4 = DefaultW4;
PositiveValue = DefaultPositiveValue;
NegativeValue = DefaultNegativeValue;
AddPlot(new Stroke(Brushes.LimeGreen, 3), PlotStyle.Line, "KST");
AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.Line, "EMA");
}
}
protected override void OnBarUpdate()
{
double rateOfChange1 = SMA(ROC(Close, RoC1), MA1)[0];
double rateOfChange2 = SMA(ROC(Close, RoC2), MA2)[0];
double rateOfChange3 = SMA(ROC(Close, RoC3), MA3)[0];
double rateOfChange4 = SMA(ROC(Close, RoC4), MA4)[0];
double kstValue = (rateOfChange1 * W1) + (rateOfChange2 * W2) + (rateOfChange3 * W3) + (rateOfChange4 * W4);
KST[0] = kstValue;
MovingAverage[0] = EMA(KST, EMAPeriod)[0];
if (KST[0] > MovingAverage[0])
PlotBrushes[0][0] = PositiveValue;
else
PlotBrushes[0][0] = NegativeValue;
}
public override string DisplayName
{
get { return Name; }
}
#region Properties
[Browsable(false)]
[XmlIgnore]
public Series<double> KST
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> MovingAverage
{
get { return Values[1]; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 1", Description = "Period used in first ROC calculation", Order = 1, GroupName = "Rate of Change")]
public int RoC1
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 2", Description = "Period used in second ROC calculation", Order = 2, GroupName = "Rate of Change")]
public int RoC2
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 3", Description = "Period used in third ROC calculation", Order = 3, GroupName = "Rate of Change")]
public int RoC3
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 4", Description = "Period used in fourth ROC calculation", Order = 4, GroupName = "Rate of Change")]
public int RoC4
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 1", Description = "Period used in first MA calculation", Order = 1, GroupName = "Moving Average")]
public int MA1
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 2", Description = "Period used in second MA calculation", Order = 2, GroupName = "Moving Average")]
public int MA2
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 3", Description = "Period used in third MA calculation", Order = 3, GroupName = "Moving Average")]
public int MA3
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period 4", Description = "Period used in fourth MA calculation", Order = 4, GroupName = "Moving Average")]
public int MA4
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Signal Period", Description = "Period used in moving average signal calculation", Order = 5, GroupName = "Moving Average")]
public int EMAPeriod
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Weight 1", Description = "First weight used in KST calculation", Order = 1, GroupName = "Weight")]
public int W1
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Weight 2", Description = "Second weight used in KST calculation", Order = 2, GroupName = "Weight")]
public int W2
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Weight 3", Description = "Third weight used in KST calculation", Order = 3, GroupName = "Weight")]
public int W3
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Weight 4", Description = "Fourth weight used in KST calculation", Order = 4, GroupName = "Weight")]
public int W4
{ get; set; }
[XmlIgnore]
[Display(Name = "Positive Value", Description = "Color of positive values", Order = 1, GroupName = "Visual")]
public Brush PositiveValue
{ get; set; }
[XmlIgnore]
[Display(Name = "Negative Value", Description = "Color of negative values", Order = 2, GroupName = "Visual")]
public Brush NegativeValue
{ get; set; }
#endregion
}
}
// Helper constructor for strategies.
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.KnowSureThing KnowSureThing()
{
return indicator.KnowSureThing(
Indicators.KnowSureThing.DefaultRoC1,
Indicators.KnowSureThing.DefaultRoC2,
Indicators.KnowSureThing.DefaultRoC3,
Indicators.KnowSureThing.DefaultRoC4,
Indicators.KnowSureThing.DefaultMA1,
Indicators.KnowSureThing.DefaultMA2,
Indicators.KnowSureThing.DefaultMA3,
Indicators.KnowSureThing.DefaultMA4,
Indicators.KnowSureThing.DefaultEMAPeriod,
Indicators.KnowSureThing.DefaultW1,
Indicators.KnowSureThing.DefaultW2,
Indicators.KnowSureThing.DefaultW3,
Indicators.KnowSureThing.DefaultW4
);
}
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private KnowSureThing[] cacheKnowSureThing;
public KnowSureThing KnowSureThing(int roC1, int roC2, int roC3, int roC4, int mA1, int mA2, int mA3, int mA4, int eMAPeriod, int w1, int w2, int w3, int w4)
{
return KnowSureThing(Input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4);
}
public KnowSureThing KnowSureThing(ISeries<double> input, int roC1, int roC2, int roC3, int roC4, int mA1, int mA2, int mA3, int mA4, int eMAPeriod, int w1, int w2, int w3, int w4)
{
if (cacheKnowSureThing != null)
for (int idx = 0; idx < cacheKnowSureThing.Length; idx++)
if (cacheKnowSureThing[idx] != null && cacheKnowSureThing[idx].RoC1 == roC1 && cacheKnowSureThing[idx].RoC2 == roC2 && cacheKnowSureThing[idx].RoC3 == roC3 && cacheKnowSureThing[idx].RoC4 == roC4 && cacheKnowSureThing[idx].MA1 == mA1 && cacheKnowSureThing[idx].MA2 == mA2 && cacheKnowSureThing[idx].MA3 == mA3 && cacheKnowSureThing[idx].MA4 == mA4 && cacheKnowSureThing[idx].EMAPeriod == eMAPeriod && cacheKnowSureThing[idx].W1 == w1 && cacheKnowSureThing[idx].W2 == w2 && cacheKnowSureThing[idx].W3 == w3 && cacheKnowSureThing[idx].W4 == w4 && cacheKnowSureThing[idx].EqualsInput(input))
return cacheKnowSureThing[idx];
return CacheIndicator<KnowSureThing>(new KnowSureThing(){ RoC1 = roC1, RoC2 = roC2, RoC3 = roC3, RoC4 = roC4, MA1 = mA1, MA2 = mA2, MA3 = mA3, MA4 = mA4, EMAPeriod = eMAPeriod, W1 = w1, W2 = w2, W3 = w3, W4 = w4 }, input, ref cacheKnowSureThing);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.KnowSureThing KnowSureThing(int roC1, int roC2, int roC3, int roC4, int mA1, int mA2, int mA3, int mA4, int eMAPeriod, int w1, int w2, int w3, int w4)
{
return indicator.KnowSureThing(Input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4);
}
public Indicators.KnowSureThing KnowSureThing(ISeries<double> input , int roC1, int roC2, int roC3, int roC4, int mA1, int mA2, int mA3, int mA4, int eMAPeriod, int w1, int w2, int w3, int w4)
{
return indicator.KnowSureThing(input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.KnowSureThing KnowSureThing(int roC1, int roC2, int roC3, int roC4, int mA1, int mA2, int mA3, int mA4, int eMAPeriod, int w1, int w2, int w3, int w4)
{
return indicator.KnowSureThing(Input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4);
}
public Indicators.KnowSureThing KnowSureThing(ISeries<double> input , int roC1, int roC2, int roC3, int roC4, int mA1, int mA2, int mA3, int mA4, int eMAPeriod, int w1, int w2, int w3, int w4)
{
return indicator.KnowSureThing(input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4);
}
}
}
#endregion