
// The top of every source code file must include this line
#include "sierrachart.h"
#include "scstudyfunctions.h"

#define MAX_DATE 2958465L // about year 9999

// For reference, refer to this page:
// https://www.sierrachart.com/index.php?page=doc/AdvancedCustomStudyInterfaceAndLanguage.php

// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies. 
SCDLLName("Merlin Studies")


/*==========================================================================*/
SCSFExport scsf_NMinuteGrid(SCStudyInterfaceRef sc)
{
	// highlight grid lines every N minutes
	SCSubgraphRef TimeLineTop = sc.Subgraph[0];
	SCSubgraphRef TimeLineBottom = sc.Subgraph[1];
	
	// Input
	SCInputRef NMinutesIn = sc.Input[0];

	if(sc.SetDefaults)
	{
		sc.GraphName="N-Minute GridLines";
		sc.StudyDescription="Draws a vertical line every minute, intended for a 1-second chart. Must be added once for each region of the chart.";

		sc.AutoLoop = 0; // manual looping
		sc.FreeDLL = 0;
		sc.GraphRegion = 0;
		sc.ScaleRangeType = SCALE_USERDEFINED;
		sc.ScaleRangeTop = 1;
		sc.ScaleRangeBottom = -1;
		sc.DrawStudyUnderneathMainPriceGraph = 1;
		
		TimeLineTop.Name = "TimeLine Top";
		TimeLineTop.DrawStyle = DRAWSTYLE_BARTOP;
		TimeLineTop.PrimaryColor = RGB(80,80,80);
		TimeLineTop.DrawZeros = false;
		TimeLineTop.LineWidth = 1;

		TimeLineBottom.Name = "TimeLine Bottom";
		TimeLineBottom.DrawStyle = DRAWSTYLE_BARBOTTOM;
		TimeLineBottom.PrimaryColor = RGB(80,80,80);
		TimeLineBottom.DrawZeros =  false;
		TimeLineBottom.LineWidth = 1;
		
		// Inputs
		NMinutesIn.Name = "# of Minutes";
		NMinutesIn.SetInt(5);

		return;
	}
	int ThisHour, ThisMinute, ThisSecond;
	int LastHour, LastMinute, LastSecond;
	int Start = sc.UpdateStartIndex - 1;
	if (Start < 1) Start = 1;
	for (int Index = Start; Index < (sc.ArraySize); Index++)
	{	sc.BaseDateTimeIn[Index].GetTimeHMS(ThisHour, ThisMinute, ThisSecond);
		sc.BaseDateTimeIn[Index-1].GetTimeHMS(LastHour, LastMinute, LastSecond);
		ThisMinute = ThisHour*60+ThisMinute;
		LastMinute = LastHour*60+LastMinute;
		if (ThisMinute/NMinutesIn.GetInt() - LastMinute/NMinutesIn.GetInt() == 1)
		{	TimeLineTop[Index] = 100;
			TimeLineBottom[Index] = -100;
		}
		else
		{	TimeLineTop[Index] = 0;
			TimeLineBottom[Index] = 0;
		}
	}
}	

