Untitled

 avatar
unknown
plain_text
a year ago
20 kB
8
Indexable
#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 SharpDX;
using System.Security.Cryptography;
#endregion

//This namespace holds Indicators in this folder and is required. Do not change it. 
namespace NinjaTrader.NinjaScript.Indicators.PROS
{
    public class NubePRO : Indicator
    {
        #region Variables
        private bool colorbars = true;
        private bool colorOutline = true;
        private bool drawArrows = true;


        private int emah = 34; // Default setting for Emah
        private int emal = 34; // Default setting for Emal

        private Brush barColorCondition1 = Brushes.Chartreuse;
        private Brush barColorCondition2 = Brushes.Green;
        private Brush barColorCondition3 = Brushes.LightBlue;
        private Brush barColorCondition4 = Brushes.RoyalBlue;
        private Brush barColorCondition5 = Brushes.DarkOrange;
        private Brush barColorCondition6 = Brushes.Red;

        private Brush candleOutlineCondition1 = Brushes.Chartreuse;
        private Brush candleOutlineCondition2 = Brushes.Green;
        private Brush candleOutlineCondition3 = Brushes.LightBlue;
        private Brush candleOutlineCondition4 = Brushes.RoyalBlue;
        private Brush candleOutlineCondition5 = Brushes.DarkOrange;
        private Brush candleOutlineCondition6 = Brushes.Red;

        private int trend;
        private ISeries<double> maHigh, maLow;

        private bool condition1 = false;
        private bool condition6 = false;

        private bool arrowUp = false;
        private bool arrowDown = false;


        private double cciVal = 0.0;
        private double atrVal = 0.0;
        private double upTrend = 0.0;
        private double downTrend = 0.0;

        // Additional variables for the new channel plot
        private double channelAtrVal = 0.0;
        private double upTrendChannel = 0.0;
        private double downTrendChannel = 0.0;
        private Series<double> channelTrend;

        private int savedUBar = 0;
        private int savedDBar = 0;

        // User defined variables (add any user defined variables below)
        #endregion


        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Description = @"";
                Name = "NubePRO";
                Calculate = Calculate.OnBarClose;
                IsOverlay = true;
                cciPeriod = 20;
                atrPeriod = 5;
                atrMult = 1.0;
                channelATRMultiplier = 3;
                channelATRPeriod = 5;
                DisplayInDataBox = true;
                DrawOnPricePanel = true;
                DrawHorizontalGridLines = true;
                DrawVerticalGridLines = true;
                PaintPriceMarkers = true;
                HighlightChart = true;
                ScaleJustification = ScaleJustification.Right;

                drawArrows = true;

                BullOpacity = 25;
                BearOpacity = 25;
                BullChart = Brushes.OliveDrab;
                BearChart = Brushes.IndianRed;
            
