#region Using declarations using System; using System.Collections; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Gui.Chart; #endregion
// This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { /// <summary> /// TdSequential /// [Description("Td Countdown")] public class TdSequential : Indicator { #region Variables int countdownBuyCount = 0; int countdownSellCount = 0; bool countdownBuy = false; bool countdownSell = false; bool logic = false; // double fiveBuy = 0; double eightBuy = 0; double eightSell = double.PositiveInfinity; double support = 0; double resistance = double.PositiveInfinity; int[] currentBuy = new int[13]; int[] currentSell = new int[13]; int setupCount = 0; bool initSetupBull = false; bool initSetupBear = false; int setupBuyCount = 0; int setupSellCount = 0; double buyPerfection = 0; double sellPerfection = double.PositiveInfinity; #endregion
/// /// This method is used to configure the indicator and is called once before any bar data is loaded. /// protected override void Initialize() { CalculateOnBarClose = true; Overlay = true; PriceTypeSupported = false; } #region Resets void ResetBull() { initSetupBull = false; setupBuyCount = 0; } void ResetBear() { initSetupBear = false; setupSellCount = 0; } #endregion #region TD Price Bull bool PriceBull(int barsAgo) { int i = barsAgo; int j = i + 4; if (Close[i] < Close[j]) { return true; } return false; } #endregion #region TD Price Bear bool PriceBear(int barsAgo) { int i = barsAgo; int j = i + 4; if (Close[i] > Close[j]) { return true; } return false; } #endregion #region TD Price Flip Bull bool PriceFlipBull(int barsAgo) { int i = barsAgo; int j = i + 1; if (PriceBull(i) && PriceBear(j)) { return true; } return false; } #endregion #region TD Price Flip Bear bool PriceFlipBear(int barsAgo) { int i = barsAgo; int j = i + 1; if (PriceBear(i) && PriceBull(j)) { return true; } return false; } #endregion #region TD Setup Buy public bool SetupBuy() { // Begins TD Buy Setup if (PriceFlipBull(0)) { initSetupBull = true; setupBuyCount++; double position = (Low[0] + Low[0] + Low[1] + Low[2] + Low[3])/5 - (ATR(4)[0] + ATR(4)[1] + ATR(4)[2] + ATR(4)[3])/2; DrawText((CurrentBar.ToString() + "BullSetup"), setupBuyCount.ToString(), 0, position, Color.Green); } // Continues TD Buy Setup else if (initSetupBull && PriceBull(0)) { setupBuyCount++; double position = (Low[0] + Low[0] + Low[1] + Low[2] + Low[3])/5 - (ATR(4)[0] + ATR(4)[1] + ATR(4)[2] + ATR(4)[3])/2; DrawText((CurrentBar.ToString() + "BullSetup"), setupBuyCount.ToString(), 0, position, Color.Green); } // Cancels TD Buy Setup else if (PriceFlipBear(0)) { for (int i = 0; i <= (setupBuyCount % 9); i++) { string myTag = (CurrentBar - i).ToString() + "BullSetup"; RemoveDrawObject(myTag); } ResetBull(); } // TD Resistance Lines if (initSetupBull && (setupBuyCount % 9) == 0) { DrawLine((CurrentBar.ToString() + "Resistance 1"), false, 8, MAX(High, 9)[0], 0, MAX(High, 9)[0], Color.Red, DashStyle.Dash, 1); DrawLine((CurrentBar.ToString() + "Resistance 2"), false, 12, MAX(High, 13)[0], 0, MAX(High, 13)[0], Color.Red, DashStyle.Solid, 2); // TD Buy Setup Perfection if ((Low[0] < Low[2] || Low[0] < Low[3] || Low[1] < Low[2] || Low[1] < Low[3]) && Low[0] < Low[1]) { DrawArrowUp((CurrentBar.ToString() + "Arrow"), 0, Low[0] - (ATR(1)[0]), Color.Green); } else { buyPerfection = MIN(Low, 4)[0]; } return true; } // TD Buy Setup Perfection After Completion of Setup if (Low[0] < buyPerfection) { DrawArrowUp((CurrentBar.ToString() + "Arrow"), 0, Low[0] - (ATR(1)[0]), Color.Green); buyPerfection = 0; } return false; } #endregion #region TD Setup Sell public bool SetupSell() { // Begins TD Sell Setup if (PriceFlipBear(0)) { initSetupBear = true; setupSellCount++; double position = (High[0] + High[0] + High[1] + High[2] + High[3])/5 + (ATR(4)[0] + ATR(4)[1] + ATR(4)[2] + ATR(4)[3])/2; DrawText((CurrentBar.ToString() + "BearSetup"), setupSellCount.ToString(), 0, High[0] + ATR(4)[0], Color.Red); } // Continues TD Sell Setup else if (initSetupBear && PriceBear(0)) { setupSellCount++; double position = (High[0] + High[0] + High[1] + High[2] + High[3])/5 + (ATR(4)[0] + ATR(4)[1] + ATR(4)[2] + ATR(4)[3])/2; DrawText((CurrentBar.ToString() + "BearSetup"), setupSellCount.ToString(), 0, position, Color.Red); } // Cancels TD Sell Setup else if (PriceFlipBull(0)) { for (int i = 0; i <= (setupSellCount % 9); i++) { string myTag = (CurrentBar - i).ToString() + "BearSetup"; RemoveDrawObject(myTag); } ResetBear(); } // TD Support Lines if (initSetupBear && (setupSellCount % 9) == 0) { DrawLine((CurrentBar.ToString() + "Support 1"), false, 8, MIN(Low, 9)[0], 1, MIN(Low, 9)[0], Color.Green, DashStyle.Dash, 1); DrawLine((CurrentBar.ToString() + "Support 2"), false, 12, MIN(Low, 13)[0], 1, MIN(Low, 13)[0], Color.Green, DashStyle.Solid, 2); // TD Sell Setup Perfection if ((High[0] > High[2] || High[0] > High[3] || High[1] > High[2] || High[1] > High[3]) && High[0] > High[1]) { DrawArrowDown((CurrentBar.ToString() + "Arrow"), 0, High[0] + (ATR(1)[0]), Color.Red); } else { sellPerfection = MAX(High, 4)[0]; } return true; } // TD Sell Setup Perfection After Comletion of Setup if (High[0] > sellPerfection) { DrawArrowDown((CurrentBar.ToString() + "Arrow"), 0, High[0] + (ATR(1)[0]), Color.Red); sellPerfection = Double.PositiveInfinity; } return false; } #endregion #region Buy public void Buy(int barsAgo) { int i = barsAgo; int j = i + 2; if (Close[i] < Close[j]) { countdownBuyCount++; switch (countdownBuyCount) { // case 5: // fiveBuy = Close[0]; // goto default; case 8: // if (Close[0] < fiveBuy) // { eightBuy = Close[0]; goto default; // } // else // { // countdownBuyCount--; // } // break; case 13: if (Close[0] <= eightBuy) { DrawText((CurrentBar.ToString() + "Bull"), countdownBuyCount.ToString(), 0, Low[0] - (Range()[0] * 0.6), Color.LightSeaGreen); DrawArrowUp((CurrentBar.ToString() + "Arrow"), 0, Low[0] - (Range()[0] * 0.6), Color.LightSeaGreen); countdownBuyCount = 0; countdownBuy = false; } else { DrawText((CurrentBar.ToString() + "Bull"), "+", 0, Low[0] - (Range()[0] * 0.6), Color.LightSeaGreen); countdownBuyCount--; } break; default: double position = (Low[0] + Low[0] + Low[1] + Low[2] + Low[3])/5 - (ATR(4)[0] + ATR(4)[1] + ATR(4)[2] + ATR(4)[3])/1.5; DrawText((CurrentBar.ToString() + "Bull"), countdownBuyCount.ToString(), 0, position, Color.LightSeaGreen); currentBuy[countdownBuyCount] = CurrentBar; break; } } // Recycle Count if (Low[0] > resistance || TdSetup().Sell()) { double position = (Low[0] + Low[0] + Low[1] + Low[2] + Low[3])/5 - (ATR(4)[0] + ATR(4)[1] + ATR(4)[2] + ATR(4)[3])/2; DrawText((CurrentBar.ToString() + "Bull"), "R", 0, position, Color.LightSeaGreen); countdownBuy = false; for (int k = 0; k <= countdownBuyCount; k++) { RemoveDrawObject(currentBuy[k] + "Bull"); } countdownBuyCount = 0; } } #endregion #region Sell public void Sell(int barsAgo) { int i = barsAgo; int j = i + 2; if (Close[i] > Close[j]) { countdownSellCount++; switch (countdownSellCount) {
case 8: eightSell = Close[0]; goto default; case 13: if (Close[0] >= eightSell) { DrawText((CurrentBar.ToString() + "Bear"), countdownSellCount.ToString(), 0, High[0] + (Range()[0] * 0.6), Color.Salmon); DrawArrowDown((CurrentBar.ToString() + "Arrow"), 0, High[0] + (Range()[0] * 0.6), Color.Salmon); countdownSellCount = 0; countdownSell = false; } else { DrawText((CurrentBar.ToString() + "Bear"), "+", 0, High[0] + (Range()[0] * 0.6), Color.Salmon); countdownSellCount--; } break; default: double position = (High[0] + High[0] + High[1] + High[2] + High[3])/5 + (ATR(4)[0] + ATR(4)[1] + ATR(4)[2] + ATR(4)[3])/1.5; DrawText((CurrentBar.ToString() + "Bear"), countdownSellCount.ToString(), 0, position, Color.Salmon); currentSell[countdownSellCount] = CurrentBar; break; } } // Recycle Count if (High[0] < support || TdSetup().Buy()) { DrawText((CurrentBar.ToString() + "Bear"), "R", 0, High[0] + (Range()[0] * 0.6), Color.Salmon); countdownSell = false; for (int k = 0; k <= countdownSellCount; k++) { RemoveDrawObject(currentSell[k] + "Bear"); } countdownSellCount = 0; } } #endregion
/// /// Called on each bar update event (incoming tick) /// protected override void OnBarUpdate() { if (CurrentBar > 30) { if (SetupBuy()) { countdownBuy = true; resistance = MAX(High, 13)[0]; } if (countdownBuy) { Buy(0); } if (SetupSell()) { countdownSell = true; support = MIN(Low, 13)[0]; } if (countdownSell) { Sell(0); } } }
#region Properties
#endregion } } #region NinjaScript generated code. Neither change nor remove. // This namespace holds all indicators and is required. Do not change it. namespace NinjaTrader.Indicator { public partial class Indicator : IndicatorBase { private TdSequential[] cacheTdSequential = null;
private static TdSequential checkTdSequential = new TdSequential();
/// <summary> /// Td Countdown /// </summary> /// <returns></returns> public TdSequential TdSequential() { return TdSequential(Input); }
/// <summary> /// Td Countdown /// </summary> /// <returns></returns> public TdSequential TdSequential(Data.IDataSeries input) {
if (cacheTdSequential != null) for (int idx = 0; idx < cacheTdSequential.Length; idx++) if (cacheTdSequential[idx].EqualsInput(input)) return cacheTdSequential[idx];
TdSequential indicator = new TdSequential(); indicator.BarsRequired = BarsRequired; indicator.CalculateOnBarClose = CalculateOnBarClose; indicator.Input = input; indicator.SetUp();
TdSequential[] tmp = new TdSequential[cacheTdSequential == null ? 1 : cacheTdSequential.Length + 1]; if (cacheTdSequential != null) cacheTdSequential.CopyTo(tmp, 0); tmp[tmp.Length - 1] = indicator; cacheTdSequential = tmp; Indicators.Add(indicator);
return indicator; }
} }
// This namespace holds all market analyzer column definitions and is required. Do not change it. namespace NinjaTrader.MarketAnalyzer { public partial class Column : ColumnBase { /// <summary> /// Td Countdown /// </summary> /// <returns></returns> [Gui.Design.WizardCondition("Indicator")] public Indicator.TdSequential TdSequential() { return _indicator.TdSequential(Input); }
/// <summary> /// Td Countdown /// </summary> /// <returns></returns> public Indicator.TdSequential TdSequential(Data.IDataSeries input) { return _indicator.TdSequential(input); }
} }
// This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { public partial class Strategy : StrategyBase { /// <summary> /// Td Countdown /// </summary> /// <returns></returns> [Gui.Design.WizardCondition("Indicator")] public Indicator.TdSequential TdSequential() { return _indicator.TdSequential(Input); }
/// <summary> /// Td Countdown /// </summary> /// <returns></returns> public Indicator.TdSequential TdSequential(Data.IDataSeries input) { if (InInitialize && input == null) throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.TdSequential(input); }
} } #endregion
|