#include  <iterator>
#include "sierrachart.h"
#include "SCStudyFunctions.h"
// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies. 
SCDLLName("FibPivotsDaily") 

	// Set the configuration variables

SCSFExport scsf_CustomPivotPointsDaily(SCStudyInterfaceRef sc)
{
	SCSubgraphRef Subgraph_R1 = sc.Subgraph[0];
	SCSubgraphRef Subgraph_R2 = sc.Subgraph[1];
	SCSubgraphRef Subgraph_S1 = sc.Subgraph[2];
	SCSubgraphRef Subgraph_S2 = sc.Subgraph[3];
	SCSubgraphRef Subgraph_R0_5 = sc.Subgraph[4];
	SCSubgraphRef Subgraph_R1_5 = sc.Subgraph[5];
	SCSubgraphRef Subgraph_R2_5 = sc.Subgraph[6];
	SCSubgraphRef Subgraph_R3 = sc.Subgraph[7];
	SCSubgraphRef Subgraph_S0_5 = sc.Subgraph[8];
	SCSubgraphRef Subgraph_S1_5 = sc.Subgraph[9];
	SCSubgraphRef Subgraph_S2_5 = sc.Subgraph[10];
	SCSubgraphRef Subgraph_S3 = sc.Subgraph[11];
	SCSubgraphRef Subgraph_PP = sc.Subgraph[12];
	SCSubgraphRef Subgraph_R4 = sc.Subgraph[13];
	SCSubgraphRef Subgraph_S4 = sc.Subgraph[14];
	SCSubgraphRef Subgraph_R3_5 = sc.Subgraph[15];
	SCSubgraphRef Subgraph_S3_5 = sc.Subgraph[16];
	SCSubgraphRef Subgraph_R5 = sc.Subgraph[17];
	SCSubgraphRef Subgraph_S5 = sc.Subgraph[18];
	SCSubgraphRef Subgraph_R6 = sc.Subgraph[19];
	SCSubgraphRef Subgraph_S6 = sc.Subgraph[20];
	SCSubgraphRef Subgraph_R7 = sc.Subgraph[21];
	SCSubgraphRef Subgraph_S7 = sc.Subgraph[22];
	SCSubgraphRef Subgraph_R8 = sc.Subgraph[23];
	SCSubgraphRef Subgraph_S8 = sc.Subgraph[24];
	SCSubgraphRef Subgraph_R9 = sc.Subgraph[25];
	SCSubgraphRef Subgraph_S9 = sc.Subgraph[26];
	SCSubgraphRef Subgraph_R10 = sc.Subgraph[27];
	SCSubgraphRef Subgraph_S10 = sc.Subgraph[28];
	SCSubgraphRef Subgraph_R4_5 = sc.Subgraph[29];
	SCSubgraphRef Subgraph_S4_5 = sc.Subgraph[30];
	const int NUMBER_OF_STUDY_SUBGRAPHS = 31;
	
	SCInputRef Input_FormulaType = sc.Input[4];
	SCInputRef Input_NumberOfDays = sc.Input[5];
	SCInputRef Input_RoundToTickSize = sc.Input[6];
	SCInputRef Input_UseSaturdayData = sc.Input[7];
	SCInputRef Input_DailyChartNumber = sc.Input[9];//previously input 3
	SCInputRef Input_ReferenceDailyChartForData = sc.Input[10];
	SCInputRef Input_ForwardProjectLines = sc.Input[11];
	SCInputRef Input_UseManualValues = sc.Input[12];
	SCInputRef Input_UserOpen = sc.Input[13];
	SCInputRef Input_UserHigh = sc.Input[14];
	SCInputRef Input_UserLow = sc.Input[15];
	SCInputRef Input_UserClose = sc.Input[16];
	SCInputRef Input_UseDaySessionOnly = sc.Input[17];
	SCInputRef Input_Version = sc.Input[18];
	SCInputRef Input_UserPivotPoint = sc.Input[19];

	if (sc.SetDefaults)
	{

		sc.GraphName = "Pivot Points-Daily";
		
		sc.ScaleRangeType = SCALE_SAMEASREGION;

		sc.GraphRegion = 0;
		sc.ValueFormat = VALUEFORMAT_INHERITED;

		Subgraph_R1.Name = "R1";
		Subgraph_R2.Name = "R2";
		Subgraph_S1.Name = "S1";
		Subgraph_S2.Name = "S2";
		Subgraph_R0_5.Name = "R.5";
		Subgraph_R1_5.Name = "R1.5";
		Subgraph_R2_5.Name = "R2.5";
		Subgraph_R3.Name = "R3";
		Subgraph_S0_5.Name = "S.5";
		Subgraph_S1_5.Name = "S1.5";
		Subgraph_S2_5.Name = "S2.5";
		Subgraph_S3.Name = "S3";
		Subgraph_PP.Name = "PP";
		Subgraph_R4.Name = "R4";
		Subgraph_S4.Name = "S4";
		Subgraph_R3_5.Name = "R3.5";
		Subgraph_S3_5.Name = "S3.5";
		Subgraph_R5.Name = "R5";
		Subgraph_S5.Name = "S5";
		Subgraph_R6.Name = "R6";
		Subgraph_S6.Name = "S6";
		Subgraph_R7.Name = "R7";
		Subgraph_S7.Name = "S7";
		Subgraph_R8.Name = "R8";
		Subgraph_S8.Name = "S8";
		Subgraph_R9.Name = "R9";
		Subgraph_S9.Name = "S9";
		Subgraph_R10.Name = "R10";
		Subgraph_S10.Name = "S10";
		Subgraph_R4_5.Name = "R4_5";
		Subgraph_S4_5.Name = "S4_5";

		Subgraph_R1.DrawStyle = DRAWSTYLE_DASH;
		Subgraph_R2.DrawStyle = DRAWSTYLE_DASH;
		Subgraph_S1.DrawStyle = DRAWSTYLE_DASH;
		Subgraph_S2.DrawStyle = DRAWSTYLE_DASH;
		Subgraph_R0_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R1_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R2_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R3.DrawStyle = DRAWSTYLE_DASH;
		Subgraph_S0_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S1_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S2_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S3.DrawStyle = DRAWSTYLE_DASH;
		Subgraph_PP.DrawStyle = DRAWSTYLE_DASH;
		Subgraph_R4.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S4.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R3_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S3_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R6.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S6.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R7.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S7.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R8.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S8.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R9.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S9.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R10.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S10.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_R4_5.DrawStyle = DRAWSTYLE_HIDDEN;
		Subgraph_S4_5.DrawStyle = DRAWSTYLE_HIDDEN;

		
		Subgraph_R1.DrawZeros = false;
		Subgraph_R2.DrawZeros = false;
		Subgraph_S1.DrawZeros = false;
		Subgraph_S2.DrawZeros = false;
		Subgraph_R0_5.DrawZeros = false;
		Subgraph_R1_5.DrawZeros = false;
		Subgraph_R2_5.DrawZeros = false;
		Subgraph_R3.DrawZeros = false;
		Subgraph_S0_5.DrawZeros = false;
		Subgraph_S1_5.DrawZeros = false;
		Subgraph_S2_5.DrawZeros = false;
		Subgraph_S3.DrawZeros = false;
		Subgraph_PP.DrawZeros = false;
		Subgraph_R4.DrawZeros = false;
		Subgraph_S4.DrawZeros = false;
		Subgraph_R3_5.DrawZeros = false;
		Subgraph_S3_5.DrawZeros = false;
		Subgraph_R5.DrawZeros = false;
		Subgraph_S5.DrawZeros = false;
		Subgraph_R6.DrawZeros = false;
		Subgraph_S6.DrawZeros = false;
		Subgraph_R7.DrawZeros = false;
		Subgraph_S7.DrawZeros = false;
		Subgraph_R8.DrawZeros = false;
		Subgraph_S8.DrawZeros = false;
		Subgraph_R9.DrawZeros = false;
		Subgraph_S9.DrawZeros = false;
		Subgraph_R10.DrawZeros = false;
		Subgraph_S10.DrawZeros = false;
		Subgraph_R4_5.DrawZeros = false;
		Subgraph_S4_5.DrawZeros = false;
		

		Subgraph_PP.PrimaryColor = RGB(255, 0, 255);

		Subgraph_R1.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R2.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R0_5.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R1_5.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R2_5.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R3.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R4.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R4_5.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R3_5.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R5.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R6.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R7.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R8.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R9.PrimaryColor = RGB(255, 0, 0);
		Subgraph_R10.PrimaryColor = RGB(255, 0, 0);

		Subgraph_S1.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S2.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S0_5.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S1_5.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S2_5.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S3.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S4.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S4_5.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S3_5.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S5.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S6.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S7.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S8.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S9.PrimaryColor = RGB(0, 255, 0);
		Subgraph_S10.PrimaryColor = RGB(0, 255, 0);

		for (int SubgraphIndex = 0; SubgraphIndex < NUMBER_OF_STUDY_SUBGRAPHS; SubgraphIndex++)
		{
			sc.Subgraph[SubgraphIndex].LineLabel = LL_DISPLAY_NAME | LL_DISPLAY_VALUE | LL_NAME_ALIGN_ABOVE | LL_NAME_ALIGN_LEFT | LL_VALUE_ALIGN_CENTER | LL_VALUE_ALIGN_VALUES_SCALE;
		}

		sc.Input[3].SetChartNumber(1);

		Input_DailyChartNumber.Name = "Daily Chart Number";
		Input_DailyChartNumber.SetChartNumber(1);

		Input_FormulaType.Name = "Formula Type";
		Input_FormulaType.SetInt(0);

		Input_NumberOfDays.Name = "Number of Days To Calculate";
		Input_NumberOfDays.SetInt(50);
		Input_NumberOfDays.SetIntLimits(1,MAX_STUDY_LENGTH);

		Input_RoundToTickSize.Name = "Round to Tick Size";
		Input_RoundToTickSize.SetYesNo(0);

		Input_UseSaturdayData.Name = "Use Saturday Data";
		Input_UseSaturdayData.SetYesNo(0);

		Input_ReferenceDailyChartForData.Name = "Reference Daily Chart For Data";
		Input_ReferenceDailyChartForData.SetYesNo(false);

		Input_ForwardProjectLines.Name ="Forward Project Pivot Point Lines";
		Input_ForwardProjectLines.SetYesNo(0);
		
		Input_UseManualValues.Name = "Use User Entered OHLC Values";
		Input_UseManualValues.SetYesNo(0);
		
		Input_UserOpen.Name = "User Entered Open Value";
		Input_UserOpen.SetFloat(0.0f);
		
		Input_UserHigh.Name = "User Entered High Value";
		Input_UserHigh.SetFloat(0.0f);
		
		Input_UserLow.Name = "User Entered Low Value";
		Input_UserLow.SetFloat(0.0f);
		
		Input_UserClose.Name = "User Entered Close Value";
		Input_UserClose.SetFloat(0.0f);

		Input_UseDaySessionOnly.Name = "Use Day Session Only";
		Input_UseDaySessionOnly.SetYesNo(false);

		Input_Version.SetInt(2);
		
		Input_PivotPoint.Name = "Enter Pivot Point Value";
		Input_UserPivotPoint.SetFloat(0.0f);
		
		return;
	}
	//Upgrade code
	if (Input_Version.GetInt()<2)
	{
		Input_Version.SetInt(2);
		Input_DailyChartNumber.SetInt(sc.Input[3].GetInt() );
		Input_ReferenceDailyChartForData .SetYesNo(!Input_ReferenceDailyChartForData .GetYesNo());
	}

	float fPivotPoint= 0;

	float fR0_5 = 0, fR1 = 0, fR1_5 = 0, fR2 = 0, fR2_5 = 0, fR3 = 0, fR3_5 = 0, fR4 = 0, fR4_5 = 0, fR5 = 0, fR6 = 0, fR7 = 0, fR8 = 0, fR9 = 0, fR10 = 0;
	float fS0_5 = 0, fS1 = 0, fS1_5 = 0, fS2 = 0, fS2_5 = 0, fS3 = 0, fS3_5 = 0, fS4 = 0, fS4_5 = 0, fS5 = 0, fS6 = 0, fS7 = 0, fS8 = 0, fS9 = 0, fS10 = 0;
	
	int IntradayChartDate = 0;
	int ValidPivotPoint = 1;

	// we get chart data only once for speed
	SCGraphData DailyChartData;
	SCDateTimeArray DailyChartDateTime;
	if (Input_ReferenceDailyChartForData.GetYesNo() )
	{
		sc.GetChartBaseData(Input_DailyChartNumber.GetChartNumber(), DailyChartData);
		sc.GetChartDateTimeArray(Input_DailyChartNumber.GetChartNumber(), DailyChartDateTime);
	}

	int NumberOfForwardBars = 0;

	if(Input_ForwardProjectLines.GetYesNo())
	{
		NumberOfForwardBars = 20;

		if(sc.UpdateStartIndex == 0)
		{
			for (int SubgraphIndex = 0; SubgraphIndex < NUMBER_OF_STUDY_SUBGRAPHS; SubgraphIndex++)
				sc.Subgraph[SubgraphIndex].ExtendedArrayElementsToGraph = NumberOfForwardBars;
		}
	}

	for (int Index = sc.UpdateStartIndex; Index < sc.ArraySize+NumberOfForwardBars; Index++ ) 
	{
		if (IntradayChartDate != sc.GetTradingDayDate(sc.BaseDateTimeIn[Index]))
		{
			IntradayChartDate = sc.GetTradingDayDate(sc.BaseDateTimeIn[Index]);

			ValidPivotPoint = 
				CalculateDailyPivotPoints(
					sc,
					IntradayChartDate,
					Input_FormulaType.GetInt(),
					Input_DailyChartNumber.GetChartNumber(),
					DailyChartData,
					DailyChartDateTime,
					Input_NumberOfDays.GetInt(),
					Input_UseSaturdayData.GetYesNo(),
					Input_ReferenceDailyChartForData.GetYesNo() ? 0 : 1,
					Input_UseManualValues.GetYesNo(),
					Input_UserOpen.GetFloat(),
					Input_UserHigh.GetFloat(),
					Input_UserLow.GetFloat(),
					Input_UserClose.GetFloat(),
					Input_UseDaySessionOnly.GetYesNo(),
					fPivotPoint,
					fR0_5,
					fR1, fR1_5,
					fR2, fR2_5,
					fR3,
					fS0_5,
					fS1, fS1_5,
					fS2, fS2_5,
					fS3,
					fR3_5,
					fS3_5,
					fR4,
					fR4_5,
					fS4,
					fS4_5,
					fR5,
					fS5,
					fR6,
					fS6,
					fR7,
					fS7,
					fR8,
					fS8,
					fR9,
					fS9,
					fR10,
					fS10
				);
		}

		if (!ValidPivotPoint)
			continue;
		
		if (Input_RoundToTickSize.GetYesNo() != 0)
		{
			Subgraph_R1[Index] = (float)sc.RoundToTickSize(fR1, sc.TickSize);
			Subgraph_R2[Index] = (float)sc.RoundToTickSize(fR2, sc.TickSize);
			Subgraph_S1[Index] = (float)sc.RoundToTickSize(fS1, sc.TickSize);
			Subgraph_S2[Index] = (float)sc.RoundToTickSize(fS2, sc.TickSize);

			Subgraph_R0_5[Index] = (float)sc.RoundToTickSize(fR0_5, sc.TickSize);
			Subgraph_R1_5[Index] = (float)sc.RoundToTickSize(fR1_5, sc.TickSize);
			Subgraph_R2_5[Index] = (float)sc.RoundToTickSize(fR2_5, sc.TickSize);
			Subgraph_R3[Index] = (float)sc.RoundToTickSize(fR3, sc.TickSize);
			Subgraph_S0_5[Index] = (float)sc.RoundToTickSize(fS0_5, sc.TickSize);
			Subgraph_S1_5[Index] = (float)sc.RoundToTickSize(fS1_5, sc.TickSize);
			Subgraph_S2_5[Index] = (float)sc.RoundToTickSize(fS2_5, sc.TickSize);
			Subgraph_S3[Index] = (float)sc.RoundToTickSize(fS3, sc.TickSize);
			Subgraph_PP[Index] = (float)sc.RoundToTickSize(fPivotPoint, sc.TickSize);
			Subgraph_R4[Index] = (float)sc.RoundToTickSize(fR4, sc.TickSize);
			Subgraph_R4_5[Index] = (float)sc.RoundToTickSize(fR4_5, sc.TickSize);
			Subgraph_S4[Index] = (float)sc.RoundToTickSize(fS4, sc.TickSize);
			Subgraph_S4_5[Index] = (float)sc.RoundToTickSize(fS4_5, sc.TickSize);
			Subgraph_R3_5[Index] = (float)sc.RoundToTickSize(fR3_5, sc.TickSize);
			Subgraph_S3_5[Index] = (float)sc.RoundToTickSize(fS3_5, sc.TickSize);
			Subgraph_R5[Index] = (float)sc.RoundToTickSize(fR5, sc.TickSize);
			Subgraph_S5[Index] = (float)sc.RoundToTickSize(fS5, sc.TickSize);
			Subgraph_R6[Index] = (float)sc.RoundToTickSize(fR6, sc.TickSize);
			Subgraph_S6[Index] = (float)sc.RoundToTickSize(fS6, sc.TickSize);
			Subgraph_R7[Index] = (float)sc.RoundToTickSize(fR7, sc.TickSize);
			Subgraph_S7[Index] = (float)sc.RoundToTickSize(fS7, sc.TickSize);
			Subgraph_R8[Index] = (float)sc.RoundToTickSize(fR8, sc.TickSize);
			Subgraph_S8[Index] = (float)sc.RoundToTickSize(fS8, sc.TickSize);
			Subgraph_R9[Index] = (float)sc.RoundToTickSize(fR9, sc.TickSize);
			Subgraph_S9[Index] = (float)sc.RoundToTickSize(fS9, sc.TickSize);
			Subgraph_R10[Index] = (float)sc.RoundToTickSize(fR10, sc.TickSize);
			Subgraph_S10[Index] = (float)sc.RoundToTickSize(fS10, sc.TickSize);
		}
		else 
		{
			Subgraph_R1[Index] = fR1;
			Subgraph_R2[Index] = fR2;
			Subgraph_S1[Index] = fS1;
			Subgraph_S2[Index] = fS2;

			Subgraph_R0_5[Index] = fR0_5;
			Subgraph_R1_5[Index] = fR1_5;
			Subgraph_R2_5[Index] = fR2_5;
			Subgraph_R3[Index] = fR3;
			Subgraph_S0_5[Index] = fS0_5;
			Subgraph_S1_5[Index] = fS1_5;
			Subgraph_S2_5[Index] = fS2_5;
			Subgraph_S3[Index] = fS3;
			Subgraph_PP[Index] = fPivotPoint;
			Subgraph_R4[Index] = fR4;
			Subgraph_R4_5[Index] = fR4_5;
			Subgraph_S4[Index] = fS4;
			Subgraph_S4_5[Index] = fS4_5;
			Subgraph_R3_5[Index] = fR3_5;
			Subgraph_S3_5[Index] = fS3_5;
			Subgraph_R5[Index] = fR5;
			Subgraph_S5[Index] = fS5;
			Subgraph_R6[Index] = fR6;
			Subgraph_S6[Index] = fS6;
			Subgraph_R7[Index] = fR7;
			Subgraph_S7[Index] = fS7;
			Subgraph_R8[Index] = fR8;
			Subgraph_S8[Index] = fS8;
			Subgraph_R9[Index] = fR9;
			Subgraph_S9[Index] = fS9;
			Subgraph_R10[Index] = fR10;
			Subgraph_S10[Index] = fS10;
		}
	} 
}

/*==========================================================================*/
int CalculatePivotPoints
( float PriorOpen
, float PriorHigh
, float PriorLow
, float PriorClose
, float CurrentOpen
, float& PivotPoint
, float& R_5
, float& R1, float& R1_5
, float& R2, float& R2_5
, float& R3
, float& S_5
, float& S1, float& S1_5
, float& S2, float& S2_5
, float& S3
, float& R3_5
, float& S3_5
, float& R4
, float& R4_5
, float& S4
, float& S4_5
, float& R5
, float& S5
, float& R6
, float& S6
, float& R7
, float& S7
, float& R8
, float& S8
, float& R9
, float& S9
, float& R10
, float& S10
, int FormulaType
)
{
	if (FormulaType == 0)
	{
		PivotPoint = GetFloat.UserPivotPoint();
		
		R1 = (PivotPoint * 2) - PriorLow;
		R_5 = (PivotPoint + R1) / 2;
		R2 = PivotPoint + (PriorHigh - PriorLow);
		R1_5 = (R1 + R2) / 2;
		S1 = (2 * PivotPoint) - PriorHigh;
		S_5 = (PivotPoint + S1) / 2;
		S2 = PivotPoint - (PriorHigh - PriorLow);
		S1_5 = (S1 + S2) / 2;
		R3 = 2 * PivotPoint + (PriorHigh - 2*PriorLow); 
		R2_5 = (R2 + R3) / 2;
		S3 = 2 * PivotPoint - (2 * PriorHigh - PriorLow);
		S2_5 = (S2 + S3) / 2;
		R4 = 3 * PivotPoint + (PriorHigh - 3 * PriorLow);
		S4 = 3 * PivotPoint - (3 * PriorHigh - PriorLow);
		R3_5 =  (R3 + R4) / 2;
		S3_5 = (S3 + S4) / 2;
		R5 = 4 * PivotPoint + (PriorHigh - 4 * PriorLow);
		S5 = 4 * PivotPoint - (4 * PriorHigh - PriorLow);
		R6 = 5 * PivotPoint + (PriorHigh - 5 * PriorLow);
		S6 = 5 * PivotPoint - (5 * PriorHigh - PriorLow);
		R7 = 6 * PivotPoint + (PriorHigh - 6 * PriorLow);
		S7 = 6 * PivotPoint - (6 * PriorHigh - PriorLow);
		R8 = 7 * PivotPoint + (PriorHigh - 7 * PriorLow);
		S8 = 7 * PivotPoint - (7 * PriorHigh - PriorLow);
		R9 = 8 * PivotPoint + (PriorHigh - 8 * PriorLow);
		S9 = 8 * PivotPoint - (8 * PriorHigh - PriorLow);
		R10 = 9 * PivotPoint + (PriorHigh - 9 * PriorLow);
		S10 = 9 * PivotPoint - (9 * PriorHigh - PriorLow);

		/*

		R3.5 = (R3 + R4)/2
		S3.5 = (S3 + S4)/2
		R5 = 4 * PivotPoint + (Yesterday's High - (4 * Yesterday's Low))
		S5 = (4 * PivotPoint) - ((4 * Yesterday's High) - Yesterday's Low)
		R4.5 = (R4 + R5)/2
		S4.5 = (S4 + S5)/2
		*/
	}

	else if (FormulaType == 1)
	{
		float YesterdaysRange;

		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		R1 = (PivotPoint * 2) - PriorLow;
		R_5 = (PivotPoint + R1) / 2;
		R2 = PivotPoint + (PriorHigh-PriorLow);
		R1_5 = (R1 + R2) / 2;
		S1 = (2 * PivotPoint) - PriorHigh;
		S_5 = (PivotPoint + S1) / 2;
		S2 = PivotPoint - (PriorHigh - PriorLow);
		S1_5 = (S1 + S2) / 2;
		YesterdaysRange = (PriorHigh - PriorLow);

		// This is R3 Pattern Trapper
		R3 = PriorHigh + YesterdaysRange; // 2 * High - Low
		R2_5 = (R2 + R3)/2;

		// This is S3 Pattern Trapper
		S3 = 2*PivotPoint - PriorHigh - YesterdaysRange; // 2 * PivotPoint - (2 * High + Low)
		S2_5 = (S2 + S3) / 2;

		R4 = PivotPoint + 3 * (PriorHigh - PriorLow);
		S4 = PivotPoint - 3 * (PriorHigh - PriorLow);
	}
	else if (FormulaType == 2)
	{
		PivotPoint = (CurrentOpen + PriorHigh + PriorLow + PriorClose) / 4;
		R1 = (PivotPoint * 2) - PriorLow;
		R_5 = (PivotPoint + R1) / 2;
		R2 = PivotPoint + (PriorHigh - PriorLow);
		R1_5 = (R1 + R2) / 2;
		S1 = (2 * PivotPoint) - PriorHigh;
		S_5 = (PivotPoint + S1) / 2;
		S2 = PivotPoint - (PriorHigh - PriorLow);
		S1_5 = (S1 + S2) / 2;
		R3 = 2*PivotPoint + (PriorHigh - 2*PriorLow); // R3 std
		R2_5 = (R2 + R3) / 2;
		S3 = 2*PivotPoint - (2*PriorHigh - PriorLow); // S3 std
		S2_5 = (S2 + S3) / 2;
		R4 = 3 * PivotPoint + (PriorHigh - 3 * PriorLow);
		S4 = 3 * PivotPoint - (3 * PriorHigh - PriorLow);
	}
	else if (FormulaType == 3)
	{
		PivotPoint = (PriorHigh + PriorLow + CurrentOpen+CurrentOpen) / 4;
		R1 = (PivotPoint * 2) - PriorLow;
		R_5 = (PivotPoint + R1) / 2;
		R2 = PivotPoint + (PriorHigh - PriorLow);
		R1_5 = (R1 + R2) / 2;
		S1 = (2 * PivotPoint) - PriorHigh;
		S_5 = (PivotPoint + S1) / 2;
		S2 = PivotPoint - (PriorHigh - PriorLow);
		S1_5 = (S1 + S2) / 2;
		R3 = 2 * PivotPoint + (PriorHigh - 2*PriorLow); // R3 std
		R2_5 = (R2 + R3) / 2;
		S3 = 2 * PivotPoint - (2 * PriorHigh - PriorLow); // S3 std
		S2_5 = (S2 + S3) / 2;
		R4 = 3 * PivotPoint + (PriorHigh - 3 * PriorLow);
		S4 = 3 * PivotPoint - (3 * PriorHigh - PriorLow);
	}
	else if (FormulaType == 4)
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		R1 = (PivotPoint * 2) - PriorLow;
		R_5 = (PivotPoint + R1) / 2;
		R2 = PivotPoint + (PriorHigh - PriorLow);
		R1_5 = (R1 + R2) / 2;
		S1 = (2 * PivotPoint) - PriorHigh;
		S_5 = (PivotPoint + S1) / 2;
		S2 = PivotPoint - (PriorHigh - PriorLow);
		S1_5 = (S1 + S2) / 2;
		R3 = PivotPoint + 2 * (PriorHigh - PriorLow); 
		R2_5 = (R2 + R3) / 2;
		S3 = PivotPoint - 2 * (PriorHigh - PriorLow); 
		S2_5 = (S2 + S3) / 2;
		R4 = PivotPoint + 3 * (PriorHigh - PriorLow);
		S4 = PivotPoint - 3 * (PriorHigh - PriorLow);
	}
	else if (FormulaType == 5) // Camarilla Pivot Points
	{
		float Range = PriorHigh - PriorLow;
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;

		R_5 = PriorClose + Range * 1.1f/18;
		R1 = PriorClose + Range * 1.1f/12;
		R1_5 = PriorClose + Range * 1.1f/9;
		R2 = PriorClose + Range * 1.1f/6;
		R2_5 = PriorClose + Range * 1.1f/5;
		R3 = PriorClose + Range * 1.1f/4;
		R3_5 = PriorClose + Range * 1.1f/3;
		R4 = PriorClose + Range * 1.1f/2;
		R4_5 = PriorClose + Range * 1.1f / 1.33f;

		S_5 = PriorClose - Range * 1.1f/18;
		S1 = PriorClose - Range * 1.1f/12;
		S1_5 = PriorClose - Range * 1.1f/9;
		S2 = PriorClose - Range * 1.1f/6;
		S2_5 = PriorClose - Range * 1.1f/5;
		S3 = PriorClose - Range * 1.1f/4;
		S3_5 = PriorClose - Range * 1.1f/3;
		S4 = PriorClose - Range * 1.1f/2;
		S4_5 = PriorClose - Range * 1.1f / 1.33f;
		R5 = (PriorHigh/PriorLow) *PriorClose;
		S5 = PriorClose -(R5-PriorClose);
	}
	else if (FormulaType == 6)  // Tom DeMark's Pivot Points
	{
		float X = 0;
		if (PriorClose < PriorOpen)
			X = PriorHigh + PriorLow + PriorLow + PriorClose;
		else if (PriorClose > PriorOpen)
			X = PriorHigh + PriorHigh + PriorLow + PriorClose;
		else if (PriorClose == PriorOpen)
			X = PriorHigh + PriorLow + PriorClose + PriorClose;

		PivotPoint = X / 4;
		R1 = X / 2 - PriorLow;
		S1 = X / 2 - PriorHigh;
		R_5 = R2 = R1_5 = S_5 = S2 = S1_5 = R3 = 
			R2_5 = S3 = S2_5 = R4 = S4 = 0;
	}
	else if (FormulaType == 7) // Frank Dilernia Pivots
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		R1 = PivotPoint + (PriorHigh - PriorLow)/2;
		R2 = PivotPoint + (PriorHigh - PriorLow) * 0.618f;
		R3 = PivotPoint + (PriorHigh - PriorLow);
		S1 = PivotPoint - (PriorHigh - PriorLow)/2;
		S2 = PivotPoint - (PriorHigh - PriorLow) * 0.618f;
		S3 = PivotPoint - (PriorHigh - PriorLow);

		S2_5 = (S3 + S2)/2;				
		S1_5 = (S2 + S1)/2;				
		S_5 = (S1 + PivotPoint)/2;			
		R_5 = (R1 + PivotPoint)/2;			
		R1_5 = (R2 + R1)/2;				
		R2_5 = (R3 + R2)/2;				
	}
	else if (FormulaType == 8) // Shadow Trader. www.shadowtrader.net
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3; 
		R1 = (2 * PivotPoint) - PriorLow; 
		R3 = 2*(PivotPoint - PriorLow) + PriorHigh; 
		R4 = PriorHigh + 3*(PivotPoint - PriorLow);

		S1 = (2 * PivotPoint) - PriorHigh; 
		S2 = PivotPoint - (R1 - S1); 
		S3 = PriorLow - 2 * (PriorHigh - PivotPoint); 
		S4 = PriorLow - 3 * (PriorHigh - PivotPoint);

		R2 = PivotPoint + (R1 - S1); 

		S3_5 = (S3 + S4)/2;
		S2_5 = (S3 + S2)/2;				
		S1_5 = (S2 + S1)/2;				
		S_5 = (S1 + PivotPoint)/2;			
		R_5 = (R1 + PivotPoint)/2;			
		R1_5 = (R2 + R1)/2;				
		R2_5 = (R3 + R2)/2;		
		R3_5 = (R3 + R4)/2;

	}
	else if (FormulaType == 9)
	{

		PivotPoint = (PriorHigh + PriorLow + PriorClose)/3; //PP = H + L + C /3
		float PPSquareRoot = sqrt(sqrt(PivotPoint));
		R1 = PivotPoint + PPSquareRoot; //R1 = PP + SQRT(SQRT(PP))
		R2 = R1 + PPSquareRoot; //R2 = R1 + SQRT(SQRT(PP))
		R3 = R2 + PPSquareRoot; //R3 = R2 + SQRT(SQRT(PP))
		S1 = PivotPoint - PPSquareRoot; //S1 = PP - SQRT(SQRT(PP))
		S2 = S1 - PPSquareRoot; //S2 = S1 - SQRT(SQRT(PP))
		S3 = S2 - PPSquareRoot; //S3 = S2 - SQRT(SQRT(PP))

		S2_5 = (S3 + S2)/2;				
		S1_5 = (S2 + S1)/2;				
		S_5 = (S1 + PivotPoint)/2;			
		R_5 = (R1 + PivotPoint)/2;			
		R1_5 = (R2 + R1)/2;				
		R2_5 = (R3 + R2)/2;				

	}
	else if (FormulaType == 10)
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose)/3;
		R1 = 2 * PivotPoint - PriorLow;
		R2 = PivotPoint + PriorHigh - PriorLow;
		R3 = R1 + PriorHigh - PriorLow;
		R4 = R3 + (R2 - R1);
			
		S1 = 2 * PivotPoint - PriorHigh;
		S2 = PivotPoint - (PriorHigh - PriorLow);
		S3 = S1 - (PriorHigh - PriorLow);
		S4 = S3 - (S1 - S2);

		S3_5 = (S4 + S3)/2;
		S2_5 = (S3 + S2)/2;
		S1_5 = (S2 + S1)/2;
		S_5 = (S1 + PivotPoint)/2;
		R_5 = (R1 + PivotPoint)/2;
		R1_5 = (R2 + R1)/2;
		R2_5 = (R3 + R2)/2;
		R3_5 = (R4 + R3)/2;
	}
	else if (FormulaType == 11)
	{
		float DailyRange = PriorHigh - PriorLow;

		R_5 = PivotPoint + 0.5f*DailyRange;
		R1 = PivotPoint + 0.618f*DailyRange;
		R1_5 = PivotPoint + DailyRange;
		R2 = PivotPoint + 1.272f*DailyRange;
		R2_5 = PivotPoint + 1.618f*DailyRange;
		R3 = PivotPoint + 2*DailyRange;
		R4 = PivotPoint + 2.618f*DailyRange;

		S_5 = PivotPoint - 0.5f*DailyRange;
		S1 = PivotPoint - 0.618f*DailyRange;
		S1_5 = PivotPoint - DailyRange;
		S2 = PivotPoint - 1.272f*DailyRange;
		S2_5 = PivotPoint - 1.618f*DailyRange;
		S3 = PivotPoint - 2*DailyRange;
		S4 = PivotPoint - 2.618f*DailyRange;

	
	}
	else if (FormulaType == 12)
	{
		PivotPoint = CurrentOpen;

		R_5 = PivotPoint;
		R1 = PivotPoint;
		R1_5 = PivotPoint;
		R2 = PivotPoint;
		R2_5 = PivotPoint;
		R3 = PivotPoint;
		R4 = PivotPoint;

		S_5 = PivotPoint;
		S1 = PivotPoint;
		S1_5 = PivotPoint;
		S2 = PivotPoint;
		S2_5 = PivotPoint;
		S3 = PivotPoint;
		S4 = PivotPoint;
	}
	else if (FormulaType == 13) // Fibonacci Pivot Points
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose)/3;
		float DailyRange = PriorHigh - PriorLow;

	
		R1 = PivotPoint + 0.382f*DailyRange;
		R2 = PivotPoint + 0.618f*DailyRange;
		R3 = PivotPoint + 1*DailyRange;

		S1 = PivotPoint - 0.382f*DailyRange;
		S2 = PivotPoint - 0.618f*DailyRange;
		S3 = PivotPoint - 1*DailyRange;

		S2_5 = (S3 + S2)/2;				
		S1_5 = (S2 + S1)/2;				
		S_5 = (S1 + PivotPoint)/2;			
		R_5 = (R1 + PivotPoint)/2;			
		R1_5 = (R2 + R1)/2;				
		R2_5 = (R3 + R2)/2;				


	}
	else if (FormulaType == 14) 
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		
		S1 = (2 * PivotPoint) - PriorHigh;
		R1 = (2 * PivotPoint) - PriorLow;
		
		S2 = PivotPoint - (R1 - S1);
		R2 = (PivotPoint - S1) + R1;
		
		S3 = PivotPoint - (R2 - S1) ;
		R3 = (PivotPoint - S1) + R2;
	}
	else if (FormulaType == 15) // Fibonacci Zone Pivots
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;

		R1 = PivotPoint + (PriorHigh - PriorLow)/2;
		R2 = PivotPoint + (PriorHigh - PriorLow);

		S1 = PivotPoint - (PriorHigh - PriorLow)/2;
		S2 = PivotPoint - (PriorHigh - PriorLow);

		// 0.5 to 0.618 defines resistance/support band 1
		// 1.0 to 1.382 defines resistance/support band 2
		// the bands could start as lines and if the user 
		// wishes they can use Fill Rect top and bottom
		// or the transparent versions.
		R1_5 = PivotPoint + (PriorHigh - PriorLow)*0.618f;
		R2_5 = PivotPoint + (PriorHigh - PriorLow)*1.382f;

		S1_5 = PivotPoint - (PriorHigh - PriorLow)*0.618f;
		S2_5 = PivotPoint - (PriorHigh - PriorLow)*1.382f;
	}
	else if (FormulaType == 16)
	{
		/*
		THE CENTRAL PIVOT RANGE FORMULA:

		TC (TOPCENTRAL) = (PIVOT-BC)+PIVOT
		PIVOT = (HIGH+LOW+CLOSE)/3
		BC (BOTTOM CENTRAL) =(HIGH+LOW)/2
		*/
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		S1 = (PriorHigh + PriorLow) / 2;
		R1 = (PivotPoint - S1) + PivotPoint;
	}
	else if (FormulaType == 17)
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3; // 	PP = (H+L+C)/3


		// H: high, L: low, C: close
			

		// Previous Day H: high, L: low, C: close
		R3 = PriorHigh+2*(PivotPoint- PriorLow); //R3 = H+2*(PP-L)
		R2 = PivotPoint+(PriorHigh-PriorLow);  //R2 = PP+(H-L)
		R2_5 = (R2+R3)/2; //M5 = (R2+R3)/2

		S1 = 2*PivotPoint - PriorHigh; //	S1 = (2*PP)-H
		
		S2 = PivotPoint - (PriorHigh - PriorLow); //	S2 = PP-(H-L)

		S3 = PriorLow - 2 * (PriorHigh - PivotPoint);//	S3 = L-2*(H-PP) 

		S2_5 = (S2 + S3)/2; //	M0 = (S2+S3)/2
		
		
		R1 = 2*PivotPoint - PriorLow; //	R1 = (2*PP)-L
		R_5 = (PivotPoint + R2)/2; //	M3 = (PP+R2)/2
		R1_5 = (R_5 + R2)/2; //	M4 = (M3+R2)/2
		S_5 = (PivotPoint + S2)/2; //	M2 = (PP+S2)/2
		S1_5 = (S_5 + S2)/2; //	M1 = (M2+S2)/2
	}
	else if (FormulaType == 18)
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;

		R1 = PivotPoint + (PriorHigh - PriorLow) * 0.75f;
		S1 = PivotPoint -  (PriorHigh - PriorLow) * 0.75f;
	}
	else if (FormulaType == 19)
	{
/*		Pivot Point (PP) = (Yesterday's High + Yesterday's Low + Current Open + Current Open) / 4

			Resistance Level (R1) = PivotPoint + 2 * (Yesterday's High - Yesterday's Low)

			Support Level (S1) = PivotPoint - 2 * (Yesterday's High - Yesterday's Low)
*/
		PivotPoint = (PriorHigh + PriorLow + 2 * CurrentOpen) / 4;
		R1 = PivotPoint + 2 * (PriorHigh - PriorLow);
		S1 = PivotPoint - 2 * (PriorHigh - PriorLow);
	}
	else if (FormulaType ==20)
	{
		/* Calculations for ACD method
		*/
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		float HighLowAverage = (PriorHigh + PriorLow)/2;
		float Difference = fabs (HighLowAverage -  PivotPoint);
		S1 = PivotPoint -Difference;
		R1 =PivotPoint  +Difference;
	}
	else if (FormulaType == 21)
	{
		////////////Fibonacci Calculation///////////////////////////////////////////

		//R10 = OP + ((HighD(1) - LowD(1)) * 3.618);
		//R9 = OP + ((HighD(1) - LOWD(1))* 3.00);
		//R8 = OP + ((HighD(1) - LowD(1)) * 2.618);
		//R7 = OP + ((HighD(1) - LOWD(1))* 2.00);
		//R6 = OP + ((HighD(1) - LowD(1)) * 1.618);
		//R5 = OP + ((HighD(1) - LowD(1)) * 1.27);
		//R4 = OP + ((HighD(1) - LowD(1)) * 1.000);
		//R3 = OP + ((HighD(1) - LowD(1)) * .786);
		//R2 = OP + ((HighD(1) - LowD(1)) * .618);
		//R1 = OP + ((HighD(1) - LowD(1)) * .382);
		//OP = (OpenD(0));// (HighD(1) + LowD(1) + CloseD(1)) / 3;
		//S1 = OP - ((HighD(1) - LowD(1)) * .382);
		//S2 = OP - ((HighD(1) - LowD(1)) * .618);
		//S3 = OP - ((HighD (1) - LowD (1)) * .786);
		//S4 = OP - ((HighD(1) - LowD(1)) * 1.000);
		//S5 = OP - ((HighD(1) - LowD(1)) * 1.27);
		//S6 = OP - ((HighD(1) - LowD(1)) * 1.618);
		//S7 = OP - ((HighD(1) - LowD(1)) * 2.00);
		//S8 = OP - ((HighD(1) - LowD(1)) * 2.618);
		//S9 = OP - ((HighD(1) - LowD(1)) * 3.00);
		//S10 = OP - ((HighD(1) - LowD(1)) * 3.618); 

		PivotPoint = CurrentOpen;
		
		R_5 = (float)(PivotPoint + ((PriorHigh - PriorLow) * 0.382));
		R1 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 0.618));
		R1_5 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 0.786));
		R2 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 1.0));
		R2_5 = (float)(PivotPoint + ((PriorHigh - PriorLow) * 1.27));
		R3 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 1.618));
		R3_5 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 2.0));
		R4 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 2.618));
		R5 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 3.0));

		S_5 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.382));
		S1 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.618));
		S1_5 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.786));
		S2 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 1.0));
		S2_5 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 1.27));
		S3 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 1.618));
		S3_5 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 2.0));
		S4 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 2.618));
		S5 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 3.0));
	}
	else if (FormulaType == 22)
	{
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		float UDIFF = PriorHigh - PivotPoint;
		float LDIFF = PivotPoint - PriorLow;

		R1 = PivotPoint + .382f * LDIFF;
		R2 = PivotPoint + .618f * LDIFF;
		R3 = PivotPoint + LDIFF;    
		R4 = PivotPoint + 1.272f * LDIFF;
		R5 = PivotPoint + 1.382f * LDIFF;
		R6 = PivotPoint + 1.618f * LDIFF;
		R7 = PivotPoint + 2.0f * LDIFF;
		R8 = PivotPoint + 2.272f * LDIFF;
		R9 = PivotPoint + 2.382f * LDIFF;
		R10 = PivotPoint + 2.618f * LDIFF;

		S1 = PivotPoint - .382f * UDIFF;
		S2 = PivotPoint - .618f * UDIFF;
		S3 = PivotPoint - UDIFF;    
		S4 = PivotPoint - 1.272f * UDIFF;
		S5 = PivotPoint - 1.382f * UDIFF;
		S6 = PivotPoint - 1.618f * UDIFF;
		S7 = PivotPoint - 2.0f * UDIFF;
		S8 = PivotPoint - 2.272f * UDIFF;
		S9 = PivotPoint - 2.382f * UDIFF;    
		S10 = PivotPoint - 2.618f * UDIFF;

	}
	else if (FormulaType == 23)
	{
		// Fibonacci Zone Pivot Points calculation
		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		// Daily Range
		const float DR = PriorHigh - PriorLow;

		R1 = PivotPoint + .5f * DR;
		R2 = PivotPoint + .618f * DR;
		R3 = PivotPoint + DR;
		R4 = PivotPoint + 1.382f * DR;
		R5 = PivotPoint + 1.618f * DR;
		R6 = PivotPoint + 2 * DR;
		R7 = PivotPoint + 2.618f * DR;

		S1 = PivotPoint - 0.5f * DR;
		S2 = PivotPoint - 0.618f * DR;
		S3 = PivotPoint - DR;
		S4 = PivotPoint - 1.382f * DR;
		S5 = PivotPoint - 1.618f * DR;
		S6 = PivotPoint - 2 * DR;
		S7 = PivotPoint - 2.618f * DR;
	}
	else if (FormulaType == 24)// Advanced Camarilla Pivot Points calculation
	{
		const float DailyRange = PriorHigh - PriorLow;

		R1 = PriorClose + .0916f * DailyRange;
		R2 = PriorClose + .183f * DailyRange;
		R3 = PriorClose + .275f * DailyRange;
		R4 = PriorClose + .555f * DailyRange;
		R5 = PriorClose + .8244f * DailyRange;
		R6 = PriorClose + 1.0076f * DailyRange;
		PivotPoint = PriorClose;
		S1 = PriorClose - .0916f * DailyRange;
		S2 = PriorClose - .183f * DailyRange;
		S3 = PriorClose - .275f * DailyRange;
		S4 = PriorClose - .55f * DailyRange;
		S5 = PriorClose - .8244f * DailyRange;
		S6 = PriorClose - 1.0992f * DailyRange;
	}
	else if (FormulaType == 25)
	{

		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		S1 = (PriorHigh + PriorLow) / 2;
		R1 = PivotPoint + (PivotPoint - S1);
		S2 = PriorLow - (PriorHigh - PriorLow)*0.25f;
		S3 = PriorLow - (PriorHigh - PriorLow)*0.5f;
		S4 = PriorLow - (PriorHigh - PriorLow)*0.75f;
		S5 = PriorLow - (PriorHigh - PriorLow)*1.0f;
		R2 = PriorHigh + (PriorHigh - PriorLow)*0.25f;
		R3 = PriorHigh + (PriorHigh - PriorLow)*0.5f;
		R4 = PriorHigh + (PriorHigh - PriorLow)*0.75f;
		R5 = PriorHigh + (PriorHigh - PriorLow);
	}
	else if (FormulaType == 26)
	{

		PivotPoint = (PriorHigh + PriorLow + PriorClose) / 3;
		const float DailyRange = PriorHigh - PriorLow;
		R1 = PivotPoint + 0.382f*(DailyRange);
		R2 = PivotPoint + 0.618f*(DailyRange);
		R3 = PivotPoint + 0.786f*(DailyRange);
		R4 = PivotPoint + 1.00f*(DailyRange);
		R5 = PivotPoint + 1.382f*(DailyRange);
		R6 = PivotPoint + 1.618f*(DailyRange);
		R7 = PivotPoint + 2.00f*(DailyRange);
		S1 = PivotPoint - 0.382f*(DailyRange);
		S2 = PivotPoint - 0.618f*(DailyRange);
		S3 = PivotPoint - 0.786f*(DailyRange);
		S4 = PivotPoint - 1.00f*(DailyRange);
		S5 = PivotPoint - 1.382f*(DailyRange);
		S6 = PivotPoint - 1.618f*(DailyRange);
		S7 = PivotPoint - 2.00f*(DailyRange);
	}
	else if (FormulaType == 27)
	{
	
		PivotPoint = CurrentOpen;

		R_5 = (float)(PivotPoint + ((PriorHigh - PriorLow) * 0.236));
		R1 = (float)(PivotPoint + ((PriorHigh - PriorLow) * 0.382));
		R1_5 = (float)(PivotPoint + ((PriorHigh - PriorLow) * 0.5));
		R2 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 0.618));
		R2_5 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 0.786));
		R3 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 1.0));
		R3_5 = (float)(PivotPoint + ((PriorHigh - PriorLow) * 1.27));
		R4 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 1.618));
		R5 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 2.0));
		R6 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 2.618));
		R7 = (float)( PivotPoint + ((PriorHigh - PriorLow) * 3.0));

		S_5 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.236));
		S1 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.382));
		S1_5 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.5));
		S2 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.618));
		S2_5 = (float)( PivotPoint - ((PriorHigh - PriorLow) * 0.786));
		S3 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 1.0));
		S3_5 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 1.27));
		S4 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 1.618));
		S5 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 2.0));
		S6 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 2.618));
		S7 = (float)(  PivotPoint - ((PriorHigh - PriorLow) * 3.0));
	}

	return 1;
}

/*==========================================================================*/
int CalculateDailyOHLC
( SCStudyInterfaceRef sc
, int IntradayChartDate
, int InNumberOfDaysBack
, int InNumberOfDaysToCalculate
, int InUseSaturdayData
, int InUseThisIntradayChart
, int InDailyChartNumber
, SCGraphData& DailyChartData
, SCDateTimeArray& DailyChartDateTimes
, int UseDaySessionOnly
, float& Open
, float& High
, float& Low
, float& Close
, float& Volume
, int InIncludeFridayEveningSessionWithSundayEveningSession
)
{
	if (InUseThisIntradayChart) // Use this Chart
	{
		int ArraySize = sc.ArraySize;
		if (ArraySize < 2)
			return 0;
		
		int LastIndex = ArraySize - 1;
		
		int LastDateInChart = sc.GetTradingDayDate(sc.BaseDateTimeIn[LastIndex]);
		
		// Return and do not calculate if IntradayChartDate is further back than the number of days to calculate from the last date in the chart.
		if (LastDateInChart - InNumberOfDaysToCalculate >= IntradayChartDate)
			return 0;
		
		
		// Look for the last good date (not a weekend and has data)
		int FirstIndexOfReferenceDay = 0;
		int ReferenceDay = IntradayChartDate;
		while (InNumberOfDaysBack > 0)
		{
			--ReferenceDay;
			
			
			if (IsWeekend(ReferenceDay, InUseSaturdayData != 0))
				continue;
			
			// It is not a weekend

			FirstIndexOfReferenceDay = sc.GetFirstIndexForDate(sc.ChartNumber, ReferenceDay);
			SCDateTime FirstIndexOfReferenceDayTradingDayDate = sc.GetTradingDayDate(sc.BaseDateTimeIn[FirstIndexOfReferenceDay]);

			if (FirstIndexOfReferenceDayTradingDayDate == ReferenceDay)
				--InNumberOfDaysBack;//Found a match. Decrement the number of days back being referenced.
			
			if (InNumberOfDaysBack <= 0)
			{
				if (FirstIndexOfReferenceDayTradingDayDate != ReferenceDay)// Prior day not found
					return 0;
				
				break;
			}
			
			if (FirstIndexOfReferenceDay == 0)
			{
				if (FirstIndexOfReferenceDayTradingDayDate != ReferenceDay)// Prior day not found
					return 0;

				// At the beginning of the date array, so we can not look back any further
				break;
			}
		}
		
		if(!UseDaySessionOnly)
		{
			if (!sc.GetOpenHighLowCloseVolumeForDate(ReferenceDay, Open, High, Low, Close, Volume, InIncludeFridayEveningSessionWithSundayEveningSession))
				return 0;
		}
		else
		{
			SCDateTime StartDateTime;
			SCDateTime EndDateTime;

			if(sc.StartTime1 <= sc.EndTime1)
			{
				StartDateTime.SetDate(ReferenceDay);
				StartDateTime.SetTime(sc.StartTime1);

				EndDateTime.SetDate(ReferenceDay);
				EndDateTime.SetTime(sc.EndTime1);
			}
			else
			{
				StartDateTime.SetDate(ReferenceDay - 1);
				StartDateTime.SetTime(sc.StartTime1);

				EndDateTime.SetDate(ReferenceDay);
				EndDateTime.SetTime(sc.EndTime1);
			}

			float NextOpen = 0;

			if(!sc.GetOHLCOfTimePeriod(StartDateTime, EndDateTime, Open, High, Low, Close, NextOpen))
				return 0;
		}
	}
	else // Use Daily Chart
	{
		
		int LastDateInDestinationChart = sc.GetTradingDayDate(sc.BaseDateTimeIn[sc.ArraySize - 1]);
		
		// Return and do not calculate if the IntradayChartDate to be calculated is further back than the number of days to calculate
		if (IntradayChartDate <= LastDateInDestinationChart - InNumberOfDaysToCalculate )
			return 0;
		
		
		// Look for a matching date that is not a weekend

		int FirstIndexOfReferenceDay = 0;
		int ReferenceDay = IntradayChartDate - InNumberOfDaysBack;

		if (InNumberOfDaysBack == 0)
		{
			FirstIndexOfReferenceDay = sc.GetFirstNearestIndexForTradingDayDate(InDailyChartNumber, ReferenceDay);
		}
		else
		{
			while (IsWeekend(ReferenceDay, InUseSaturdayData != 0))
			{
				--ReferenceDay;
			}

			FirstIndexOfReferenceDay = sc.GetFirstNearestIndexForTradingDayDate(InDailyChartNumber, ReferenceDay);

			if (sc.GetTradingDayDate(DailyChartDateTimes[FirstIndexOfReferenceDay]) == IntradayChartDate
				&& FirstIndexOfReferenceDay >= 1)
				--FirstIndexOfReferenceDay;

#ifdef _DEBUG
			SCString DateString1 = sc.DateTimeToString(ReferenceDay, FLAG_DT_COMPLETE_DATETIME);
			SCString DateString2 = sc.DateTimeToString(DailyChartDateTimes[FirstIndexOfReferenceDay], FLAG_DT_COMPLETE_DATETIME);
			SCString LogMessage;
			LogMessage.Format("%s, %s", DateString1.GetChars(), DateString2.GetChars());
			sc.AddMessageToLog(LogMessage, 0);
#endif
		}

		
		Open = DailyChartData[SC_OPEN][FirstIndexOfReferenceDay];
		High = DailyChartData[SC_HIGH][FirstIndexOfReferenceDay];
		Low = DailyChartData[SC_LOW][FirstIndexOfReferenceDay];
		Close = DailyChartData[SC_LAST][FirstIndexOfReferenceDay];
	}
	
	return 1;
}