                AddPlot(Brushes.Orange, Name);
                AddPlot(Brushes.Orange, Name + "-Channel");

            }          
            else if (State == State.DataLoaded)
            {
                maHigh = SMA(High, 14);
                maLow = SMA(Low, 14);
                Trend[0] = 0;
                channelTrend = new Series<double>(this);
            }
        }

        protected override void OnBarUpdate()
        {

             if (CurrentBar < cciPeriod || CurrentBar < atrPeriod)
                return;

            BackBrush = null;

            #region Candle colors
            //CANDLE COLORS
            condition1 = false;
            condition6 = false;
            // Condition set 1
            if (Open[0] <= Close[0]
                && Close[0] > EMA(High, Emah)[0])
            {
                condition1 = true;
                if (colorbars)
                {
                    BarBrush = barColorCondition1;
                }
                if (colorOutline)
                {
                    CandleOutlineBrush = candleOutlineCondition1;
                }
            }

            // Condition set 2
            if (Open[0] >= Close[0]
                && Close[0] > EMA(High, Emah)[0])
            {
                if (colorbars)
                {
                    BarBrush = barColorCondition2;
                }
                if (colorOutline)
                {
                    CandleOutlineBrush = candleOutlineCondition2;
                }
            }

            // Condition set 3
            if (Open[0] <= Close[0]
                && Close[0] < EMA(High, Emah)[0]
                && Close[0] > EMA(Low, Emal)[0])
            {
                if (colorbars)
                {
                    BarBrush = barColorCondition3;
                }
                if (colorOutline)
                {
                    CandleOutlineBrush = candleOutlineCondition3;
                }
            }

            // Condition set 4
            if (Open[0] >= Close[0]
                && Close[0] < EMA(High, Emah)[0]
                && Close[0] > EMA(Low, Emal)[0])
            {
                if (colorbars)
                {
                    BarBrush = barColorCondition4;
                }
                if (colorOutline)
                {
                    CandleOutlineBrush = candleOutlineCondition4;
                }
            }

            // Condition set 5
            if (Open[0] <= Close[0]
                && Close[0] < EMA(Low, Emal)[0])
            {
                if (colorbars)
                {
                    BarBrush = barColorCondition5;
                }
                if (colorOutline)
                {
                    CandleOutlineBrush = candleOutlineCondition5;
                }
            }

            // Condition set 6
            if (Open[0] >= Close[0]
                && Close[0] < EMA(Low, Emal)[0])
            {
                condition6 = true;
                if (colorbars)
                {
                    BarBrush = barColorCondition6;
                }
                if (colorOutline)
                {
                    CandleOutlineBrush = candleOutlineCondition6;
                }
            }
            #endregion

            #region Drawing arrows
            //Arrows
            if (drawArrows)
            {
                double maHighCurrent = maHigh[0];
                double maLowCurrent = maLow[0];
                double atrCurrent = ATR(14)[0];

                double threshold = atrCurrent * 0.5;

                if (Close[0] > maHighCurrent + threshold && trend != 1)
                {
                    if (condition1)
                    {
                        Draw.ArrowUp(this, "Up " + CurrentBar, false, 0, Low[0] - 2 * TickSize, Brushes.White);
                        arrowUp = true;
                        arrowDown = false;
                        trend = 1;                     
                    }
                }
                else if (Close[0] < maLowCurrent - threshold && trend != -1)
                {
                    if (condition6)
                    {
                        Draw.ArrowDown(this, "Down " + CurrentBar, false, 0, High[0] + 2 * TickSize, Brushes.White);
                        arrowDown = true;
                        arrowUp = false;
                        trend = -1;
                    }
                }
            }
            #endregion

            // TREND
            cciVal = CCI(Close, cciPeriod)[0];
            atrVal = ATR(Close, atrPeriod)[0];
            channelAtrVal = ATR(Close, channelATRPeriod)[0];

            upTrend = Low[0] - atrVal * atrMult;
            downTrend = High[0] + atrVal * atrMult;

            upTrendChannel = Low[0] - channelAtrVal * channelATRMultiplier;
            downTrendChannel = High[0] + channelAtrVal * channelATRMultiplier;

            if (cciVal >= 0)
            {
                if (upTrend < Trend[1])
                    Trend[0] = Trend[1];
                else
                    Trend[0] = upTrend;
            }
            else
            {
                if (downTrend > Trend[1])
                    Trend[0] = Trend[1];
                else
                    Trend[0] = downTrend;
            }

            if (cciVal >= 0)
            {
                if (upTrendChannel < channelTrend[1])
                    channelTrend[0] = channelTrend[1];
                else
                    channelTrend[0] = upTrendChannel;
            }
            else
            {
                if (downTrendChannel > channelTrend[1])
                    channelTrend[0] = channelTrend[1];

                else
                    channelTrend[0] = downTrendChannel;
            }

            // Update the values for plotting
            Values[0][0] = Trend[0];
            Values[1][0] = channelTrend[0];

            // Set plot brushes and draw region based on condition
            if (arrowUp && CurrentBar != savedUBar)
            {
                savedUBar = CurrentBar;
                PlotBrushes[0][0] = Brushes.Lime;
                PlotBrushes[1][0] = Brushes.Lime;
                Draw.Region(this, "NubePROS-ChannelUp" + savedUBar, CurrentBar - savedUBar + 1, 0, Values[0], Values[1], null, Brushes.Lime, 15);
            }
            else if (arrowDown && CurrentBar != savedDBar)
            {
                savedDBar = CurrentBar;
                PlotBrushes[0][0] = Brushes.Red;
                PlotBrushes[1][0] = Brushes.Red;
                Draw.Region(this, "NubePROS-ChannelDown" + savedDBar, CurrentBar - savedDBar + 1, 0, Values[0], Values[1], null, Brushes.Red, 15);
            }


            #region HighlightChart
            if (HighlightChart)
            {
                if (arrowDown)
                {
                    BackBrush = BearChart;
                }
                if (arrowUp)
                {
                    BackBrush = BullChart;
                }
            }
            #endregion
        }




        #region Properties      

        [Browsable(false)]
        [XmlIgnore]
        public Series<double> SslUp
        {
            get { return Values[0]; }
        }

        [Browsable(false)]
        [XmlIgnore]
        public Series<double> SslDown
        {
            get { return Values[1]; }
        }

        [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
        [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
        public Series<double> EmaHigh
        {
            get { return Values[0]; }
        }

        [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
        [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
        public Series<double> EmaClose
        {
            get { return Values[1]; }
        }

        [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
        [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
        public Series<double> EmaLow
        {
            get { return Values[2]; }
        }

        [NinjaScriptProperty]
        [Display(Name = "1. Draw Arrows?", Description = "Draw Suy/Sell Arrows?", GroupName = "Colors", Order = 0)]
        public bool DrawArrows
        {
            get { return drawArrows; }
            set { drawArrows = value; }
        }

        [NinjaScriptProperty]
        [Display(Description = "Default value for EMA of High", GroupName = "Parameters", Order = 1)]
        public int Emah
        {
            get { return emah; }
            set { emah = Math.Max(1, value); }
        }

        [NinjaScriptProperty]
        [Display(Description = "Default value for EMA of Low", GroupName = "Parameters", Order = 1)]
        public int Emal
        {
            get { return emal; }
            set { emal = Math.Max(1, value); }
        }

        [NinjaScriptProperty]
        [Range(1, int.MaxValue)]
        [Display(Name = "CCI Period", Order = 1, GroupName = "Parameters")]
        public int cciPeriod
        { get; set; }

        [NinjaScriptProperty]
        [Range(1, int.MaxValue)]
        [Display(Name = "ATR Period", Order = 2, GroupName = "Parameters")]
        public int atrPeriod
        { get; set; }

        [NinjaScriptProperty]
        [Range(-20, int.MaxValue)]
        [Display(Name = "ATR Multiplier", Order = 3, GroupName = "Parameters")]
        public double atrMult
        { get; set; }

        [NinjaScriptProperty]
        [Range(1, int.MaxValue)]
        [Display(Name = "Channel ATR Period", Order = 4, GroupName = "Parameters")]
        public int channelATRPeriod
        { get; set; }

        [NinjaScriptProperty]
        [Range(1, int.MaxValue)]
        [Display(Name = "Channel ATR Multiplier", Order = 5, GroupName = "Parameters")]
        public int channelATRMultiplier
        { get; set; }

        [Browsable(false)]
        [XmlIgnore]
        public Series<double> Trend
        {
            get { return Values[0]; }
        }

        [NinjaScriptProperty]
        [XmlIgnore]
        [Display(Name = "Bull Opacity", Order = 2, GroupName = "1. Highlight Chart")]
        public int BullOpacity
        { get; set; }

        [NinjaScriptProperty]
        [XmlIgnore]
        [Display(Name = "Bear Opacity", Order = 4, GroupName = "1. Highlight Chart")]
        public int BearOpacity
        { get; set; }

        [XmlIgnore()]
        [Display(Name = "Bear Color", GroupName = "1. Highlight Chart", Order = 3)]
        public Brush BearChart
        { get; set; }

        [Browsable(false)]
        public string BearChartSerialize
        {
            get { return Serialize.BrushToString(BearChart); }
            set { BearChart = Serialize.StringToBrush(value); }
        }

        [XmlIgnore()]
        [Display(Name = "Bull Color ", GroupName = "1. Highlight Chart", Order = 1)]
        public Brush BullChart
        { get; set; }

        [Browsable(false)]
        public string BullChartSerialize
        {
            get { return Serialize.BrushToString(BullChart); }
            set { BullChart = Serialize.StringToBrush(value); }
        }

        [NinjaScriptProperty]
        [Display(Name = "HighlightChart", Order = 0, GroupName = "1. Highlight Chart")]
        public bool HighlightChart
        { get; set; }
        #endregion
    }
}


    

#region NinjaScript generated code. Neither change nor remove.

namespace NinjaTrader.NinjaScript.Indicators
{
	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
	{
		private PROS.NubePRO[] cacheNubePRO;
		public PROS.NubePRO NubePRO(bool drawArrows, int emah, int emal, int cciPeriod, int atrPeriod, double atrMult, int channelATRPeriod, int channelATRMultiplier, int bullOpacity, int bearOpacity, bool highlightChart)
		{
			return NubePRO(Input, drawArrows, emah, emal, cciPeriod, atrPeriod, atrMult, channelATRPeriod, channelATRMultiplier, bullOpacity, bearOpacity, highlightChart);
		}

		public PROS.NubePRO NubePRO(ISeries<double> input, bool drawArrows, int emah, int emal, int cciPeriod, int atrPeriod, double atrMult, int channelATRPeriod, int channelATRMultiplier, int bullOpacity, int bearOpacity, bool highlightChart)
		{
			if (cacheNubePRO != null)
				for (int idx = 0; idx < cacheNubePRO.Length; idx++)
					if (cacheNubePRO[idx] != null && cacheNubePRO[idx].DrawArrows == drawArrows && cacheNubePRO[idx].Emah == emah && cacheNubePRO[idx].Emal == emal && cacheNubePRO[idx].cciPeriod == cciPeriod && cacheNubePRO[idx].atrPeriod == atrPeriod && cacheNubePRO[idx].atrMult == atrMult && cacheNubePRO[idx].channelATRPeriod == channelATRPeriod && cacheNubePRO[idx].channelATRMultiplier == channelATRMultiplier && cacheNubePRO[idx].BullOpacity == bullOpacity && cacheNubePRO[idx].BearOpacity == bearOpacity && cacheNubePRO[idx].HighlightChart == highlightChart && cacheNubePRO[idx].EqualsInput(input))
						return cacheNubePRO[idx];
			return CacheIndicator<PROS.NubePRO>(new PROS.NubePRO(){ DrawArrows = drawArrows, Emah = emah, Emal = emal, cciPeriod = cciPeriod, atrPeriod = atrPeriod, atrMult = atrMult, channelATRPeriod = channelATRPeriod, channelATRMultiplier = channelATRMultiplier, BullOpacity = bullOpacity, BearOpacity = bearOpacity, HighlightChart = highlightChart }, input, ref cacheNubePRO);
		}
	}
}

namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
	{
		public Indicators.PROS.NubePRO NubePRO(bool drawArrows, int emah, int emal, int cciPeriod, int atrPeriod, double atrMult, int channelATRPeriod, int channelATRMultiplier, int bullOpacity, int bearOpacity, bool highlightChart)
		{
			return indicator.NubePRO(Input, drawArrows, emah, emal, cciPeriod, atrPeriod, atrMult, channelATRPeriod, channelATRMultiplier, bullOpacity, bearOpacity, highlightChart);
		}

		public Indicators.PROS.NubePRO NubePRO(ISeries<double> input , bool drawArrows, int emah, int emal, int cciPeriod, int atrPeriod, double atrMult, int channelATRPeriod, int channelATRMultiplier, int bullOpacity, int bearOpacity, bool highlightChart)
		{
			return indicator.NubePRO(input, drawArrows, emah, emal, cciPeriod, atrPeriod, atrMult, channelATRPeriod, channelATRMultiplier, bullOpacity, bearOpacity, highlightChart);
		}
	}
}

namespace NinjaTrader.NinjaScript.Strategies
{
	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
	{
		public Indicators.PROS.NubePRO NubePRO(bool drawArrows, int emah, int emal, int cciPeriod, int atrPeriod, double atrMult, int channelATRPeriod, int channelATRMultiplier, int bullOpacity, int bearOpacity, bool highlightChart)
		{
			return indicator.NubePRO(Input, drawArrows, emah, emal, cciPeriod, atrPeriod, atrMult, channelATRPeriod, channelATRMultiplier, bullOpacity, bearOpacity, highlightChart);
		}

		public Indicators.PROS.NubePRO NubePRO(ISeries<double> input , bool drawArrows, int emah, int emal, int cciPeriod, int atrPeriod, double atrMult, int channelATRPeriod, int channelATRMultiplier, int bullOpacity, int bearOpacity, bool highlightChart)
		{
			return indicator.NubePRO(input, drawArrows, emah, emal, cciPeriod, atrPeriod, atrMult, channelATRPeriod, channelATRMultiplier, bullOpacity, bearOpacity, highlightChart);
		}
	}
}

#endregion
Editor is loading...
Leave a Comment