ninjatrader/indicators/KnowSureThing.cs

255 lines
11 KiB
C#

#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
{
[CategoryOrder("Rate of Change", 1)]
[CategoryOrder("Moving Average", 2)]
[CategoryOrder("Weight", 3)]
[CategoryOrder("Visual", 4)]
public class KnowSureThing : Indicator
{
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;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
AddPlot(Brushes.LimeGreen, "KST");
AddPlot(Brushes.Yellow, "EMA");
RoC1 = 10;
RoC2 = 15;
RoC3 = 20;
RoC4 = 30;
MA1 = 10;
MA2 = 10;
MA3 = 10;
MA4 = 15;
EMAPeriod = 9;
W1 = 1;
W2 = 2;
W3 = 3;
W4 = 4;
PositiveValue = Brushes.LimeGreen;
NegativeValue = Brushes.Red;
}
}
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 (kstValue > 0.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; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "Positive Value", Description = "Color of positive values", Order = 1, GroupName = "Visual")]
public Brush PositiveValue
{ get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "Negative Value", Description = "Color of negative values", Order = 2, GroupName = "Visual")]
public Brush NegativeValue
{ get; set; }
#endregion
}
}
#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, Brush positiveValue, Brush negativeValue)
{
return KnowSureThing(Input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4, positiveValue, negativeValue);
}
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, Brush positiveValue, Brush negativeValue)
{
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].PositiveValue == positiveValue && cacheKnowSureThing[idx].NegativeValue == negativeValue && 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, PositiveValue = positiveValue, NegativeValue = negativeValue }, 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, Brush positiveValue, Brush negativeValue)
{
return indicator.KnowSureThing(Input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4, positiveValue, negativeValue);
}
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, Brush positiveValue, Brush negativeValue)
{
return indicator.KnowSureThing(input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4, positiveValue, negativeValue);
}
}
}
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, Brush positiveValue, Brush negativeValue)
{
return indicator.KnowSureThing(Input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4, positiveValue, negativeValue);
}
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, Brush positiveValue, Brush negativeValue)
{
return indicator.KnowSureThing(input, roC1, roC2, roC3, roC4, mA1, mA2, mA3, mA4, eMAPeriod, w1, w2, w3, w4, positiveValue, negativeValue);
}
}
}
#endregion