/*==========================================================================*/
int CalculateDailyPivotPoints
( SCStudyInterfaceRef sc
, int IntradayChartDate
, int FormulaType
, int DailyChartNumber
, SCGraphData& DailyChartData
, SCDateTimeArray& DailyChartDateTime
, int NumberOfDays
, int UseSaturdayData
, int UseThisChart
, int UseManualValues
, float UserOpen
, float UserHigh
, float UserLow
, float UserClose
, int  UseDaySessionOnly
, float& PivotPoint
, float& R_5
, float& R1, float& R1_5
, float& R2, float& R2_5
, float& R3
, float& S_5
, float& S1, float& S1_5
, float& S2, float& S2_5
, float& S3
, float& R3_5
, float& S3_5
, float& R4
, float& R4_5
, float& S4
, float& S4_5
, float& R5
, float& S5
, float& R6
, float& S6
, float& R7
, float& S7
, float& R8
, float& S8
, float& R9
, float& S9
, float& R10
, float& S10
)
{

	float Open = 0.0, High = 0.0, Low = 0.0, Close = 0.0, NextOpen = 0.0;
	
	if (UseThisChart != 0) // Use this Chart
	{
		int ArraySize = sc.BaseDateTimeIn.GetArraySize();
		if (ArraySize < 2)
			return 0;

		int LastIndex = ArraySize - 1;

		int CurrentDate = sc.GetTradingDayDate(sc.BaseDateTimeIn[LastIndex]);
		
		// Return and do not calculate pivots if the current bar to be calculated is further back than the number of days to calculate
		if (CurrentDate - NumberOfDays >= IntradayChartDate)
			return 0;


		// Look for the last good date which is not a weekend and has data
		int FirstIndexOfPriorDay;
		int PreviousDay = IntradayChartDate;
		while (true)
		{
			--PreviousDay;

		
			if (IsWeekend(PreviousDay, UseSaturdayData != 0))
				continue;

			// It is not a weekend
			FirstIndexOfPriorDay = sc.GetFirstIndexForDate(sc.ChartNumber, PreviousDay);
			if (sc.GetTradingDayDate(sc.BaseDateTimeIn[FirstIndexOfPriorDay]) == PreviousDay)
				break;

			if (FirstIndexOfPriorDay == 0)
			{
				// At the beginning of the date array, so we can't look back any farther
				break;
			}
		}

		if (sc.GetTradingDayDate(sc.BaseDateTimeIn[FirstIndexOfPriorDay]) != PreviousDay)
		{
			// Previous day not found
			return 0;
		}


		if(!UseDaySessionOnly)
		{


			if (!sc.GetOHLCForDate(PreviousDay, Open, High, Low, Close))
				return 0;

			float NextHigh, NextLow, NextClose;  // The values returned for these are unused
			if (!sc.GetOHLCForDate(IntradayChartDate, NextOpen, NextHigh, NextLow, NextClose))
				NextOpen = Close;  // If there is a failure, use the prior Close
		}
		else
		{
			SCDateTime StartDateTime;
			SCDateTime EndDateTime;

			if(sc.StartTime1 <= sc.EndTime1)
			{
				StartDateTime.SetDate(PreviousDay);
				StartDateTime.SetTime(sc.StartTime1);

				EndDateTime.SetDate(PreviousDay);
				EndDateTime.SetTime(sc.EndTime1);
			}
			else
			{
				StartDateTime.SetDate(PreviousDay - 1);
				StartDateTime.SetTime(sc.StartTime1);

				EndDateTime.SetDate(PreviousDay);
				EndDateTime.SetTime(sc.EndTime1);
			}


			
			if(!sc.GetOHLCOfTimePeriod(StartDateTime, EndDateTime, Open, High, Low, Close, NextOpen))
				return 0;
		}
	}
	else // Use Daily Chart
	{
		if (DailyChartData[0].GetArraySize() < 2)
			return 0;

		int DailyChartBarIndex = -1; 
		int CountBack = DailyChartData[SC_OPEN].GetArraySize() - NumberOfDays - 1;
		for(int CurrentIndex = DailyChartData[SC_OPEN].GetArraySize() - 1; CurrentIndex >= CountBack && CurrentIndex >= 0; CurrentIndex-- )
		{
			if (sc.GetTradingDayDateForChartNumber(DailyChartNumber, DailyChartDateTime[CurrentIndex]) < IntradayChartDate)
			{
				DailyChartBarIndex = CurrentIndex;
				break;
			}
		}

		if(DailyChartBarIndex == -1)
			return 0;

		Open = DailyChartData[SC_OPEN][DailyChartBarIndex];
		High = DailyChartData[SC_HIGH][DailyChartBarIndex];
		Low = DailyChartData[SC_LOW][DailyChartBarIndex];
		Close = DailyChartData[SC_LAST][DailyChartBarIndex];

		if (DailyChartBarIndex == DailyChartData[0].GetArraySize() - 1 )
			NextOpen = DailyChartData[SC_LAST][DailyChartBarIndex];
		else
			NextOpen = DailyChartData[SC_OPEN][DailyChartBarIndex+1];
	}
	
	// If we are calculating the last day, and the user has manually entered OHLC values, then use those
	if (UseManualValues)
	{
		int ArraySize = sc.BaseDateTimeIn.GetArraySize();

		int LastIndex = ArraySize - 1;

		int CurrentDate = sc.GetTradingDayDate(sc.BaseDateTimeIn[LastIndex]);

		if (CurrentDate == IntradayChartDate)
		{
			Open = (UserHigh + UserLow + UserClose) / 3.0f;  // This is not normally used, but we need to set it to something reasonable
			High = UserHigh;
			Low = UserLow;
			Close = UserClose;
			NextOpen = UserOpen;
		}
	}

	return CalculatePivotPoints
		(  Open
		,  High
		,  Low
		,  Close
		,  NextOpen
		,  PivotPoint
		,  R_5
		,  R1,  R1_5
		,  R2,  R2_5
		,  R3
		,  S_5
		,  S1,  S1_5
		,  S2,  S2_5
		,  S3
		,  R3_5
		,  S3_5
		,  R4
		,  R4_5
		,  S4
		,  S4_5
		,  R5
		,  S5
		,  R6
		,  S6
		,  R7
		,  S7
		,  R8
		,  S8
		,  R9
		,  S9
		,  R10
		,  S10
		,  FormulaType
		);
}