#include "sierrachart.h"

// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies. 
SCDLLName("VolumeThreshold3") 


void drawText(SCStudyInterfaceRef sc, int TradeVolume, int TextSize, float TextColor)
{
	s_UseTool Tool;

	//const int UniqueID1 = 3000000;
	Tool.Clear();
	Tool.DrawingType = DRAWING_TEXT;
	Tool.ChartNumber = sc.ChartNumber;
	Tool.Region = sc.GraphRegion;
	Tool.LineNumber = sc.Index;
	Tool.BeginIndex = sc.Index;
	Tool.BeginValue = sc.Close[sc.Index];
	Tool.Color = TextColor;
	Tool.FontSize = TextSize;
	Tool.TextAlignment = DT_CENTER | DT_VCENTER;
	Tool.AddMethod = UTAM_ADD_OR_ADJUST;
	//Tool.Text = sc.Close;
	Tool.Text.Format("%d", TradeVolume);
	sc.UseTool(Tool);

	return;
}

SCSFExport scsf_VolumeThreshold(SCStudyInterfaceRef sc)
{
	SCInputRef VolumeThreshold = sc.Input[0];
	SCInputRef AggregateTS = sc.Input[1];
	SCInputRef PaintColor = sc.Input[2];
	SCInputRef TextSize = sc.Input[3];
	SCInputRef TextColor = sc.Input[4];

	SCSubgraphRef ColorBar = sc.Subgraph[0];

	if (sc.SetDefaults)
	{
		
		VolumeThreshold.Name = "VolumeThreshold";
		VolumeThreshold.SetInt(50);
		VolumeThreshold.SetIntLimits(1, INT_MAX);

		AggregateTS.Name = "AggregateTS(Yes/No)";
		AggregateTS.SetYesNo(false);

		PaintColor.Name = "PaintColor";
		PaintColor.SetColor(COLOR_BLUE);

		TextSize.Name = "FontSize(1-10)";
		TextSize.SetIntLimits(1, 10);
		TextSize.SetInt(8);
		
		TextColor.Name = "TextColor";
		TextColor.SetColor(COLOR_YELLOW);

		sc.GraphName = "VolumeThreshold3";
		sc.StudyDescription = "This study function colors bars if the there is a trade at certain threshold.";
		sc.GraphRegion = 0;
		
		ColorBar.Name = "Color Bar";
		ColorBar.DrawStyle = DRAWSTYLE_COLORBAR;
		ColorBar.PrimaryColor = PaintColor.GetColor();
		ColorBar.DrawZeros=false;

		sc.AutoLoop = 1;

		//During development set this flag to 1, so the DLL can be modified. When development is completed, set it to 0 to improve performance.
		sc.FreeDLL = 0;

		return;
	}
		
	if ( sc.Index < sc.ArraySize-1 ) return;

	SCTimeAndSalesArray TimeSales;
	sc.GetTimeAndSales(TimeSales);

	if (TimeSales.GetArraySize() == 0) return;

	int TSIndex = TimeSales.GetArraySize() - 1;
	
	SCDateTime PrevDateTime = sc.PersistVars ->scdt1;

	int& TradeVolume = sc.PersistVars -> i2;

	while((TSIndex > 0) && (ColorBar[sc.Index]!=1))
	{
		SCDateTime TradeDateTime = COMBINE_DATE_TIME(TimeSales[TSIndex].Date, HMS_TIME(TimeSales[TSIndex].Hour, TimeSales[TSIndex].Minute, TimeSales[TSIndex].Second));
		TradeDateTime += sc.TimeScaleAdjustment;

		if (TradeDateTime < sc.BaseDateTimeIn[sc.Index]) break;
		
		if (TimeSales[TSIndex].Level == SC_TS_BID || TimeSales[TSIndex].Level == SC_TS_ASK)
		{
			TradeVolume=( AggregateTS.GetYesNo() && (TradeDateTime == PrevDateTime) )?TimeSales[TSIndex].Volume+TradeVolume:TimeSales[TSIndex].Volume;
			PrevDateTime=TradeDateTime;

			//ColorBar[sc.Index]= (TradeVolume >= VolumeThreshold.GetInt())?1.0f:0.0f;
			if (TradeVolume >= VolumeThreshold.GetInt())
			{
				ColorBar[sc.Index]=1;
				drawText(sc,TradeVolume,TextSize.GetInt(),TextColor.GetColor());
				//drawText(sc,1000);
			}
		}
		--TSIndex;
	}

	return;
}

