Last active
August 26, 2024 17:23
-
-
Save danielnegri/e6b61053273e3e346d47513bd607a889 to your computer and use it in GitHub Desktop.
NinjaTrader 8 - Power Stochastics Indicator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 PowerStochastics : Indicator | |
{ | |
private Stochastics stochastics; | |
private RSI rsi; | |
private Boolean shouldBuy; | |
private Boolean shouldSell; | |
private int drawnRanges; | |
private const int LIMIT_DRAWN_RANGES = 255; | |
protected override void OnStateChange() | |
{ | |
if (State == State.SetDefaults) | |
{ | |
Description = @"Slow Stochastic."; | |
Name = "Power Stochastics"; | |
Calculate = Calculate.OnPriceChange; | |
IsOverlay = true; | |
DisplayInDataBox = true; | |
DrawOnPricePanel = true; | |
DrawHorizontalGridLines = true; | |
DrawVerticalGridLines = true; | |
PaintPriceMarkers = true; | |
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; | |
//Disable this property if your indicator requires custom values that cumulate with each new market data event. | |
//See Help Guide for additional information. | |
IsSuspendedWhileInactive = true; | |
AddPlot(Brushes.Blue, "Slow Stochastic"); | |
AddPlot(Brushes.Red, "Moving Average"); | |
AddLine(Brushes.Black, 20, NinjaTrader.Custom.Resource.NinjaScriptIndicatorLower); | |
AddLine(Brushes.Black, 50, "Mid Point"); | |
AddLine(Brushes.Black, 80, NinjaTrader.Custom.Resource.NinjaScriptIndicatorUpper); | |
} | |
else if (State == State.DataLoaded) | |
{ | |
drawnRanges = 10; | |
stochastics = Stochastics(3, 14, 3); | |
rsi = RSI(14, 3); | |
} | |
} | |
protected override void OnBarUpdate() | |
{ | |
if (CurrentBar < 14) return; | |
bool isUpBar = IsRising(Close); | |
shouldBuy = CrossAbove(stochastics.K, stochastics.D, 1) && stochastics.K[0] < 20 && isUpBar; | |
shouldSell = CrossBelow(stochastics.K, stochastics.D, 1) && stochastics.K[0] > 80 && !isUpBar; | |
SlowStochastic[0] = stochastics.K[0]; | |
MovingAverage[0] = stochastics.D[0]; | |
if (shouldBuy) | |
{ | |
drawRange(isUpBar, Brushes.Green); | |
} | |
if (shouldSell) | |
{ | |
drawRange(isUpBar, Brushes.Red); | |
} | |
if (stochastics.K[0] < 20 && rsi[0] < 30) | |
{ | |
BarBrushes[0] = Brushes.Green; | |
} | |
else if (stochastics.K[0] > 80 && rsi[0] > 70) | |
{ | |
BarBrushes[0] = Brushes.Red; | |
} | |
} | |
private void drawRange(bool isUpBar, SolidColorBrush color) | |
{ | |
string oldTag = "Power Range #" + ((drawnRanges + 1) % LIMIT_DRAWN_RANGES).ToString(); | |
if (DrawObjects[oldTag] != null) RemoveDrawObject(oldTag); | |
int barAgo = 0; | |
double range = Instrument.MasterInstrument.RoundToTickSize(High[barAgo] - Low[barAgo]); | |
string tag = "Power Range #" + drawnRanges.ToString(); | |
drawnRanges = (drawnRanges + 1) % LIMIT_DRAWN_RANGES; | |
double y = isUpBar ? Low[barAgo] - TickSize : High[barAgo] + TickSize; | |
SimpleFont font = new SimpleFont(); | |
font.Bold = true; | |
font.Size = 8; | |
Draw.Text(this, tag, true, range.ToString(), barAgo, y, 0, color, font, TextAlignment.Center, null, color, 20); | |
} | |
#region Properties | |
[Browsable(false)] | |
[XmlIgnore()] | |
public Series<double> SlowStochastic | |
{ | |
get { return Values[0]; } | |
} | |
[Browsable(false)] | |
[XmlIgnore()] | |
public Series<double> MovingAverage | |
{ | |
get { return Values[1]; } | |
} | |
#endregion | |
} | |
} | |
#region NinjaScript generated code. Neither change nor remove. | |
namespace NinjaTrader.NinjaScript.Indicators | |
{ | |
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase | |
{ | |
private PowerStochastics[] cachePowerStochastics; | |
public PowerStochastics PowerStochastics() | |
{ | |
return PowerStochastics(Input); | |
} | |
public PowerStochastics PowerStochastics(ISeries<double> input) | |
{ | |
if (cachePowerStochastics != null) | |
for (int idx = 0; idx < cachePowerStochastics.Length; idx++) | |
if (cachePowerStochastics[idx] != null && cachePowerStochastics[idx].EqualsInput(input)) | |
return cachePowerStochastics[idx]; | |
return CacheIndicator<PowerStochastics>(new PowerStochastics(), input, ref cachePowerStochastics); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns | |
{ | |
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase | |
{ | |
public Indicators.PowerStochastics PowerStochastics() | |
{ | |
return indicator.PowerStochastics(Input); | |
} | |
public Indicators.PowerStochastics PowerStochastics(ISeries<double> input ) | |
{ | |
return indicator.PowerStochastics(input); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.Strategies | |
{ | |
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase | |
{ | |
public Indicators.PowerStochastics PowerStochastics() | |
{ | |
return indicator.PowerStochastics(Input); | |
} | |
public Indicators.PowerStochastics PowerStochastics(ISeries<double> input ) | |
{ | |
return indicator.PowerStochastics(input); | |
} | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment