
//
// Copyright (C) 2019, Order Flow Analytics Inc <www.orderflowanalytics.com>.
// OFA reserves the right to modify or overwrite this NinjaScript component with each release.
//
// By installing, using in whole or in part any sample code from Order Flow Analytics you agree to fully indemnify and hold harmless OrderFlowAnalytics, Inc 
// and its affiliates from and against any claims, losses, liabilities and expenses that may be incurred 
// as a result having downloaded and/or used any or all of the available code.
// Do thoroughly test any code & modifications for indicators and strategies in Replay Test mode

#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 NinjaTrader.OFA;
#endregion

//This namespace holds Indicators in this folder and is required. Do not change it. 
namespace NinjaTrader.NinjaScript.Indicators
{
	public class OFASampleIndClusterCloseLogic : Indicator
	{
		private OFAIndicatorSTCORE OFAIndicatorSTCORE1;
		
		private string pivotMarker = "=";
		private string pivotMarker2 = "~";
		private string BreakMarker = "n";
			
		private SimpleFont pfont; 
	
		private int pipcount = 5;
		private int pivotcount = 3;
		private int fontsize1=16;
		
		
		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Description									= @"Enter the description for your new custom Indicator here.";
				Name										= "OFASampleIndClusterCloseLogic";
				Calculate									= Calculate.OnBarClose;
				IsOverlay									= true;
				DisplayInDataBox							= true;
				DrawOnPricePanel							= true;
				DrawHorizontalGridLines						= false;
				DrawVerticalGridLines						= false;
				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					= false;
				
				Offset = 2;
				Opacity = 40;
				
			}
			else if (State == State.Configure)
			{
				// Use Webdings character set which can be used to plot markers that are in scale to the OFA custom bars
				
				pfont = new SimpleFont( "Webdings", fontsize1) { Bold = true };
			}
			
			else if (State == State.DataLoaded)
			{				
				
				OFAIndicatorSTCORE1				= OFAIndicatorSTCORE(Close, 30, 70, OFAColorLogic.POC);
				
			}
		}

		protected override void OnBarUpdate()
		{
			//Add your custom indicator logic here
			//			if (BarsInProgress != 0) 
			//				return;
			
			// Ensure you have enough bars on the chart if you are referencing prior bars in your logic below !			

			if (CurrentBar < 4)
				return;
			
			// Test the indicator series data is valid - good practice test before processing the data
			//
			
			if (OFAIndicatorSTCORE1.IsValidDataPoint(0))
			{
				 // Set 1
				if ((OFAIndicatorSTCORE1.OFABarDirectionValue[0] == 1)
					
					 // Condition group 1
					 // the Classification must be either a Buy or Neutral with this logic setting
					 // ie any Sell classification for the current bar does not meet the rules
					
					 && ((OFAIndicatorSTCORE1.OFABarClassification[0] == 2)
					 || (OFAIndicatorSTCORE1.OFABarClassification[0] == 1))
					
					 // Try with this condition and without - just comment out 
					
					 //&& (OFAIndicatorSTCORE1.PocPrice[0] <= (Median[0] + Offset*TickSize))
					
					 // Condition group 2
					 // a line provided here if you want to include a 3rd bar lookback condition
					
					 && ((OFAIndicatorSTCORE1.OFABarDirectionValue[1] == -1)
					 || (OFAIndicatorSTCORE1.OFABarDirectionValue[1] == -1)
					 || (OFAIndicatorSTCORE1.OFABarDirectionValue[2] == -1))
					
					 // Can modify the next group for how many prior POCS to close above - adjust the strength of pivot looking for
					 // modify the 2nd close if > or >= compare in testing
					 // a line provided here if you want to include a 3rd bar lookback comparison condition - change one of the [2]'s to a [3]
					
					 && (OFAIndicatorSTCORE1.OFABarClose[0] > OFAIndicatorSTCORE1.OFAPocPrice[1])
					 && (OFAIndicatorSTCORE1.OFABarClose[0] >= OFAIndicatorSTCORE1.OFAPocPrice[2])
					 && (OFAIndicatorSTCORE1.OFABarClose[0] >= OFAIndicatorSTCORE1.OFAPocPrice[2])
					 )
				{
					// Three alternatives to draw the marker when above conditions are true
					// Uncomment and recompile as appropriate
					
//					Draw.VerticalLine(this, @"MyCustomStrategyLogicA Vertical line_1"+CurrentBar, 0, _UpBrush, DashStyleHelper.Solid, 1);
					
					//Draw.Region(this, "tag1"+CurrentBar, 1, 0, High, Low, null, _UpBrush, Opacity);
					
					Draw.Text(this,"Bartextpivota"+CurrentBar,false, pivotMarker2.ToString(),0, Low[0] - Offset*TickSize,0,
									_UpBrush,pfont,TextAlignment.Center,Brushes.Transparent,Brushes.Transparent,10);
					
					
				}
				else if ((OFAIndicatorSTCORE1.OFABarDirectionValue[0] == -1)
					 
					 // Condition group 1
					 // the Classification must be either a Sell or Neutral with this logic setting
					 // ie any Buy classification for the current bar does not meet the rules
					
					 && ((OFAIndicatorSTCORE1.OFABarClassification[0] == 2)
					 || (OFAIndicatorSTCORE1.OFABarClassification[0] == -1))
					
					
					 // Try with this condition and without - just comment out 
					
					 //&& (OFAIndicatorSTCORE1.PocPrice[0] >= (Median[0] - Offset*TickSize))
					
					 // Condition group 2
					 // a line provided here if you want to include a 3rd bar lookback condition
					
					 && ((OFAIndicatorSTCORE1.OFABarDirectionValue[1] == 1)
					 || (OFAIndicatorSTCORE1.OFABarDirectionValue[1] == 1)
					 || (OFAIndicatorSTCORE1.OFABarDirectionValue[2] == 1))
					
					// Can modify the next group for how many prior POCS to close below - adjust the strength of pivot looking for
					// modify the 2nd close if < or <= compare in testing
					
					// a line provided here if you want to include a 3rd bar lookback comparison condition - change one of the [2]'s to a [3]
					
					 && (OFAIndicatorSTCORE1.OFABarClose[0] < OFAIndicatorSTCORE1.OFAPocPrice[1])
					 && (OFAIndicatorSTCORE1.OFABarClose[0] <= OFAIndicatorSTCORE1.OFAPocPrice[2])
					 && (OFAIndicatorSTCORE1.OFABarClose[0] <= OFAIndicatorSTCORE1.OFAPocPrice[2])
					)
				{
					// Three alternatives to draw the marker when above conditions are true
					
//					Draw.VerticalLine(this, @"MyCustomStrategyLogicA Vertical line_1"+CurrentBar, 0, _DnBrush, DashStyleHelper.Solid, 1);
					
//					Draw.Region(this, "tag1"+CurrentBar, 1, 0, High, Low, null, _DnBrush, Opacity);
					
					Draw.Text(this,"Bartextpivot"+CurrentBar,false, pivotMarker2.ToString(),0, High[0] + Offset*TickSize,0,
										_DnBrush,pfont,TextAlignment.Center,Brushes.Transparent,Brushes.Transparent,10);
				}
			}
			
		}

		#region Properties

		
		
		[NinjaScriptProperty]
		[Range(1, int.MaxValue)]
		[Display(Name="Offset", Order=1, GroupName="Parameters")]
		public int Offset
		{ get; set; }
		
		// Up Color 
        private System.Windows.Media.Brush _UpBrush = Brushes.Lime;
        [XmlIgnore]
        [Display(Name = "Up Rule Color", GroupName = "Rule Parameters", Order = 2)]
        public System.Windows.Media.Brush UpRuleBrush
        {
            get { return _UpBrush; }
            set { _UpBrush = value; }
        }

        [Browsable(false)]
        public string UpBrushSerialize
        {
            get { return Serialize.BrushToString(_UpBrush); }
            set { _UpBrush = Serialize.StringToBrush(value); }
        }
		
		private System.Windows.Media.Brush _DnBrush = Brushes.Red;
        [XmlIgnore]
        [Display(Name = "Dn Rule Color", GroupName = "Rule Parameters", Order = 3)]
        public System.Windows.Media.Brush DnRuleBrush
        {
            get { return _DnBrush; }
            set { _DnBrush = value; }
        }

        [Browsable(false)]
        public string DnBrushSerialize
        {
            get { return Serialize.BrushToString(_DnBrush); }
            set { _DnBrush = Serialize.StringToBrush(value); }
        }
		
		[NinjaScriptProperty]
		[Range(1, 100)]
		[Display(Name="Opacity", Order=4, GroupName="Parameters")]
		public int Opacity
		{ get; set; }
		
		#endregion
		

	}
}

#region NinjaScript generated code. Neither change nor remove.

namespace NinjaTrader.NinjaScript.Indicators
{
	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
	{
		private OFASampleIndClusterCloseLogic[] cacheOFASampleIndClusterCloseLogic;
		public OFASampleIndClusterCloseLogic OFASampleIndClusterCloseLogic(int offset, int opacity)
		{
			return OFASampleIndClusterCloseLogic(Input, offset, opacity);
		}

		public OFASampleIndClusterCloseLogic OFASampleIndClusterCloseLogic(ISeries<double> input, int offset, int opacity)
		{
			if (cacheOFASampleIndClusterCloseLogic != null)
				for (int idx = 0; idx < cacheOFASampleIndClusterCloseLogic.Length; idx++)
					if (cacheOFASampleIndClusterCloseLogic[idx] != null && cacheOFASampleIndClusterCloseLogic[idx].Offset == offset && cacheOFASampleIndClusterCloseLogic[idx].Opacity == opacity && cacheOFASampleIndClusterCloseLogic[idx].EqualsInput(input))
						return cacheOFASampleIndClusterCloseLogic[idx];
			return CacheIndicator<OFASampleIndClusterCloseLogic>(new OFASampleIndClusterCloseLogic(){ Offset = offset, Opacity = opacity }, input, ref cacheOFASampleIndClusterCloseLogic);
		}
	}
}

namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
	{
		public Indicators.OFASampleIndClusterCloseLogic OFASampleIndClusterCloseLogic(int offset, int opacity)
		{
			return indicator.OFASampleIndClusterCloseLogic(Input, offset, opacity);
		}

		public Indicators.OFASampleIndClusterCloseLogic OFASampleIndClusterCloseLogic(ISeries<double> input , int offset, int opacity)
		{
			return indicator.OFASampleIndClusterCloseLogic(input, offset, opacity);
		}
	}
}

namespace NinjaTrader.NinjaScript.Strategies
{
	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
	{
		public Indicators.OFASampleIndClusterCloseLogic OFASampleIndClusterCloseLogic(int offset, int opacity)
		{
			return indicator.OFASampleIndClusterCloseLogic(Input, offset, opacity);
		}

		public Indicators.OFASampleIndClusterCloseLogic OFASampleIndClusterCloseLogic(ISeries<double> input , int offset, int opacity)
		{
			return indicator.OFASampleIndClusterCloseLogic(input, offset, opacity);
		}
	}
}

#endregion
